diff options
author | Matt A. Tobin <email@mattatobin.com> | 2018-02-10 02:51:36 -0500 |
---|---|---|
committer | Matt A. Tobin <email@mattatobin.com> | 2018-02-10 02:51:36 -0500 |
commit | 37d5300335d81cecbecc99812747a657588c63eb (patch) | |
tree | 765efa3b6a56bb715d9813a8697473e120436278 /toolkit/jetpack/sdk/content/utils.js | |
parent | b2bdac20c02b12f2057b9ef70b0a946113a00e00 (diff) | |
parent | 4fb11cd5966461bccc3ed1599b808237be6b0de9 (diff) | |
download | UXP-37d5300335d81cecbecc99812747a657588c63eb.tar UXP-37d5300335d81cecbecc99812747a657588c63eb.tar.gz UXP-37d5300335d81cecbecc99812747a657588c63eb.tar.lz UXP-37d5300335d81cecbecc99812747a657588c63eb.tar.xz UXP-37d5300335d81cecbecc99812747a657588c63eb.zip |
Merge branch 'ext-work'
Diffstat (limited to 'toolkit/jetpack/sdk/content/utils.js')
-rw-r--r-- | toolkit/jetpack/sdk/content/utils.js | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/toolkit/jetpack/sdk/content/utils.js b/toolkit/jetpack/sdk/content/utils.js new file mode 100644 index 000000000..90995a614 --- /dev/null +++ b/toolkit/jetpack/sdk/content/utils.js @@ -0,0 +1,105 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +'use strict'; + +module.metadata = { + 'stability': 'unstable' +}; + +var { merge } = require('../util/object'); +var { data } = require('../self'); +var assetsURI = data.url(); +var isArray = Array.isArray; +var method = require('../../method/core'); +var { uuid } = require('../util/uuid'); + +const isAddonContent = ({ contentURL }) => + contentURL && data.url(contentURL).startsWith(assetsURI); + +exports.isAddonContent = isAddonContent; + +function hasContentScript({ contentScript, contentScriptFile }) { + return (isArray(contentScript) ? contentScript.length > 0 : + !!contentScript) || + (isArray(contentScriptFile) ? contentScriptFile.length > 0 : + !!contentScriptFile); +} +exports.hasContentScript = hasContentScript; + +function requiresAddonGlobal(model) { + return model.injectInDocument || (isAddonContent(model) && !hasContentScript(model)); +} +exports.requiresAddonGlobal = requiresAddonGlobal; + +function getAttachEventType(model) { + if (!model) return null; + let when = model.contentScriptWhen; + return requiresAddonGlobal(model) ? 'document-element-inserted' : + when === 'start' ? 'document-element-inserted' : + when === 'ready' ? 'DOMContentLoaded' : + when === 'end' ? 'load' : + null; +} +exports.getAttachEventType = getAttachEventType; + +var attach = method('worker-attach'); +exports.attach = attach; + +var connect = method('worker-connect'); +exports.connect = connect; + +var detach = method('worker-detach'); +exports.detach = detach; + +var destroy = method('worker-destroy'); +exports.destroy = destroy; + +function WorkerHost (workerFor) { + // Define worker properties that just proxy to underlying worker + return ['postMessage', 'port', 'url', 'tab'].reduce(function(proto, name) { + // Use descriptor properties instead so we can call + // the worker function in the context of the worker so we + // don't have to create new functions with `fn.bind(worker)` + let descriptorProp = { + value: function (...args) { + let worker = workerFor(this); + return worker[name].apply(worker, args); + } + }; + + let accessorProp = { + get: function () { return workerFor(this)[name]; }, + set: function (value) { workerFor(this)[name] = value; } + }; + + Object.defineProperty(proto, name, merge({ + enumerable: true, + configurable: false, + }, isDescriptor(name) ? descriptorProp : accessorProp)); + return proto; + }, {}); + + function isDescriptor (prop) { + return ~['postMessage'].indexOf(prop); + } +} +exports.WorkerHost = WorkerHost; + +function makeChildOptions(options) { + function makeStringArray(arrayOrValue) { + if (!arrayOrValue) + return []; + return [].concat(arrayOrValue).map(String); + } + + return { + id: String(uuid()), + contentScript: makeStringArray(options.contentScript), + contentScriptFile: makeStringArray(options.contentScriptFile), + contentScriptOptions: options.contentScriptOptions ? + JSON.stringify(options.contentScriptOptions) : + null, + } +} +exports.makeChildOptions = makeChildOptions; |