extensions/pref/autoconfig/src/nsJSConfigTriggers.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial