utils.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. 'use strict';
  5. module.metadata = {
  6. 'stability': 'unstable'
  7. };
  8. let { merge } = require('../util/object');
  9. let assetsURI = require('../self').data.url();
  10. let isArray = Array.isArray;
  11. let method = require('../../method/core');
  12. function isAddonContent({ contentURL }) {
  13. return typeof(contentURL) === 'string' && contentURL.indexOf(assetsURI) === 0;
  14. }
  15. exports.isAddonContent = isAddonContent;
  16. function hasContentScript({ contentScript, contentScriptFile }) {
  17. return (isArray(contentScript) ? contentScript.length > 0 :
  18. !!contentScript) ||
  19. (isArray(contentScriptFile) ? contentScriptFile.length > 0 :
  20. !!contentScriptFile);
  21. }
  22. exports.hasContentScript = hasContentScript;
  23. function requiresAddonGlobal(model) {
  24. return model.injectInDocument || (isAddonContent(model) && !hasContentScript(model));
  25. }
  26. exports.requiresAddonGlobal = requiresAddonGlobal;
  27. function getAttachEventType(model) {
  28. if (!model) return null;
  29. let when = model.contentScriptWhen;
  30. return requiresAddonGlobal(model) ? 'document-element-inserted' :
  31. when === 'start' ? 'document-element-inserted' :
  32. when === 'end' ? 'load' :
  33. when === 'ready' ? 'DOMContentLoaded' :
  34. null;
  35. }
  36. exports.getAttachEventType = getAttachEventType;
  37. let attach = method('worker-attach');
  38. exports.attach = attach;
  39. let detach = method('worker-detach');
  40. exports.detach = detach;
  41. let destroy = method('worker-destroy');
  42. exports.destroy = destroy;
  43. function WorkerHost (workerFor) {
  44. // Define worker properties that just proxy to underlying worker
  45. return ['postMessage', 'port', 'url', 'tab'].reduce(function(proto, name) {
  46. // Use descriptor properties instead so we can call
  47. // the worker function in the context of the worker so we
  48. // don't have to create new functions with `fn.bind(worker)`
  49. let descriptorProp = {
  50. value: function (...args) {
  51. let worker = workerFor(this);
  52. return worker[name].apply(worker, args);
  53. }
  54. };
  55. let accessorProp = {
  56. get: function () { return workerFor(this)[name]; },
  57. set: function (value) { workerFor(this)[name] = value; }
  58. };
  59. Object.defineProperty(proto, name, merge({
  60. enumerable: true,
  61. configurable: false,
  62. }, isDescriptor(name) ? descriptorProp : accessorProp));
  63. return proto;
  64. }, {});
  65. function isDescriptor (prop) {
  66. return ~['postMessage'].indexOf(prop);
  67. }
  68. }
  69. exports.WorkerHost = WorkerHost;