addon-sdk/source/lib/sdk/content/utils.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/addon-sdk/source/lib/sdk/content/utils.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,82 @@
     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 +'use strict';
     1.8 +
     1.9 +module.metadata = {
    1.10 +  'stability': 'unstable'
    1.11 +};
    1.12 +
    1.13 +let { merge } = require('../util/object');
    1.14 +let assetsURI = require('../self').data.url();
    1.15 +let isArray = Array.isArray;
    1.16 +let method = require('../../method/core');
    1.17 +
    1.18 +function isAddonContent({ contentURL }) {
    1.19 +  return typeof(contentURL) === 'string' && contentURL.indexOf(assetsURI) === 0;
    1.20 +}
    1.21 +exports.isAddonContent = isAddonContent;
    1.22 +
    1.23 +function hasContentScript({ contentScript, contentScriptFile }) {
    1.24 +  return (isArray(contentScript) ? contentScript.length > 0 :
    1.25 +         !!contentScript) ||
    1.26 +         (isArray(contentScriptFile) ? contentScriptFile.length > 0 :
    1.27 +         !!contentScriptFile);
    1.28 +}
    1.29 +exports.hasContentScript = hasContentScript;
    1.30 +
    1.31 +function requiresAddonGlobal(model) {
    1.32 +  return model.injectInDocument || (isAddonContent(model) && !hasContentScript(model));
    1.33 +}
    1.34 +exports.requiresAddonGlobal = requiresAddonGlobal;
    1.35 +
    1.36 +function getAttachEventType(model) {
    1.37 +  if (!model) return null;
    1.38 +  let when = model.contentScriptWhen;
    1.39 +  return requiresAddonGlobal(model) ? 'document-element-inserted' :
    1.40 +         when === 'start' ? 'document-element-inserted' :
    1.41 +         when === 'ready' ? 'DOMContentLoaded' :
    1.42 +         when === 'end' ? 'load' :
    1.43 +         null;
    1.44 +}
    1.45 +exports.getAttachEventType = getAttachEventType;
    1.46 +
    1.47 +let attach = method('worker-attach');
    1.48 +exports.attach = attach;
    1.49 +
    1.50 +let detach = method('worker-detach');
    1.51 +exports.detach = detach;
    1.52 +
    1.53 +let destroy = method('worker-destroy');
    1.54 +exports.destroy = destroy;
    1.55 +
    1.56 +function WorkerHost (workerFor) {
    1.57 +  // Define worker properties that just proxy to underlying worker
    1.58 +  return ['postMessage', 'port', 'url', 'tab'].reduce(function(proto, name) {
    1.59 +    // Use descriptor properties instead so we can call
    1.60 +    // the worker function in the context of the worker so we
    1.61 +    // don't have to create new functions with `fn.bind(worker)`
    1.62 +    let descriptorProp = {
    1.63 +      value: function (...args) {
    1.64 +        let worker = workerFor(this);
    1.65 +        return worker[name].apply(worker, args);
    1.66 +      }
    1.67 +    };
    1.68 +    
    1.69 +    let accessorProp = {
    1.70 +      get: function () { return workerFor(this)[name]; },
    1.71 +      set: function (value) { workerFor(this)[name] = value; }
    1.72 +    };
    1.73 +
    1.74 +    Object.defineProperty(proto, name, merge({
    1.75 +      enumerable: true,
    1.76 +      configurable: false,
    1.77 +    }, isDescriptor(name) ? descriptorProp : accessorProp));
    1.78 +    return proto;
    1.79 +  }, {});
    1.80 +  
    1.81 +  function isDescriptor (prop) {
    1.82 +    return ~['postMessage'].indexOf(prop);
    1.83 +  }
    1.84 +}
    1.85 +exports.WorkerHost = WorkerHost;

mercurial