Skip to content
This repository has been archived by the owner on Feb 26, 2022. It is now read-only.

Commit

Permalink
Revert "Merge pull request #533 from ochameau/bug/764840-use-sandboxe…
Browse files Browse the repository at this point in the history
…s-for-loader"

This reverts commit 274ed55, reversing
changes made to 8f03c79.
  • Loading branch information
ochameau committed Sep 2, 2012
1 parent 274ed55 commit 7bafac0
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 56 deletions.
53 changes: 38 additions & 15 deletions packages/api-utils/lib/cuddlefish.js
Expand Up @@ -3,30 +3,53 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
;(function(id, factory) { // Module boilerplate :(
if (typeof(define) === 'function') { // RequireJS
define(factory);
} else if (typeof(require) === 'function') { // CommonJS
factory.call(this, require, exports, module);
} else if (~String(this).indexOf('BackstagePass')) { // JSM
factory(function require(uri) {
var imports = {};
this['Components'].utils.import(uri, imports);
return imports;
}, this, { uri: __URI__, id: id });
this.EXPORTED_SYMBOLS = Object.keys(this);
} else { // Browser or alike
var globals = this
factory(function require(id) {
return globals[id];
}, (globals[id] = {}), { uri: document.location.href + '#' + id, id: id });
}
}).call(this, 'loader', function(require, exports, module) {

'use strict';

// This module is manually loaded by bootstrap.js in a sandbox and immediatly
// put in module cache so that it is never loaded in any other way.

/* Workarounds to include dependencies in the manifest
require('chrome') // Otherwise CFX will complain about Components
require('api-utils/loader') // Otherwise CFX will stip out loader.js
require('api-utils/addon/runner') // Otherwise CFX will stip out addon/runner.js
*/

const { classes: Cc, Constructor: CC, interfaces: Ci, utils: Cu } = Components;

// `loadSandbox` is exposed by bootstrap.js
// Note require here in this context is just an alias for Cu.import which is
// used since regular require is not available at loader bootstrap.
const loaderURI = module.uri.replace(/\/[^\/]*$/, '/loader.js');
// We need to keep a reference to the sandbox in order to unload it in
// bootstrap.js
const loaderSandbox = loadSandbox(loaderURI);
const loaderModule = loaderSandbox.exports;
const loaderModule = require(loaderURI);
const { Loader: BaseLoader, Require, Sandbox, resolveURI, evaluate, load,
Module, unload, override, descriptor, main } = loaderModule;

const { override } = loaderModule;
exports.resolveURI = resolveURI;
exports.Require = Require;
exports.Sandbox = Sandbox;
exports.evaluate = evaluate;
exports.load = load;
exports.Module = Module;
exports.unload = unload;
exports.override = override;
exports.descriptor = descriptor;
exports.main = main;

function CuddlefishLoader(options) {
function Loader(options) {
let { manifest } = options;

options = override(options, {
Expand Down Expand Up @@ -62,9 +85,9 @@ function CuddlefishLoader(options) {
}
});

return loaderModule.Loader(options);
return BaseLoader(options);
}
Loader.prototype = null;
exports.Loader = Object.freeze(Loader);

exports = override(loaderModule, {
Loader: CuddlefishLoader
});
3 changes: 1 addition & 2 deletions packages/api-utils/lib/loader.js
Expand Up @@ -15,8 +15,6 @@
return imports;
}, this, { uri: __URI__, id: id });
this.EXPORTED_SYMBOLS = Object.keys(this);
} else if (~String(this).indexOf('Sandbox')) { // Sandbox
factory(function require(uri) {}, this, { uri: __URI__, id: id });
} else { // Browser or alike
var globals = this
factory(function require(id) {
Expand Down Expand Up @@ -252,6 +250,7 @@ const Require = iced(function Require(loader, requirer) {
// Resolves `uri` of module using loaders resolve function.
let uri = resolveURI(requirement, mapping);


if (!uri) // Throw if `uri` can not be resolved.
throw Error('Module: Can not resolve "' + id + '" module required by ' +
requirer.id + ' located at ' + requirer.uri, requirer.uri);
Expand Down
51 changes: 12 additions & 39 deletions python-lib/cuddlefish/app-extension/bootstrap.js
Expand Up @@ -16,16 +16,12 @@ const ioService = Cc['@mozilla.org/network/io-service;1'].
getService(Ci.nsIIOService);
const resourceHandler = ioService.getProtocolHandler('resource').
QueryInterface(Ci.nsIResProtocolHandler);
const systemPrincipal = CC('@mozilla.org/systemprincipal;1', 'nsIPrincipal')();
const scriptLoader = Cc['@mozilla.org/moz/jssubscript-loader;1'].
getService(Ci.mozIJSSubScriptLoader);

const REASON = [ 'unknown', 'startup', 'shutdown', 'enable', 'disable',
'install', 'uninstall', 'upgrade', 'downgrade' ];

let loader = null;
let unload = null;
let cuddlefishSandbox = null;
let cuddlefishURI = null;
let nukeTimer = null;

// Utility function that synchronously reads local resource from the given
Expand Down Expand Up @@ -127,11 +123,11 @@ function startup(data, reasonCode) {
// Make version 2 of the manifest
let manifest = manifestV2(options.manifest);

// Import `cuddlefish.js` module using a Sandbox and bootstrap loader.
let cuddlefishURI = prefixURI + options.loader;
cuddlefishSandbox = loadSandbox(cuddlefishURI);
let cuddlefish = cuddlefishSandbox.exports;
// We use global `loaderURI` to allow unload.
cuddlefishURI = prefixURI + options.loader;

// Import `cuddlefish.js` module using `Cu.import` and bootstrap loader.
let cuddlefish = Cu.import(cuddlefishURI);
// Normalize `options.mainPath` so that it looks like one that will come
// in a new version of linker.
let main = path2id(options.mainPath);
Expand Down Expand Up @@ -178,8 +174,7 @@ function startup(data, reasonCode) {
});

let module = cuddlefish.Module('api-utils/cuddlefish', cuddlefishURI);
let require = cuddlefish.Require(loader, module);

let require = Require(loader, module);
require('api-utils/addon/runner').startup(reason, {
loader: loader,
main: main,
Expand All @@ -192,25 +187,6 @@ function startup(data, reasonCode) {
}
};

function loadSandbox(uri) {
let proto = { sandboxPrototype: { loadSandbox: loadSandbox } };
let sandbox = Cu.Sandbox(systemPrincipal, proto);
// Create a fake commonjs environnement just to enable loading loader.js
// correctly
sandbox.exports = {};
sandbox.module = { uri: uri, exports: sandbox.exports };
sandbox.require = function () {
throw new Error("Bootstrap sandbox `require` method isn't implemented.");
};
scriptLoader.loadSubScript(uri, sandbox, 'UTF-8');
return sandbox;
}

function unloadSandbox(sandbox) {
if ("nukeSandbox" in Cu)
Cu.nukeSandbox(sandbox);
}

function setTimeout(callback, delay) {
let timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
timer.initWithCallback({ notify: callback }, delay,
Expand All @@ -223,8 +199,11 @@ function shutdown(data, reasonCode) {
if (loader) {
unload(loader, reason);
unload = null;
// Bug 724433: We need to unload JSM otherwise it will stay alive
// and keep a reference to this compartment.
Cu.unload(cuddlefishURI);
// Avoid leaking all modules when something goes wrong with one particular
// module. Do not clean it up immediatly in order to allow executing some
// module. Do not clean it up immadiatly in order to allow executing some
// actions on addon disabling.
// We need to keep a reference to the timer, otherwise it is collected
// and won't ever fire.
Expand All @@ -244,14 +223,8 @@ function nukeModules() {
let sandbox = loader.sandboxes[key];
delete loader.sandboxes[key];
// Bug 775067: From FF17 we can kill all CCW from a given sandbox
unloadSandbox(sandbox);
if ("nukeSandbox" in Cu)
Cu.nukeSandbox(sandbox);
}
loader = null;

// Unload sandbox used to evaluate loader.js
unloadSandbox(cuddlefishSandbox.loaderSandbox);
// Bug 764840: We need to unload cuddlefish otherwise it will stay alive
// and keep a reference to this compartment.
unloadSandbox(cuddlefishSandbox);
cuddlefishSandbox = null;
}

0 comments on commit 7bafac0

Please sign in to comment.