diff options
Diffstat (limited to 'tools/addon-sdk-1.3/packages/api-utils/lib/content')
4 files changed, 0 insertions, 1799 deletions
diff --git a/tools/addon-sdk-1.3/packages/api-utils/lib/content/content-proxy.js b/tools/addon-sdk-1.3/packages/api-utils/lib/content/content-proxy.js deleted file mode 100644 index ae1222f..0000000 --- a/tools/addon-sdk-1.3/packages/api-utils/lib/content/content-proxy.js +++ /dev/null @@ -1,798 +0,0 @@ -const { Ci, Cc } = require("chrome"); -const xulApp = require("xul-app"); - -/** - * Access key that allows privileged code to unwrap proxy wrappers through - * valueOf: - * let xpcWrapper = proxyWrapper.valueOf(UNWRAP_ACCESS_KEY); - */ -const UNWRAP_ACCESS_KEY = {}; -exports.UNWRAP_ACCESS_KEY = UNWRAP_ACCESS_KEY; - - /** - * Returns a closure that wraps arguments before calling the given function, - * which can be given to native functions that accept a function, such that when - * the closure is called, the given function is called with wrapped arguments. - * - * @param fun {Function} - * the function for which to create a closure wrapping its arguments - * @param obj {Object} - * target object from which `fun` comes from - * (optional, for debugging purpose) - * @param name {String} - * name of the attribute from which `fun` is binded on `obj` - * (optional, for debugging purpose) - * - * Example: - * function contentScriptListener(event) {} - * let wrapper = ContentScriptFunctionWrapper(contentScriptListener); - * xray.addEventListener("...", wrapper, false); - * -> Allow to `event` to be wrapped - */ -function ContentScriptFunctionWrapper(fun, obj, name) { - if ("___proxy" in fun && typeof fun.___proxy == "function") - return fun.___proxy; - - let wrappedFun = function () { - let args = []; - for (let i = 0, l = arguments.length; i < l; i++) - args.push(wrap(arguments[i])); - - //console.log("Called from native :"+obj+"."+name); - //console.log(">args "+arguments.length); - //console.log(fun); - - // Native code can execute this callback with `this` being the wrapped - // function. For example, window.mozRequestAnimationFrame. - if (this == wrappedFun) - return fun.apply(fun, args); - - return fun.apply(wrap(this), args); - }; - - Object.defineProperty(fun, "___proxy", {value : wrappedFun, - writable : false, - enumerable : false, - configurable : false}); - - return wrappedFun; -} - -/** - * Returns a closure that unwraps arguments before calling the `fun` function, - * which can be used to build a wrapper for a native function that accepts - * wrapped arguments, since native function only accept unwrapped arguments. - * - * @param fun {Function} - * the function to wrap - * @param originalObject {Object} - * target object from which `fun` comes from - * (optional, for debugging purpose) - * @param name {String} - * name of the attribute from which `fun` is binded on `originalObject` - * (optional, for debugging purpose) - * - * Example: - * wrapper.appendChild = NativeFunctionWrapper(xray.appendChild, xray); - * wrapper.appendChild(anotherWrapper); - * -> Allow to call xray.appendChild with unwrapped version of anotherWrapper - */ -function NativeFunctionWrapper(fun, originalObject, name) { - return function () { - let args = []; - let obj = this.valueOf ? this.valueOf(UNWRAP_ACCESS_KEY) : this; - - for (let i = 0, l = arguments.length; i < l; i++) - args.push( unwrap(arguments[i], obj, name) ); - - //if (name != "toString") - //console.log(">>calling native ["+(name?name:'#closure#')+"]: \n"+fun.apply+"\n"+obj+"\n("+args.join(', ')+")\nthis :"+obj+"from:"+originalObject+"\n"); - - // Need to use Function.prototype.apply.apply because XMLHttpRequest - // is a function (typeof return 'function') and fun.apply is null :/ - let unwrapResult = Function.prototype.apply.apply(fun, [obj, args]); - let result = wrap(unwrapResult, obj, name); - - //console.log("<< "+rr+" -> "+r); - - return result; - }; -} - -/* - * Unwrap a JS value that comes from the content script. - * Mainly converts proxy wrapper to XPCNativeWrapper. - */ -function unwrap(value, obj, name) { - //console.log("unwrap : "+value+" ("+name+")"); - if (!value) - return value; - let type = typeof value; - - // In case of proxy, unwrap them recursively - // (it should not be recursive, just in case of) - if (["object", "function"].indexOf(type) !== -1 && - "__isWrappedProxy" in value) { - while("__isWrappedProxy" in value) - value = value.valueOf(UNWRAP_ACCESS_KEY); - return value; - } - - // In case of functions we need to return a wrapper that converts native - // arguments applied to this function into proxies. - if (type == "function") - return ContentScriptFunctionWrapper(value, obj, name); - - // We must wrap objects coming from content script too, as they may have - // a function that will be called by a native method. - // For example: - // addEventListener(..., { handleEvent: function(event) {} }, ...); - if (type == "object") - return ContentScriptObjectWrapper(value); - - if (["string", "number", "boolean"].indexOf(type) !== -1) - return value; - //console.log("return non-wrapped to native : "+typeof value+" -- "+value); - return value; -} - -/** - * Returns a XrayWrapper proxy object that allow to wrap any of its function - * though `ContentScriptFunctionWrapper`. These proxies are given to - * XrayWrappers in order to automatically wrap values when they call a method - * of these proxies. So that they are only used internaly and content script, - * nor web page have ever access to them. As a conclusion, we can consider - * this code as being safe regarding web pages overload. - * - * - * @param obj {Object} - * object coming from content script context to wrap - * - * Example: - * let myListener = { handleEvent: function (event) {} }; - * node.addEventListener("click", myListener, false); - * `event` has to be wrapped, so handleEvent has to be wrapped using - * `ContentScriptFunctionWrapper` function. - * In order to do so, we build this new kind of proxies. - */ -function ContentScriptObjectWrapper(obj) { - if ("___proxy" in obj && typeof obj.___proxy == "object") - return obj.___proxy; - - function valueOf(key) { - if (key === UNWRAP_ACCESS_KEY) - return obj; - return this; - } - - let proxy = Proxy.create({ - // Fundamental traps - getPropertyDescriptor: function(name) { - return Object.getOwnPropertyDescriptor(obj, name); - }, - defineProperty: function(name, desc) { - return Object.defineProperty(obj, name, desc); - }, - getOwnPropertyNames: function () { - return Object.getOwnPropertyNames(obj); - }, - delete: function(name) { - return delete obj[name]; - }, - // derived traps - has: function(name) { - return name === "__isXrayWrapperProxy" || - name in obj; - }, - hasOwn: function(name) { - return Object.prototype.hasOwnProperty.call(obj, name); - }, - get: function(receiver, name) { - if (name == "valueOf") - return valueOf; - let value = obj[name]; - if (!value) - return value; - - return unwrap(value); - }, - set: function(receiver, name, val) { - obj[name] = val; - return true; - }, - enumerate: function() { - var result = []; - for each (let name in obj) { - result.push(name); - }; - return result; - }, - keys: function() { - return Object.keys(obj); - } - }); - - Object.defineProperty(obj, "___proxy", {value : proxy, - writable : false, - enumerable : false, - configurable : false}); - - return proxy; -} - -/* - * Wrap a JS value coming from the document by building a proxy wrapper. - */ -function wrap(value, obj, name, debug) { - if (!value) - return value; - let type = typeof value; - if (type == "object") { - // We may have a XrayWrapper proxy. - // For example: - // let myListener = { handleEvent: function () {} }; - // node.addEventListener("click", myListener, false); - // When native code want to call handleEvent, - // we go though ContentScriptFunctionWrapper that calls `wrap(this)` - // `this` is the XrayWrapper proxy of myListener. - // We return this object without building a CS proxy as it is already - // a value coming from the CS. - if ("__isXrayWrapperProxy" in value) - return value.valueOf(UNWRAP_ACCESS_KEY); - - // Unwrap object before wrapping it. - // It should not happen with CS proxy objects. - while("__isWrappedProxy" in value) { - value = value.valueOf(UNWRAP_ACCESS_KEY); - } - - if (XPCNativeWrapper.unwrap(value) !== value) - return getProxyForObject(value); - // In case of Event, HTMLCollection or NodeList or ??? - // XPCNativeWrapper.unwrap(value) === value - // but it's still a XrayWrapper so let's build a proxy - return getProxyForObject(value); - } - if (type == "function") { - if (XPCNativeWrapper.unwrap(value) !== value - || (typeof value.toString === "function" && - value.toString().match(/\[native code\]/))) { - return getProxyForFunction(value, NativeFunctionWrapper(value, obj, name)); - } - return value; - } - if (type == "string") - return value; - if (type == "number") - return value; - if (type == "boolean") - return value; - //console.log("return non-wrapped to wrapped : "+value); - return value; -} - -/* - * Wrap an object from the document to a proxy wrapper - */ -function getProxyForObject(obj) { - if (typeof obj != "object") { - let msg = "tried to proxify something other than an object: " + typeof obj; - console.warn(msg); - throw msg; - } - if ("__isWrappedProxy" in obj) { - return obj; - } - // Check if there is a proxy cached on this wrapper, - // but take care of prototype ___proxy attribute inheritance! - if (obj && obj.___proxy && obj.___proxy.valueOf(UNWRAP_ACCESS_KEY) === obj) { - return obj.___proxy; - } - - let proxy = Proxy.create(handlerMaker(obj)); - - Object.defineProperty(obj, "___proxy", {value : proxy, - writable : false, - enumerable : false, - configurable : false}); - return proxy; -} - -/* - * Wrap a function from the document to a proxy wrapper - */ -function getProxyForFunction(fun, callTrap) { - if (typeof fun != "function") { - let msg = "tried to proxify something other than a function: " + typeof fun; - console.warn(msg); - throw msg; - } - if ("__isWrappedProxy" in fun) - return obj; - if ("___proxy" in fun) - return fun.___proxy; - - let proxy = Proxy.createFunction(handlerMaker(fun), callTrap); - - Object.defineProperty(fun, "___proxy", {value : proxy, - writable : false, - enumerable : false, - configurable : false}); - - return proxy; -} - -/* - * Check if a DOM attribute name is an event name. - */ -function isEventName(id) { - if (id.indexOf("on") != 0 || id.length == 2) - return false; - // Taken from: - // http://mxr.mozilla.org/mozilla-central/source/dom/base/nsDOMClassInfo.cpp#7616 - switch (id[2]) { - case 'a' : - return (id == "onabort" || - id == "onafterscriptexecute" || - id == "onafterprint"); - case 'b' : - return (id == "onbeforeunload" || - id == "onbeforescriptexecute" || - id == "onblur" || - id == "onbeforeprint"); - case 'c' : - return (id == "onchange" || - id == "onclick" || - id == "oncontextmenu" || - id == "oncopy" || - id == "oncut" || - id == "oncanplay" || - id == "oncanplaythrough"); - case 'd' : - return (id == "ondblclick" || - id == "ondrag" || - id == "ondragend" || - id == "ondragenter" || - id == "ondragleave" || - id == "ondragover" || - id == "ondragstart" || - id == "ondrop" || - id == "ondurationchange"); - case 'e' : - return (id == "onerror" || - id == "onemptied" || - id == "onended"); - case 'f' : - return id == "onfocus"; - case 'h' : - return id == "onhashchange"; - case 'i' : - return (id == "oninput" || - id == "oninvalid"); - case 'k' : - return (id == "onkeydown" || - id == "onkeypress" || - id == "onkeyup"); - case 'l' : - return (id == "onload" || - id == "onloadeddata" || - id == "onloadedmetadata" || - id == "onloadstart"); - case 'm' : - return (id == "onmousemove" || - id == "onmouseout" || - id == "onmouseover" || - id == "onmouseup" || - id == "onmousedown" || - id == "onmessage"); - case 'p' : - return (id == "onpaint" || - id == "onpageshow" || - id == "onpagehide" || - id == "onpaste" || - id == "onpopstate" || - id == "onpause" || - id == "onplay" || - id == "onplaying" || - id == "onprogress"); - case 'r' : - return (id == "onreadystatechange" || - id == "onreset" || - id == "onresize" || - id == "onratechange"); - case 's' : - return (id == "onscroll" || - id == "onselect" || - id == "onsubmit" || - id == "onseeked" || - id == "onseeking" || - id == "onstalled" || - id == "onsuspend"); - case 't': - return id == "ontimeupdate" - /* - // TODO: Make it work for mobile version - || - (nsDOMTouchEvent::PrefEnabled() && - (id == "ontouchstart" || - id == "ontouchend" || - id == "ontouchmove" || - id == "ontouchenter" || - id == "ontouchleave" || - id == "ontouchcancel"))*/; - - case 'u' : - return id == "onunload"; - case 'v': - return id == "onvolumechange"; - case 'w': - return id == "onwaiting"; - } - - return false; -} - -// XrayWrappers miss some attributes. -// Here is a list of functions that return a value when it detects a miss: -const NODES_INDEXED_BY_NAME = ["IMG", "FORM", "APPLET", "EMBED", "OBJECT"]; -const xRayWrappersMissFixes = [ - - // Fix bug with XPCNativeWrapper on HTMLCollection - // We can only access array item once, then it's undefined :o - function (obj, name) { - let i = parseInt(name); - if (obj.toString().match(/HTMLCollection|NodeList/) && - i >= 0 && i < obj.length) { - return wrap(XPCNativeWrapper(obj.wrappedJSObject[name]), obj, name); - } - return null; - }, - - // Trap access to document["form name"] - // that may refer to an existing form node - // http://mxr.mozilla.org/mozilla-central/source/dom/base/nsDOMClassInfo.cpp#9285 - function (obj, name) { - if ("nodeType" in obj && obj.nodeType == 9) { - let node = obj.wrappedJSObject[name]; - // List of supported tag: - // http://mxr.mozilla.org/mozilla-central/source/content/html/content/src/nsGenericHTMLElement.cpp#1267 - if (node && NODES_INDEXED_BY_NAME.indexOf(node.tagName) != -1) - return wrap(XPCNativeWrapper(node)); - } - return null; - }, - - // Trap access to window["frame name"] and window.frames[i] - // that refer to an (i)frame internal window object - // http://mxr.mozilla.org/mozilla-central/source/dom/base/nsDOMClassInfo.cpp#6824 - function (obj, name) { - if (typeof obj == "object" && "document" in obj) { - // Ensure that we are on a window object - try { - obj.QueryInterface(Ci.nsIDOMWindow); - } - catch(e) { - return null; - } - // Integer case: - let i = parseInt(name); - if (i >= 0 && i in obj) { - return wrap(XPCNativeWrapper(obj[i])); - } - // String name case: - if (name in obj.wrappedJSObject) { - let win = obj.wrappedJSObject[name]; - let nodes = obj.document.getElementsByName(name); - for (let i = 0, l = nodes.length; i < l; i++) { - let node = nodes[i]; - if ("contentWindow" in node && - node.contentWindow.wrappedJSObject == win) - return wrap(node.contentWindow); - } - } - } - return null; - }, - - // Trap access to form["node name"] - // http://mxr.mozilla.org/mozilla-central/source/dom/base/nsDOMClassInfo.cpp#9477 - function (obj, name) { - if (typeof obj == "object" && obj.tagName == "FORM") { - let match = obj.wrappedJSObject[name]; - let nodes = obj.ownerDocument.getElementsByName(name); - for (let i = 0, l = nodes.length; i < l; i++) { - let node = nodes[i]; - if (node.wrappedJSObject == match) - return wrap(node); - } - } - } - -]; - -if (!xulApp.versionInRange(xulApp.platformVersion, "10.0a1", "*")) { - // Fix XPathResult's constants being undefined on XrayWrappers - // these constants are defined here: - // http://mxr.mozilla.org/mozilla-central/source/dom/interfaces/xpath/nsIDOMXPathResult.idl - // and are only numbers. - // The platform bug 665279 was fixed in Gecko 10.0a1. - // FIXME: remove this workaround once the SDK no longer supports Firefox 9. - xRayWrappersMissFixes.push( - function (obj, name) { - if (typeof obj == "object" && name in Ci.nsIDOMXPathResult) { - let value = Ci.nsIDOMXPathResult[name]; - if (typeof value == "number" && value === obj.wrappedJSObject[name]) - return value; - } - } - ); -} - -// XrayWrappers have some buggy methods. -// Here is the list of them with functions returning some replacement -// for a given object `obj`: -const xRayWrappersMethodsFixes = { - // postMessage method is checking the Javascript global - // and it expects it to be a window object. - // But in our case, the global object is our sandbox global object. - // See nsGlobalWindow::CallerInnerWindow(): - // http://mxr.mozilla.org/mozilla-central/source/dom/base/nsGlobalWindow.cpp#5893 - // nsCOMPtr<nsPIDOMWindow> win = do_QueryWrappedNative(wrapper); - // win is null - postMessage: function (obj) { - // Ensure that we are on a window object - try { - obj.QueryInterface(Ci.nsIDOMWindow); - } - catch(e) { - return null; - } - // Create a wrapper that is going to call `postMessage` through `eval` - let f = function postMessage(message, targetOrigin) { - message = message.toString().replace(/'/g,"\\'"); - targetOrigin = targetOrigin.toString().replace(/'/g,"\\'"); - let jscode = "this.postMessage('" + message + "', '" + - targetOrigin + "')"; - return this.wrappedJSObject.eval(jscode); - }; - return getProxyForFunction(f, NativeFunctionWrapper(f)); - }, - - // Fix mozMatchesSelector uses that is broken on XrayWrappers - // when we use document.documentElement.mozMatchesSelector.call(node, expr) - // It's only working if we call mozMatchesSelector on the node itself. - // SEE BUG 658909: mozMatchesSelector returns incorrect results with XrayWrappers - mozMatchesSelector: function (obj) { - // Ensure that we are on an object to expose this buggy method - try { - obj.QueryInterface(Ci.nsIDOMNSElement); - } - catch(e) { - return null; - } - // We can't use `wrap` function as `f` is not a native function, - // so wrap it manually: - let f = function mozMatchesSelector(selectors) { - return this.mozMatchesSelector(selectors); - }; - - return getProxyForFunction(f, NativeFunctionWrapper(f)); - }, - - // Bug 679054: History API doesn't work with Proxy objects. - // We have to pass regular JS objects on `pushState` and replaceState methods. - pushState: function (obj) { - // Ensure that we are on an object that expose History API - try { - obj.QueryInterface(Ci.nsIDOMHistory); - } - catch(e) { - return null; - } - let f = function fix() { - // Call native method with JSON objects - // (need to convert `arguments` to an array via `slice`) - return this.pushState.apply(this, JSON.parse(JSON.stringify(Array.slice(arguments)))); - }; - - return getProxyForFunction(f, NativeFunctionWrapper(f)); - }, - replaceState: function (obj) { - // Ensure that we are on an object that expose History API - try { - obj.QueryInterface(Ci.nsIDOMHistory); - } - catch(e) { - return null; - } - let f = function fix() { - // Call native method with JSON objects - // (need to convert `arguments` to an array via `slice`) - return this.replaceState.apply(this, JSON.parse(JSON.stringify(Array.slice(arguments)))); - }; - - return getProxyForFunction(f, NativeFunctionWrapper(f)); - } -}; - -/* - * Generate handler for proxy wrapper - */ -function handlerMaker(obj) { - // Overloaded attributes dictionary - let overload = {}; - // Expando attributes dictionary (i.e. onclick, onfocus, on* ...) - let expando = {}; - // Cache of methods overloaded to fix XrayWrapper bug - let methodFixes = {}; - return { - // Fundamental traps - getPropertyDescriptor: function(name) { - return Object.getOwnPropertyDescriptor(obj, name); - }, - defineProperty: function(name, desc) { - return Object.defineProperty(obj, name, desc); - }, - getOwnPropertyNames: function () { - return Object.getOwnPropertyNames(obj); - }, - delete: function(name) { - delete expando[name]; - delete overload[name]; - return delete obj[name]; - }, - - // derived traps - has: function(name) { - if (name == "___proxy") return false; - if (isEventName(name)) { - // XrayWrappers throw exception when we try to access expando attributes - // even on "name in wrapper". So avoid doing it! - return name in expando; - } - return name in obj || name in overload || name == "__isWrappedProxy"; - }, - hasOwn: function(name) { - return Object.prototype.hasOwnProperty.call(obj, name); - }, - get: function(receiver, name) { - - if (name == "___proxy") - return undefined; - - // Overload toString in order to avoid returning "[XrayWrapper [object HTMLElement]]" - // or "[object Function]" for function's Proxy - if (name == "toString") - return wrap(obj.wrappedJSObject ? obj.wrappedJSObject.toString - : obj.toString, - obj, name); - - // Offer a way to retrieve XrayWrapper from a proxified node through `valueOf` - if (name == "valueOf") - return function (key) { - if (key === UNWRAP_ACCESS_KEY) - return obj; - return this; - }; - - // Return overloaded value if there is one. - // It allows to overload native methods like addEventListener that - // are not saved, even on the wrapper itself. - // (And avoid some methods like toSource from being returned here! [__proto__ test]) - if (name in overload && - overload[name] != Object.getPrototypeOf(overload)[name] && - name != "__proto__") { - return overload[name]; - } - - // Catch exceptions thrown by XrayWrappers when we try to access on* - // attributes like onclick, onfocus, ... - if (isEventName(name)) { - //console.log("expando:"+obj+" - "+obj.nodeType); - return name in expando ? expando[name].original : undefined; - } - - // Overload some XrayWrappers method in order to fix its bugs - if (name in methodFixes && - methodFixes[name] != Object.getPrototypeOf(methodFixes)[name] && - name != "__proto__") - return methodFixes[name]; - if (Object.keys(xRayWrappersMethodsFixes).indexOf(name) !== -1) { - let fix = xRayWrappersMethodsFixes[name](obj); - if (fix) - return methodFixes[name] = fix; - } - - let o = obj[name]; - - // XrayWrapper miss some attributes, try to catch these and return a value - if (!o) { - for each(let atttributeFixer in xRayWrappersMissFixes) { - let fix = atttributeFixer(obj, name); - if (fix) - return fix; - } - } - - // Generic case - return wrap(o, obj, name); - - }, - - set: function(receiver, name, val) { - - if (isEventName(name)) { - //console.log("SET on* attribute : " + name + " / " + val + "/" + obj); - let shortName = name.replace(/^on/,""); - - // Unregister previously set listener - if (expando[name]) { - obj.removeEventListener(shortName, expando[name], true); - delete expando[name]; - } - - // Only accept functions - if (typeof val != "function") - return false; - - // Register a new listener - let original = val; - val = ContentScriptFunctionWrapper(val); - expando[name] = val; - val.original = original; - obj.addEventListener(name.replace(/^on/, ""), val, true); - return true; - } - - obj[name] = val; - - // Handle native method not overloaded on XrayWrappers: - // obj.addEventListener = val; -> obj.addEventlistener = native method - // And, XPCNativeWrapper bug where nested values appear to be wrapped: - // obj.customNestedAttribute = val -> obj.customNestedAttribute !== val - // obj.customNestedAttribute = "waive wrapper something" - // SEE BUG 658560: Fix identity problem with CrossOriginWrappers - // TODO: check that DOM can't be updated by the document itself and so overloaded value becomes wrong - // but I think such behavior is limited to primitive type - if ((typeof val == "function" || typeof val == "object") && name) { - overload[name] = val; - } - - return true; - }, - - enumerate: function() { - var result = []; - for each (name in Object.keys(obj)) { - result.push(name); - }; - return result; - }, - - keys: function() { - return Object.keys(obj); - } - }; -}; - - -/* - * Wrap an object from the document to a proxy wrapper. - */ -exports.create = function create(object) { - // We accept either a XrayWrapper or an unwrapped reference - if ("wrappedJSObject" in object) - object = object.wrappedJSObject; - - let xpcWrapper = XPCNativeWrapper(object); - - // If we can't build an XPCNativeWrapper, it doesn't make sense to build - // a proxy. All proxy code is based on having such wrapper that store - // different JS attributes set. - // (we can't build XPCNativeWrapper when object is from the same - // principal/domain) - if (object === xpcWrapper) { - return object; - } - return getProxyForObject(xpcWrapper); -} diff --git a/tools/addon-sdk-1.3/packages/api-utils/lib/content/loader.js b/tools/addon-sdk-1.3/packages/api-utils/lib/content/loader.js deleted file mode 100644 index 25bc651..0000000 --- a/tools/addon-sdk-1.3/packages/api-utils/lib/content/loader.js +++ /dev/null @@ -1,203 +0,0 @@ -/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* vim:set ts=2 sw=2 sts=2 et: */ -/* ***** BEGIN LICENSE BLOCK ***** - * Version: MPL 1.1/GPL 2.0/LGPL 2.1 - * - * The contents of this file are subject to the Mozilla Public License Version - * 1.1 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * http://www.mozilla.org/MPL/ - * - * Software distributed under the License is distributed on an "AS IS" basis, - * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License - * for the specific language governing rights and limitations under the - * License. - * - * The Original Code is Jetpack. - * - * The Initial Developer of the Original Code is - * the Mozilla Foundation. - * Portions created by the Initial Developer are Copyright (C) 2010 - * the Initial Developer. All Rights Reserved. - * - * Contributor(s): - * Irakli Gozalishvili <gozala@mozilla.com> (Original Author) - * - * Alternatively, the contents of this file may be used under the terms of - * either the GNU General Public License Version 2 or later (the "GPL"), or - * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), - * in which case the provisions of the GPL or the LGPL are applicable instead - * of those above. If you wish to allow use of your version of this file only - * under the terms of either the GPL or the LGPL, and not to allow others to - * use your version of this file under the terms of the MPL, indicate your - * decision by deleting the provisions above and replace them with the notice - * and other provisions required by the GPL or the LGPL. If you do not delete - * the provisions above, a recipient may use your version of this file under - * the terms of any one of the MPL, the GPL or the LGPL. - * - * ***** END LICENSE BLOCK ***** */ - -"use strict"; - -const { EventEmitter } = require('../events'); -const { validateOptions, getTypeOf } = require('../api-utils'); -const { URL, toFilename } = require('../url'); -const file = require('../file'); - -// map of property validations -const valid = { - contentURL: { - ok: function (value) { - try { - URL(value); - } - catch(e) { - return false; - } - return true; - }, - msg: 'The `contentURL` option must be a valid URL.' - }, - contentScriptFile: { - is: ['undefined', 'null', 'string', 'array'], - map: function(value) 'undefined' === getTypeOf(value) ? null : value, - ok: function(value) { - if (getTypeOf(value) === 'array') { - // Make sure every item is a local file URL. - return value.every(function (item) { - try { - toFilename(item); - return true; - } - catch(e) { - return false; - } - }); - } - return true; - }, - msg: 'The `contentScriptFile` option must be a local file URL or an array of' - + 'URLs.' - }, - contentScript: { - is: ['undefined', 'null', 'string', 'array'], - map: function(value) 'undefined' === getTypeOf(value) ? null : value, - ok: function(value) 'array' !== getTypeOf(value) ? true : - value.every(function(item) 'string' === getTypeOf(item)) - , - msg: 'The script option must be a string or an array of strings.' - }, - contentScriptWhen: { - is: ['string'], - ok: function(value) ['start', 'ready', 'end'].indexOf(value) >= 0, - map: function(value) { - return value || 'end'; - }, - msg: 'The `contentScriptWhen` option must be either "start", "ready" or "end".' - } -}; -exports.validationAttributes = valid; - -/** - * Shortcut function to validate property with validation. - * @param {Object|Number|String} suspect - * value to validate - * @param {Object} validation - * validation rule passed to `api-utils` - */ -function validate(suspect, validation) validateOptions( - { $: suspect }, - { $: validation } -).$ - -function Allow(script) ({ - get script() script, - set script(value) script = !!value -}) - -/** - * Trait is intended to be used in some composition. It provides set of core - * properties and bounded validations to them. Trait is useful for all the - * compositions providing high level APIs for interaction with content. - * Property changes emit `"propertyChange"` events on instances. - */ -const Loader = EventEmitter.compose({ - /** - * Permissions for the content, with the following keys: - * @property {Object} [allow = { script: true }] - * @property {Boolean} [allow.script = true] - * Whether or not to execute script in the content. Defaults to true. - */ - get allow() this._allow || (this._allow = Allow(true)), - set allow(value) this.allow.script = value && value.script, - _allow: null, - /** - * The content to load. Either a string of HTML or a URL. - * @type {String} - */ - get contentURL() this._contentURL, - set contentURL(value) { - value = validate(value, valid.contentURL); - if (this._contentURL != value) { - this._emit('propertyChange', { - contentURL: this._contentURL = value - }); - } - }, - _contentURL: null, - /** - * When to load the content scripts. - * Possible values are "end" (default), which loads them once all page - * contents have been loaded, "ready", which loads them once DOM nodes are - * ready (ie like DOMContentLoaded event), and "start", which loads them once - * the `window` object for the page has been created, but before any scripts - * specified by the page have been loaded. - * Property change emits `propertyChange` event on instance with this key - * and new value. - * @type {'start'|'ready'|'end'} - */ - get contentScriptWhen() this._contentScriptWhen, - set contentScriptWhen(value) { - value = validate(value, valid.contentScriptWhen); - if (value !== this._contentScriptWhen) { - this._emit('propertyChange', { - contentScriptWhen: this._contentScriptWhen = value - }); - } - }, - _contentScriptWhen: 'end', - /** - * The URLs of content scripts. - * Property change emits `propertyChange` event on instance with this key - * and new value. - * @type {String[]} - */ - get contentScriptFile() this._contentScriptFile, - set contentScriptFile(value) { - value = validate(value, valid.contentScriptFile); - if (value != this._contentScriptFile) { - this._emit('propertyChange', { - contentScriptFile: this._contentScriptFile = value - }); - } - }, - _contentScriptFile: null, - /** - * The texts of content script. - * Property change emits `propertyChange` event on instance with this key - * and new value. - * @type {String|undefined} - */ - get contentScript() this._contentScript, - set contentScript(value) { - value = validate(value, valid.contentScript); - if (value != this._contentScript) { - this._emit('propertyChange', { - contentScript: this._contentScript = value - }); - } - }, - _contentScript: null -}); -exports.Loader = Loader; - diff --git a/tools/addon-sdk-1.3/packages/api-utils/lib/content/symbiont.js b/tools/addon-sdk-1.3/packages/api-utils/lib/content/symbiont.js deleted file mode 100644 index 25d80a1..0000000 --- a/tools/addon-sdk-1.3/packages/api-utils/lib/content/symbiont.js +++ /dev/null @@ -1,201 +0,0 @@ -/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* vim:set ts=2 sw=2 sts=2 et: */ -/* ***** BEGIN LICENSE BLOCK ***** - * Version: MPL 1.1/GPL 2.0/LGPL 2.1 - * - * The contents of this file are subject to the Mozilla Public License Version - * 1.1 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * http://www.mozilla.org/MPL/ - * - * Software distributed under the License is distributed on an "AS IS" basis, - * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License - * for the specific language governing rights and limitations under the - * License. - * - * The Original Code is Jetpack. - * - * The Initial Developer of the Original Code is - * the Mozilla Foundation. - * Portions created by the Initial Developer are Copyright (C) 2010 - * the Initial Developer. All Rights Reserved. - * - * Contributor(s): - * Myk Melez <myk@mozilla.org> (Original Author) - * Irakli Gozalishvili <gozala@mozilla.com> - * - * Alternatively, the contents of this file may be used under the terms of - * either the GNU General Public License Version 2 or later (the "GPL"), or - * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), - * in which case the provisions of the GPL or the LGPL are applicable instead - * of those above. If you wish to allow use of your version of this file only - * under the terms of either the GPL or the LGPL, and not to allow others to - * use your version of this file under the terms of the MPL, indicate your - * decision by deleting the provisions above and replace them with the notice - * and other provisions required by the GPL or the LGPL. If you do not delete - * the provisions above, a recipient may use your version of this file under - * the terms of any one of the MPL, the GPL or the LGPL. - * - * ***** END LICENSE BLOCK ***** */ -"use strict"; - -const { Worker } = require('./worker'); -const { Loader } = require('./loader'); -const hiddenFrames = require('../hidden-frame'); -const observers = require('../observer-service'); -const unload = require('../unload'); - -/** - * This trait is layered on top of `Worker` and in contrast to symbiont - * Worker constructor requires `content` option that represents content - * that will be loaded in the provided frame, if frame is not provided - * Worker will create hidden one. - */ -const Symbiont = Worker.resolve({ - constructor: '_initWorker', - destroy: '_workerDestroy' - }).compose(Loader, { - - /** - * The constructor requires all the options that are required by - * `require('content').Worker` with the difference that the `frame` option - * is optional. If `frame` is not provided, `contentURL` is expected. - * @param {Object} options - * @param {String} options.contentURL - * URL of a content to load into `this._frame` and create worker for. - * @param {Element} [options.frame] - * iframe element that is used to load `options.contentURL` into. - * if frame is not provided hidden iframe will be created. - */ - constructor: function Symbiont(options) { - options = options || {}; - - if ('contentURL' in options) - this.contentURL = options.contentURL; - if ('contentScriptWhen' in options) - this.contentScriptWhen = options.contentScriptWhen; - if ('contentScriptFile' in options) - this.contentScriptFile = options.contentScriptFile; - if ('contentScript' in options) - this.contentScript = options.contentScript; - if ('allow' in options) - this.allow = options.allow; - if ('onError' in options) - this.on('error', options.onError); - if ('onMessage' in options) - this.on('message', options.onMessage); - if ('frame' in options) { - this._initFrame(options.frame); - } - else { - let self = this; - this._hiddenFrame = hiddenFrames.HiddenFrame({ - onReady: function onFrame() { - self._initFrame(this.element); - } - }); - hiddenFrames.add(this._hiddenFrame); - } - - unload.ensure(this._public, "destroy"); - }, - - destroy: function destroy() { - this._workerDestroy(); - this._unregisterListener(); - this._frame = null; - if (this._hiddenFrame) { - hiddenFrames.remove(this._hiddenFrame); - this._hiddenFrame = null; - } - }, - - /** - * XUL iframe or browser elements with attribute `type` being `content`. - * Used to create `ContentSymbiont` from. - * @type {nsIFrame|nsIBrowser} - */ - _frame: null, - - /** - * Listener to the `'frameReady"` event (emitted when `iframe` is ready). - * Removes listener, sets right permissions to the frame and loads content. - */ - _initFrame: function _initFrame(frame) { - if (this._loadListener) - this._unregisterListener(); - - this._frame = frame; - frame.docShell.allowJavascript = this.allow.script; - frame.setAttribute("src", this._contentURL); - - if ((frame.contentDocument.readyState == "complete" || - (frame.contentDocument.readyState == "interactive" && - this.contentScriptWhen != 'end' )) && - frame.contentDocument.location == this._contentURL) { - // In some cases src doesn't change and document is already ready - // (for ex: when the user moves a widget while customizing toolbars.) - this._onInit(); - return; - } - - let self = this; - - if ('start' == this.contentScriptWhen) { - this._loadEvent = 'start'; - observers.add('document-element-inserted', - this._loadListener = function onStart(doc) { - - let window = doc.defaultView; - if (window && window == frame.contentWindow) { - self._unregisterListener(); - self._onInit(); - } - - }); - return; - } - - let eventName = 'end' == this.contentScriptWhen ? 'load' : 'DOMContentLoaded'; - let self = this; - this._loadEvent = eventName; - frame.addEventListener(eventName, - this._loadListener = function _onReady(event) { - - if (event.target != frame.contentDocument) - return; - self._unregisterListener(); - - self._onInit(); - - }, true); - - }, - - /** - * Unregister listener that watchs for document being ready to be injected. - * This listener is registered in `Symbiont._initFrame`. - */ - _unregisterListener: function _unregisterListener() { - if (!this._loadListener) - return; - if (this._loadEvent == "start") { - observers.remove('document-element-inserted', this._loadListener); - } - else { - this._frame.removeEventListener(this._loadEvent, this._loadListener, - true); - } - this._loadListener = null; - }, - - /** - * Called by Symbiont itself when the frame is ready to load - * content scripts according to contentScriptWhen. Overloaded by Panel. - */ - _onInit: function () { - this._initWorker({ window: this._frame.contentWindow }); - } - -}); -exports.Symbiont = Symbiont; diff --git a/tools/addon-sdk-1.3/packages/api-utils/lib/content/worker.js b/tools/addon-sdk-1.3/packages/api-utils/lib/content/worker.js deleted file mode 100644 index e3126b6..0000000 --- a/tools/addon-sdk-1.3/packages/api-utils/lib/content/worker.js +++ /dev/null @@ -1,597 +0,0 @@ -/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* vim:set ts=2 sw=2 sts=2 et: */ -/* ***** BEGIN LICENSE BLOCK ***** - * Version: MPL 1.1/GPL 2.0/LGPL 2.1 - * - * The contents of this file are subject to the Mozilla Public License Version - * 1.1 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * http://www.mozilla.org/MPL/ - * - * Software distributed under the License is distributed on an "AS IS" basis, - * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License - * for the specific language governing rights and limitations under the - * License. - * - * The Original Code is Jetpack. - * - * The Initial Developer of the Original Code is - * the Mozilla Foundation. - * Portions created by the Initial Developer are Copyright (C) 2010 - * the Initial Developer. All Rights Reserved. - * - * Contributor(s): - * Irakli Gozalishvili <gozala@mozilla.com> - * - * Alternatively, the contents of this file may be used under the terms of - * either the GNU General Public License Version 2 or later (the "GPL"), or - * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), - * in which case the provisions of the GPL or the LGPL are applicable instead - * of those above. If you wish to allow use of your version of this file only - * under the terms of either the GPL or the LGPL, and not to allow others to - * use your version of this file under the terms of the MPL, indicate your - * decision by deleting the provisions above and replace them with the notice - * and other provisions required by the GPL or the LGPL. If you do not delete - * the provisions above, a recipient may use your version of this file under - * the terms of any one of the MPL, the GPL or the LGPL. - * - * ***** END LICENSE BLOCK ***** */ -"use strict"; - -const { shims } = require('../cuddlefish'); -const { Trait } = require('../traits'); -const { EventEmitter, EventEmitterTrait } = require('../events'); -const { Ci, Cu, Cc } = require('chrome'); -const timer = require('../timer'); -const { toFilename } = require('../url'); -const file = require('../file'); -const unload = require('../unload'); -const observers = require('../observer-service'); -const { Cortex } = require('../cortex'); -const { Enqueued } = require('../utils/function'); -const proxy = require('./content-proxy'); - -const JS_VERSION = '1.8'; - -const ERR_DESTROYED = - "The page has been destroyed and can no longer be used."; - - -function ensureArgumentsAreJSON(args) { - // First convert to real array - let array = Array.prototype.slice.call(args); - // JSON.stringify is buggy with cross-sandbox values, - // it may return "{}" on functions. Use a replacer to match them correctly. - function replacer(k, v) { - return typeof v === "function" ? undefined : v; - } - return JSON.parse(JSON.stringify(array, replacer)); -} - -/** - * Extended `EventEmitter` allowing us to emit events asynchronously. - */ -const AsyncEventEmitter = EventEmitter.compose({ - /** - * Emits event in the next turn of event loop. - */ - _asyncEmit: function _asyncEmit() { - timer.setTimeout(function emitter(emit, scope, params) { - emit.apply(scope, params); - }, 0, this._emit, this, arguments) - } -}); - -/** - * Local trait providing implementation of the workers global scope. - * Used to configure global object in the sandbox. - * @see http://www.w3.org/TR/workers/#workerglobalscope - */ -const WorkerGlobalScope = AsyncEventEmitter.compose({ - on: Trait.required, - _removeAllListeners: Trait.required, - - // wrapped functions from `'timer'` module. - // Wrapper adds `try catch` blocks to the callbacks in order to - // emit `error` event on a symbiont if exception is thrown in - // the Worker global scope. - // @see http://www.w3.org/TR/workers/#workerutils - - // List of all living timeouts/intervals - _timers: null, - - setTimeout: function setTimeout(callback, delay) { - let params = Array.slice(arguments, 2); - let id = timer.setTimeout(function(self) { - try { - delete self._timers[id]; - callback.apply(null, params); - } catch(e) { - self._addonWorker._asyncEmit('error', e); - } - }, delay, this); - this._timers[id] = true; - return id; - }, - clearTimeout: function clearTimeout(id){ - delete this._timers[id]; - return timer.clearTimeout(id); - }, - - setInterval: function setInterval(callback, delay) { - let params = Array.slice(arguments, 2); - let id = timer.setInterval(function(self) { - try { - callback.apply(null, params); - } catch(e) { - self._addonWorker._asyncEmit('error', e); - } - }, delay, this); - this._timers[id] = true; - return id; - }, - clearInterval: function clearInterval(id) { - delete this._timers[id]; - return timer.clearInterval(id); - }, - - /** - * `onMessage` function defined in the global scope of the worker context. - */ - get _onMessage() this.__onMessage, - set _onMessage(value) { - let listener = this.__onMessage; - if (listener && value !== listener) { - this.removeListener('message', listener); - this.__onMessage = undefined; - } - if (value) - this.on('message', this.__onMessage = value); - }, - __onMessage: undefined, - - /** - * Function for sending data to the addon side. - * Validates that data is a `JSON` or primitive value and emits - * 'message' event on the worker in the next turn of the event loop. - * _Later this will be sending data across process boundaries._ - * @param {JSON|String|Number|Boolean} data - */ - postMessage: function postMessage(data) { - if (!this._addonWorker) - throw new Error(ERR_DESTROYED); - this._addonWorker._asyncEmit('message', - JSON.parse(JSON.stringify(data))); - }, - - /** - * EventEmitter, that behaves (calls listeners) asynchronously. - * A way to send customized messages to / from the worker. - * Events from in the worker can be observed / emitted via self.on / self.emit - */ - get port() this._port._public, - - /** - * Same object than this.port but private API. - * Allow access to _asyncEmit, in order to send event to port. - */ - _port: null, - - /** - * Alias to the global scope in the context of worker. Similar to - * `window` concept. - */ - get self() this._public, - - /** - * Configures sandbox and loads content scripts into it. - * @param {Worker} worker - * content worker - */ - constructor: function WorkerGlobalScope(worker) { - this._addonWorker = worker; - - // Hack in order to allow addon worker to access _asyncEmit - // as this is the private object of WorkerGlobalScope - worker._contentWorker = this; - - // create an event emitter that receive and send events from/to the addon - let contentWorker = this; - this._port = EventEmitterTrait.create({ - emit: function () { - let addonWorker = contentWorker._addonWorker; - if (!addonWorker) - throw new Error(ERR_DESTROYED); - addonWorker._onContentScriptEvent.apply(addonWorker, arguments); - } - }); - // create emit that executes in next turn of event loop. - this._port._asyncEmit = Enqueued(this._port._emit); - // expose wrapped port, that exposes only public properties. - this._port._public = Cortex(this._port); - - // We receive an unwrapped window, with raw js access - let window = worker._window; - - // Create the sandbox and bind it to window in order for content scripts to - // have access to all standard globals (window, document, ...) - let sandbox = this._sandbox = new Cu.Sandbox(window, { - sandboxPrototype: proxy.create(window), - wantXrays: false - }); - Object.defineProperties(sandbox, { - window: { get: function() sandbox }, - top: { get: function() sandbox }, - // Use the Greasemonkey naming convention to provide access to the - // unwrapped window object so the content script can access document - // JavaScript values. - // NOTE: this functionality is experimental and may change or go away - // at any time! - unsafeWindow: { get: function () window.wrappedJSObject } - }); - - // Overriding / Injecting some natives into sandbox. - Cu.evalInSandbox(shims.contents, sandbox, JS_VERSION, shims.filename); - - // Initialize timer lists - this._timers = {}; - - let publicAPI = this._public; - - // List of content script globals: - let keys = ['setTimeout', 'clearTimeout', 'setInterval', 'clearInterval', - 'self']; - for each (let key in keys) { - Object.defineProperty( - sandbox, key, Object.getOwnPropertyDescriptor(publicAPI, key) - ); - } - let self = this; - Object.defineProperties(sandbox, { - onMessage: { - get: function() self._onMessage, - set: function(value) { - console.warn("The global `onMessage` function in content scripts " + - "is deprecated in favor of the `self.on()` function. " + - "Replace `onMessage = function (data){}` definitions " + - "with calls to `self.on('message', function (data){})`. " + - "For more info on `self.on`, see " + - "<https://addons.mozilla.org/en-US/developers/docs/sdk/latest/dev-guide/addon-development/web-content.html>."); - self._onMessage = value; - }, - configurable: true - }, - console: { value: console, configurable: true }, - - // Deprecated use of on/postMessage from globals - on: { - value: function () { - console.warn("The global `on()` function in content scripts is " + - "deprecated in favor of the `self.on()` function, " + - "which works the same. Replace calls to `on()` with " + - "calls to `self.on()`" + - "For more info on `self.on`, see " + - "<https://addons.mozilla.org/en-US/developers/docs/sdk/latest/dev-guide/addon-development/web-content.html>."); - publicAPI.on.apply(publicAPI, arguments); - }, - configurable: true - }, - postMessage: { - value: function () { - console.warn("The global `postMessage()` function in content " + - "scripts is deprecated in favor of the " + - "`self.postMessage()` function, which works the same. " + - "Replace calls to `postMessage()` with calls to " + - "`self.postMessage()`." + - "For more info on `self.on`, see " + - "<https://addons.mozilla.org/en-US/developers/docs/sdk/latest/dev-guide/addon-development/web-content.html>."); - publicAPI.postMessage.apply(publicAPI, arguments); - }, - configurable: true - } - }); - - // The order of `contentScriptFile` and `contentScript` evaluation is - // intentional, so programs can load libraries like jQuery from script URLs - // and use them in scripts. - let contentScriptFile = ('contentScriptFile' in worker) ? worker.contentScriptFile - : null, - contentScript = ('contentScript' in worker) ? worker.contentScript : null; - - if (contentScriptFile) { - if (Array.isArray(contentScriptFile)) - this._importScripts.apply(this, contentScriptFile); - else - this._importScripts(contentScriptFile); - } - if (contentScript) { - this._evaluate( - Array.isArray(contentScript) ? contentScript.join(';\n') : contentScript - ); - } - }, - _destructor: function _destructor() { - this._removeAllListeners(); - // Unregister all setTimeout/setInterval - // We can use `clearTimeout` for both setTimeout/setInterval - // as internal implementation of timer module use same method for both. - for (let id in this._timers) - timer.clearTimeout(id); - let publicAPI = this._public, - sandbox = this._sandbox; - delete sandbox.__proto__; - for (let key in publicAPI) - delete sandbox[key]; - this._sandbox = null; - this._addonWorker = null; - this.__onMessage = undefined; - }, - - /** - * JavaScript sandbox where all the content scripts are evaluated. - * {Sandbox} - */ - _sandbox: null, - - /** - * Reference to the addon side of the worker. - * @type {Worker} - */ - _addonWorker: null, - - /** - * Evaluates code in the sandbox. - * @param {String} code - * JavaScript source to evaluate. - * @param {String} [filename='javascript:' + code] - * Name of the file - */ - _evaluate: function(code, filename) { - filename = filename || 'javascript:' + code; - try { - Cu.evalInSandbox(code, this._sandbox, JS_VERSION, filename, 1); - } - catch(e) { - this._addonWorker._asyncEmit('error', e); - } - }, - /** - * Imports scripts to the sandbox by reading files under urls and - * evaluating its source. If exception occurs during evaluation - * `"error"` event is emitted on the worker. - * This is actually an analog to the `importScript` method in web - * workers but in our case it's not exposed even though content - * scripts may be able to do it synchronously since IO operation - * takes place in the UI process. - */ - _importScripts: function _importScripts(url) { - let urls = Array.slice(arguments, 0); - for each (let contentScriptFile in urls) { - try { - let filename = toFilename(contentScriptFile); - this._evaluate(file.read(filename), filename); - } - catch(e) { - this._addonWorker._asyncEmit('error', e) - } - } - } -}); - -/** - * Message-passing facility for communication between code running - * in the content and add-on process. - * @see https://jetpack.mozillalabs.com/sdk/latest/docs/#module/api-utils/content/worker - */ -const Worker = AsyncEventEmitter.compose({ - on: Trait.required, - _asyncEmit: Trait.required, - _removeAllListeners: Trait.required, - - /** - * Sends a message to the worker's global scope. Method takes single - * argument, which represents data to be sent to the worker. The data may - * be any primitive type value or `JSON`. Call of this method asynchronously - * emits `message` event with data value in the global scope of this - * symbiont. - * - * `message` event listeners can be set either by calling - * `self.on` with a first argument string `"message"` or by - * implementing `onMessage` function in the global scope of this worker. - * @param {Number|String|JSON} data - */ - postMessage: function postMessage(data) { - if (!this._contentWorker) - throw new Error(ERR_DESTROYED); - this._contentWorker._asyncEmit('message', JSON.parse(JSON.stringify(data))); - }, - - /** - * EventEmitter, that behaves (calls listeners) asynchronously. - * A way to send customized messages to / from the worker. - * Events from in the worker can be observed / emitted via - * worker.on / worker.emit. - */ - get port() { - // We generate dynamically this attribute as it needs to be accessible - // before Worker.constructor gets called. (For ex: Panel) - - // create an event emitter that receive and send events from/to the worker - let self = this; - this._port = EventEmitterTrait.create({ - emit: function () self._emitEventToContent(arguments) - }); - // create emit that executes in next turn of event loop. - this._port._asyncEmit = Enqueued(this._port._emit); - // expose wrapped port, that exposes only public properties: - // We need to destroy this getter in order to be able to set the - // final value. We need to update only public port attribute as we never - // try to access port attribute from private API. - delete this._public.port; - this._public.port = Cortex(this._port); - // Replicate public port to the private object - delete this.port; - this.port = this._public.port; - - return this._port; - }, - - /** - * Same object than this.port but private API. - * Allow access to _asyncEmit, in order to send event to port. - */ - _port: null, - - /** - * Emit a custom event to the content script, - * i.e. emit this event on `self.port` - */ - _emitEventToContent: function _emitEventToContent(args) { - // We need to save events that are emitted before the worker is - // initialized - if (!this._inited) { - this._earlyEvents.push(args); - return; - } - - // We throw exception when the worker has been destroyed - if (!this._contentWorker) { - throw new Error(ERR_DESTROYED); - } - - let scope = this._contentWorker._port; - // Ensure that we pass only JSON values - scope._asyncEmit.apply(scope, ensureArgumentsAreJSON(args)); - }, - - // Is worker connected to the content worker (i.e. WorkerGlobalScope) ? - _inited: false, - - // List of custom events fired before worker is initialized - get _earlyEvents() { - delete this._earlyEvents; - this._earlyEvents = []; - return this._earlyEvents; - }, - - constructor: function Worker(options) { - options = options || {}; - - if ('window' in options) - this._window = options.window; - if ('contentScriptFile' in options) - this.contentScriptFile = options.contentScriptFile; - if ('contentScript' in options) - this.contentScript = options.contentScript; - if ('onError' in options) - this.on('error', options.onError); - if ('onMessage' in options) - this.on('message', options.onMessage); - if ('onDetach' in options) - this.on('detach', options.onDetach); - - // Track document unload to destroy this worker. - // We can't watch for unload event on page's window object as it - // prevents bfcache from working: - // https://developer.mozilla.org/En/Working_with_BFCache - this._windowID = this._window. - QueryInterface(Ci.nsIInterfaceRequestor). - getInterface(Ci.nsIDOMWindowUtils). - currentInnerWindowID; - observers.add("inner-window-destroyed", - this._documentUnload = this._documentUnload.bind(this)); - - unload.ensure(this._public, "destroy"); - - // Ensure that worker._port is initialized for contentWorker to be able - // to send use event during WorkerGlobalScope(this) - this.port; - - // will set this._contentWorker pointing to the private API: - WorkerGlobalScope(this); - - // Mainly enable worker.port.emit to send event to the content worker - this._inited = true; - - // Flush all events that have been fired before the worker is initialized. - this._earlyEvents.forEach((function (args) this._emitEventToContent(args)). - bind(this)); - }, - - _documentUnload: function _documentUnload(subject, topic, data) { - let innerWinID = subject.QueryInterface(Ci.nsISupportsPRUint64).data; - if (innerWinID != this._windowID) return false; - this._workerCleanup(); - return true; - }, - - get url() { - // this._window will be null after detach - return this._window ? this._window.document.location.href : null; - }, - - get tab() { - if (this._window) { - let tab = require("../tabs/tab"); - // this._window will be null after detach - return tab.getTabForWindow(this._window); - } - return null; - }, - - /** - * Tells content worker to unload itself and - * removes all the references from itself. - */ - destroy: function destroy() { - this._workerCleanup(); - this._removeAllListeners('message'); - this._removeAllListeners('error'); - this._removeAllListeners('detach'); - }, - - /** - * Remove all internal references to the attached document - * Tells _port to unload itself and removes all the references from itself. - */ - _workerCleanup: function _workerCleanup() { - // maybe unloaded before content side is created - // As Symbiont call worker.constructor on document load - if (this._contentWorker) - this._contentWorker._destructor(); - this._contentWorker = null; - this._window = null; - // This method may be called multiple times, - // avoid dispatching `detach` event more than once - if (this._windowID) { - this._windowID = null; - observers.remove("inner-window-destroyed", this._documentUnload); - this._earlyEvents.slice(0, this._earlyEvents.length); - this._emit("detach"); - } - }, - - /** - * Receive an event from the content script that need to be sent to - * worker.port. Provide a way for composed object to catch all events. - */ - _onContentScriptEvent: function _onContentScriptEvent() { - // Ensure that we pass only JSON values - this._port._asyncEmit.apply(this._port, ensureArgumentsAreJSON(arguments)); - }, - - /** - * Reference to the content side of the worker. - * @type {WorkerGlobalScope} - */ - _contentWorker: null, - - /** - * Reference to the window that is accessible from - * the content scripts. - * @type {Object} - */ - _window: null, -}); -exports.Worker = Worker; - |