nsresult ProxyAutoConfig::SetupJS() { mJSNeedsSetup = false; NS_ABORT_IF_FALSE(!sRunning, "JIT is running"); delete mJSRuntime; mJSRuntime = nullptr; if (mPACScript.IsEmpty()) return NS_ERROR_FAILURE; mJSRuntime = JSRuntimeWrapper::Create(); if (!mJSRuntime) return NS_ERROR_FAILURE; JSAutoRequest ar(mJSRuntime->Context()); JSAutoCompartment ac(mJSRuntime->Context(), mJSRuntime->Global()); sRunning = this; JSScript *script = JS_CompileScript(mJSRuntime->Context(), mJSRuntime->Global(), mPACScript.get(), mPACScript.Length(), mPACURI.get(), 1); if (!script || !JS_ExecuteScript(mJSRuntime->Context(), mJSRuntime->Global(), script, nullptr)) { nsString alertMessage(NS_LITERAL_STRING("PAC file failed to install from ")); alertMessage += NS_ConvertUTF8toUTF16(mPACURI); PACLogToConsole(alertMessage); sRunning = nullptr; return NS_ERROR_FAILURE; } sRunning = nullptr; mJSRuntime->SetOK(); nsString alertMessage(NS_LITERAL_STRING("PAC file installed from ")); alertMessage += NS_ConvertUTF8toUTF16(mPACURI); PACLogToConsole(alertMessage); // we don't need these now mPACScript.Truncate(); mPACURI.Truncate(); return NS_OK; }
// Javascript errors are logged to the main error console static void PACErrorReporter(JSContext *cx, const char *message, JSErrorReport *report) { nsString formattedMessage(NS_LITERAL_STRING("PAC Execution Error: ")); formattedMessage += report->ucmessage; formattedMessage += NS_LITERAL_STRING(" ["); formattedMessage += report->uclinebuf; formattedMessage += NS_LITERAL_STRING("]"); PACLogToConsole(formattedMessage); }
// Javascript errors and warnings are logged to the main error console static void PACLogErrorOrWarning(const nsAString& aKind, JSErrorReport* aReport) { nsString formattedMessage(NS_LITERAL_STRING("PAC Execution ")); formattedMessage += aKind; formattedMessage += NS_LITERAL_STRING(": "); formattedMessage += aReport->ucmessage; formattedMessage += NS_LITERAL_STRING(" ["); formattedMessage.Append(aReport->linebuf(), aReport->linebufLength()); formattedMessage += NS_LITERAL_STRING("]"); PACLogToConsole(formattedMessage); }
// proxyAlert(msg) javascript implementation static JSBool PACProxyAlert(JSContext *cx, unsigned int argc, jsval *vp) { JSString *arg1 = nullptr; if (!JS_ConvertArguments(cx, argc, JS_ARGV(cx, vp), "S", &arg1)) return false; nsDependentJSString message; if (!message.init(cx, arg1)) return false; nsString alertMessage; alertMessage.SetCapacity(32 + message.Length()); alertMessage += NS_LITERAL_STRING("PAC-alert: "); alertMessage += message; PACLogToConsole(alertMessage); JS_SET_RVAL(cx, vp, JSVAL_VOID); /* return undefined */ return true; }
// proxyAlert(msg) javascript implementation static bool PACProxyAlert(JSContext *cx, unsigned int argc, JS::Value *vp) { JS::CallArgs args = CallArgsFromVp(argc, vp); JS::Rooted<JSString*> arg1(cx); if (!JS_ConvertArguments(cx, args, "S", arg1.address())) return false; nsDependentJSString message; if (!message.init(cx, arg1)) return false; nsString alertMessage; alertMessage.SetCapacity(32 + message.Length()); alertMessage += NS_LITERAL_STRING("PAC-alert: "); alertMessage += message; PACLogToConsole(alertMessage); args.rval().setUndefined(); /* return undefined */ return true; }
nsresult ProxyAutoConfig::SetupJS() { mJSNeedsSetup = false; NS_ABORT_IF_FALSE(!sRunning, "JIT is running"); delete mJSRuntime; mJSRuntime = nullptr; if (mPACScript.IsEmpty()) return NS_ERROR_FAILURE; mJSRuntime = JSRuntimeWrapper::Create(); if (!mJSRuntime) return NS_ERROR_FAILURE; JSAutoRequest ar(mJSRuntime->Context()); JSAutoCompartment ac(mJSRuntime->Context(), mJSRuntime->Global()); // check if this is a data: uri so that we don't spam the js console with // huge meaningless strings. this is not on the main thread, so it can't // use nsIRUI scheme methods bool isDataURI = nsDependentCSubstring(mPACURI, 0, 5).LowerCaseEqualsASCII("data:", 5); sRunning = this; JS::Rooted<JSObject *> global(mJSRuntime->Context(), mJSRuntime->Global()); JS::CompileOptions options(mJSRuntime->Context()); options.setFileAndLine(mPACURI.get(), 1); JSScript *script = JS_CompileScript(mJSRuntime->Context(), global, mPACScript.get(), mPACScript.Length(), options); if (!script || !JS_ExecuteScript(mJSRuntime->Context(), mJSRuntime->Global(), script, nullptr)) { nsString alertMessage(NS_LITERAL_STRING("PAC file failed to install from ")); if (isDataURI) { alertMessage += NS_LITERAL_STRING("data: URI"); } else { alertMessage += NS_ConvertUTF8toUTF16(mPACURI); } PACLogToConsole(alertMessage); sRunning = nullptr; return NS_ERROR_FAILURE; } sRunning = nullptr; mJSRuntime->SetOK(); nsString alertMessage(NS_LITERAL_STRING("PAC file installed from ")); if (isDataURI) { alertMessage += NS_LITERAL_STRING("data: URI"); } else { alertMessage += NS_ConvertUTF8toUTF16(mPACURI); } PACLogToConsole(alertMessage); // we don't need these now mPACScript.Truncate(); mPACURI.Truncate(); return NS_OK; }