1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/extensions/pref/autoconfig/src/nsJSConfigTriggers.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,122 @@ 1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#ifdef MOZ_LOGGING 1.10 +// sorry, this has to be before the pre-compiled header 1.11 +#define FORCE_PR_LOG /* Allow logging in the release build */ 1.12 +#endif 1.13 +#include "jsapi.h" 1.14 +#include "nsIXPCSecurityManager.h" 1.15 +#include "nsIXPConnect.h" 1.16 +#include "nsIJSRuntimeService.h" 1.17 +#include "nsCOMPtr.h" 1.18 +#include "nsIServiceManager.h" 1.19 +#include "nsIComponentManager.h" 1.20 +#include "nsString.h" 1.21 +#include "nsIPrefService.h" 1.22 +#include "nspr.h" 1.23 +#include "mozilla/Attributes.h" 1.24 +#include "mozilla/Maybe.h" 1.25 +#include "nsContentUtils.h" 1.26 +#include "nsCxPusher.h" 1.27 +#include "nsIScriptSecurityManager.h" 1.28 +#include "nsJSPrincipals.h" 1.29 +#include "jswrapper.h" 1.30 + 1.31 +extern PRLogModuleInfo *MCD; 1.32 +using mozilla::AutoSafeJSContext; 1.33 + 1.34 +//***************************************************************************** 1.35 + 1.36 +static mozilla::Maybe<JS::PersistentRooted<JSObject *> > autoconfigSb; 1.37 + 1.38 +nsresult CentralizedAdminPrefManagerInit() 1.39 +{ 1.40 + nsresult rv; 1.41 + 1.42 + // If the sandbox is already created, no need to create it again. 1.43 + if (!autoconfigSb.empty()) 1.44 + return NS_OK; 1.45 + 1.46 + // Grab XPConnect. 1.47 + nsCOMPtr<nsIXPConnect> xpc = do_GetService(nsIXPConnect::GetCID(), &rv); 1.48 + if (NS_FAILED(rv)) { 1.49 + return rv; 1.50 + } 1.51 + 1.52 + // Grab the system principal. 1.53 + nsCOMPtr<nsIPrincipal> principal; 1.54 + nsContentUtils::GetSecurityManager()->GetSystemPrincipal(getter_AddRefs(principal)); 1.55 + 1.56 + 1.57 + // Create a sandbox. 1.58 + AutoSafeJSContext cx; 1.59 + nsCOMPtr<nsIXPConnectJSObjectHolder> sandbox; 1.60 + rv = xpc->CreateSandbox(cx, principal, getter_AddRefs(sandbox)); 1.61 + NS_ENSURE_SUCCESS(rv, rv); 1.62 + 1.63 + // Unwrap, store and root the sandbox. 1.64 + NS_ENSURE_STATE(sandbox->GetJSObject()); 1.65 + autoconfigSb.construct(cx, js::UncheckedUnwrap(sandbox->GetJSObject())); 1.66 + 1.67 + return NS_OK; 1.68 +} 1.69 + 1.70 +nsresult CentralizedAdminPrefManagerFinish() 1.71 +{ 1.72 + if (!autoconfigSb.empty()) { 1.73 + AutoSafeJSContext cx; 1.74 + autoconfigSb.destroy(); 1.75 + JS_MaybeGC(cx); 1.76 + } 1.77 + return NS_OK; 1.78 +} 1.79 + 1.80 +nsresult EvaluateAdminConfigScript(const char *js_buffer, size_t length, 1.81 + const char *filename, bool bGlobalContext, 1.82 + bool bCallbacks, bool skipFirstLine) 1.83 +{ 1.84 + nsresult rv = NS_OK; 1.85 + 1.86 + if (skipFirstLine) { 1.87 + /* In order to protect the privacy of the JavaScript preferences file 1.88 + * from loading by the browser, we make the first line unparseable 1.89 + * by JavaScript. We must skip that line here before executing 1.90 + * the JavaScript code. 1.91 + */ 1.92 + unsigned int i = 0; 1.93 + while (i < length) { 1.94 + char c = js_buffer[i++]; 1.95 + if (c == '\r') { 1.96 + if (js_buffer[i] == '\n') 1.97 + i++; 1.98 + break; 1.99 + } 1.100 + if (c == '\n') 1.101 + break; 1.102 + } 1.103 + 1.104 + length -= i; 1.105 + js_buffer += i; 1.106 + } 1.107 + 1.108 + // Grab XPConnect. 1.109 + nsCOMPtr<nsIXPConnect> xpc = do_GetService(nsIXPConnect::GetCID(), &rv); 1.110 + if (NS_FAILED(rv)) { 1.111 + return rv; 1.112 + } 1.113 + 1.114 + AutoSafeJSContext cx; 1.115 + JSAutoCompartment ac(cx, autoconfigSb.ref()); 1.116 + 1.117 + nsAutoCString script(js_buffer, length); 1.118 + JS::RootedValue v(cx); 1.119 + rv = xpc->EvalInSandboxObject(NS_ConvertASCIItoUTF16(script), filename, cx, autoconfigSb.ref(), 1.120 + /* returnStringOnly = */ false, &v); 1.121 + NS_ENSURE_SUCCESS(rv, rv); 1.122 + 1.123 + return NS_OK; 1.124 +} 1.125 +