Bug 1268134 - Destroy toolbox on host unload. r=ochameau draft
authorJ. Ryan Stinnett <jryans@gmail.com>
Fri, 15 Apr 2016 16:26:51 -0500
changeset 362699 da82a7ad1395551cea0e05aa821a83de0ff782f7
parent 362698 0eb07dded04a3d50421e722e3d1400caf23e5d6d
child 519866 998fedd884d1f4f411c5bf1d6c7213304a55d716
push id17021
push userbmo:jryans@gmail.com
push dateTue, 03 May 2016 01:11:59 +0000
reviewersochameau
bugs1268134
milestone49.0a1
Bug 1268134 - Destroy toolbox on host unload. r=ochameau MozReview-Commit-ID: IzZrJYVp5mp
devtools/client/framework/toolbox-init.js
--- a/devtools/client/framework/toolbox-init.js
+++ b/devtools/client/framework/toolbox-init.js
@@ -37,16 +37,17 @@ if (url.search.length > 1) {
       setAttribute() {},
       ownerDocument: document,
     };
   }
 
   // Specify the default tool to open
   let tool = url.searchParams.get("tool");
 
+  let toolboxOpened;
   if (url.searchParams.has("target")) {
     // Attach toolbox to a given browser iframe (<xul:browser> or <html:iframe
     // mozbrowser>) whose reference is set on the host iframe.
 
     // `iframe` is the targeted document to debug
     let iframe = host.wrappedJSObject ? host.wrappedJSObject.target
                                       : host.target;
     // Need to use a xray and query some interfaces to have
@@ -59,27 +60,45 @@ if (url.search.length > 1) {
       // linkedBrowser is the only one attribute being queried by client.getTab
       let tab = { linkedBrowser: iframe };
 
       if (!DebuggerServer.initialized) {
         DebuggerServer.init();
         DebuggerServer.addBrowserActors();
       }
       let client = new DebuggerClient(DebuggerServer.connectPipe());
-      Task.spawn(function*() {
+      toolboxOpened = Task.spawn(function*() {
         yield client.connect();
         // Creates a target for a given browser iframe.
         let response = yield client.getTab({ tab });
         let form = response.tab;
-        let target = yield TargetFactory.forRemoteTab({client, form, chrome: false});
+        let target = yield TargetFactory.forRemoteTab({
+          client,
+          form,
+          chrome: false
+        });
         let options = { customIframe: host };
-        yield gDevTools.showToolbox(target, tool, Toolbox.HostType.CUSTOM, options);
+        return gDevTools.showToolbox(target, tool, Toolbox.HostType.CUSTOM,
+                                     options);
       });
     }
   } else {
-    targetFromURL(url).then(target => {
+    toolboxOpened = Task.spawn(function*() {
+      let target = yield targetFromURL(url);
       let options = { customIframe: host };
-      return gDevTools.showToolbox(target, tool, Toolbox.HostType.CUSTOM, options);
-    }).then(null, e => {
-      window.alert("Unable to start the toolbox:" + e.message);
+      return gDevTools.showToolbox(target, tool, Toolbox.HostType.CUSTOM,
+                                   options);
     });
   }
+
+  toolboxOpened.catch(e => {
+    window.alert("Unable to start the toolbox: " + e.message);
+  });
+
+  // Ensure the toolbox gets destroyed if the host unloads
+  toolboxOpened.then(toolbox => {
+    let hostUnload = () => {
+      host.contentWindow.removeEventListener("unload", hostUnload);
+      toolbox.destroy();
+    };
+    host.contentWindow.addEventListener("unload", hostUnload);
+  });
 }