addon-sdk/source/lib/sdk/addon/runner.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
michael@0 5 module.metadata = {
michael@0 6 "stability": "experimental"
michael@0 7 };
michael@0 8
michael@0 9 const { Cc, Ci } = require('chrome');
michael@0 10 const { descriptor, Sandbox, evaluate, main, resolveURI } = require('toolkit/loader');
michael@0 11 const { once } = require('../system/events');
michael@0 12 const { exit, env, staticArgs } = require('../system');
michael@0 13 const { when: unload } = require('../system/unload');
michael@0 14 const { loadReason } = require('../self');
michael@0 15 const { rootURI } = require("@loader/options");
michael@0 16 const globals = require('../system/globals');
michael@0 17 const xulApp = require('../system/xul-app');
michael@0 18 const appShellService = Cc['@mozilla.org/appshell/appShellService;1'].
michael@0 19 getService(Ci.nsIAppShellService);
michael@0 20
michael@0 21 const NAME2TOPIC = {
michael@0 22 'Firefox': 'sessionstore-windows-restored',
michael@0 23 'Fennec': 'sessionstore-windows-restored',
michael@0 24 'SeaMonkey': 'sessionstore-windows-restored',
michael@0 25 'Thunderbird': 'mail-startup-done'
michael@0 26 };
michael@0 27
michael@0 28 // Set 'final-ui-startup' as default topic for unknown applications
michael@0 29 let appStartup = 'final-ui-startup';
michael@0 30
michael@0 31 // Gets the topic that fit best as application startup event, in according with
michael@0 32 // the current application (e.g. Firefox, Fennec, Thunderbird...)
michael@0 33 for (let name of Object.keys(NAME2TOPIC)) {
michael@0 34 if (xulApp.is(name)) {
michael@0 35 appStartup = NAME2TOPIC[name];
michael@0 36 break;
michael@0 37 }
michael@0 38 }
michael@0 39
michael@0 40 // Initializes default preferences
michael@0 41 function setDefaultPrefs(prefsURI) {
michael@0 42 const prefs = Cc['@mozilla.org/preferences-service;1'].
michael@0 43 getService(Ci.nsIPrefService).
michael@0 44 QueryInterface(Ci.nsIPrefBranch2);
michael@0 45 const branch = prefs.getDefaultBranch('');
michael@0 46 const sandbox = Sandbox({
michael@0 47 name: prefsURI,
michael@0 48 prototype: {
michael@0 49 pref: function(key, val) {
michael@0 50 switch (typeof val) {
michael@0 51 case 'boolean':
michael@0 52 branch.setBoolPref(key, val);
michael@0 53 break;
michael@0 54 case 'number':
michael@0 55 if (val % 1 == 0) // number must be a integer, otherwise ignore it
michael@0 56 branch.setIntPref(key, val);
michael@0 57 break;
michael@0 58 case 'string':
michael@0 59 branch.setCharPref(key, val);
michael@0 60 break;
michael@0 61 }
michael@0 62 }
michael@0 63 }
michael@0 64 });
michael@0 65 // load preferences.
michael@0 66 evaluate(sandbox, prefsURI);
michael@0 67 }
michael@0 68
michael@0 69 function definePseudo(loader, id, exports) {
michael@0 70 let uri = resolveURI(id, loader.mapping);
michael@0 71 loader.modules[uri] = { exports: exports };
michael@0 72 }
michael@0 73
michael@0 74 function wait(reason, options) {
michael@0 75 once(appStartup, function() {
michael@0 76 startup(null, options);
michael@0 77 });
michael@0 78 }
michael@0 79
michael@0 80 function startup(reason, options) {
michael@0 81 // Try accessing hidden window to guess if we are running during firefox
michael@0 82 // startup, so that we should wait for session restore event before
michael@0 83 // running the addon
michael@0 84 let initialized = false;
michael@0 85 try {
michael@0 86 appShellService.hiddenDOMWindow;
michael@0 87 initialized = true;
michael@0 88 }
michael@0 89 catch(e) {}
michael@0 90 if (reason === 'startup' || !initialized) {
michael@0 91 return wait(reason, options);
michael@0 92 }
michael@0 93
michael@0 94 // Inject globals ASAP in order to have console API working ASAP
michael@0 95 Object.defineProperties(options.loader.globals, descriptor(globals));
michael@0 96
michael@0 97 // NOTE: Module is intentionally required only now because it relies
michael@0 98 // on existence of hidden window, which does not exists until startup.
michael@0 99 let { ready } = require('../addon/window');
michael@0 100 // Load localization manifest and .properties files.
michael@0 101 // Run the addon even in case of error (best effort approach)
michael@0 102 require('../l10n/loader').
michael@0 103 load(rootURI).
michael@0 104 then(null, function failure(error) {
michael@0 105 console.info("Error while loading localization: " + error.message);
michael@0 106 }).
michael@0 107 then(function onLocalizationReady(data) {
michael@0 108 // Exports data to a pseudo module so that api-utils/l10n/core
michael@0 109 // can get access to it
michael@0 110 definePseudo(options.loader, '@l10n/data', data ? data : null);
michael@0 111 return ready;
michael@0 112 }).then(function() {
michael@0 113 run(options);
michael@0 114 }).then(null, console.exception);
michael@0 115 return void 0; // otherwise we raise a warning, see bug 910304
michael@0 116 }
michael@0 117
michael@0 118 function run(options) {
michael@0 119 try {
michael@0 120 // Try initializing HTML localization before running main module. Just print
michael@0 121 // an exception in case of error, instead of preventing addon to be run.
michael@0 122 try {
michael@0 123 // Do not enable HTML localization while running test as it is hard to
michael@0 124 // disable. Because unit tests are evaluated in a another Loader who
michael@0 125 // doesn't have access to this current loader.
michael@0 126 if (options.main !== 'test-harness/run-tests')
michael@0 127 require('../l10n/html').enable();
michael@0 128 }
michael@0 129 catch(error) {
michael@0 130 console.exception(error);
michael@0 131 }
michael@0 132 // Initialize inline options localization, without preventing addon to be
michael@0 133 // run in case of error
michael@0 134 try {
michael@0 135 require('../l10n/prefs');
michael@0 136 }
michael@0 137 catch(error) {
michael@0 138 console.exception(error);
michael@0 139 }
michael@0 140
michael@0 141 // TODO: When bug 564675 is implemented this will no longer be needed
michael@0 142 // Always set the default prefs, because they disappear on restart
michael@0 143 if (options.prefsURI) {
michael@0 144 // Only set if `prefsURI` specified
michael@0 145 setDefaultPrefs(options.prefsURI);
michael@0 146 }
michael@0 147
michael@0 148 // this is where the addon's main.js finally run.
michael@0 149 let program = main(options.loader, options.main);
michael@0 150
michael@0 151 if (typeof(program.onUnload) === 'function')
michael@0 152 unload(program.onUnload);
michael@0 153
michael@0 154 if (typeof(program.main) === 'function') {
michael@0 155
michael@0 156 program.main({
michael@0 157 loadReason: loadReason,
michael@0 158 staticArgs: staticArgs
michael@0 159 }, {
michael@0 160 print: function print(_) { dump(_ + '\n') },
michael@0 161 quit: exit
michael@0 162 });
michael@0 163 }
michael@0 164 } catch (error) {
michael@0 165 console.exception(error);
michael@0 166 throw error;
michael@0 167 }
michael@0 168 }
michael@0 169 exports.startup = startup;
michael@0 170
michael@0 171 // If add-on is lunched via `cfx run` we need to use `system.exit` to let
michael@0 172 // cfx know we're done (`cfx test` will take care of exit so we don't do
michael@0 173 // anything here).
michael@0 174 if (env.CFX_COMMAND === 'run') {
michael@0 175 unload(function(reason) {
michael@0 176 if (reason === 'shutdown')
michael@0 177 exit(0);
michael@0 178 });
michael@0 179 }

mercurial