Improve logging in jssh, allow loading of remote scripts, fix leak. Bug

369816, patch by Chris Shoemaker <chris.shoemaker@cox.net>, r=afri, sr=bzbarsky
This commit is contained in:
bzbarsky%mit.edu 2007-07-24 04:07:57 +00:00
Родитель 979686a3ad
Коммит 4c68420462
3 изменённых файлов: 47 добавлений и 39 удалений

Просмотреть файл

@ -49,6 +49,11 @@
#include "nsServiceManagerUtils.h" #include "nsServiceManagerUtils.h"
#include "nsXPCOMCIDInternal.h" #include "nsXPCOMCIDInternal.h"
#include "nsMemory.h" #include "nsMemory.h"
#include "nsAutoPtr.h"
#ifdef PR_LOGGING
PRLogModuleInfo *gJSShLog = PR_NewLogModule("jssh");
#endif
//********************************************************************** //**********************************************************************
// Javascript Environment // Javascript Environment
@ -213,9 +218,7 @@ Suspend(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
PR_AtomicIncrement(&shell->mSuspendCount); PR_AtomicIncrement(&shell->mSuspendCount);
while (shell->mSuspendCount) { while (shell->mSuspendCount) {
#ifdef DEBUG LOG(("|"));
printf("|");
#endif
NS_ProcessNextEvent(thread); NS_ProcessNextEvent(thread);
} }
@ -248,7 +251,7 @@ AddressOf(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
} }
char buf[80]; char buf[80];
sprintf(buf,"%p",arg_obj); sprintf(buf, "%p", arg_obj);
JSString *str = JS_NewStringCopyZ(cx, buf); JSString *str = JS_NewStringCopyZ(cx, buf);
*rval = STRING_TO_JSVAL(str); *rval = STRING_TO_JSVAL(str);
return JS_TRUE; return JS_TRUE;
@ -429,9 +432,7 @@ nsJSSh::nsJSSh(nsIInputStream* input,
nsJSSh::~nsJSSh() nsJSSh::~nsJSSh()
{ {
#ifdef DEBUG LOG(("JSSh: ~connection!\n"));
printf("JSSh: ~connection!\n");
#endif
} }
already_AddRefed<nsIRunnable> already_AddRefed<nsIRunnable>
@ -474,9 +475,7 @@ NS_IMETHODIMP nsJSSh::Run()
getter_AddRefs(proxied_shell)); getter_AddRefs(proxied_shell));
} }
else { else {
#ifdef DEBUG LOG(("jssh shell will block main thread!\n"));
printf("jssh shell will block main thread!\n");
#endif
proxied_shell = this; proxied_shell = this;
} }
proxied_shell->Init(); proxied_shell->Init();
@ -673,8 +672,12 @@ NS_IMETHODIMP nsJSSh::ExecuteBuffer()
// native object is getting released before we reach this: // native object is getting released before we reach this:
JSString *str = JS_ValueToString(mJSContext, result); JSString *str = JS_ValueToString(mJSContext, result);
if (str) { if (str) {
nsDependentString chars(reinterpret_cast<const PRUnichar*>
(JS_GetStringChars(str)),
JS_GetStringLength(str));
NS_ConvertUTF16toUTF8 cstr(chars);
PRUint32 bytesWritten; PRUint32 bytesWritten;
mOutput->Write(JS_GetStringBytes(str), JS_GetStringLength(str), &bytesWritten); mOutput->Write(cstr.get(), cstr.Length(), &bytesWritten);
} }
} }
JS_DestroyScript(mJSContext, script); JS_DestroyScript(mJSContext, script);
@ -761,24 +764,25 @@ PRBool nsJSSh::LoadURL(const char *url, jsval* retval)
return PR_FALSE; return PR_FALSE;
} }
PRInt32 content_length=-1; nsCString buffer;
if (NS_FAILED(channel->GetContentLength(&content_length))) { nsAutoArrayPtr<char> buf(new char[1024]);
NS_ERROR("could not get content length");
return PR_FALSE;
}
char *buf = new char[content_length+1];
if (!buf) { if (!buf) {
NS_ERROR("could not alloc buffer"); NS_ERROR("could not alloc buffer");
return PR_FALSE; return PR_FALSE;
} }
PRUint32 bytesRead=0; PRUint32 bytesRead = 0;
instream->Read(buf, content_length, &bytesRead);
if (bytesRead!=content_length) { do {
if (NS_FAILED(instream->Read(buf, 1024, &bytesRead))) {
NS_ERROR("stream read error"); NS_ERROR("stream read error");
return PR_FALSE; return PR_FALSE;
} }
buffer.Append(buf, bytesRead);
LOG(("appended %d bytes:\n%s", bytesRead, buffer.get()));
} while (bytesRead > 0);
LOG(("loaded %d bytes:\n%s", buffer.Length(), buffer.get()));
JS_BeginRequest(mJSContext); JS_BeginRequest(mJSContext);
JSPrincipals *jsprincipals; JSPrincipals *jsprincipals;
@ -791,7 +795,8 @@ PRBool nsJSSh::LoadURL(const char *url, jsval* retval)
jsval result; jsval result;
JSBool ok = JS_EvaluateScriptForPrincipals(mJSContext, mContextObj, JSBool ok = JS_EvaluateScriptForPrincipals(mJSContext, mContextObj,
jsprincipals, buf, content_length, jsprincipals, buffer.get(),
buffer.Length(),
url, 1, &result); url, 1, &result);
JSPRINCIPALS_DROP(mJSContext, jsprincipals); JSPRINCIPALS_DROP(mJSContext, jsprincipals);
@ -803,8 +808,6 @@ PRBool nsJSSh::LoadURL(const char *url, jsval* retval)
JS_EndRequest(mJSContext); JS_EndRequest(mJSContext);
delete[] buf;
return ok; return ok;
} }

Просмотреть файл

@ -51,6 +51,19 @@
#include "nsIScriptObjectPrincipal.h" #include "nsIScriptObjectPrincipal.h"
#include "nsIXPCScriptable.h" #include "nsIXPCScriptable.h"
#ifdef MOZ_LOGGING
#define FORCE_PR_LOG
#endif
#include "prlog.h"
// NSPR_LOG_MODULES=jssh
#ifdef PR_LOGGING
extern PRLogModuleInfo *gJSShLog;
#define LOG(args) PR_LOG(gJSShLog, PR_LOG_DEBUG, args)
#else
#define LOG(args)
#endif
class nsJSSh : public nsIRunnable, public nsIJSSh, class nsJSSh : public nsIRunnable, public nsIJSSh,
public nsIScriptObjectPrincipal, public nsIScriptObjectPrincipal,
public nsIXPCScriptable public nsIXPCScriptable

Просмотреть файл

@ -103,9 +103,7 @@ NS_IMETHODIMP ConnectionListener::OnSocketAccepted(nsIServerSocket *aServ, nsISo
aTransport->OpenInputStream(nsITransport::OPEN_BLOCKING, 0, 0, getter_AddRefs(input)); aTransport->OpenInputStream(nsITransport::OPEN_BLOCKING, 0, 0, getter_AddRefs(input));
aTransport->OpenOutputStream(nsITransport::OPEN_BLOCKING, 0, 0, getter_AddRefs(output)); aTransport->OpenOutputStream(nsITransport::OPEN_BLOCKING, 0, 0, getter_AddRefs(output));
#ifdef DEBUG LOG(("JSSh server: new connection!\n"));
printf("JSSh server: new connection!\n");
#endif
nsCOMPtr<nsIRunnable> shell = CreateJSSh(input, output, mStartupURI); nsCOMPtr<nsIRunnable> shell = CreateJSSh(input, output, mStartupURI);
@ -117,9 +115,7 @@ NS_IMETHODIMP ConnectionListener::OnSocketAccepted(nsIServerSocket *aServ, nsISo
/* void onStopListening (in nsIServerSocket aServ, in nsresult aStatus); */ /* void onStopListening (in nsIServerSocket aServ, in nsresult aStatus); */
NS_IMETHODIMP ConnectionListener::OnStopListening(nsIServerSocket *aServ, nsresult aStatus) NS_IMETHODIMP ConnectionListener::OnStopListening(nsIServerSocket *aServ, nsresult aStatus)
{ {
#ifdef DEBUG LOG(("JSSh server: stopped listening!\n"));
printf("JSSh server: stopped listening!\n");
#endif
return NS_OK; return NS_OK;
} }
@ -128,16 +124,12 @@ NS_IMETHODIMP ConnectionListener::OnStopListening(nsIServerSocket *aServ, nsresu
nsJSShServer::nsJSShServer() nsJSShServer::nsJSShServer()
{ {
#ifdef DEBUG LOG(("nsJSShServer ctor\n"));
printf("nsJSShServer ctor\n");
#endif
} }
nsJSShServer::~nsJSShServer() nsJSShServer::~nsJSShServer()
{ {
#ifdef DEBUG LOG(("nsJSShServer dtor\n"));
printf("nsJSShServer dtor\n");
#endif
// XXX should we stop listening or not?? // XXX should we stop listening or not??
StopServerSocket(); StopServerSocket();
} }