toolkit/devtools/DevToolsExtensions.jsm

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/devtools/DevToolsExtensions.jsm	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,46 @@
     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 +this.EXPORTED_SYMBOLS = ["gDevToolsExtensions"];
    1.10 +
    1.11 +Components.utils.import("resource://gre/modules/Services.jsm");
    1.12 +
    1.13 +let globalsCache = {};
    1.14 +
    1.15 +this.gDevToolsExtensions = {
    1.16 +  addContentGlobal: function(options) {
    1.17 +    if (!options || !options.global || !options['inner-window-id']) {
    1.18 +      throw Error('Invalid arguments');
    1.19 +    }
    1.20 +    let cache = getGlobalCache(options['inner-window-id']);
    1.21 +    cache.push(options.global);
    1.22 +    return undefined;
    1.23 +  },
    1.24 +  getContentGlobals: function(options) {
    1.25 +    if (!options || !options['inner-window-id']) {
    1.26 +      throw Error('Invalid arguments');
    1.27 +    }
    1.28 +    return Array.slice(globalsCache[options['inner-window-id']] || []);
    1.29 +  },
    1.30 +  removeContentGlobal: function(options) {
    1.31 +    if (!options || !options.global || !options['inner-window-id']) {
    1.32 +      throw Error('Invalid arguments');
    1.33 +    }
    1.34 +    let cache = getGlobalCache(options['inner-window-id']);
    1.35 +    let index = cache.indexOf(options.global);
    1.36 +    cache.splice(index, 1);
    1.37 +    return undefined;
    1.38 +  }
    1.39 +};
    1.40 +
    1.41 +function getGlobalCache(aInnerWindowID) {
    1.42 +  return globalsCache[aInnerWindowID] = globalsCache[aInnerWindowID] || [];
    1.43 +}
    1.44 +
    1.45 +// when the window is destroyed, eliminate the associated globals cache
    1.46 +Services.obs.addObserver(function observer(subject, topic, data) {
    1.47 +  let id = subject.QueryInterface(Components.interfaces.nsISupportsPRUint64).data;
    1.48 +  delete globalsCache[id];
    1.49 +}, 'inner-window-destroyed', false);

mercurial