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-23 21:07:57 -07:00
parent f781b6b986
commit a7c949f21c
3 changed files with 47 additions and 39 deletions

View File

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

View File

@ -51,6 +51,19 @@
#include "nsIScriptObjectPrincipal.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,
public nsIScriptObjectPrincipal,
public nsIXPCScriptable

View File

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