addon-sdk/source/lib/sdk/loader/sandbox.js

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4 "use strict";
michael@0 5
michael@0 6 module.metadata = {
michael@0 7 "stability": "experimental"
michael@0 8 };
michael@0 9
michael@0 10 const { Cc, Ci, CC, Cu } = require('chrome');
michael@0 11 const systemPrincipal = CC('@mozilla.org/systemprincipal;1', 'nsIPrincipal')();
michael@0 12 const scriptLoader = Cc['@mozilla.org/moz/jssubscript-loader;1'].
michael@0 13 getService(Ci.mozIJSSubScriptLoader);
michael@0 14 const self = require('sdk/self');
michael@0 15 const { getTabId, getTabForContentWindow } = require('../tabs/utils');
michael@0 16 const { getInnerId } = require('../window/utils');
michael@0 17
michael@0 18 const { gDevToolsExtensions: {
michael@0 19 addContentGlobal, removeContentGlobal
michael@0 20 } } = Cu.import("resource://gre/modules/devtools/DevToolsExtensions.jsm", {});
michael@0 21
michael@0 22 /**
michael@0 23 * Make a new sandbox that inherits given `source`'s principals. Source can be
michael@0 24 * URI string, DOMWindow or `null` for system principals.
michael@0 25 */
michael@0 26 function sandbox(target, options) {
michael@0 27 options = options || {};
michael@0 28 options.metadata = options.metadata ? options.metadata : {};
michael@0 29 options.metadata.addonID = options.metadata.addonID ?
michael@0 30 options.metadata.addonID : self.id;
michael@0 31
michael@0 32 let sandbox = Cu.Sandbox(target || systemPrincipal, options);
michael@0 33 Cu.setSandboxMetadata(sandbox, options.metadata);
michael@0 34 let innerWindowID = options.metadata['inner-window-id']
michael@0 35 if (innerWindowID) {
michael@0 36 addContentGlobal({
michael@0 37 global: sandbox,
michael@0 38 'inner-window-id': innerWindowID
michael@0 39 });
michael@0 40 }
michael@0 41 return sandbox;
michael@0 42 }
michael@0 43 exports.sandbox = sandbox;
michael@0 44
michael@0 45 /**
michael@0 46 * Evaluates given `source` in a given `sandbox` and returns result.
michael@0 47 */
michael@0 48 function evaluate(sandbox, code, uri, line, version) {
michael@0 49 return Cu.evalInSandbox(code, sandbox, version || '1.8', uri || '', line || 1);
michael@0 50 }
michael@0 51 exports.evaluate = evaluate;
michael@0 52
michael@0 53 /**
michael@0 54 * Evaluates code under the given `uri` in the given `sandbox`.
michael@0 55 *
michael@0 56 * @param {String} uri
michael@0 57 * The URL pointing to the script to load.
michael@0 58 * It must be a local chrome:, resource:, file: or data: URL.
michael@0 59 */
michael@0 60 function load(sandbox, uri) {
michael@0 61 if (uri.indexOf('data:') === 0) {
michael@0 62 let source = uri.substr(uri.indexOf(',') + 1);
michael@0 63
michael@0 64 return evaluate(sandbox, decodeURIComponent(source), '1.8', uri, 0);
michael@0 65 } else {
michael@0 66 return scriptLoader.loadSubScript(uri, sandbox, 'UTF-8');
michael@0 67 }
michael@0 68 }
michael@0 69 exports.load = load;
michael@0 70
michael@0 71 /**
michael@0 72 * Forces the given `sandbox` to be freed immediately.
michael@0 73 */
michael@0 74 exports.nuke = Cu.nukeSandbox

mercurial