Bug 1261857 - [webext] use addContentGlobal/removeContentGlobal to track/untrack globals associated to the content window. draft
authorLuca Greco <lgreco@mozilla.com>
Mon, 04 Apr 2016 17:40:34 +0200
changeset 347299 80e1728c2717c2f0f18cbcbd0c6bd0bdc264cb6d
parent 347297 5817ebeaf7178318381a1b0fadb50141eb719d0b
child 517597 42ed52d486110a316b2b319289479670ac822d03
push id14546
push userluca.greco@alcacoop.it
push dateMon, 04 Apr 2016 15:41:37 +0000
bugs1261857
milestone48.0a1
Bug 1261857 - [webext] use addContentGlobal/removeContentGlobal to track/untrack globals associated to the content window. MozReview-Commit-ID: 3zkozKcOt0r
toolkit/components/extensions/ExtensionContent.jsm
--- a/toolkit/components/extensions/ExtensionContent.jsm
+++ b/toolkit/components/extensions/ExtensionContent.jsm
@@ -35,16 +35,19 @@ XPCOMUtils.defineLazyModuleGetter(this, 
                                   "resource://gre/modules/PrivateBrowsingUtils.jsm");
 XPCOMUtils.defineLazyModuleGetter(this, "PromiseUtils",
                                   "resource://gre/modules/PromiseUtils.jsm");
 XPCOMUtils.defineLazyModuleGetter(this, "MessageChannel",
                                   "resource://gre/modules/MessageChannel.jsm");
 XPCOMUtils.defineLazyModuleGetter(this, "WebNavigationFrames",
                                   "resource://gre/modules/WebNavigationFrames.jsm");
 
+const {devtools} = Cu.import("resource://devtools/shared/Loader.jsm", {});
+const {addContentGlobal, removeContentGlobal} = devtools.require("devtools/server/content-globals");
+
 Cu.import("resource://gre/modules/ExtensionUtils.jsm");
 var {
   runSafeSyncWithoutClone,
   BaseContext,
   LocaleData,
   Messenger,
   injectAPI,
   flushJarCache,
@@ -337,16 +340,27 @@ class ExtensionContext extends BaseConte
 
       this.sandbox = Cu.Sandbox(prin, {
         metadata,
         sandboxPrototype: contentWindow,
         wantXrays: true,
         isWebExtensionContentScript: true,
         wantGlobalProperties: ["XMLHttpRequest"],
       });
+
+      try {
+        // add the content global to the devtools tracked globals
+        this.devtoolsGlobalDetails = {
+          global: this.sandbox,
+          "inner-window-id": innerWindowID,
+        };
+        addContentGlobal(this.devtoolsGlobalDetails);
+      } catch (e) {
+        Cu.reportError(`Exception on devtools addContentGlobal: ${e} - ${e.stack}.`);
+      }
     }
 
     let delegate = {
       getSender(context, target, sender) {
         // Nothing to do here.
       },
     };
 
@@ -407,16 +421,26 @@ class ExtensionContext extends BaseConte
 
     // Overwrite the content script APIs with an empty object if the APIs objects are still
     // defined in the content window (See Bug 1214658 for rationale).
     if (this.isExtensionPage && !Cu.isDeadWrapper(this.contentWindow) &&
         Cu.waiveXrays(this.contentWindow).browser === this.chromeObj) {
       Cu.createObjectIn(this.contentWindow, {defineAs: "browser"});
       Cu.createObjectIn(this.contentWindow, {defineAs: "chrome"});
     }
+
+    if (this.devtoolsGlobalDetails) {
+      try {
+        removeContentGlobal(this.devtoolsGlobalDetails);
+        this.devtoolsGlobalDetails = null;
+      } catch (e) {
+        Cu.reportError(`Exception on devtools removeContentGlobal: ${e} - ${e.stack}.`);
+      }
+    }
+
     Cu.nukeSandbox(this.sandbox);
     this.sandbox = null;
   }
 }
 
 // Responsible for creating ExtensionContexts and injecting content
 // scripts into them when new documents are created.
 DocumentManager = {