michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: const Ci = Components.interfaces; michael@0: const Cu = Components.utils; michael@0: const Cc = Components.classes; michael@0: michael@0: Cu.import("resource://gre/modules/XPCOMUtils.jsm"); michael@0: Cu.import("resource://gre/modules/Services.jsm"); michael@0: michael@0: XPCOMUtils.defineLazyServiceGetter(this, "gSystemMessenger", michael@0: "@mozilla.org/system-message-internal;1", michael@0: "nsISystemMessagesInternal"); michael@0: michael@0: XPCOMUtils.defineLazyServiceGetter(this, "uuidGenerator", michael@0: "@mozilla.org/uuid-generator;1", michael@0: "nsIUUIDGenerator"); michael@0: michael@0: XPCOMUtils.defineLazyServiceGetter(this, "notificationStorage", michael@0: "@mozilla.org/notificationStorage;1", michael@0: "nsINotificationStorage"); michael@0: michael@0: michael@0: XPCOMUtils.defineLazyGetter(this, "cpmm", function() { michael@0: return Cc["@mozilla.org/childprocessmessagemanager;1"] michael@0: .getService(Ci.nsIMessageSender); michael@0: }); michael@0: michael@0: function debug(str) { michael@0: dump("=*= AlertsService.js : " + str + "\n"); michael@0: } michael@0: michael@0: // ----------------------------------------------------------------------- michael@0: // Alerts Service michael@0: // ----------------------------------------------------------------------- michael@0: michael@0: function AlertsService() { michael@0: cpmm.addMessageListener("app-notification-return", this); michael@0: } michael@0: michael@0: AlertsService.prototype = { michael@0: classID: Components.ID("{fe33c107-82a4-41d6-8c64-5353267e04c9}"), michael@0: QueryInterface: XPCOMUtils.generateQI([Ci.nsIAlertsService, michael@0: Ci.nsIAppNotificationService]), michael@0: michael@0: // nsIAlertsService michael@0: showAlertNotification: function showAlertNotification(aImageUrl, michael@0: aTitle, michael@0: aText, michael@0: aTextClickable, michael@0: aCookie, michael@0: aAlertListener, michael@0: aName, michael@0: aBidi, michael@0: aLang) { michael@0: let browser = Services.wm.getMostRecentWindow("navigator:browser"); michael@0: browser.AlertsHelper.showAlertNotification(aImageUrl, aTitle, aText, michael@0: aTextClickable, aCookie, michael@0: aAlertListener, aName, aBidi, michael@0: aLang); michael@0: }, michael@0: michael@0: closeAlert: function(aName) { michael@0: let browser = Services.wm.getMostRecentWindow("navigator:browser"); michael@0: browser.AlertsHelper.closeAlert(aName); michael@0: }, michael@0: michael@0: // nsIAppNotificationService michael@0: showAppNotification: function showAppNotification(aImageURL, michael@0: aTitle, michael@0: aText, michael@0: aAlertListener, michael@0: aDetails) { michael@0: let uid = (aDetails.id == "") ? michael@0: "app-notif-" + uuidGenerator.generateUUID() : aDetails.id; michael@0: michael@0: this._listeners[uid] = { michael@0: observer: aAlertListener, michael@0: title: aTitle, michael@0: text: aText, michael@0: manifestURL: aDetails.manifestURL, michael@0: imageURL: aImageURL, michael@0: lang: aDetails.lang || undefined, michael@0: id: aDetails.id || undefined, michael@0: dbId: aDetails.dbId || undefined, michael@0: dir: aDetails.dir || undefined, michael@0: tag: aDetails.tag || undefined michael@0: }; michael@0: michael@0: cpmm.sendAsyncMessage("app-notification-send", { michael@0: imageURL: aImageURL, michael@0: title: aTitle, michael@0: text: aText, michael@0: uid: uid, michael@0: details: aDetails michael@0: }); michael@0: }, michael@0: michael@0: // AlertsService.js custom implementation michael@0: _listeners: [], michael@0: michael@0: receiveMessage: function receiveMessage(aMessage) { michael@0: let data = aMessage.data; michael@0: let listener = this._listeners[data.uid]; michael@0: if (aMessage.name !== "app-notification-return" || !listener) { michael@0: return; michael@0: } michael@0: michael@0: let topic = data.topic; michael@0: michael@0: try { michael@0: listener.observer.observe(null, topic, null); michael@0: } catch (e) { michael@0: // It seems like there is no callbacks anymore, forward the click on michael@0: // notification via a system message containing the title/text/icon of michael@0: // the notification so the app get a change to react. michael@0: if (data.target) { michael@0: gSystemMessenger.sendMessage("notification", { michael@0: title: listener.title, michael@0: body: listener.text, michael@0: imageURL: listener.imageURL, michael@0: lang: listener.lang, michael@0: dir: listener.dir, michael@0: id: listener.id, michael@0: tag: listener.tag, michael@0: dbId: listener.dbId michael@0: }, michael@0: Services.io.newURI(data.target, null, null), michael@0: Services.io.newURI(listener.manifestURL, null, null)); michael@0: } michael@0: } michael@0: michael@0: // we're done with this notification michael@0: if (topic === "alertfinished") { michael@0: if (listener.dbId) { michael@0: notificationStorage.delete(listener.manifestURL, listener.dbId); michael@0: } michael@0: delete this._listeners[data.uid]; michael@0: } michael@0: } michael@0: }; michael@0: michael@0: this.NSGetFactory = XPCOMUtils.generateNSGetFactory([AlertsService]);