Bug 797404 - Sources which take too long to fetch incorrectly show up after a page refresh, r=past
authorVictor Porof <vporof@mozilla.com>
Wed, 03 Oct 2012 22:57:01 +0300
changeset 109265 b3a19d36344ae51b7a7985873a53bf69acbefc4b
parent 109264 d2850363666b163d6b8833f90974d1e65cc5c838
child 109266 7129a1d713f43cf7834e07b937cbd31c3a38562d
push id82
push usershu@rfrn.org
push dateFri, 05 Oct 2012 13:20:22 +0000
reviewerspast
bugs797404
milestone18.0a1
Bug 797404 - Sources which take too long to fetch incorrectly show up after a page refresh, r=past
browser/devtools/debugger/debugger-controller.js
browser/devtools/debugger/test/browser_dbg_location-changes-new.js
--- a/browser/devtools/debugger/debugger-controller.js
+++ b/browser/devtools/debugger/debugger-controller.js
@@ -4,16 +4,17 @@
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 "use strict";
 
 const Cc = Components.classes;
 const Ci = Components.interfaces;
 const Cu = Components.utils;
 
+const NEW_SCRIPT_DISPLAY_DELAY = 100; // ms
 const FRAME_STEP_CACHE_DURATION = 100; // ms
 const DBG_STRINGS_URI = "chrome://browser/locale/devtools/debugger.properties";
 const SCRIPTS_URL_MAX_LENGTH = 64; // chars
 const SYNTAX_HIGHLIGHT_MAX_FILE_SIZE = 1048576; // 1 MB in bytes
 
 Cu.import("resource:///modules/source-editor.jsm");
 Cu.import("resource://gre/modules/devtools/dbg-server.jsm");
 Cu.import("resource://gre/modules/devtools/dbg-client.jsm");
@@ -936,25 +937,31 @@ SourceScripts.prototype = {
     }, true);
 
     let preferredScriptUrl = DebuggerView.Scripts.preferredScriptUrl;
 
     // Select this script if it's the preferred one.
     if (aPacket.url === DebuggerView.Scripts.preferredScriptUrl) {
       DebuggerView.Scripts.selectScript(aPacket.url);
     }
-    // ..or the first entry if there's not one selected yet.
-    else if (!DebuggerView.Scripts.selected) {
-      DebuggerView.Scripts.selectIndex(0);
-      // Selecting a script would make it "preferred", which is a lie here,
-      // because we're only displaying a script to make sure there's always
-      // something available in the SourceEditor and the scripts menulist.
-      // Hence the need revert back to the initial preferred script, just
-      // in case it will be available soon.
-      DebuggerView.Scripts.preferredScriptUrl = preferredScriptUrl;
+    // ..or the first entry if there's none selected yet after a while
+    else {
+      window.setTimeout(function() {
+        // If after a certain delay the preferred script still wasn't received,
+        // just give up on waiting and display the first entry.
+        if (!DebuggerView.Scripts.selected) {
+          DebuggerView.Scripts.selectIndex(0);
+          // Selecting a script would make it "preferred", which is a lie here,
+          // because we're only displaying a script to make sure there's always
+          // something available in the SourceEditor and the scripts menulist.
+          // Hence the need revert back to the initial preferred script, just
+          // in case it will be available soon.
+          DebuggerView.Scripts.preferredScriptUrl = preferredScriptUrl;
+        }
+      }, NEW_SCRIPT_DISPLAY_DELAY);
     }
 
     // If there are any stored breakpoints for this script, display them again,
     // both in the editor and the pane.
     for each (let breakpoint in DebuggerController.Breakpoints.store) {
       if (breakpoint.location.url == aPacket.url) {
         DebuggerController.Breakpoints.displayBreakpoint(breakpoint);
       }
@@ -1205,17 +1212,21 @@ SourceScripts.prototype = {
    * @param object aScript
    *        The script object coming from the active thread.
    * @param object aOptions [optional]
    *        Additional options for showing the script. Supported options:
    *        - targetLine: place the editor at the given line number.
    */
   showScript: function SS_showScript(aScript, aOptions = {}) {
     if (aScript.loaded) {
-      this._onShowScript(aScript, aOptions);
+      // Scripts may take a longer time to load than expected, therefore the
+      // required one may change at any time after a previous request was made.
+      if (aScript.url === DebuggerView.Scripts.selected) {
+        this._onShowScript(aScript, aOptions);
+      }
       return;
     }
 
     let editor = DebuggerView.editor;
     editor.setMode(SourceEditor.MODES.TEXT);
     editor.setText(L10N.getStr("loadingText"));
     editor.resetUndo();
 
--- a/browser/devtools/debugger/test/browser_dbg_location-changes-new.js
+++ b/browser/devtools/debugger/test/browser_dbg_location-changes-new.js
@@ -54,17 +54,17 @@ function testSimpleCall() {
 
 function testLocationChange()
 {
   gDebugger.DebuggerController.activeThread.resume(function() {
     gDebugger.DebuggerController.client.addOneTimeListener("tabNavigated", function(aEvent, aPacket) {
       ok(true, "tabNavigated event was fired.");
       info("Still attached to the tab.");
 
-      gDebugger.addEventListener("Debugger:AfterNewScript", function _onEvent(aEvent) {
+      gDebugger.addEventListener("Debugger:ScriptShown", function _onEvent(aEvent) {
         gDebugger.removeEventListener(aEvent.type, _onEvent);
 
         isnot(gDebugger.DebuggerView.Scripts.selected, null,
           "There should be a selected script.");
         isnot(gDebugger.editor.getText().length, 0,
           "The source editor should have some text displayed.");
 
         let menulist = gDebugger.DebuggerView.Scripts._scripts;