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

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

mercurial