Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
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 | 'use strict'; |
michael@0 | 5 | |
michael@0 | 6 | module.metadata = { |
michael@0 | 7 | 'stability': 'unstable' |
michael@0 | 8 | }; |
michael@0 | 9 | |
michael@0 | 10 | let { merge } = require('../util/object'); |
michael@0 | 11 | let assetsURI = require('../self').data.url(); |
michael@0 | 12 | let isArray = Array.isArray; |
michael@0 | 13 | let method = require('../../method/core'); |
michael@0 | 14 | |
michael@0 | 15 | function isAddonContent({ contentURL }) { |
michael@0 | 16 | return typeof(contentURL) === 'string' && contentURL.indexOf(assetsURI) === 0; |
michael@0 | 17 | } |
michael@0 | 18 | exports.isAddonContent = isAddonContent; |
michael@0 | 19 | |
michael@0 | 20 | function hasContentScript({ contentScript, contentScriptFile }) { |
michael@0 | 21 | return (isArray(contentScript) ? contentScript.length > 0 : |
michael@0 | 22 | !!contentScript) || |
michael@0 | 23 | (isArray(contentScriptFile) ? contentScriptFile.length > 0 : |
michael@0 | 24 | !!contentScriptFile); |
michael@0 | 25 | } |
michael@0 | 26 | exports.hasContentScript = hasContentScript; |
michael@0 | 27 | |
michael@0 | 28 | function requiresAddonGlobal(model) { |
michael@0 | 29 | return model.injectInDocument || (isAddonContent(model) && !hasContentScript(model)); |
michael@0 | 30 | } |
michael@0 | 31 | exports.requiresAddonGlobal = requiresAddonGlobal; |
michael@0 | 32 | |
michael@0 | 33 | function getAttachEventType(model) { |
michael@0 | 34 | if (!model) return null; |
michael@0 | 35 | let when = model.contentScriptWhen; |
michael@0 | 36 | return requiresAddonGlobal(model) ? 'document-element-inserted' : |
michael@0 | 37 | when === 'start' ? 'document-element-inserted' : |
michael@0 | 38 | when === 'ready' ? 'DOMContentLoaded' : |
michael@0 | 39 | when === 'end' ? 'load' : |
michael@0 | 40 | null; |
michael@0 | 41 | } |
michael@0 | 42 | exports.getAttachEventType = getAttachEventType; |
michael@0 | 43 | |
michael@0 | 44 | let attach = method('worker-attach'); |
michael@0 | 45 | exports.attach = attach; |
michael@0 | 46 | |
michael@0 | 47 | let detach = method('worker-detach'); |
michael@0 | 48 | exports.detach = detach; |
michael@0 | 49 | |
michael@0 | 50 | let destroy = method('worker-destroy'); |
michael@0 | 51 | exports.destroy = destroy; |
michael@0 | 52 | |
michael@0 | 53 | function WorkerHost (workerFor) { |
michael@0 | 54 | // Define worker properties that just proxy to underlying worker |
michael@0 | 55 | return ['postMessage', 'port', 'url', 'tab'].reduce(function(proto, name) { |
michael@0 | 56 | // Use descriptor properties instead so we can call |
michael@0 | 57 | // the worker function in the context of the worker so we |
michael@0 | 58 | // don't have to create new functions with `fn.bind(worker)` |
michael@0 | 59 | let descriptorProp = { |
michael@0 | 60 | value: function (...args) { |
michael@0 | 61 | let worker = workerFor(this); |
michael@0 | 62 | return worker[name].apply(worker, args); |
michael@0 | 63 | } |
michael@0 | 64 | }; |
michael@0 | 65 | |
michael@0 | 66 | let accessorProp = { |
michael@0 | 67 | get: function () { return workerFor(this)[name]; }, |
michael@0 | 68 | set: function (value) { workerFor(this)[name] = value; } |
michael@0 | 69 | }; |
michael@0 | 70 | |
michael@0 | 71 | Object.defineProperty(proto, name, merge({ |
michael@0 | 72 | enumerable: true, |
michael@0 | 73 | configurable: false, |
michael@0 | 74 | }, isDescriptor(name) ? descriptorProp : accessorProp)); |
michael@0 | 75 | return proto; |
michael@0 | 76 | }, {}); |
michael@0 | 77 | |
michael@0 | 78 | function isDescriptor (prop) { |
michael@0 | 79 | return ~['postMessage'].indexOf(prop); |
michael@0 | 80 | } |
michael@0 | 81 | } |
michael@0 | 82 | exports.WorkerHost = WorkerHost; |