b2g/components/AlertsService.js

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 const Ci = Components.interfaces;
michael@0 6 const Cu = Components.utils;
michael@0 7 const Cc = Components.classes;
michael@0 8
michael@0 9 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
michael@0 10 Cu.import("resource://gre/modules/Services.jsm");
michael@0 11
michael@0 12 XPCOMUtils.defineLazyServiceGetter(this, "gSystemMessenger",
michael@0 13 "@mozilla.org/system-message-internal;1",
michael@0 14 "nsISystemMessagesInternal");
michael@0 15
michael@0 16 XPCOMUtils.defineLazyServiceGetter(this, "uuidGenerator",
michael@0 17 "@mozilla.org/uuid-generator;1",
michael@0 18 "nsIUUIDGenerator");
michael@0 19
michael@0 20 XPCOMUtils.defineLazyServiceGetter(this, "notificationStorage",
michael@0 21 "@mozilla.org/notificationStorage;1",
michael@0 22 "nsINotificationStorage");
michael@0 23
michael@0 24
michael@0 25 XPCOMUtils.defineLazyGetter(this, "cpmm", function() {
michael@0 26 return Cc["@mozilla.org/childprocessmessagemanager;1"]
michael@0 27 .getService(Ci.nsIMessageSender);
michael@0 28 });
michael@0 29
michael@0 30 function debug(str) {
michael@0 31 dump("=*= AlertsService.js : " + str + "\n");
michael@0 32 }
michael@0 33
michael@0 34 // -----------------------------------------------------------------------
michael@0 35 // Alerts Service
michael@0 36 // -----------------------------------------------------------------------
michael@0 37
michael@0 38 function AlertsService() {
michael@0 39 cpmm.addMessageListener("app-notification-return", this);
michael@0 40 }
michael@0 41
michael@0 42 AlertsService.prototype = {
michael@0 43 classID: Components.ID("{fe33c107-82a4-41d6-8c64-5353267e04c9}"),
michael@0 44 QueryInterface: XPCOMUtils.generateQI([Ci.nsIAlertsService,
michael@0 45 Ci.nsIAppNotificationService]),
michael@0 46
michael@0 47 // nsIAlertsService
michael@0 48 showAlertNotification: function showAlertNotification(aImageUrl,
michael@0 49 aTitle,
michael@0 50 aText,
michael@0 51 aTextClickable,
michael@0 52 aCookie,
michael@0 53 aAlertListener,
michael@0 54 aName,
michael@0 55 aBidi,
michael@0 56 aLang) {
michael@0 57 let browser = Services.wm.getMostRecentWindow("navigator:browser");
michael@0 58 browser.AlertsHelper.showAlertNotification(aImageUrl, aTitle, aText,
michael@0 59 aTextClickable, aCookie,
michael@0 60 aAlertListener, aName, aBidi,
michael@0 61 aLang);
michael@0 62 },
michael@0 63
michael@0 64 closeAlert: function(aName) {
michael@0 65 let browser = Services.wm.getMostRecentWindow("navigator:browser");
michael@0 66 browser.AlertsHelper.closeAlert(aName);
michael@0 67 },
michael@0 68
michael@0 69 // nsIAppNotificationService
michael@0 70 showAppNotification: function showAppNotification(aImageURL,
michael@0 71 aTitle,
michael@0 72 aText,
michael@0 73 aAlertListener,
michael@0 74 aDetails) {
michael@0 75 let uid = (aDetails.id == "") ?
michael@0 76 "app-notif-" + uuidGenerator.generateUUID() : aDetails.id;
michael@0 77
michael@0 78 this._listeners[uid] = {
michael@0 79 observer: aAlertListener,
michael@0 80 title: aTitle,
michael@0 81 text: aText,
michael@0 82 manifestURL: aDetails.manifestURL,
michael@0 83 imageURL: aImageURL,
michael@0 84 lang: aDetails.lang || undefined,
michael@0 85 id: aDetails.id || undefined,
michael@0 86 dbId: aDetails.dbId || undefined,
michael@0 87 dir: aDetails.dir || undefined,
michael@0 88 tag: aDetails.tag || undefined
michael@0 89 };
michael@0 90
michael@0 91 cpmm.sendAsyncMessage("app-notification-send", {
michael@0 92 imageURL: aImageURL,
michael@0 93 title: aTitle,
michael@0 94 text: aText,
michael@0 95 uid: uid,
michael@0 96 details: aDetails
michael@0 97 });
michael@0 98 },
michael@0 99
michael@0 100 // AlertsService.js custom implementation
michael@0 101 _listeners: [],
michael@0 102
michael@0 103 receiveMessage: function receiveMessage(aMessage) {
michael@0 104 let data = aMessage.data;
michael@0 105 let listener = this._listeners[data.uid];
michael@0 106 if (aMessage.name !== "app-notification-return" || !listener) {
michael@0 107 return;
michael@0 108 }
michael@0 109
michael@0 110 let topic = data.topic;
michael@0 111
michael@0 112 try {
michael@0 113 listener.observer.observe(null, topic, null);
michael@0 114 } catch (e) {
michael@0 115 // It seems like there is no callbacks anymore, forward the click on
michael@0 116 // notification via a system message containing the title/text/icon of
michael@0 117 // the notification so the app get a change to react.
michael@0 118 if (data.target) {
michael@0 119 gSystemMessenger.sendMessage("notification", {
michael@0 120 title: listener.title,
michael@0 121 body: listener.text,
michael@0 122 imageURL: listener.imageURL,
michael@0 123 lang: listener.lang,
michael@0 124 dir: listener.dir,
michael@0 125 id: listener.id,
michael@0 126 tag: listener.tag,
michael@0 127 dbId: listener.dbId
michael@0 128 },
michael@0 129 Services.io.newURI(data.target, null, null),
michael@0 130 Services.io.newURI(listener.manifestURL, null, null));
michael@0 131 }
michael@0 132 }
michael@0 133
michael@0 134 // we're done with this notification
michael@0 135 if (topic === "alertfinished") {
michael@0 136 if (listener.dbId) {
michael@0 137 notificationStorage.delete(listener.manifestURL, listener.dbId);
michael@0 138 }
michael@0 139 delete this._listeners[data.uid];
michael@0 140 }
michael@0 141 }
michael@0 142 };
michael@0 143
michael@0 144 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([AlertsService]);

mercurial