Sat, 03 Jan 2015 20:18:00 +0100
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 | // @see http://mxr.mozilla.org/mozilla-central/source/js/src/xpconnect/loader/mozJSComponentLoader.cpp |
michael@0 | 6 | |
michael@0 | 7 | 'use strict'; |
michael@0 | 8 | |
michael@0 | 9 | // IMPORTANT: Avoid adding any initialization tasks here, if you need to do |
michael@0 | 10 | // something before add-on is loaded consider addon/runner module instead! |
michael@0 | 11 | |
michael@0 | 12 | const { classes: Cc, Constructor: CC, interfaces: Ci, utils: Cu, |
michael@0 | 13 | results: Cr, manager: Cm } = Components; |
michael@0 | 14 | const ioService = Cc['@mozilla.org/network/io-service;1']. |
michael@0 | 15 | getService(Ci.nsIIOService); |
michael@0 | 16 | const resourceHandler = ioService.getProtocolHandler('resource'). |
michael@0 | 17 | QueryInterface(Ci.nsIResProtocolHandler); |
michael@0 | 18 | const systemPrincipal = CC('@mozilla.org/systemprincipal;1', 'nsIPrincipal')(); |
michael@0 | 19 | const scriptLoader = Cc['@mozilla.org/moz/jssubscript-loader;1']. |
michael@0 | 20 | getService(Ci.mozIJSSubScriptLoader); |
michael@0 | 21 | const prefService = Cc['@mozilla.org/preferences-service;1']. |
michael@0 | 22 | getService(Ci.nsIPrefService). |
michael@0 | 23 | QueryInterface(Ci.nsIPrefBranch); |
michael@0 | 24 | const appInfo = Cc["@mozilla.org/xre/app-info;1"]. |
michael@0 | 25 | getService(Ci.nsIXULAppInfo); |
michael@0 | 26 | const vc = Cc["@mozilla.org/xpcom/version-comparator;1"]. |
michael@0 | 27 | getService(Ci.nsIVersionComparator); |
michael@0 | 28 | |
michael@0 | 29 | |
michael@0 | 30 | const REASON = [ 'unknown', 'startup', 'shutdown', 'enable', 'disable', |
michael@0 | 31 | 'install', 'uninstall', 'upgrade', 'downgrade' ]; |
michael@0 | 32 | |
michael@0 | 33 | const bind = Function.call.bind(Function.bind); |
michael@0 | 34 | |
michael@0 | 35 | let loader = null; |
michael@0 | 36 | let unload = null; |
michael@0 | 37 | let cuddlefishSandbox = null; |
michael@0 | 38 | let nukeTimer = null; |
michael@0 | 39 | |
michael@0 | 40 | let resourceDomains = []; |
michael@0 | 41 | function setResourceSubstitution(domain, uri) { |
michael@0 | 42 | resourceDomains.push(domain); |
michael@0 | 43 | resourceHandler.setSubstitution(domain, uri); |
michael@0 | 44 | } |
michael@0 | 45 | |
michael@0 | 46 | // Utility function that synchronously reads local resource from the given |
michael@0 | 47 | // `uri` and returns content string. |
michael@0 | 48 | function readURI(uri) { |
michael@0 | 49 | let ioservice = Cc['@mozilla.org/network/io-service;1']. |
michael@0 | 50 | getService(Ci.nsIIOService); |
michael@0 | 51 | let channel = ioservice.newChannel(uri, 'UTF-8', null); |
michael@0 | 52 | let stream = channel.open(); |
michael@0 | 53 | |
michael@0 | 54 | let cstream = Cc['@mozilla.org/intl/converter-input-stream;1']. |
michael@0 | 55 | createInstance(Ci.nsIConverterInputStream); |
michael@0 | 56 | cstream.init(stream, 'UTF-8', 0, 0); |
michael@0 | 57 | |
michael@0 | 58 | let str = {}; |
michael@0 | 59 | let data = ''; |
michael@0 | 60 | let read = 0; |
michael@0 | 61 | do { |
michael@0 | 62 | read = cstream.readString(0xffffffff, str); |
michael@0 | 63 | data += str.value; |
michael@0 | 64 | } while (read != 0); |
michael@0 | 65 | |
michael@0 | 66 | cstream.close(); |
michael@0 | 67 | |
michael@0 | 68 | return data; |
michael@0 | 69 | } |
michael@0 | 70 | |
michael@0 | 71 | // We don't do anything on install & uninstall yet, but in a future |
michael@0 | 72 | // we should allow add-ons to cleanup after uninstall. |
michael@0 | 73 | function install(data, reason) {} |
michael@0 | 74 | function uninstall(data, reason) {} |
michael@0 | 75 | |
michael@0 | 76 | function startup(data, reasonCode) { |
michael@0 | 77 | try { |
michael@0 | 78 | let reason = REASON[reasonCode]; |
michael@0 | 79 | // URI for the root of the XPI file. |
michael@0 | 80 | // 'jar:' URI if the addon is packed, 'file:' URI otherwise. |
michael@0 | 81 | // (Used by l10n module in order to fetch `locale` folder) |
michael@0 | 82 | let rootURI = data.resourceURI.spec; |
michael@0 | 83 | |
michael@0 | 84 | // TODO: Maybe we should perform read harness-options.json asynchronously, |
michael@0 | 85 | // since we can't do anything until 'sessionstore-windows-restored' anyway. |
michael@0 | 86 | let options = JSON.parse(readURI(rootURI + './harness-options.json')); |
michael@0 | 87 | |
michael@0 | 88 | let id = options.jetpackID; |
michael@0 | 89 | let name = options.name; |
michael@0 | 90 | |
michael@0 | 91 | // Clean the metadata |
michael@0 | 92 | options.metadata[name]['permissions'] = options.metadata[name]['permissions'] || {}; |
michael@0 | 93 | |
michael@0 | 94 | // freeze the permissionss |
michael@0 | 95 | Object.freeze(options.metadata[name]['permissions']); |
michael@0 | 96 | // freeze the metadata |
michael@0 | 97 | Object.freeze(options.metadata[name]); |
michael@0 | 98 | |
michael@0 | 99 | // Register a new resource 'domain' for this addon which is mapping to |
michael@0 | 100 | // XPI's `resources` folder. |
michael@0 | 101 | // Generate the domain name by using jetpack ID, which is the extension ID |
michael@0 | 102 | // by stripping common characters that doesn't work as a domain name: |
michael@0 | 103 | let uuidRe = |
michael@0 | 104 | /^\{([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})\}$/; |
michael@0 | 105 | |
michael@0 | 106 | let domain = id. |
michael@0 | 107 | toLowerCase(). |
michael@0 | 108 | replace(/@/g, '-at-'). |
michael@0 | 109 | replace(/\./g, '-dot-'). |
michael@0 | 110 | replace(uuidRe, '$1'); |
michael@0 | 111 | |
michael@0 | 112 | let prefixURI = 'resource://' + domain + '/'; |
michael@0 | 113 | let resourcesURI = ioService.newURI(rootURI + '/resources/', null, null); |
michael@0 | 114 | setResourceSubstitution(domain, resourcesURI); |
michael@0 | 115 | |
michael@0 | 116 | // Create path to URLs mapping supported by loader. |
michael@0 | 117 | let paths = { |
michael@0 | 118 | // Relative modules resolve to add-on package lib |
michael@0 | 119 | './': prefixURI + name + '/lib/', |
michael@0 | 120 | './tests/': prefixURI + name + '/tests/', |
michael@0 | 121 | '': 'resource://gre/modules/commonjs/' |
michael@0 | 122 | }; |
michael@0 | 123 | |
michael@0 | 124 | // Maps addon lib and tests ressource folders for each package |
michael@0 | 125 | paths = Object.keys(options.metadata).reduce(function(result, name) { |
michael@0 | 126 | result[name + '/'] = prefixURI + name + '/lib/' |
michael@0 | 127 | result[name + '/tests/'] = prefixURI + name + '/tests/' |
michael@0 | 128 | return result; |
michael@0 | 129 | }, paths); |
michael@0 | 130 | |
michael@0 | 131 | // We need to map tests folder when we run sdk tests whose package name |
michael@0 | 132 | // is stripped |
michael@0 | 133 | if (name == 'addon-sdk') |
michael@0 | 134 | paths['tests/'] = prefixURI + name + '/tests/'; |
michael@0 | 135 | |
michael@0 | 136 | let useBundledSDK = options['force-use-bundled-sdk']; |
michael@0 | 137 | if (!useBundledSDK) { |
michael@0 | 138 | try { |
michael@0 | 139 | useBundledSDK = prefService.getBoolPref("extensions.addon-sdk.useBundledSDK"); |
michael@0 | 140 | } |
michael@0 | 141 | catch (e) { |
michael@0 | 142 | // Pref doesn't exist, allow using Firefox shipped SDK |
michael@0 | 143 | } |
michael@0 | 144 | } |
michael@0 | 145 | |
michael@0 | 146 | // Starting with Firefox 21.0a1, we start using modules shipped into firefox |
michael@0 | 147 | // Still allow using modules from the xpi if the manifest tell us to do so. |
michael@0 | 148 | // And only try to look for sdk modules in xpi if the xpi actually ship them |
michael@0 | 149 | if (options['is-sdk-bundled'] && |
michael@0 | 150 | (vc.compare(appInfo.version, '21.0a1') < 0 || useBundledSDK)) { |
michael@0 | 151 | // Maps sdk module folders to their resource folder |
michael@0 | 152 | paths[''] = prefixURI + 'addon-sdk/lib/'; |
michael@0 | 153 | // test.js is usually found in root commonjs or SDK_ROOT/lib/ folder, |
michael@0 | 154 | // so that it isn't shipped in the xpi. Keep a copy of it in sdk/ folder |
michael@0 | 155 | // until we no longer support SDK modules in XPI: |
michael@0 | 156 | paths['test'] = prefixURI + 'addon-sdk/lib/sdk/test.js'; |
michael@0 | 157 | } |
michael@0 | 158 | |
michael@0 | 159 | // Retrieve list of module folder overloads based on preferences in order to |
michael@0 | 160 | // eventually used a local modules instead of files shipped into Firefox. |
michael@0 | 161 | let branch = prefService.getBranch('extensions.modules.' + id + '.path'); |
michael@0 | 162 | paths = branch.getChildList('', {}).reduce(function (result, name) { |
michael@0 | 163 | // Allows overloading of any sub folder by replacing . by / in pref name |
michael@0 | 164 | let path = name.substr(1).split('.').join('/'); |
michael@0 | 165 | // Only accept overloading folder by ensuring always ending with `/` |
michael@0 | 166 | if (path) path += '/'; |
michael@0 | 167 | let fileURI = branch.getCharPref(name); |
michael@0 | 168 | |
michael@0 | 169 | // On mobile, file URI has to end with a `/` otherwise, setSubstitution |
michael@0 | 170 | // takes the parent folder instead. |
michael@0 | 171 | if (fileURI[fileURI.length-1] !== '/') |
michael@0 | 172 | fileURI += '/'; |
michael@0 | 173 | |
michael@0 | 174 | // Maps the given file:// URI to a resource:// in order to avoid various |
michael@0 | 175 | // failure that happens with file:// URI and be close to production env |
michael@0 | 176 | let resourcesURI = ioService.newURI(fileURI, null, null); |
michael@0 | 177 | let resName = 'extensions.modules.' + domain + '.commonjs.path' + name; |
michael@0 | 178 | setResourceSubstitution(resName, resourcesURI); |
michael@0 | 179 | |
michael@0 | 180 | result[path] = 'resource://' + resName + '/'; |
michael@0 | 181 | return result; |
michael@0 | 182 | }, paths); |
michael@0 | 183 | |
michael@0 | 184 | // Make version 2 of the manifest |
michael@0 | 185 | let manifest = options.manifest; |
michael@0 | 186 | |
michael@0 | 187 | // Import `cuddlefish.js` module using a Sandbox and bootstrap loader. |
michael@0 | 188 | let cuddlefishPath = 'loader/cuddlefish.js'; |
michael@0 | 189 | let cuddlefishURI = 'resource://gre/modules/commonjs/sdk/' + cuddlefishPath; |
michael@0 | 190 | if (paths['sdk/']) { // sdk folder has been overloaded |
michael@0 | 191 | // (from pref, or cuddlefish is still in the xpi) |
michael@0 | 192 | cuddlefishURI = paths['sdk/'] + cuddlefishPath; |
michael@0 | 193 | } |
michael@0 | 194 | else if (paths['']) { // root modules folder has been overloaded |
michael@0 | 195 | cuddlefishURI = paths[''] + 'sdk/' + cuddlefishPath; |
michael@0 | 196 | } |
michael@0 | 197 | |
michael@0 | 198 | cuddlefishSandbox = loadSandbox(cuddlefishURI); |
michael@0 | 199 | let cuddlefish = cuddlefishSandbox.exports; |
michael@0 | 200 | |
michael@0 | 201 | // Normalize `options.mainPath` so that it looks like one that will come |
michael@0 | 202 | // in a new version of linker. |
michael@0 | 203 | let main = options.mainPath; |
michael@0 | 204 | |
michael@0 | 205 | unload = cuddlefish.unload; |
michael@0 | 206 | loader = cuddlefish.Loader({ |
michael@0 | 207 | paths: paths, |
michael@0 | 208 | // modules manifest. |
michael@0 | 209 | manifest: manifest, |
michael@0 | 210 | |
michael@0 | 211 | // Add-on ID used by different APIs as a unique identifier. |
michael@0 | 212 | id: id, |
michael@0 | 213 | // Add-on name. |
michael@0 | 214 | name: name, |
michael@0 | 215 | // Add-on version. |
michael@0 | 216 | version: options.metadata[name].version, |
michael@0 | 217 | // Add-on package descriptor. |
michael@0 | 218 | metadata: options.metadata[name], |
michael@0 | 219 | // Add-on load reason. |
michael@0 | 220 | loadReason: reason, |
michael@0 | 221 | |
michael@0 | 222 | prefixURI: prefixURI, |
michael@0 | 223 | // Add-on URI. |
michael@0 | 224 | rootURI: rootURI, |
michael@0 | 225 | // options used by system module. |
michael@0 | 226 | // File to write 'OK' or 'FAIL' (exit code emulation). |
michael@0 | 227 | resultFile: options.resultFile, |
michael@0 | 228 | // Arguments passed as --static-args |
michael@0 | 229 | staticArgs: options.staticArgs, |
michael@0 | 230 | // Add-on preferences branch name |
michael@0 | 231 | preferencesBranch: options.preferencesBranch, |
michael@0 | 232 | |
michael@0 | 233 | // Arguments related to test runner. |
michael@0 | 234 | modules: { |
michael@0 | 235 | '@test/options': { |
michael@0 | 236 | allTestModules: options.allTestModules, |
michael@0 | 237 | iterations: options.iterations, |
michael@0 | 238 | filter: options.filter, |
michael@0 | 239 | profileMemory: options.profileMemory, |
michael@0 | 240 | stopOnError: options.stopOnError, |
michael@0 | 241 | verbose: options.verbose, |
michael@0 | 242 | parseable: options.parseable, |
michael@0 | 243 | checkMemory: options.check_memory, |
michael@0 | 244 | } |
michael@0 | 245 | } |
michael@0 | 246 | }); |
michael@0 | 247 | |
michael@0 | 248 | let module = cuddlefish.Module('sdk/loader/cuddlefish', cuddlefishURI); |
michael@0 | 249 | let require = cuddlefish.Require(loader, module); |
michael@0 | 250 | |
michael@0 | 251 | require('sdk/addon/runner').startup(reason, { |
michael@0 | 252 | loader: loader, |
michael@0 | 253 | main: main, |
michael@0 | 254 | prefsURI: rootURI + 'defaults/preferences/prefs.js' |
michael@0 | 255 | }); |
michael@0 | 256 | } catch (error) { |
michael@0 | 257 | dump('Bootstrap error: ' + |
michael@0 | 258 | (error.message ? error.message : String(error)) + '\n' + |
michael@0 | 259 | (error.stack || error.fileName + ': ' + error.lineNumber) + '\n'); |
michael@0 | 260 | throw error; |
michael@0 | 261 | } |
michael@0 | 262 | }; |
michael@0 | 263 | |
michael@0 | 264 | function loadSandbox(uri) { |
michael@0 | 265 | let proto = { |
michael@0 | 266 | sandboxPrototype: { |
michael@0 | 267 | loadSandbox: loadSandbox, |
michael@0 | 268 | ChromeWorker: ChromeWorker |
michael@0 | 269 | } |
michael@0 | 270 | }; |
michael@0 | 271 | let sandbox = Cu.Sandbox(systemPrincipal, proto); |
michael@0 | 272 | // Create a fake commonjs environnement just to enable loading loader.js |
michael@0 | 273 | // correctly |
michael@0 | 274 | sandbox.exports = {}; |
michael@0 | 275 | sandbox.module = { uri: uri, exports: sandbox.exports }; |
michael@0 | 276 | sandbox.require = function (id) { |
michael@0 | 277 | if (id !== "chrome") |
michael@0 | 278 | throw new Error("Bootstrap sandbox `require` method isn't implemented."); |
michael@0 | 279 | |
michael@0 | 280 | return Object.freeze({ Cc: Cc, Ci: Ci, Cu: Cu, Cr: Cr, Cm: Cm, |
michael@0 | 281 | CC: bind(CC, Components), components: Components, |
michael@0 | 282 | ChromeWorker: ChromeWorker }); |
michael@0 | 283 | }; |
michael@0 | 284 | scriptLoader.loadSubScript(uri, sandbox, 'UTF-8'); |
michael@0 | 285 | return sandbox; |
michael@0 | 286 | } |
michael@0 | 287 | |
michael@0 | 288 | function unloadSandbox(sandbox) { |
michael@0 | 289 | if ("nukeSandbox" in Cu) |
michael@0 | 290 | Cu.nukeSandbox(sandbox); |
michael@0 | 291 | } |
michael@0 | 292 | |
michael@0 | 293 | function setTimeout(callback, delay) { |
michael@0 | 294 | let timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer); |
michael@0 | 295 | timer.initWithCallback({ notify: callback }, delay, |
michael@0 | 296 | Ci.nsITimer.TYPE_ONE_SHOT); |
michael@0 | 297 | return timer; |
michael@0 | 298 | } |
michael@0 | 299 | |
michael@0 | 300 | function shutdown(data, reasonCode) { |
michael@0 | 301 | let reason = REASON[reasonCode]; |
michael@0 | 302 | if (loader) { |
michael@0 | 303 | unload(loader, reason); |
michael@0 | 304 | unload = null; |
michael@0 | 305 | |
michael@0 | 306 | // Don't waste time cleaning up if the application is shutting down |
michael@0 | 307 | if (reason != "shutdown") { |
michael@0 | 308 | // Avoid leaking all modules when something goes wrong with one particular |
michael@0 | 309 | // module. Do not clean it up immediatly in order to allow executing some |
michael@0 | 310 | // actions on addon disabling. |
michael@0 | 311 | // We need to keep a reference to the timer, otherwise it is collected |
michael@0 | 312 | // and won't ever fire. |
michael@0 | 313 | nukeTimer = setTimeout(nukeModules, 1000); |
michael@0 | 314 | |
michael@0 | 315 | // Bug 944951 - bootstrap.js must remove the added resource: URIs on unload |
michael@0 | 316 | resourceDomains.forEach(domain => { |
michael@0 | 317 | resourceHandler.setSubstitution(domain, null); |
michael@0 | 318 | }) |
michael@0 | 319 | } |
michael@0 | 320 | } |
michael@0 | 321 | }; |
michael@0 | 322 | |
michael@0 | 323 | function nukeModules() { |
michael@0 | 324 | nukeTimer = null; |
michael@0 | 325 | // module objects store `exports` which comes from sandboxes |
michael@0 | 326 | // We should avoid keeping link to these object to avoid leaking sandboxes |
michael@0 | 327 | for (let key in loader.modules) { |
michael@0 | 328 | delete loader.modules[key]; |
michael@0 | 329 | } |
michael@0 | 330 | // Direct links to sandboxes should be removed too |
michael@0 | 331 | for (let key in loader.sandboxes) { |
michael@0 | 332 | let sandbox = loader.sandboxes[key]; |
michael@0 | 333 | delete loader.sandboxes[key]; |
michael@0 | 334 | // Bug 775067: From FF17 we can kill all CCW from a given sandbox |
michael@0 | 335 | unloadSandbox(sandbox); |
michael@0 | 336 | } |
michael@0 | 337 | loader = null; |
michael@0 | 338 | |
michael@0 | 339 | // both `toolkit/loader` and `system/xul-app` are loaded as JSM's via |
michael@0 | 340 | // `cuddlefish.js`, and needs to be unloaded to avoid memory leaks, when |
michael@0 | 341 | // the addon is unload. |
michael@0 | 342 | |
michael@0 | 343 | unloadSandbox(cuddlefishSandbox.loaderSandbox); |
michael@0 | 344 | unloadSandbox(cuddlefishSandbox.xulappSandbox); |
michael@0 | 345 | |
michael@0 | 346 | // Bug 764840: We need to unload cuddlefish otherwise it will stay alive |
michael@0 | 347 | // and keep a reference to this compartment. |
michael@0 | 348 | unloadSandbox(cuddlefishSandbox); |
michael@0 | 349 | cuddlefishSandbox = null; |
michael@0 | 350 | } |