Bug 1036117 - Make WebappRT users wait for the configuration to be loaded. r=myk
authorMarco Castelluccio <mar.castelluccio@studenti.unina.it>
Tue, 15 Jul 2014 01:35:43 +0200
changeset 215877 a60905d4dfd8fb6b4bc4bff66dc222caa36b7006
parent 215876 d2c09afed727bc9392a966fec39383fc2a7ddd89
child 215878 1aa504e620340f66568e113e25f8d08d9c509a2f
push id515
push userraliiev@mozilla.com
push dateMon, 06 Oct 2014 12:51:51 +0000
treeherdermozilla-release@267c7a481bef [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmyk
bugs1036117
milestone33.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 1036117 - Make WebappRT users wait for the configuration to be loaded. r=myk
webapprt/Startup.jsm
webapprt/WebappRT.jsm
webapprt/content/mochitest-shared.js
webapprt/content/webapp.js
--- a/webapprt/Startup.jsm
+++ b/webapprt/Startup.jsm
@@ -98,17 +98,17 @@ this.startup = function(window) {
     }
 
     let appUpdated = false;
     let updatePending = yield WebappRT.isUpdatePending();
     if (updatePending) {
       appUpdated = yield WebappRT.applyUpdate();
     }
 
-    yield WebappRT.loadConfig();
+    yield WebappRT.configPromise;
 
     let appData = WebappRT.config.app;
 
     // Initialize DOMApplicationRegistry by importing Webapps.jsm.
     Cu.import("resource://gre/modules/Webapps.jsm");
     // Initialize window-independent handling of webapps- notifications.
     Cu.import("resource://webapprt/modules/WebappManager.jsm");
 
--- a/webapprt/WebappRT.jsm
+++ b/webapprt/WebappRT.jsm
@@ -5,16 +5,17 @@
 this.EXPORTED_SYMBOLS = ["WebappRT"];
 
 const Cc = Components.classes;
 const Ci = Components.interfaces;
 const Cu = Components.utils;
 
 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
 Cu.import("resource://gre/modules/Services.jsm");
+Cu.import("resource://gre/modules/Promise.jsm");
 Cu.import("resource://gre/modules/AppsUtils.jsm");
 
 XPCOMUtils.defineLazyModuleGetter(this, "FileUtils",
   "resource://gre/modules/FileUtils.jsm");
 
 XPCOMUtils.defineLazyModuleGetter(this, "OS",
   "resource://gre/modules/osfile.jsm");
 
@@ -24,44 +25,49 @@ XPCOMUtils.defineLazyModuleGetter(this, 
 XPCOMUtils.defineLazyModuleGetter(this, 'NativeApp',
   'resource://gre/modules/NativeApp.jsm');
 
 XPCOMUtils.defineLazyServiceGetter(this, "appsService",
                                   "@mozilla.org/AppsService;1",
                                   "nsIAppsService");
 
 this.WebappRT = {
+  _configPromise: null,
+
+  get configPromise() {
+    if (!this._configPromise) {
+      this._configPromise = Task.spawn(function*() {
+        let webappJson = OS.Path.join(Services.dirsvc.get("AppRegD", Ci.nsIFile).path,
+                                      "webapp.json");
+
+        WebappRT.config = yield AppsUtils.loadJSONAsync(webappJson);
+      });
+    }
+
+    return this._configPromise;
+  },
+
   get launchURI() {
     return this.localeManifest.fullLaunchPath();
   },
 
   get localeManifest() {
     return new ManifestHelper(this.config.app.manifest,
                               this.config.app.origin);
   },
 
   get appID() {
-    let manifestURL = WebappRT.config.app.manifestURL;
+    let manifestURL = this.config.app.manifestURL;
     if (!manifestURL) {
       return Ci.nsIScriptSecurityManager.NO_APP_ID;
     }
 
     return appsService.getAppLocalIdByManifestURL(manifestURL);
   },
 
-  loadConfig: function() {
-    if (this.config) {
-      return;
-    }
-
-    let webappJson = OS.Path.join(Services.dirsvc.get("AppRegD", Ci.nsIFile).path,
-                                  "webapp.json");
-    this.config = yield AppsUtils.loadJSONAsync(webappJson);
-  },
-
   isUpdatePending: Task.async(function*() {
     let webappJson = OS.Path.join(Services.dirsvc.get("AppRegD", Ci.nsIFile).path,
                                   "update", "webapp.json");
 
     if (!(yield OS.File.exists(webappJson))) {
       return false;
     }
 
@@ -80,22 +86,23 @@ this.WebappRT = {
       yield nativeApp.applyUpdate(config.app);
     } catch (ex) {
       return false;
     }
 
     // The update has been applied successfully, the new config file
     // is the config file that was in the update directory.
     this.config = config;
+    this._configPromise = Promise.resolve();
 
     return true;
   }),
 
   startUpdateService: function() {
-    let manifestURL = WebappRT.config.app.manifestURL;
+    let manifestURL = this.config.app.manifestURL;
     // We used to install apps without storing their manifest URL.
     // Now we can't update them.
     if (!manifestURL) {
       return;
     }
 
     // Check for updates once a day.
     let timerManager = Cc["@mozilla.org/updates/timer-manager;1"].
--- a/webapprt/content/mochitest-shared.js
+++ b/webapprt/content/mochitest-shared.js
@@ -54,16 +54,17 @@ function becomeWebapp(manifestURL, param
     Cu.import("resource://gre/modules/Webapps.jsm", scope);
     Cu.import("resource://webapprt/modules/Startup.jsm", scope);
     yield scope.DOMApplicationRegistry.confirmInstall(JSON.parse(data));
 
     let installRecord = JSON.parse(data);
     installRecord.mm = subj;
     installRecord.registryDir = Services.dirsvc.get("ProfD", Ci.nsIFile).path;
     WebappRT.config = installRecord;
+    WebappRT._configPromise = new Promise((resolve) => resolve());
 
     let win = Services.wm.getMostRecentWindow("webapprt:webapp");
     if (!win) {
       win = Services.ww.openWindow(null,
                                    "chrome://webapprt/content/webapp.xul",
                                    "_blank",
                                    "chrome,dialog=no,resizable,scrollbars,centerscreen",
                                    null);
--- a/webapprt/content/webapp.js
+++ b/webapprt/content/webapp.js
@@ -4,16 +4,17 @@
 
 const Cc = Components.classes;
 const Ci = Components.interfaces;
 const Cu = Components.utils;
 
 Cu.import("resource://webapprt/modules/WebappRT.jsm");
 Cu.import("resource://gre/modules/Services.jsm");
 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
+Cu.import("resource://gre/modules/Task.jsm");
 
 XPCOMUtils.defineLazyGetter(this, "gAppBrowser",
                             function() document.getElementById("content"));
 
 #ifdef MOZ_CRASHREPORTER
 XPCOMUtils.defineLazyServiceGetter(this, "gCrashReporter",
                                    "@mozilla.org/toolkit/crash-reporter;1",
                                    "nsICrashReporter");
@@ -49,17 +50,17 @@ let progressListener = {
         }
       }
     }
 
     // Set the title of the window to the name of the webapp, adding the origin
     // of the page being loaded if it's from a different origin than the app
     // (per security bug 741955, which specifies that other-origin pages loaded
     // in runtime windows must be identified in chrome).
-    let title = WebappRT.config.app.manifest.name;
+    let title = WebappRT.localeManifest.name;
     if (!isSameOrigin(location.spec)) {
       title = location.prePath + " - " + title;
     }
     document.documentElement.setAttribute("title", title);
   },
 
   onStateChange: function onStateChange(aProgress, aRequest, aFlags, aStatus) {
     if (aRequest instanceof Ci.nsIChannel &&
@@ -136,30 +137,31 @@ document.addEventListener('mozfullscreen
   } else {
     document.getElementById("main-menubar").style.display = "";
   }
 }, false);
 #endif
 
 // On Mac, we dynamically create the label for the Quit menuitem, using
 // a string property to inject the name of the webapp into it.
-function updateMenuItems() {
+let updateMenuItems = Task.async(function*() {
 #ifdef XP_MACOSX
-  let installRecord = WebappRT.config.app;
-  let manifest = WebappRT.config.app.manifest;
+  yield WebappRT.configPromise;
+
+  let manifest = WebappRT.localeManifest;
   let bundle =
     Services.strings.createBundle("chrome://webapprt/locale/webapp.properties");
   let quitLabel = bundle.formatStringFromName("quitApplicationCmdMac.label",
                                               [manifest.name], 1);
   let hideLabel = bundle.formatStringFromName("hideApplicationCmdMac.label",
                                               [manifest.name], 1);
   document.getElementById("menu_FileQuitItem").setAttribute("label", quitLabel);
   document.getElementById("menu_mac_hide_app").setAttribute("label", hideLabel);
 #endif
-}
+});
 
 #ifndef XP_MACOSX
 let gEditUIVisible = true;
 #endif
 
 function updateEditUIVisibility() {
 #ifndef XP_MACOSX
   let editMenuPopupState = document.getElementById("menu_EditPopup").state;