Bug 1378133 - Fix the inspector when loading pages from bfcache. r=jdescottes draft
authorAlexandre Poirot <poirot.alex@gmail.com>
Mon, 24 Jul 2017 15:55:42 +0200
changeset 614350 9e8c74cef5de58f2d37762762fd1454d75748f20
parent 614015 5928d905c0bc0b28f5488b236444c7d7991cf8d4
child 638852 b581bbee927b1f20b4b83a67f558a90a06d0f729
push id69997
push userbmo:poirot.alex@gmail.com
push dateMon, 24 Jul 2017 14:11:37 +0000
reviewersjdescottes
bugs1378133
milestone56.0a1
Bug 1378133 - Fix the inspector when loading pages from bfcache. r=jdescottes MozReview-Commit-ID: Ek2lw4Vh1km
devtools/client/inspector/test/browser_inspector_navigation.js
devtools/server/actors/tab.js
--- a/devtools/client/inspector/test/browser_inspector_navigation.js
+++ b/devtools/client/inspector/test/browser_inspector_navigation.js
@@ -8,16 +8,23 @@
 // Test that inspector updates when page is navigated.
 
 const TEST_URL_FILE = "browser/devtools/client/inspector/test/" +
   "doc_inspector_breadcrumbs.html";
 
 const TEST_URL_1 = "http://test1.example.org/" + TEST_URL_FILE;
 const TEST_URL_2 = "http://test2.example.org/" + TEST_URL_FILE;
 
+// Bug 1340592: "srcset" attribute causes bfcache events (pageshow/pagehide)
+// with buggy "persisted" values.
+const TEST_URL_3 = "data:text/html;charset=utf-8," +
+  encodeURIComponent("<img src=\"foo.png\" srcset=\"foo.png 1.5x\" />");
+const TEST_URL_4 = "data:text/html;charset=utf-8," +
+  encodeURIComponent("<h1>bar</h1>");
+
 add_task(function* () {
   let { inspector, testActor } = yield openInspectorForURL(TEST_URL_1);
 
   yield selectNode("#i1", inspector);
 
   info("Navigating to a different page.");
   yield navigateTo(inspector, TEST_URL_2);
 
@@ -36,8 +43,37 @@ add_task(function* () {
   info("Check that the inspector updates");
   yield onUpdated;
 
   ok(true, "Old page loaded");
   is((yield testActor.eval("location.href;")), TEST_URL_1, "URL is correct.");
 
   yield selectNode("#i1", inspector);
 });
+
+add_task(function* () {
+  let { inspector, testActor } = yield openInspectorForURL(TEST_URL_3);
+
+  yield selectNode("img", inspector);
+
+  info("Navigating to a different page.");
+  yield navigateTo(inspector, TEST_URL_4);
+
+  ok(true, "New page loaded");
+  yield selectNode("#h1", inspector);
+
+  let markuploaded = inspector.once("markuploaded");
+  let onUpdated = inspector.once("inspector-updated");
+
+  info("Going back in history");
+  yield testActor.eval("history.go(-1)");
+
+  info("Waiting for markup view to load after going back in history.");
+  yield markuploaded;
+
+  info("Check that the inspector updates");
+  yield onUpdated;
+
+  ok(true, "Old page loaded");
+  is((yield testActor.eval("location.href;")), TEST_URL_3, "URL is correct.");
+
+  yield selectNode("img", inspector);
+});
--- a/devtools/server/actors/tab.js
+++ b/devtools/server/actors/tab.js
@@ -1528,29 +1528,33 @@ DebuggerProgressListener.prototype = {
     });
   },
 
   onWindowCreated: DevToolsUtils.makeInfallible(function (evt) {
     if (!this._tabActor.attached) {
       return;
     }
 
-    // pageshow events for non-persisted pages have already been handled by a
-    // prior DOMWindowCreated event. For persisted pages, act as if the window
-    // had just been created since it's been unfrozen from bfcache.
-    if (evt.type == "pageshow" && !evt.persisted) {
+    let window = evt.target.defaultView;
+    let innerID = getWindowID(window);
+
+    // This method is alled on DOMWindowCreated and pageshow
+    // The common scenario is DOMWindowCreated, which is fired when the document
+    // loads. But we are to listen for pageshow in order to handle BFCache.
+    // When a page does into the BFCache, a pagehide event is fired with persisted=true
+    // but it doesn't necessarely mean persisted will be true for the pageshow
+    // event fired when the page is reloaded from the BFCache (see bug 1378133)
+    // So just check if we already know this window and accept any that isn't known yet
+    if (this._knownWindowIDs.has(innerID)) {
       return;
     }
 
-    let window = evt.target.defaultView;
     this._tabActor._windowReady(window);
 
-    if (evt.type !== "pageshow") {
-      this._knownWindowIDs.set(getWindowID(window), window);
-    }
+    this._knownWindowIDs.set(innerID, window);
   }, "DebuggerProgressListener.prototype.onWindowCreated"),
 
   onWindowHidden: DevToolsUtils.makeInfallible(function (evt) {
     if (!this._tabActor.attached) {
       return;
     }
 
     // Only act as if the window has been destroyed if the 'pagehide' event
@@ -1558,16 +1562,17 @@ DebuggerProgressListener.prototype = {
     // and frozen in the bfcache). If the page isn't persisted, the observer's
     // inner-window-destroyed event will handle it.
     if (!evt.persisted) {
       return;
     }
 
     let window = evt.target.defaultView;
     this._tabActor._windowDestroyed(window, null, true);
+    this._knownWindowIDs.delete(getWindowID(window));
   }, "DebuggerProgressListener.prototype.onWindowHidden"),
 
   observe: DevToolsUtils.makeInfallible(function (subject, topic) {
     if (!this._tabActor.attached) {
       return;
     }
 
     // Because this observer will be called for all inner-window-destroyed in