Bug 787064 - System Message API: add broadcastMessage(). r=fabrice

This commit is contained in:
Hsin-Yi Tsai 2012-08-31 03:27:41 +08:00
Родитель 737b9cca1f
Коммит ecd5571a7e
2 изменённых файлов: 44 добавлений и 24 удалений

Просмотреть файл

@ -41,31 +41,14 @@ function SystemMessageInternal() {
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.spec });
this._sendMessage(aType, aMessage, aPageURI.spec, aManifestURI.spec);
},
// 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.spec ||
aPage.uri != aPageURI.spec) {
return;
broadcastMessage: function broadcastMessage(aType, aMessage) {
this._pages.forEach(function(aPage) {
if (aPage.type == aType) {
this._sendMessage(aType, aMessage, aPage.uri, aPage.manifest);
}
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))
},
@ -118,6 +101,35 @@ 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 });
// 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))
},
classID: Components.ID("{70589ca5-91ac-4b9e-b839-d6a88167d714}"),
QueryInterface: XPCOMUtils.generateQI([Ci.nsISystemMessagesInternal, Ci.nsIObserver])

Просмотреть файл

@ -9,7 +9,7 @@ interface nsIDOMWindow;
// Implemented by the contract id @mozilla.org/system-message-internal;1
[scriptable, uuid(3a50fd6b-0263-45c1-b738-a002052ad31b)]
[scriptable, uuid(d8de761a-94fe-44d5-80eb-3c8bd8cd7d0b)]
interface nsISystemMessagesInternal : nsISupports
{
/*
@ -21,6 +21,14 @@ interface nsISystemMessagesInternal : nsISupports
*/
void sendMessage(in DOMString type, in jsval message, in nsIURI pageURI, in nsIURI manifestURI);
/*
* Allow any internal user to broadcast a message of a given type.
* The application that registers the message will be launched.
* @param type The type of the message to be sent.
* @param message The message payload.
*/
void broadcastMessage(in DOMString type, in jsval message);
/*
* Registration of a page that wants to be notified of a message type.
* @param type The message type.