Bug 933083 - Refactor app requests into app-actor-front. r=ochameau
authorJ. Ryan Stinnett <jryans@gmail.com>
Fri, 01 Nov 2013 16:41:19 -0500
changeset 153437 81e276c73edeabac19a5bfa886a438386a7d3cfc
parent 153436 eb06bfe7f9b6a7c7e3a43b062f65132001e3d24d
child 153438 c800ad2e84d2384287217377c7ed3a922b1f3caf
push id25587
push userkwierso@gmail.com
push dateTue, 05 Nov 2013 05:07:06 +0000
treeherdermozilla-central@b0bce439c6e2 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersochameau
bugs933083
milestone28.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 933083 - Refactor app requests into app-actor-front. r=ochameau
browser/devtools/app-manager/content/device.js
browser/devtools/app-manager/content/projects.js
toolkit/devtools/apps/app-actor-front.js
--- a/browser/devtools/app-manager/content/device.js
+++ b/browser/devtools/app-manager/content/device.js
@@ -4,19 +4,21 @@
 
 const Cu = Components.utils;
 Cu.import("resource://gre/modules/Services.jsm");
 Cu.import("resource:///modules/devtools/gDevTools.jsm");
 
 const {devtools} = Cu.import("resource://gre/modules/devtools/Loader.jsm", {});
 const {require} = devtools;
 
-const {ConnectionManager, Connection} = require("devtools/client/connection-manager");
+const {ConnectionManager, Connection}
+  = require("devtools/client/connection-manager");
 const {getDeviceFront} = require("devtools/server/actors/device");
-const {getTargetForApp} = require("devtools/app-actor-front");
+const {getTargetForApp, launchApp, closeApp}
+  = require("devtools/app-actor-front");
 const DeviceStore = require("devtools/app-manager/device-store");
 const WebappsStore = require("devtools/app-manager/webapps-store");
 const promise = require("sdk/core/promise");
 
 window.addEventListener("message", function(event) {
   try {
     let message = JSON.parse(event.data);
     if (message.name == "connection") {
@@ -118,97 +120,80 @@ let UI = {
               this.setWallpaper(dataURL);
             });
           });
         }
       );
     }
   },
 
+  get connected() { return !!this.listTabsResponse; },
+
   setTab: function(name) {
     var tab = document.querySelector(".tab.selected");
     var panel = document.querySelector(".tabpanel.selected");
 
     if (tab) tab.classList.remove("selected");
     if (panel) panel.classList.remove("selected");
 
     var tab = document.querySelector(".tab." + name);
     var panel = document.querySelector(".tabpanel." + name);
 
     if (tab) tab.classList.add("selected");
     if (panel) panel.classList.add("selected");
   },
 
   screenshot: function() {
-    if (!this.listTabsResponse)
+    if (!this.connected) {
       return;
-
+    }
     let front = getDeviceFront(this.connection.client, this.listTabsResponse);
     front.screenshotToBlob().then(blob => {
       let topWindow = Services.wm.getMostRecentWindow("navigator:browser");
       let gBrowser = topWindow.gBrowser;
       let url = topWindow.URL.createObjectURL(blob);
       let tab = gBrowser.selectedTab = gBrowser.addTab(url);
       tab.addEventListener("TabClose", function onTabClose() {
         tab.removeEventListener("TabClose", onTabClose, false);
         topWindow.URL.revokeObjectURL(url);
       }, false);
     }).then(null, console.error);
   },
 
   openToolbox: function(manifest) {
-    if (!this.listTabsResponse)
+    if (!this.connected) {
       return;
+    }
     getTargetForApp(this.connection.client,
                     this.listTabsResponse.webappsActor,
                     manifest).then((target) => {
       gDevTools.showToolbox(target,
                             null,
                             devtools.Toolbox.HostType.WINDOW).then(toolbox => {
         this.connection.once(Connection.Events.DISCONNECTED, () => {
           toolbox.destroy();
         });
       });
     }, console.error);
   },
 
   startApp: function(manifest) {
-    let deferred = promise.defer();
-
-    if (!this.listTabsResponse) {
-      deferred.reject();
-    } else {
-      let actor = this.listTabsResponse.webappsActor;
-      let request = {
-        to: actor,
-        type: "launch",
-        manifestURL: manifest,
-      }
-      this.connection.client.request(request, (res) => {
-        deferred.resolve()
-      });
+    if (!this.connected) {
+      return promise.reject();
     }
-    return deferred.promise;
+    return launchApp(this.connection.client,
+                     this.listTabsResponse.webappsActor,
+                     manifest);
   },
 
   stopApp: function(manifest) {
-    let deferred = promise.defer();
-
-    if (!this.listTabsResponse) {
-      deferred.reject();
-    } else {
-      let actor = this.listTabsResponse.webappsActor;
-      let request = {
-        to: actor,
-        type: "close",
-        manifestURL: manifest,
-      }
-      this.connection.client.request(request, (res) => {
-        deferred.resolve()
-      });
+    if (!this.connected) {
+      return promise.reject();
     }
-    return deferred.promise;
+    return closeApp(this.connection.client,
+                    this.listTabsResponse.webappsActor,
+                    manifest);
   },
 }
 
 // This must be bound immediately, as it might be used via the message listener
 // before UI.init() has been called.
 UI._onConnectionStatusChange = UI._onConnectionStatusChange.bind(UI);
--- a/browser/devtools/app-manager/content/projects.js
+++ b/browser/devtools/app-manager/content/projects.js
@@ -9,17 +9,18 @@ const Cr = Components.results;
 Cu.import("resource:///modules/devtools/gDevTools.jsm");
 const {devtools} = Cu.import("resource://gre/modules/devtools/Loader.jsm", {});
 const {require} = devtools;
 const {ConnectionManager, Connection} = require("devtools/client/connection-manager");
 const {AppProjects} = require("devtools/app-manager/app-projects");
 const {AppValidator} = require("devtools/app-manager/app-validator");
 const {Services} = Cu.import("resource://gre/modules/Services.jsm");
 const {FileUtils} = Cu.import("resource://gre/modules/FileUtils.jsm");
-const {installHosted, installPackaged, getTargetForApp, reloadApp} = require("devtools/app-actor-front");
+const {installHosted, installPackaged, getTargetForApp,
+       reloadApp, launchApp, closeApp} = require("devtools/app-actor-front");
 const {EventEmitter} = Cu.import("resource:///modules/devtools/shared/event-emitter.js");
 
 const promise = require("sdk/core/promise");
 
 const MANIFEST_EDITOR_ENABLED = "devtools.appmanager.manifestEditor.enabled";
 
 window.addEventListener("message", function(event) {
   try {
@@ -201,16 +202,19 @@ let UI = {
           button.disabled = false;
           let message = res.error + ": " + res.message;
           alert(message);
           this.connection.log(message);
         });
   },
 
   reload: function (project) {
+    if (!this.connected) {
+      return promise.reject();
+    }
     return reloadApp(this.connection.client,
               this.listTabsResponse.webappsActor,
               this._getProjectManifestURL(project)).
       then(() => {
         this.connection.log("App reloaded");
       });
   },
 
@@ -248,16 +252,19 @@ let UI = {
     if (project.type == "packaged") {
       return "app://" + project.packagedAppOrigin + "/manifest.webapp";
     } else if (project.type == "hosted") {
       return project.location;
     }
   },
 
   install: function(project) {
+    if (!this.connected) {
+      return promise.reject();
+    }
     this.connection.log("Installing the " + project.manifest.name + " app...");
     let installPromise;
     if (project.type == "packaged") {
       installPromise = installPackaged(this.connection.client, this.listTabsResponse.webappsActor, project.location, project.packagedAppOrigin)
         .then(({ appId }) => {
           // If the packaged app specified a custom origin override,
           // we need to update the local project origin
           project.packagedAppOrigin = appId;
@@ -280,46 +287,40 @@ let UI = {
     }, () => {
       this.connection.log("Install failed.");
     });
 
     return installPromise;
   },
 
   start: function(project) {
-    let deferred = promise.defer();
-    let request = {
-      to: this.listTabsResponse.webappsActor,
-      type: "launch",
-      manifestURL: this._getProjectManifestURL(project)
-    };
-    this.connection.client.request(request, (res) => {
-      if (res.error)
-        deferred.reject(res.error);
-      else
-        deferred.resolve(res);
-    });
-    return deferred.promise;
+    if (!this.connected) {
+      return promise.reject();
+    }
+    let manifestURL = this._getProjectManifestURL(project);
+    return launchApp(this.connection.client,
+                     this.listTabsResponse.webappsActor,
+                     manifestURL);
   },
 
   stop: function(location) {
+    if (!this.connected) {
+      return promise.reject();
+    }
     let project = AppProjects.get(location);
-    let deferred = promise.defer();
-    let request = {
-      to: this.listTabsResponse.webappsActor,
-      type: "close",
-      manifestURL: this._getProjectManifestURL(project)
-    };
-    this.connection.client.request(request, (res) => {
-      promive.resolve(res);
-    });
-    return deferred.promise;
+    let manifestURL = this._getProjectManifestURL(project);
+    return closeApp(this.connection.client,
+                    this.listTabsResponse.webappsActor,
+                    manifestURL);
   },
 
   debug: function(button, location) {
+    if (!this.connected) {
+      return promise.reject();
+    }
     button.disabled = true;
     let project = AppProjects.get(location);
 
     let onFailedToStart = (error) => {
       // If not installed, install and open it
       if (error == "NO_SUCH_APP") {
         return this.install(project);
       } else {
--- a/toolkit/devtools/apps/app-actor-front.js
+++ b/toolkit/devtools/apps/app-actor-front.js
@@ -269,8 +269,44 @@ function reloadApp(client, webappsActor,
         deferred.resolve();
       });
     }, () => {
      deferred.reject("Not running");
     });
   return deferred.promise;
 }
 exports.reloadApp = reloadApp;
+
+function launchApp(client, webappsActor, manifestURL) {
+  let deferred = promise.defer();
+  let request = {
+    to: webappsActor,
+    type: "launch",
+    manifestURL: manifestURL
+  };
+  client.request(request, (res) => {
+    if (res.error) {
+      deferred.reject(res.error);
+    } else {
+      deferred.resolve(res);
+    }
+  });
+  return deferred.promise;
+}
+exports.launchApp = launchApp;
+
+function closeApp(client, webappsActor, manifestURL) {
+  let deferred = promise.defer();
+  let request = {
+    to: webappsActor,
+    type: "close",
+    manifestURL: manifestURL
+  };
+  client.request(request, (res) => {
+    if (res.error) {
+      deferred.reject(res.error);
+    } else {
+      deferred.resolve(res);
+    }
+  });
+  return deferred.promise;
+}
+exports.closeApp = closeApp;