extensions/pref/autoconfig/src/nsJSConfigTriggers.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #ifdef MOZ_LOGGING
michael@0 7 // sorry, this has to be before the pre-compiled header
michael@0 8 #define FORCE_PR_LOG /* Allow logging in the release build */
michael@0 9 #endif
michael@0 10 #include "jsapi.h"
michael@0 11 #include "nsIXPCSecurityManager.h"
michael@0 12 #include "nsIXPConnect.h"
michael@0 13 #include "nsIJSRuntimeService.h"
michael@0 14 #include "nsCOMPtr.h"
michael@0 15 #include "nsIServiceManager.h"
michael@0 16 #include "nsIComponentManager.h"
michael@0 17 #include "nsString.h"
michael@0 18 #include "nsIPrefService.h"
michael@0 19 #include "nspr.h"
michael@0 20 #include "mozilla/Attributes.h"
michael@0 21 #include "mozilla/Maybe.h"
michael@0 22 #include "nsContentUtils.h"
michael@0 23 #include "nsCxPusher.h"
michael@0 24 #include "nsIScriptSecurityManager.h"
michael@0 25 #include "nsJSPrincipals.h"
michael@0 26 #include "jswrapper.h"
michael@0 27
michael@0 28 extern PRLogModuleInfo *MCD;
michael@0 29 using mozilla::AutoSafeJSContext;
michael@0 30
michael@0 31 //*****************************************************************************
michael@0 32
michael@0 33 static mozilla::Maybe<JS::PersistentRooted<JSObject *> > autoconfigSb;
michael@0 34
michael@0 35 nsresult CentralizedAdminPrefManagerInit()
michael@0 36 {
michael@0 37 nsresult rv;
michael@0 38
michael@0 39 // If the sandbox is already created, no need to create it again.
michael@0 40 if (!autoconfigSb.empty())
michael@0 41 return NS_OK;
michael@0 42
michael@0 43 // Grab XPConnect.
michael@0 44 nsCOMPtr<nsIXPConnect> xpc = do_GetService(nsIXPConnect::GetCID(), &rv);
michael@0 45 if (NS_FAILED(rv)) {
michael@0 46 return rv;
michael@0 47 }
michael@0 48
michael@0 49 // Grab the system principal.
michael@0 50 nsCOMPtr<nsIPrincipal> principal;
michael@0 51 nsContentUtils::GetSecurityManager()->GetSystemPrincipal(getter_AddRefs(principal));
michael@0 52
michael@0 53
michael@0 54 // Create a sandbox.
michael@0 55 AutoSafeJSContext cx;
michael@0 56 nsCOMPtr<nsIXPConnectJSObjectHolder> sandbox;
michael@0 57 rv = xpc->CreateSandbox(cx, principal, getter_AddRefs(sandbox));
michael@0 58 NS_ENSURE_SUCCESS(rv, rv);
michael@0 59
michael@0 60 // Unwrap, store and root the sandbox.
michael@0 61 NS_ENSURE_STATE(sandbox->GetJSObject());
michael@0 62 autoconfigSb.construct(cx, js::UncheckedUnwrap(sandbox->GetJSObject()));
michael@0 63
michael@0 64 return NS_OK;
michael@0 65 }
michael@0 66
michael@0 67 nsresult CentralizedAdminPrefManagerFinish()
michael@0 68 {
michael@0 69 if (!autoconfigSb.empty()) {
michael@0 70 AutoSafeJSContext cx;
michael@0 71 autoconfigSb.destroy();
michael@0 72 JS_MaybeGC(cx);
michael@0 73 }
michael@0 74 return NS_OK;
michael@0 75 }
michael@0 76
michael@0 77 nsresult EvaluateAdminConfigScript(const char *js_buffer, size_t length,
michael@0 78 const char *filename, bool bGlobalContext,
michael@0 79 bool bCallbacks, bool skipFirstLine)
michael@0 80 {
michael@0 81 nsresult rv = NS_OK;
michael@0 82
michael@0 83 if (skipFirstLine) {
michael@0 84 /* In order to protect the privacy of the JavaScript preferences file
michael@0 85 * from loading by the browser, we make the first line unparseable
michael@0 86 * by JavaScript. We must skip that line here before executing
michael@0 87 * the JavaScript code.
michael@0 88 */
michael@0 89 unsigned int i = 0;
michael@0 90 while (i < length) {
michael@0 91 char c = js_buffer[i++];
michael@0 92 if (c == '\r') {
michael@0 93 if (js_buffer[i] == '\n')
michael@0 94 i++;
michael@0 95 break;
michael@0 96 }
michael@0 97 if (c == '\n')
michael@0 98 break;
michael@0 99 }
michael@0 100
michael@0 101 length -= i;
michael@0 102 js_buffer += i;
michael@0 103 }
michael@0 104
michael@0 105 // Grab XPConnect.
michael@0 106 nsCOMPtr<nsIXPConnect> xpc = do_GetService(nsIXPConnect::GetCID(), &rv);
michael@0 107 if (NS_FAILED(rv)) {
michael@0 108 return rv;
michael@0 109 }
michael@0 110
michael@0 111 AutoSafeJSContext cx;
michael@0 112 JSAutoCompartment ac(cx, autoconfigSb.ref());
michael@0 113
michael@0 114 nsAutoCString script(js_buffer, length);
michael@0 115 JS::RootedValue v(cx);
michael@0 116 rv = xpc->EvalInSandboxObject(NS_ConvertASCIItoUTF16(script), filename, cx, autoconfigSb.ref(),
michael@0 117 /* returnStringOnly = */ false, &v);
michael@0 118 NS_ENSURE_SUCCESS(rv, rv);
michael@0 119
michael@0 120 return NS_OK;
michael@0 121 }
michael@0 122

mercurial