From b7cf2b3eab9c3f35ffd6f773ad47e2f1f53b5649 Mon Sep 17 00:00:00 2001 From: Hsin-Yi Tsai Date: Tue, 11 Sep 2012 18:07:03 +0800 Subject: [PATCH] Bug 789005 - System message API: decrease complexity of broadcastMessage(). r=fabrice --- dom/messages/SystemMessageInternal.js | 61 +++++++++++++++------------ 1 file changed, 33 insertions(+), 28 deletions(-) diff --git a/dom/messages/SystemMessageInternal.js b/dom/messages/SystemMessageInternal.js index a7ee06d89cf..4663f3a35f2 100644 --- a/dom/messages/SystemMessageInternal.js +++ b/dom/messages/SystemMessageInternal.js @@ -41,13 +41,31 @@ function SystemMessageInternal() { SystemMessageInternal.prototype = { sendMessage: function sendMessage(aType, aMessage, aPageURI, aManifestURI) { - this._sendMessage(aType, aMessage, aPageURI.spec, aManifestURI.spec); + debug("Broadcasting " + aType + " " + JSON.stringify(aMessage)); + ppmm.broadcastAsyncMessage("SystemMessageManager:Message" , { type: aType, + msg: aMessage, + manifest: aManifestURI.spec }); + this._pages.forEach(function sendMess_openPage(aPage) { + if (aPage.type != aType || + aPage.manifest != aManifestURI.spec || + aPage.uri != aPageURI.spec) { + return; + } + + this._processPage(aPage, aMessage); + }.bind(this)) }, broadcastMessage: function broadcastMessage(aType, aMessage) { + debug("Broadcasting " + aType + " " + JSON.stringify(aMessage)); + // Find pages that registered an handler for this type. this._pages.forEach(function(aPage) { if (aPage.type == aType) { - this._sendMessage(aType, aMessage, aPage.uri, aPage.manifest); + ppmm.broadcastAsyncMessage("SystemMessageManager:Message" , { type: aType, + msg: aMessage, + manifest: aPage.manifest }); + + this._processPage(aPage, aMessage); } }.bind(this)) }, @@ -101,33 +119,20 @@ SystemMessageInternal.prototype = { } }, - _sendMessage: function _sendMessage(aType, aMessage, aPageURI, aManifestURI) { - debug("Broadcasting " + aType + " " + JSON.stringify(aMessage)); - ppmm.broadcastAsyncMessage("SystemMessageManager:Message" , { type: aType, - msg: aMessage, - manifest: aManifestURI }); + _processPage: function _processPage(aPage, aMessage) { + // Queue the message for the page. + aPage.pending.push(aMessage); + if (aPage.pending.length > kMaxPendingMessages) { + aPage.pending.splice(0, 1); + } - // Queue the message for pages that registered an handler for this type. - this._pages.forEach(function sendMess_openPage(aPage) { - if (aPage.type != aType || - aPage.manifest != aManifestURI || - aPage.uri != aPageURI) { - return; - } - - aPage.pending.push(aMessage); - if (aPage.pending.length > kMaxPendingMessages) { - aPage.pending.splice(0, 1); - } - - // We don't need to send the full object to observers. - let page = { uri: aPage.uri, - manifest: aPage.manifest, - type: aPage.type, - target: aMessage.target }; - debug("Asking to open " + JSON.stringify(page)); - Services.obs.notifyObservers(this, "system-messages-open-app", JSON.stringify(page)); - }.bind(this)) + // We don't need to send the full object to observers. + let page = { uri: aPage.uri, + manifest: aPage.manifest, + type: aPage.type, + target: aMessage.target }; + debug("Asking to open " + JSON.stringify(page)); + Services.obs.notifyObservers(this, "system-messages-open-app", JSON.stringify(page)); }, classID: Components.ID("{70589ca5-91ac-4b9e-b839-d6a88167d714}"),