1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/addon-sdk/source/lib/sdk/deprecated/traits-worker.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,656 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +/** 1.9 + * 1.10 + * `deprecated/traits-worker` was previously `content/worker` and kept 1.11 + * only due to `deprecated/symbiont` using it, which is necessary for 1.12 + * `widget`, until that reaches deprecation EOL. 1.13 + * 1.14 + */ 1.15 + 1.16 +"use strict"; 1.17 + 1.18 +module.metadata = { 1.19 + "stability": "deprecated" 1.20 +}; 1.21 + 1.22 +const { Trait } = require('./traits'); 1.23 +const { EventEmitter, EventEmitterTrait } = require('./events'); 1.24 +const { Ci, Cu, Cc } = require('chrome'); 1.25 +const timer = require('../timers'); 1.26 +const { URL } = require('../url'); 1.27 +const unload = require('../system/unload'); 1.28 +const observers = require('../system/events'); 1.29 +const { Cortex } = require('./cortex'); 1.30 +const { sandbox, evaluate, load } = require("../loader/sandbox"); 1.31 +const { merge } = require('../util/object'); 1.32 +const { getInnerId } = require("../window/utils"); 1.33 +const { getTabForWindow } = require('../tabs/helpers'); 1.34 +const { getTabForContentWindow } = require('../tabs/utils'); 1.35 + 1.36 +/* Trick the linker in order to ensure shipping these files in the XPI. 1.37 + require('../content/content-worker.js'); 1.38 + Then, retrieve URL of these files in the XPI: 1.39 +*/ 1.40 +let prefix = module.uri.split('deprecated/traits-worker.js')[0]; 1.41 +const CONTENT_WORKER_URL = prefix + 'content/content-worker.js'; 1.42 + 1.43 +// Fetch additional list of domains to authorize access to for each content 1.44 +// script. It is stored in manifest `metadata` field which contains 1.45 +// package.json data. This list is originaly defined by authors in 1.46 +// `permissions` attribute of their package.json addon file. 1.47 +const permissions = require('@loader/options').metadata['permissions'] || {}; 1.48 +const EXPANDED_PRINCIPALS = permissions['cross-domain-content'] || []; 1.49 + 1.50 +const JS_VERSION = '1.8'; 1.51 + 1.52 +const ERR_DESTROYED = 1.53 + "Couldn't find the worker to receive this message. " + 1.54 + "The script may not be initialized yet, or may already have been unloaded."; 1.55 + 1.56 +const ERR_FROZEN = "The page is currently hidden and can no longer be used " + 1.57 + "until it is visible again."; 1.58 + 1.59 + 1.60 +const WorkerSandbox = EventEmitter.compose({ 1.61 + 1.62 + /** 1.63 + * Emit a message to the worker content sandbox 1.64 + */ 1.65 + emit: function emit() { 1.66 + // First ensure having a regular array 1.67 + // (otherwise, `arguments` would be mapped to an object by `stringify`) 1.68 + let array = Array.slice(arguments); 1.69 + // JSON.stringify is buggy with cross-sandbox values, 1.70 + // it may return "{}" on functions. Use a replacer to match them correctly. 1.71 + function replacer(k, v) { 1.72 + return typeof v === "function" ? undefined : v; 1.73 + } 1.74 + // Ensure having an asynchronous behavior 1.75 + let self = this; 1.76 + timer.setTimeout(function () { 1.77 + self._emitToContent(JSON.stringify(array, replacer)); 1.78 + }, 0); 1.79 + }, 1.80 + 1.81 + /** 1.82 + * Synchronous version of `emit`. 1.83 + * /!\ Should only be used when it is strictly mandatory /!\ 1.84 + * Doesn't ensure passing only JSON values. 1.85 + * Mainly used by context-menu in order to avoid breaking it. 1.86 + */ 1.87 + emitSync: function emitSync() { 1.88 + let args = Array.slice(arguments); 1.89 + return this._emitToContent(args); 1.90 + }, 1.91 + 1.92 + /** 1.93 + * Tells if content script has at least one listener registered for one event, 1.94 + * through `self.on('xxx', ...)`. 1.95 + * /!\ Shouldn't be used. Implemented to avoid breaking context-menu API. 1.96 + */ 1.97 + hasListenerFor: function hasListenerFor(name) { 1.98 + return this._hasListenerFor(name); 1.99 + }, 1.100 + 1.101 + /** 1.102 + * Method called by the worker sandbox when it needs to send a message 1.103 + */ 1.104 + _onContentEvent: function onContentEvent(args) { 1.105 + // As `emit`, we ensure having an asynchronous behavior 1.106 + let self = this; 1.107 + timer.setTimeout(function () { 1.108 + // We emit event to chrome/addon listeners 1.109 + self._emit.apply(self, JSON.parse(args)); 1.110 + }, 0); 1.111 + }, 1.112 + 1.113 + /** 1.114 + * Configures sandbox and loads content scripts into it. 1.115 + * @param {Worker} worker 1.116 + * content worker 1.117 + */ 1.118 + constructor: function WorkerSandbox(worker) { 1.119 + this._addonWorker = worker; 1.120 + 1.121 + // Ensure that `emit` has always the right `this` 1.122 + this.emit = this.emit.bind(this); 1.123 + this.emitSync = this.emitSync.bind(this); 1.124 + 1.125 + // We receive a wrapped window, that may be an xraywrapper if it's content 1.126 + let window = worker._window; 1.127 + let proto = window; 1.128 + 1.129 + // Eventually use expanded principal sandbox feature, if some are given. 1.130 + // 1.131 + // But prevent it when the Worker isn't used for a content script but for 1.132 + // injecting `addon` object into a Panel, Widget, ... scope. 1.133 + // That's because: 1.134 + // 1/ It is useless to use multiple domains as the worker is only used 1.135 + // to communicate with the addon, 1.136 + // 2/ By using it it would prevent the document to have access to any JS 1.137 + // value of the worker. As JS values coming from multiple domain principals 1.138 + // can't be accessed by "mono-principals" (principal with only one domain). 1.139 + // Even if this principal is for a domain that is specified in the multiple 1.140 + // domain principal. 1.141 + let principals = window; 1.142 + let wantGlobalProperties = [] 1.143 + if (EXPANDED_PRINCIPALS.length > 0 && !worker._injectInDocument) { 1.144 + principals = EXPANDED_PRINCIPALS.concat(window); 1.145 + // We have to replace XHR constructor of the content document 1.146 + // with a custom cross origin one, automagically added by platform code: 1.147 + delete proto.XMLHttpRequest; 1.148 + wantGlobalProperties.push("XMLHttpRequest"); 1.149 + } 1.150 + 1.151 + // Instantiate trusted code in another Sandbox in order to prevent content 1.152 + // script from messing with standard classes used by proxy and API code. 1.153 + let apiSandbox = sandbox(principals, { wantXrays: true, sameZoneAs: window }); 1.154 + apiSandbox.console = console; 1.155 + 1.156 + // Create the sandbox and bind it to window in order for content scripts to 1.157 + // have access to all standard globals (window, document, ...) 1.158 + let content = this._sandbox = sandbox(principals, { 1.159 + sandboxPrototype: proto, 1.160 + wantXrays: true, 1.161 + wantGlobalProperties: wantGlobalProperties, 1.162 + sameZoneAs: window, 1.163 + metadata: { 1.164 + SDKContentScript: true, 1.165 + 'inner-window-id': getInnerId(window) 1.166 + } 1.167 + }); 1.168 + // We have to ensure that window.top and window.parent are the exact same 1.169 + // object than window object, i.e. the sandbox global object. But not 1.170 + // always, in case of iframes, top and parent are another window object. 1.171 + let top = window.top === window ? content : content.top; 1.172 + let parent = window.parent === window ? content : content.parent; 1.173 + merge(content, { 1.174 + // We need "this === window === top" to be true in toplevel scope: 1.175 + get window() content, 1.176 + get top() top, 1.177 + get parent() parent, 1.178 + // Use the Greasemonkey naming convention to provide access to the 1.179 + // unwrapped window object so the content script can access document 1.180 + // JavaScript values. 1.181 + // NOTE: this functionality is experimental and may change or go away 1.182 + // at any time! 1.183 + get unsafeWindow() window.wrappedJSObject 1.184 + }); 1.185 + 1.186 + // Load trusted code that will inject content script API. 1.187 + // We need to expose JS objects defined in same principal in order to 1.188 + // avoid having any kind of wrapper. 1.189 + load(apiSandbox, CONTENT_WORKER_URL); 1.190 + 1.191 + // prepare a clean `self.options` 1.192 + let options = 'contentScriptOptions' in worker ? 1.193 + JSON.stringify( worker.contentScriptOptions ) : 1.194 + undefined; 1.195 + 1.196 + // Then call `inject` method and communicate with this script 1.197 + // by trading two methods that allow to send events to the other side: 1.198 + // - `onEvent` called by content script 1.199 + // - `result.emitToContent` called by addon script 1.200 + // Bug 758203: We have to explicitely define `__exposedProps__` in order 1.201 + // to allow access to these chrome object attributes from this sandbox with 1.202 + // content priviledges 1.203 + // https://developer.mozilla.org/en/XPConnect_wrappers#Other_security_wrappers 1.204 + let chromeAPI = { 1.205 + timers: { 1.206 + setTimeout: timer.setTimeout, 1.207 + setInterval: timer.setInterval, 1.208 + clearTimeout: timer.clearTimeout, 1.209 + clearInterval: timer.clearInterval, 1.210 + __exposedProps__: { 1.211 + setTimeout: 'r', 1.212 + setInterval: 'r', 1.213 + clearTimeout: 'r', 1.214 + clearInterval: 'r' 1.215 + } 1.216 + }, 1.217 + sandbox: { 1.218 + evaluate: evaluate, 1.219 + __exposedProps__: { 1.220 + evaluate: 'r', 1.221 + } 1.222 + }, 1.223 + __exposedProps__: { 1.224 + timers: 'r', 1.225 + sandbox: 'r', 1.226 + } 1.227 + }; 1.228 + let onEvent = this._onContentEvent.bind(this); 1.229 + // `ContentWorker` is defined in CONTENT_WORKER_URL file 1.230 + let result = apiSandbox.ContentWorker.inject(content, chromeAPI, onEvent, options); 1.231 + this._emitToContent = result.emitToContent; 1.232 + this._hasListenerFor = result.hasListenerFor; 1.233 + 1.234 + // Handle messages send by this script: 1.235 + let self = this; 1.236 + // console.xxx calls 1.237 + this.on("console", function consoleListener(kind) { 1.238 + console[kind].apply(console, Array.slice(arguments, 1)); 1.239 + }); 1.240 + 1.241 + // self.postMessage calls 1.242 + this.on("message", function postMessage(data) { 1.243 + // destroyed? 1.244 + if (self._addonWorker) 1.245 + self._addonWorker._emit('message', data); 1.246 + }); 1.247 + 1.248 + // self.port.emit calls 1.249 + this.on("event", function portEmit(name, args) { 1.250 + // destroyed? 1.251 + if (self._addonWorker) 1.252 + self._addonWorker._onContentScriptEvent.apply(self._addonWorker, arguments); 1.253 + }); 1.254 + 1.255 + // unwrap, recreate and propagate async Errors thrown from content-script 1.256 + this.on("error", function onError({instanceOfError, value}) { 1.257 + if (self._addonWorker) { 1.258 + let error = value; 1.259 + if (instanceOfError) { 1.260 + error = new Error(value.message, value.fileName, value.lineNumber); 1.261 + error.stack = value.stack; 1.262 + error.name = value.name; 1.263 + } 1.264 + self._addonWorker._emit('error', error); 1.265 + } 1.266 + }); 1.267 + 1.268 + // Inject `addon` global into target document if document is trusted, 1.269 + // `addon` in document is equivalent to `self` in content script. 1.270 + if (worker._injectInDocument) { 1.271 + let win = window.wrappedJSObject ? window.wrappedJSObject : window; 1.272 + Object.defineProperty(win, "addon", { 1.273 + value: content.self 1.274 + } 1.275 + ); 1.276 + } 1.277 + 1.278 + // Inject our `console` into target document if worker doesn't have a tab 1.279 + // (e.g Panel, PageWorker, Widget). 1.280 + // `worker.tab` can't be used because bug 804935. 1.281 + if (!getTabForContentWindow(window)) { 1.282 + let win = window.wrappedJSObject ? window.wrappedJSObject : window; 1.283 + 1.284 + // export our chrome console to content window as described here: 1.285 + // https://developer.mozilla.org/en-US/docs/Components.utils.createObjectIn 1.286 + let con = Cu.createObjectIn(win); 1.287 + 1.288 + let genPropDesc = function genPropDesc(fun) { 1.289 + return { enumerable: true, configurable: true, writable: true, 1.290 + value: console[fun] }; 1.291 + } 1.292 + 1.293 + const properties = { 1.294 + log: genPropDesc('log'), 1.295 + info: genPropDesc('info'), 1.296 + warn: genPropDesc('warn'), 1.297 + error: genPropDesc('error'), 1.298 + debug: genPropDesc('debug'), 1.299 + trace: genPropDesc('trace'), 1.300 + dir: genPropDesc('dir'), 1.301 + group: genPropDesc('group'), 1.302 + groupCollapsed: genPropDesc('groupCollapsed'), 1.303 + groupEnd: genPropDesc('groupEnd'), 1.304 + time: genPropDesc('time'), 1.305 + timeEnd: genPropDesc('timeEnd'), 1.306 + profile: genPropDesc('profile'), 1.307 + profileEnd: genPropDesc('profileEnd'), 1.308 + __noSuchMethod__: { enumerable: true, configurable: true, writable: true, 1.309 + value: function() {} } 1.310 + }; 1.311 + 1.312 + Object.defineProperties(con, properties); 1.313 + Cu.makeObjectPropsNormal(con); 1.314 + 1.315 + win.console = con; 1.316 + }; 1.317 + 1.318 + // The order of `contentScriptFile` and `contentScript` evaluation is 1.319 + // intentional, so programs can load libraries like jQuery from script URLs 1.320 + // and use them in scripts. 1.321 + let contentScriptFile = ('contentScriptFile' in worker) ? worker.contentScriptFile 1.322 + : null, 1.323 + contentScript = ('contentScript' in worker) ? worker.contentScript : null; 1.324 + 1.325 + if (contentScriptFile) { 1.326 + if (Array.isArray(contentScriptFile)) 1.327 + this._importScripts.apply(this, contentScriptFile); 1.328 + else 1.329 + this._importScripts(contentScriptFile); 1.330 + } 1.331 + if (contentScript) { 1.332 + this._evaluate( 1.333 + Array.isArray(contentScript) ? contentScript.join(';\n') : contentScript 1.334 + ); 1.335 + } 1.336 + }, 1.337 + destroy: function destroy() { 1.338 + this.emitSync("detach"); 1.339 + this._sandbox = null; 1.340 + this._addonWorker = null; 1.341 + }, 1.342 + 1.343 + /** 1.344 + * JavaScript sandbox where all the content scripts are evaluated. 1.345 + * {Sandbox} 1.346 + */ 1.347 + _sandbox: null, 1.348 + 1.349 + /** 1.350 + * Reference to the addon side of the worker. 1.351 + * @type {Worker} 1.352 + */ 1.353 + _addonWorker: null, 1.354 + 1.355 + /** 1.356 + * Evaluates code in the sandbox. 1.357 + * @param {String} code 1.358 + * JavaScript source to evaluate. 1.359 + * @param {String} [filename='javascript:' + code] 1.360 + * Name of the file 1.361 + */ 1.362 + _evaluate: function(code, filename) { 1.363 + try { 1.364 + evaluate(this._sandbox, code, filename || 'javascript:' + code); 1.365 + } 1.366 + catch(e) { 1.367 + this._addonWorker._emit('error', e); 1.368 + } 1.369 + }, 1.370 + /** 1.371 + * Imports scripts to the sandbox by reading files under urls and 1.372 + * evaluating its source. If exception occurs during evaluation 1.373 + * `"error"` event is emitted on the worker. 1.374 + * This is actually an analog to the `importScript` method in web 1.375 + * workers but in our case it's not exposed even though content 1.376 + * scripts may be able to do it synchronously since IO operation 1.377 + * takes place in the UI process. 1.378 + */ 1.379 + _importScripts: function _importScripts(url) { 1.380 + let urls = Array.slice(arguments, 0); 1.381 + for each (let contentScriptFile in urls) { 1.382 + try { 1.383 + let uri = URL(contentScriptFile); 1.384 + if (uri.scheme === 'resource') 1.385 + load(this._sandbox, String(uri)); 1.386 + else 1.387 + throw Error("Unsupported `contentScriptFile` url: " + String(uri)); 1.388 + } 1.389 + catch(e) { 1.390 + this._addonWorker._emit('error', e); 1.391 + } 1.392 + } 1.393 + } 1.394 +}); 1.395 + 1.396 +/** 1.397 + * Message-passing facility for communication between code running 1.398 + * in the content and add-on process. 1.399 + * @see https://addons.mozilla.org/en-US/developers/docs/sdk/latest/modules/sdk/content/worker.html 1.400 + */ 1.401 +const Worker = EventEmitter.compose({ 1.402 + on: Trait.required, 1.403 + _removeAllListeners: Trait.required, 1.404 + 1.405 + // List of messages fired before worker is initialized 1.406 + get _earlyEvents() { 1.407 + delete this._earlyEvents; 1.408 + this._earlyEvents = []; 1.409 + return this._earlyEvents; 1.410 + }, 1.411 + 1.412 + /** 1.413 + * Sends a message to the worker's global scope. Method takes single 1.414 + * argument, which represents data to be sent to the worker. The data may 1.415 + * be any primitive type value or `JSON`. Call of this method asynchronously 1.416 + * emits `message` event with data value in the global scope of this 1.417 + * symbiont. 1.418 + * 1.419 + * `message` event listeners can be set either by calling 1.420 + * `self.on` with a first argument string `"message"` or by 1.421 + * implementing `onMessage` function in the global scope of this worker. 1.422 + * @param {Number|String|JSON} data 1.423 + */ 1.424 + postMessage: function (data) { 1.425 + let args = ['message'].concat(Array.slice(arguments)); 1.426 + if (!this._inited) { 1.427 + this._earlyEvents.push(args); 1.428 + return; 1.429 + } 1.430 + processMessage.apply(this, args); 1.431 + }, 1.432 + 1.433 + /** 1.434 + * EventEmitter, that behaves (calls listeners) asynchronously. 1.435 + * A way to send customized messages to / from the worker. 1.436 + * Events from in the worker can be observed / emitted via 1.437 + * worker.on / worker.emit. 1.438 + */ 1.439 + get port() { 1.440 + // We generate dynamically this attribute as it needs to be accessible 1.441 + // before Worker.constructor gets called. (For ex: Panel) 1.442 + 1.443 + // create an event emitter that receive and send events from/to the worker 1.444 + this._port = EventEmitterTrait.create({ 1.445 + emit: this._emitEventToContent.bind(this) 1.446 + }); 1.447 + 1.448 + // expose wrapped port, that exposes only public properties: 1.449 + // We need to destroy this getter in order to be able to set the 1.450 + // final value. We need to update only public port attribute as we never 1.451 + // try to access port attribute from private API. 1.452 + delete this._public.port; 1.453 + this._public.port = Cortex(this._port); 1.454 + // Replicate public port to the private object 1.455 + delete this.port; 1.456 + this.port = this._public.port; 1.457 + 1.458 + return this._port; 1.459 + }, 1.460 + 1.461 + /** 1.462 + * Same object than this.port but private API. 1.463 + * Allow access to _emit, in order to send event to port. 1.464 + */ 1.465 + _port: null, 1.466 + 1.467 + /** 1.468 + * Emit a custom event to the content script, 1.469 + * i.e. emit this event on `self.port` 1.470 + */ 1.471 + _emitEventToContent: function () { 1.472 + let args = ['event'].concat(Array.slice(arguments)); 1.473 + if (!this._inited) { 1.474 + this._earlyEvents.push(args); 1.475 + return; 1.476 + } 1.477 + processMessage.apply(this, args); 1.478 + }, 1.479 + 1.480 + // Is worker connected to the content worker sandbox ? 1.481 + _inited: false, 1.482 + 1.483 + // Is worker being frozen? i.e related document is frozen in bfcache. 1.484 + // Content script should not be reachable if frozen. 1.485 + _frozen: true, 1.486 + 1.487 + constructor: function Worker(options) { 1.488 + options = options || {}; 1.489 + 1.490 + if ('contentScriptFile' in options) 1.491 + this.contentScriptFile = options.contentScriptFile; 1.492 + if ('contentScriptOptions' in options) 1.493 + this.contentScriptOptions = options.contentScriptOptions; 1.494 + if ('contentScript' in options) 1.495 + this.contentScript = options.contentScript; 1.496 + 1.497 + this._setListeners(options); 1.498 + 1.499 + unload.ensure(this._public, "destroy"); 1.500 + 1.501 + // Ensure that worker._port is initialized for contentWorker to be able 1.502 + // to send events during worker initialization. 1.503 + this.port; 1.504 + 1.505 + this._documentUnload = this._documentUnload.bind(this); 1.506 + this._pageShow = this._pageShow.bind(this); 1.507 + this._pageHide = this._pageHide.bind(this); 1.508 + 1.509 + if ("window" in options) this._attach(options.window); 1.510 + }, 1.511 + 1.512 + _setListeners: function(options) { 1.513 + if ('onError' in options) 1.514 + this.on('error', options.onError); 1.515 + if ('onMessage' in options) 1.516 + this.on('message', options.onMessage); 1.517 + if ('onDetach' in options) 1.518 + this.on('detach', options.onDetach); 1.519 + }, 1.520 + 1.521 + _attach: function(window) { 1.522 + this._window = window; 1.523 + // Track document unload to destroy this worker. 1.524 + // We can't watch for unload event on page's window object as it 1.525 + // prevents bfcache from working: 1.526 + // https://developer.mozilla.org/En/Working_with_BFCache 1.527 + this._windowID = getInnerId(this._window); 1.528 + observers.on("inner-window-destroyed", this._documentUnload); 1.529 + 1.530 + // Listen to pagehide event in order to freeze the content script 1.531 + // while the document is frozen in bfcache: 1.532 + this._window.addEventListener("pageshow", this._pageShow, true); 1.533 + this._window.addEventListener("pagehide", this._pageHide, true); 1.534 + 1.535 + // will set this._contentWorker pointing to the private API: 1.536 + this._contentWorker = WorkerSandbox(this); 1.537 + 1.538 + // Mainly enable worker.port.emit to send event to the content worker 1.539 + this._inited = true; 1.540 + this._frozen = false; 1.541 + 1.542 + // Process all events and messages that were fired before the 1.543 + // worker was initialized. 1.544 + this._earlyEvents.forEach((function (args) { 1.545 + processMessage.apply(this, args); 1.546 + }).bind(this)); 1.547 + }, 1.548 + 1.549 + _documentUnload: function _documentUnload({ subject, data }) { 1.550 + let innerWinID = subject.QueryInterface(Ci.nsISupportsPRUint64).data; 1.551 + if (innerWinID != this._windowID) return false; 1.552 + this._workerCleanup(); 1.553 + return true; 1.554 + }, 1.555 + 1.556 + _pageShow: function _pageShow() { 1.557 + this._contentWorker.emitSync("pageshow"); 1.558 + this._emit("pageshow"); 1.559 + this._frozen = false; 1.560 + }, 1.561 + 1.562 + _pageHide: function _pageHide() { 1.563 + this._contentWorker.emitSync("pagehide"); 1.564 + this._emit("pagehide"); 1.565 + this._frozen = true; 1.566 + }, 1.567 + 1.568 + get url() { 1.569 + // this._window will be null after detach 1.570 + return this._window ? this._window.document.location.href : null; 1.571 + }, 1.572 + 1.573 + get tab() { 1.574 + // this._window will be null after detach 1.575 + if (this._window) 1.576 + return getTabForWindow(this._window); 1.577 + return null; 1.578 + }, 1.579 + 1.580 + /** 1.581 + * Tells content worker to unload itself and 1.582 + * removes all the references from itself. 1.583 + */ 1.584 + destroy: function destroy() { 1.585 + this._workerCleanup(); 1.586 + this._inited = true; 1.587 + this._removeAllListeners(); 1.588 + }, 1.589 + 1.590 + /** 1.591 + * Remove all internal references to the attached document 1.592 + * Tells _port to unload itself and removes all the references from itself. 1.593 + */ 1.594 + _workerCleanup: function _workerCleanup() { 1.595 + // maybe unloaded before content side is created 1.596 + // As Symbiont call worker.constructor on document load 1.597 + if (this._contentWorker) 1.598 + this._contentWorker.destroy(); 1.599 + this._contentWorker = null; 1.600 + if (this._window) { 1.601 + this._window.removeEventListener("pageshow", this._pageShow, true); 1.602 + this._window.removeEventListener("pagehide", this._pageHide, true); 1.603 + } 1.604 + this._window = null; 1.605 + // This method may be called multiple times, 1.606 + // avoid dispatching `detach` event more than once 1.607 + if (this._windowID) { 1.608 + this._windowID = null; 1.609 + observers.off("inner-window-destroyed", this._documentUnload); 1.610 + this._earlyEvents.length = 0; 1.611 + this._emit("detach"); 1.612 + } 1.613 + this._inited = false; 1.614 + }, 1.615 + 1.616 + /** 1.617 + * Receive an event from the content script that need to be sent to 1.618 + * worker.port. Provide a way for composed object to catch all events. 1.619 + */ 1.620 + _onContentScriptEvent: function _onContentScriptEvent() { 1.621 + this._port._emit.apply(this._port, arguments); 1.622 + }, 1.623 + 1.624 + /** 1.625 + * Reference to the content side of the worker. 1.626 + * @type {WorkerGlobalScope} 1.627 + */ 1.628 + _contentWorker: null, 1.629 + 1.630 + /** 1.631 + * Reference to the window that is accessible from 1.632 + * the content scripts. 1.633 + * @type {Object} 1.634 + */ 1.635 + _window: null, 1.636 + 1.637 + /** 1.638 + * Flag to enable `addon` object injection in document. (bug 612726) 1.639 + * @type {Boolean} 1.640 + */ 1.641 + _injectInDocument: false 1.642 +}); 1.643 + 1.644 +/** 1.645 + * Fired from postMessage and _emitEventToContent, or from the _earlyMessage 1.646 + * queue when fired before the content is loaded. Sends arguments to 1.647 + * contentWorker if able 1.648 + */ 1.649 + 1.650 +function processMessage () { 1.651 + if (!this._contentWorker) 1.652 + throw new Error(ERR_DESTROYED); 1.653 + if (this._frozen) 1.654 + throw new Error(ERR_FROZEN); 1.655 + 1.656 + this._contentWorker.emit.apply(null, Array.slice(arguments)); 1.657 +} 1.658 + 1.659 +exports.Worker = Worker;