Bug 1240804 - Wait for RDM tool to init first. r=me
authorJ. Ryan Stinnett <jryans@gmail.com>
Tue, 29 Mar 2016 10:53:54 -0500
changeset 291337 57e1fe3dfc6da778da6ac4b3349d5e0293a214f3
parent 291336 c6faa92b435d084736b2856cf9fbb0cb50abfdfd
child 291338 76e4e7620f839730994c6b9242ed78b648795409
push id19656
push usergwagner@mozilla.com
push dateMon, 04 Apr 2016 13:43:23 +0000
treeherderb2g-inbound@e99061fde28a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersme
bugs1240804
milestone48.0a1
Bug 1240804 - Wait for RDM tool to init first. r=me MozReview-Commit-ID: Eb1xsE5irMw
devtools/client/responsive.html/index.js
devtools/client/responsive.html/manager.js
--- a/devtools/client/responsive.html/index.js
+++ b/devtools/client/responsive.html/index.js
@@ -38,20 +38,21 @@ let bootstrap = {
     // Load a special UA stylesheet to reset certain styles such as dropdown
     // lists.
     loadSheet(window,
               "resource://devtools/client/responsive.html/responsive-ua.css",
               "agent");
     this.telemetry.toolOpened("responsive");
     let store = this.store = Store();
     let app = App({
-      onExit: () => window.postMessage({type: "exit"}, "*"),
+      onExit: () => window.postMessage({ type: "exit" }, "*"),
     });
     let provider = createElement(Provider, { store }, app);
     ReactDOM.render(provider, document.querySelector("#root"));
+    window.postMessage({ type: "init" }, "*");
   },
 
   destroy() {
     this.store = null;
     this.telemetry.toolClosed("responsive");
     this.telemetry = null;
   },
 
--- a/devtools/client/responsive.html/manager.js
+++ b/devtools/client/responsive.html/manager.js
@@ -191,16 +191,17 @@ ResponsiveUI.prototype = {
    * bug 1238160 about <iframe mozbrowser> for more details.
    */
   init: Task.async(function*() {
     let tabBrowser = this.tab.linkedBrowser;
     let contentURI = tabBrowser.documentURI.spec;
     tabBrowser.loadURI(TOOL_URL);
     yield tabLoaded(this.tab);
     let toolWindow = this.toolWindow = tabBrowser.contentWindow;
+    yield waitForMessage(toolWindow, "init");
     toolWindow.addInitialViewport(contentURI);
     toolWindow.addEventListener("message", this);
   }),
 
   destroy() {
     let tabBrowser = this.tab.linkedBrowser;
     tabBrowser.goBack();
     this.window = null;
@@ -221,16 +222,31 @@ ResponsiveUI.prototype = {
       case "exit":
         toolWindow.removeEventListener(event.type, this);
         ResponsiveUIManager.closeIfNeeded(window, tab);
         break;
     }
   },
 };
 
+function waitForMessage(win, type) {
+  let deferred = promise.defer();
+
+  let onMessage = event => {
+    if (event.data.type !== type) {
+      return;
+    }
+    win.removeEventListener("message", onMessage);
+    deferred.resolve();
+  };
+  win.addEventListener("message", onMessage);
+
+  return deferred.promise;
+}
+
 function tabLoaded(tab) {
   let deferred = promise.defer();
 
   function handle(event) {
     if (event.originalTarget != tab.linkedBrowser.contentDocument ||
         event.target.location.href == "about:blank") {
       return;
     }