Bug 1168095 - Repair view source from sidebar pages. r=Gijs
authorJ. Ryan Stinnett <jryans@gmail.com>
Wed, 27 May 2015 20:17:18 -0500
changeset 265514 e88fd1006b84878c31b2ef9cec0e94459040d713
parent 265513 d93f6b15c00318f0cd0f0214afd37b9afc2cb9f5
child 265515 9e0263028c048f5741c1244b437864ebbf567808
push id8157
push userjlund@mozilla.com
push dateMon, 29 Jun 2015 20:36:23 +0000
treeherdermozilla-aurora@d480e05bd276 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersGijs
bugs1168095
milestone41.0a1
Bug 1168095 - Repair view source from sidebar pages. r=Gijs
browser/base/content/browser.js
browser/base/content/nsContextMenu.js
--- a/browser/base/content/browser.js
+++ b/browser/base/content/browser.js
@@ -2358,21 +2358,29 @@ function BrowserViewSourceOfDocument(aAr
     args = { browser, outerWindowID, URL };
   } else {
     args = aArgsOrDocument;
   }
 
   let inTab = Services.prefs.getBoolPref("view_source.tab");
   if (inTab) {
     let viewSourceURL = `view-source:${args.URL}`;
-    let tab = gBrowser.loadOneTab(viewSourceURL, {
+    let tabBrowser = gBrowser;
+    // In the case of sidebars and chat windows, gBrowser is defined but null,
+    // because no #content element exists.  For these cases, we need to find
+    // the most recent browser window.
+    if (!tabBrowser) {
+      let browserWindow = RecentWindow.getMostRecentBrowserWindow();
+      tabBrowser = browserWindow.gBrowser;
+    }
+    let tab = tabBrowser.loadOneTab(viewSourceURL, {
       relatedToCurrent: true,
       inBackground: false
     });
-    args.viewSourceBrowser = gBrowser.getBrowserForTab(tab);
+    args.viewSourceBrowser = tabBrowser.getBrowserForTab(tab);
     top.gViewSourceUtils.viewSourceInBrowser(args);
   } else {
     top.gViewSourceUtils.viewSource(args);
   }
 }
 
 /**
  * Opens the View Source dialog for the source loaded in the root
--- a/browser/base/content/nsContextMenu.js
+++ b/browser/base/content/nsContextMenu.js
@@ -1002,21 +1002,29 @@ nsContextMenu.prototype = {
       reference = focusedWindow.getSelection();
     else if (aContext == "mathml")
       reference = this.target;
     else
       throw "not reached";
 
     let inTab = Services.prefs.getBoolPref("view_source.tab");
     if (inTab) {
-      let tab = gBrowser.loadOneTab("about:blank", {
+      let tabBrowser = gBrowser;
+      // In the case of sidebars and chat windows, gBrowser is defined but null,
+      // because no #content element exists.  For these cases, we need to find
+      // the most recent browser window.
+      if (!tabBrowser) {
+        let browserWindow = RecentWindow.getMostRecentBrowserWindow();
+        tabBrowser = browserWindow.gBrowser;
+      }
+      let tab = tabBrowser.loadOneTab("about:blank", {
         relatedToCurrent: true,
         inBackground: false
       });
-      let viewSourceBrowser = gBrowser.getBrowserForTab(tab);
+      let viewSourceBrowser = tabBrowser.getBrowserForTab(tab);
       if (aContext == "selection") {
         top.gViewSourceUtils
            .viewSourceFromSelectionInBrowser(reference, viewSourceBrowser);
       } else {
         top.gViewSourceUtils
            .viewSourceFromFragmentInBrowser(reference, aContext,
                                             viewSourceBrowser);
       }