Bug 1249119 - Fix inspector startup when opening it on a loading document r=jdescottes draft
authorAlexandre Poirot <poirot.alex@gmail.com>
Wed, 19 Oct 2016 05:25:26 -0700
changeset 427781 b9bcce380d5ba721763c10333b8916a7ba8bf17d
parent 427780 dc1317c5d8474a56209fc75ca6978dbadebd9094
child 427782 9e8da4d69b92a5ed29a72c8ed845e87095bcef85
push id33111
push userbmo:poirot.alex@gmail.com
push dateThu, 20 Oct 2016 21:28:50 +0000
reviewersjdescottes
bugs1249119
milestone52.0a1
Bug 1249119 - Fix inspector startup when opening it on a loading document r=jdescottes MozReview-Commit-ID: 5d4K6VFLTgE
devtools/client/inspector/inspector.js
devtools/server/actors/inspector.js
devtools/shared/fronts/inspector.js
--- a/devtools/client/inspector/inspector.js
+++ b/devtools/client/inspector/inspector.js
@@ -124,17 +124,22 @@ Inspector.prototype = {
   init: Task.async(function* () {
     // Localize all the nodes containing a data-localization attribute.
     localizeMarkup(this.panelDoc);
 
     this._cssPropertiesLoaded = initCssProperties(this.toolbox);
     yield this._cssPropertiesLoaded;
     yield this.target.makeRemote();
     yield this._getPageStyle();
-    let defaultSelection = yield this._getDefaultNodeForSelection();
+
+    // This may throw if the document is still loading and we are
+    // refering to a dead about:blank document
+    let defaultSelection = yield this._getDefaultNodeForSelection()
+      .catch(this._handleRejectionIfNotDestroyed);
+
     return yield this._deferredOpen(defaultSelection);
   }),
 
   get toolbox() {
     return this._toolbox;
   },
 
   get inspector() {
@@ -208,24 +213,24 @@ Inspector.prototype = {
         }).catch(e => console.error(e)),
       ]);
     });
   },
 
   _deferredOpen: function (defaultSelection) {
     let deferred = defer();
 
+    this.breadcrumbs = new HTMLBreadcrumbs(this);
+
     this.walker.on("new-root", this.onNewRoot);
 
     this.selection.on("new-node-front", this.onNewSelection);
     this.selection.on("before-new-node-front", this.onBeforeNewSelection);
     this.selection.on("detached-front", this.onDetached);
 
-    this.breadcrumbs = new HTMLBreadcrumbs(this);
-
     if (this.target.isLocalTab) {
       // Show a warning when the debugger is paused.
       // We show the warning only when the inspector
       // is selected.
       this.updateDebuggerPausedWarning = () => {
         let notificationBox = this._toolbox.getNotificationBox();
         let notification =
           notificationBox.getNotificationWithValue("inspector-script-paused");
@@ -252,18 +257,20 @@ Inspector.prototype = {
 
     this._initMarkup();
     this.isReady = false;
 
     this.once("markuploaded", () => {
       this.isReady = true;
 
       // All the components are initialized. Let's select a node.
-      this.selection.setNodeFront(defaultSelection, "inspector-open");
-      this.markup.expandNode(this.selection.nodeFront);
+      if (defaultSelection) {
+        this.selection.setNodeFront(defaultSelection, "inspector-open");
+        this.markup.expandNode(this.selection.nodeFront);
+      }
 
       // And setup the toolbar only now because it may depend on the document.
       this.setupToolbar();
 
       this.emit("ready");
       deferred.resolve(this);
     });
 
--- a/devtools/server/actors/inspector.js
+++ b/devtools/server/actors/inspector.js
@@ -2349,17 +2349,23 @@ var WalkerActor = protocol.ActorClassWit
       type: "inlineTextChild",
       target: parentActor.actorID,
       inlineTextChild:
         inlineTextChild ? inlineTextChild.form() : undefined
     });
   },
 
   onFrameLoad: function ({ window, isTopLevel }) {
-    if (!this.rootDoc && isTopLevel) {
+    if (isTopLevel) {
+      // If we initialize the inspector while the document is loading,
+      // we may already have a root document set in the constructor.
+      if (this.rootDoc && !Cu.isDeadWrapper(this.rootDoc) &&
+          this.rootDoc.defaultView) {
+        this.onFrameUnload({ window: this.rootDoc.defaultView });
+      }
       this.rootDoc = window.document;
       this.rootNode = this.document();
       this.queueMutation({
         type: "newRoot",
         target: this.rootNode.form()
       });
       return;
     }
--- a/devtools/shared/fronts/inspector.js
+++ b/devtools/shared/fronts/inspector.js
@@ -746,16 +746,21 @@ const WalkerFront = FrontClassWithSpec(w
     return this._getMutations(options).then(mutations => {
       let emitMutations = [];
       for (let change of mutations) {
         // The target is only an actorID, get the associated front.
         let targetID;
         let targetFront;
 
         if (change.type === "newRoot") {
+          // We may receive a new root without receiving any documentUnload
+          // beforehand. Like when opening tools in middle of a document load.
+          if (this.rootNode) {
+            this._createRootNodePromise();
+          }
           this.rootNode = types.getType("domnode").read(change.target, this);
           this._rootNodeDeferred.resolve(this.rootNode);
           targetID = this.rootNode.actorID;
           targetFront = this.rootNode;
         } else {
           targetID = change.target;
           targetFront = this.get(targetID);
         }