Bug 1137722 - Ensure that system message cache is refreshed before openapp. r=fabrice
authorKan-Ru Chen (陳侃如) <kanru@kanru.info>
Thu, 05 Mar 2015 22:28:00 -0500
changeset 233545 fbc8ebe1adacd24a28973aed81a696307465e11b
parent 233544 47f212881821c771b4acda13e220dd42fac6d850
child 233546 b068aca286b5331f9927a955f7cf74c48f1fdd46
push id28417
push userryanvm@gmail.com
push dateFri, 13 Mar 2015 19:52:44 +0000
treeherdermozilla-central@977add19414a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfabrice
bugs1137722
milestone39.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 1137722 - Ensure that system message cache is refreshed before openapp. r=fabrice
dom/messages/SystemMessageCache.js
dom/messages/SystemMessageInternal.js
--- a/dom/messages/SystemMessageCache.js
+++ b/dom/messages/SystemMessageCache.js
@@ -20,17 +20,16 @@ function debug(aMsg) {
 
 const kMessages = ["SystemMessageCache:RefreshCache"];
 
 function SystemMessageCache() {
   debug("init");
 
   this._pagesCache = [];
 
-  dump("SystemMessageCache: init");
   Services.obs.addObserver(this, "xpcom-shutdown", false);
   kMessages.forEach(function(aMessage) {
     cpmm.addMessageListener(aMessage, this);
   }, this);
 }
 
 SystemMessageCache.prototype = {
 
--- a/dom/messages/SystemMessageInternal.js
+++ b/dom/messages/SystemMessageInternal.js
@@ -245,33 +245,23 @@ SystemMessageInternal.prototype = {
       let result = this._sendMessageCommon(aType,
                                            aMessage,
                                            messageID,
                                            aPageURL,
                                            manifestURL,
                                            aExtra);
       debug("Returned status of sending message: " + result);
 
-      // Don't need to open the pages and queue the system message
-      // which was not allowed to be sent.
       if (result === MSG_SENT_FAILURE_PERM_DENIED) {
         return;
       }
 
       // For each page we must receive a confirm.
       ++pendingPromise.counter;
 
-      let page = this._findPage(aType, aPageURL, manifestURL);
-      if (page) {
-        // Queue this message in the corresponding pages.
-        this._queueMessage(page, aMessage, messageID);
-
-        this._openAppPage(page, aMessage, aExtra, result);
-      }
-
     }, this);
 
     if (pendingPromise.counter) {
       this._pendingPromises.set(messageID, pendingPromise);
     }
   },
 
   broadcastMessage: function(aType, aMessage, aExtra) {
@@ -303,28 +293,16 @@ SystemMessageInternal.prototype = {
       let doDispatch = () => {
         let result = this._sendMessageCommon(aType,
                                              aMessage,
                                              messageID,
                                              aPage.pageURL,
                                              aPage.manifestURL,
                                              aExtra);
         debug("Returned status of sending message: " + result);
-
-
-        // Don't need to open the pages and queue the system message
-        // which was not allowed to be sent.
-        if (result === MSG_SENT_FAILURE_PERM_DENIED) {
-          return;
-        }
-
-        // Queue this message in the corresponding pages.
-        this._queueMessage(aPage, aMessage, messageID);
-
-        this._openAppPage(aPage, aMessage, aExtra, result);
       };
 
       if ('function' !== typeof shouldDispatchFunc) {
         // If the configurator has no 'shouldDispatch' defined,
         // always dispatch this message.
         doDispatch();
         return;
       }
@@ -733,25 +711,42 @@ SystemMessageInternal.prototype = {
     // Don't send the system message not granted by the app's permissions.
     if (!SystemMessagePermissionsChecker
           .isSystemMessagePermittedToSend(aType,
                                           aPageURL,
                                           aManifestURL)) {
       return MSG_SENT_FAILURE_PERM_DENIED;
     }
 
+    // Queue this message in the corresponding pages.
+    let page = this._findPage(aType, aPageURL, aManifestURL);
+    if (!page) {
+      debug("Message " + aType + " is not registered for " +
+            aPageURL + " @ " + aManifestURL);
+      // FIXME bug 1140275 should only send message to page registered in manifest
+      // return MSG_SENT_FAILURE_PERM_DENIED;
+    }
+    if (page)
+      this._queueMessage(page, aMessage, aMessageID);
+
     let appPageIsRunning = false;
     let pageKey = this._createKeyForPage({ type: aType,
                                            manifestURL: aManifestURL,
                                            pageURL: aPageURL });
 
+    let cache = this._findCacheForApp(aManifestURL);
     let targets = this._listeners[aManifestURL];
     if (targets) {
       for (let index = 0; index < targets.length; ++index) {
         let target = targets[index];
+        let manager = target.target;
+
+        // Ensure hasPendingMessage cache is refreshed before we open app
+        manager.sendAsyncMessage("SystemMessageCache:RefreshCache", cache);
+
         // We only need to send the system message to the targets (processes)
         // which contain the window page that matches the manifest/page URL of
         // the destination of system message.
         if (target.winCounts[aPageURL] === undefined) {
           continue;
         }
 
         appPageIsRunning = true;
@@ -760,39 +755,39 @@ SystemMessageInternal.prototype = {
         // "SystemMessageManager:HandleMessageDone"  message when the page
         // finishes handling the system message. At that point, we'll release
         // the lock we acquired.
         this._acquireCpuWakeLock(pageKey);
 
         // Multiple windows can share the same target (process), the content
         // window needs to check if the manifest/page URL is matched. Only
         // *one* window should handle the system message.
-        let manager = target.target;
         manager.sendAsyncMessage("SystemMessageManager:Message",
                                  { type: aType,
                                    msg: aMessage,
                                    manifestURL: aManifestURL,
                                    pageURL: aPageURL,
                                    msgID: aMessageID });
       }
     }
 
+    let result = MSG_SENT_SUCCESS;
     if (!appPageIsRunning) {
       // The app page isn't running and relies on the 'open-app' chrome event to
       // wake it up. We still need to acquire a CPU wake lock for that page and
       // expect that we will receive a "SystemMessageManager:HandleMessagesDone"
       // or a "SystemMessageManager:HandleMessageDone" message when the page
       // finishes handling the system message with other pending messages. At
       // that point, we'll release the lock we acquired.
+      result = MSG_SENT_FAILURE_APP_NOT_RUNNING;
       this._acquireCpuWakeLock(pageKey);
-      return MSG_SENT_FAILURE_APP_NOT_RUNNING;
-    } else {
-      return MSG_SENT_SUCCESS;
     }
-
+    if (page)
+      this._openAppPage(page, aMessage, aExtra, result);
+    return result;
   },
 
   _resolvePendingPromises: function(aMessageID) {
     if (!this._pendingPromises.has(aMessageID)) {
       debug("Unknown pendingPromise messageID. This seems a bug!!");
       return;
     }