Bug 1448499 - Destroy deviceFront when destroying the toolbox;r=jryans draft
authorJulian Descottes <jdescottes@mozilla.com>
Fri, 23 Mar 2018 23:19:36 +0100
changeset 773284 746806af98af61f3db7aea732654c766c594bd03
parent 773096 97cdd8febc40ac6025bce5dec9f8dadb8e62f906
push id104205
push userjdescottes@mozilla.com
push dateTue, 27 Mar 2018 20:01:11 +0000
reviewersjryans
bugs1448499
milestone61.0a1
Bug 1448499 - Destroy deviceFront when destroying the toolbox;r=jryans MozReview-Commit-ID: IMlKbiaUKu7
devtools/client/framework/toolbox.js
devtools/shared/fronts/device.js
--- a/devtools/client/framework/toolbox.js
+++ b/devtools/client/framework/toolbox.js
@@ -66,16 +66,18 @@ loader.lazyRequireGetter(this, "SourceMa
 loader.lazyRequireGetter(this, "HUDService",
   "devtools/client/webconsole/hudservice", true);
 loader.lazyRequireGetter(this, "viewSource",
   "devtools/client/shared/view-source");
 loader.lazyRequireGetter(this, "StyleSheetsFront",
   "devtools/shared/fronts/stylesheets", true);
 loader.lazyRequireGetter(this, "buildHarLog",
   "devtools/client/netmonitor/src/har/har-builder-utils", true);
+loader.lazyRequireGetter(this, "getKnownDeviceFront",
+  "devtools/shared/fronts/device", true);
 
 loader.lazyGetter(this, "domNodeConstants", () => {
   return require("devtools/shared/dom-node-constants");
 });
 
 loader.lazyGetter(this, "registerHarOverlay", () => {
   return require("devtools/client/netmonitor/src/har/toolbox-overlay").register;
 });
@@ -2760,16 +2762,24 @@ Toolbox.prototype = {
     outstanding.push(this.destroyPreference());
 
     // Destroy the style sheet front.
     if (this._styleSheets) {
       this._styleSheets.destroy();
       this._styleSheets = null;
     }
 
+    // Destroy the device front for the current client if any.
+    // A given DeviceFront instance can cached and shared between different panels, so
+    // destroying it is the responsibility of the toolbox.
+    let deviceFront = getKnownDeviceFront(this.target.client);
+    if (deviceFront) {
+      deviceFront.destroy();
+    }
+
     // Detach the thread
     detachThread(this._threadClient);
     this._threadClient = null;
 
     // Unregister buttons listeners
     this.toolbarButtons.forEach(button => {
       if (typeof button.teardown == "function") {
         // teardown arguments have already been bound in _createButtonState
--- a/devtools/shared/fronts/device.js
+++ b/devtools/shared/fronts/device.js
@@ -33,16 +33,27 @@ const DeviceFront = protocol.FrontClassW
         return deferred.promise;
       });
     });
   },
 });
 
 const _knownDeviceFronts = new WeakMap();
 
+/**
+ * Retrieve the device front already created for the provided client, if available.
+ */
+exports.getKnownDeviceFront = function(client) {
+  return _knownDeviceFronts.get(client);
+};
+
+/**
+ * Only one DeviceFront is created for a given client, afterwards the instance is cached
+ * and returned immediately.
+ */
 exports.getDeviceFront = function(client, form) {
   if (!form.deviceActor) {
     return null;
   }
 
   if (_knownDeviceFronts.has(client)) {
     return _knownDeviceFronts.get(client);
   }