b2g/components/AlertsService.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial