michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifdef MOZ_LOGGING michael@0: // sorry, this has to be before the pre-compiled header michael@0: #define FORCE_PR_LOG /* Allow logging in the release build */ michael@0: #endif michael@0: #include "jsapi.h" michael@0: #include "nsIXPCSecurityManager.h" michael@0: #include "nsIXPConnect.h" michael@0: #include "nsIJSRuntimeService.h" michael@0: #include "nsCOMPtr.h" michael@0: #include "nsIServiceManager.h" michael@0: #include "nsIComponentManager.h" michael@0: #include "nsString.h" michael@0: #include "nsIPrefService.h" michael@0: #include "nspr.h" michael@0: #include "mozilla/Attributes.h" michael@0: #include "mozilla/Maybe.h" michael@0: #include "nsContentUtils.h" michael@0: #include "nsCxPusher.h" michael@0: #include "nsIScriptSecurityManager.h" michael@0: #include "nsJSPrincipals.h" michael@0: #include "jswrapper.h" michael@0: michael@0: extern PRLogModuleInfo *MCD; michael@0: using mozilla::AutoSafeJSContext; michael@0: michael@0: //***************************************************************************** michael@0: michael@0: static mozilla::Maybe > autoconfigSb; michael@0: michael@0: nsresult CentralizedAdminPrefManagerInit() michael@0: { michael@0: nsresult rv; michael@0: michael@0: // If the sandbox is already created, no need to create it again. michael@0: if (!autoconfigSb.empty()) michael@0: return NS_OK; michael@0: michael@0: // Grab XPConnect. michael@0: nsCOMPtr xpc = do_GetService(nsIXPConnect::GetCID(), &rv); michael@0: if (NS_FAILED(rv)) { michael@0: return rv; michael@0: } michael@0: michael@0: // Grab the system principal. michael@0: nsCOMPtr principal; michael@0: nsContentUtils::GetSecurityManager()->GetSystemPrincipal(getter_AddRefs(principal)); michael@0: michael@0: michael@0: // Create a sandbox. michael@0: AutoSafeJSContext cx; michael@0: nsCOMPtr sandbox; michael@0: rv = xpc->CreateSandbox(cx, principal, getter_AddRefs(sandbox)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: // Unwrap, store and root the sandbox. michael@0: NS_ENSURE_STATE(sandbox->GetJSObject()); michael@0: autoconfigSb.construct(cx, js::UncheckedUnwrap(sandbox->GetJSObject())); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult CentralizedAdminPrefManagerFinish() michael@0: { michael@0: if (!autoconfigSb.empty()) { michael@0: AutoSafeJSContext cx; michael@0: autoconfigSb.destroy(); michael@0: JS_MaybeGC(cx); michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult EvaluateAdminConfigScript(const char *js_buffer, size_t length, michael@0: const char *filename, bool bGlobalContext, michael@0: bool bCallbacks, bool skipFirstLine) michael@0: { michael@0: nsresult rv = NS_OK; michael@0: michael@0: if (skipFirstLine) { michael@0: /* In order to protect the privacy of the JavaScript preferences file michael@0: * from loading by the browser, we make the first line unparseable michael@0: * by JavaScript. We must skip that line here before executing michael@0: * the JavaScript code. michael@0: */ michael@0: unsigned int i = 0; michael@0: while (i < length) { michael@0: char c = js_buffer[i++]; michael@0: if (c == '\r') { michael@0: if (js_buffer[i] == '\n') michael@0: i++; michael@0: break; michael@0: } michael@0: if (c == '\n') michael@0: break; michael@0: } michael@0: michael@0: length -= i; michael@0: js_buffer += i; michael@0: } michael@0: michael@0: // Grab XPConnect. michael@0: nsCOMPtr xpc = do_GetService(nsIXPConnect::GetCID(), &rv); michael@0: if (NS_FAILED(rv)) { michael@0: return rv; michael@0: } michael@0: michael@0: AutoSafeJSContext cx; michael@0: JSAutoCompartment ac(cx, autoconfigSb.ref()); michael@0: michael@0: nsAutoCString script(js_buffer, length); michael@0: JS::RootedValue v(cx); michael@0: rv = xpc->EvalInSandboxObject(NS_ConvertASCIItoUTF16(script), filename, cx, autoconfigSb.ref(), michael@0: /* returnStringOnly = */ false, &v); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: return NS_OK; michael@0: } michael@0: