Bug 1340750 fix remote handling of sidebar browser, r=kmag
authorShane Caraveo <scaraveo@mozilla.com>
Tue, 21 Feb 2017 13:51:14 -0800
changeset 373126 6d18f9e14d5d8a6f749aff26ee83930ca5b997aa
parent 373125 fe06b3adf47e5122381c7594e86af52a80e3e642
child 373127 b26e27c14e49f0c025dadc61b3ea6d9aa58ca67f
push id10863
push userjlorenzo@mozilla.com
push dateMon, 06 Mar 2017 23:02:23 +0000
treeherdermozilla-aurora@0931190cd725 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerskmag
bugs1340750
milestone54.0a1
Bug 1340750 fix remote handling of sidebar browser, r=kmag MozReview-Commit-ID: 3hamdXtxKjh
browser/base/content/webext-panels.js
browser/base/content/webext-panels.xul
--- a/browser/base/content/webext-panels.js
+++ b/browser/base/content/webext-panels.js
@@ -1,32 +1,64 @@
 /* -*- indent-tabs-mode: nil; js-indent-level: 4 -*- */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * 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/. */
 
 XPCOMUtils.defineLazyModuleGetter(this, "ExtensionParent",
                                   "resource://gre/modules/ExtensionParent.jsm");
+Cu.import("resource://gre/modules/ExtensionUtils.jsm");
+
+var {
+  promiseEvent,
+} = ExtensionUtils;
+
+const XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
+
+function getBrowser(sidebar) {
+  let browser = document.getElementById("webext-panels-browser");
+  if (browser) {
+    return Promise.resolve(browser);
+  }
+
+  browser = document.createElementNS(XUL_NS, "browser");
+  browser.setAttribute("id", "webext-panels-browser");
+  browser.setAttribute("type", "content");
+  browser.setAttribute("flex", "1");
+  browser.setAttribute("disableglobalhistory", "true");
+  browser.setAttribute("webextension-view-type", "sidebar");
+  browser.setAttribute("context", "contentAreaContextMenu");
+  browser.setAttribute("tooltip", "aHTMLTooltip");
+  browser.setAttribute("onclick", "window.parent.contentAreaClick(event, true);");
+
+  let readyPromise;
+  if (sidebar.remote) {
+    browser.setAttribute("remote", "true");
+    browser.setAttribute("remoteType",
+                         E10SUtils.getRemoteTypeForURI(sidebar.uri, true,
+                                                       E10SUtils.EXTENSION_REMOTE_TYPE));
+    readyPromise = promiseEvent(browser, "XULFrameLoaderCreated");
+  } else {
+    readyPromise = Promise.resolve();
+  }
+  document.documentElement.appendChild(browser);
+
+  return readyPromise.then(() => {
+    browser.messageManager.loadFrameScript("chrome://browser/content/content.js", false);
+    ExtensionParent.apiManager.emit("extension-browser-inserted", browser);
+    return browser;
+  });
+}
 
 function loadWebPanel() {
   let sidebarURI = new URL(location);
-  let uri = sidebarURI.searchParams.get("panel");
-  let remote = sidebarURI.searchParams.get("remote");
-  let browser = document.getElementById("webext-panels-browser");
-  if (remote) {
-    let remoteType = E10SUtils.getRemoteTypeForURI(uri, true,
-                                                   E10SUtils.EXTENSION_REMOTE_TYPE);
-    browser.setAttribute("remote", "true");
-    browser.setAttribute("remoteType", remoteType);
-  } else {
-    browser.removeAttribute("remote");
-    browser.removeAttribute("remoteType");
-  }
-  browser.loadURI(uri);
+  let sidebar = {
+    uri: sidebarURI.searchParams.get("panel"),
+    remote: sidebarURI.searchParams.get("remote"),
+  };
+  getBrowser(sidebar).then(browser => {
+    browser.loadURI(sidebar.uri);
+  });
 }
 
 function load() {
-  let browser = document.getElementById("webext-panels-browser");
-  browser.messageManager.loadFrameScript("chrome://browser/content/content.js", true);
-  ExtensionParent.apiManager.emit("extension-browser-inserted", browser);
-
   this.loadWebPanel();
 }
--- a/browser/base/content/webext-panels.xul
+++ b/browser/base/content/webext-panels.xul
@@ -60,14 +60,9 @@
                                 return;
                               gContextMenu.hiding();
                               gContextMenu = null;">
 #include browser-context.inc
     </menupopup>
   </popupset>
 
   <commandset id="editMenuCommands"/>
-  <browser id="webext-panels-browser"
-           type="content" flex="1"
-           webextension-view-type="sidebar"
-           context="contentAreaContextMenu" tooltip="aHTMLTooltip"
-           onclick="window.parent.contentAreaClick(event, true);"/>
 </page>