b2g/components/AlertsService.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/b2g/components/AlertsService.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,144 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +const Ci = Components.interfaces;
     1.9 +const Cu = Components.utils;
    1.10 +const Cc = Components.classes;
    1.11 +
    1.12 +Cu.import("resource://gre/modules/XPCOMUtils.jsm");
    1.13 +Cu.import("resource://gre/modules/Services.jsm");
    1.14 +
    1.15 +XPCOMUtils.defineLazyServiceGetter(this, "gSystemMessenger",
    1.16 +                                   "@mozilla.org/system-message-internal;1",
    1.17 +                                   "nsISystemMessagesInternal");
    1.18 +
    1.19 +XPCOMUtils.defineLazyServiceGetter(this, "uuidGenerator",
    1.20 +                                   "@mozilla.org/uuid-generator;1",
    1.21 +                                   "nsIUUIDGenerator");
    1.22 +
    1.23 +XPCOMUtils.defineLazyServiceGetter(this, "notificationStorage",
    1.24 +                                   "@mozilla.org/notificationStorage;1",
    1.25 +                                   "nsINotificationStorage");
    1.26 +
    1.27 +
    1.28 +XPCOMUtils.defineLazyGetter(this, "cpmm", function() {
    1.29 +  return Cc["@mozilla.org/childprocessmessagemanager;1"]
    1.30 +           .getService(Ci.nsIMessageSender);
    1.31 +});
    1.32 +
    1.33 +function debug(str) {
    1.34 +  dump("=*= AlertsService.js : " + str + "\n");
    1.35 +}
    1.36 +
    1.37 +// -----------------------------------------------------------------------
    1.38 +// Alerts Service
    1.39 +// -----------------------------------------------------------------------
    1.40 +
    1.41 +function AlertsService() {
    1.42 +  cpmm.addMessageListener("app-notification-return", this);
    1.43 +}
    1.44 +
    1.45 +AlertsService.prototype = {
    1.46 +  classID: Components.ID("{fe33c107-82a4-41d6-8c64-5353267e04c9}"),
    1.47 +  QueryInterface: XPCOMUtils.generateQI([Ci.nsIAlertsService,
    1.48 +                                         Ci.nsIAppNotificationService]),
    1.49 +
    1.50 +  // nsIAlertsService
    1.51 +  showAlertNotification: function showAlertNotification(aImageUrl,
    1.52 +                                                        aTitle,
    1.53 +                                                        aText,
    1.54 +                                                        aTextClickable,
    1.55 +                                                        aCookie,
    1.56 +                                                        aAlertListener,
    1.57 +                                                        aName,
    1.58 +                                                        aBidi,
    1.59 +                                                        aLang) {
    1.60 +    let browser = Services.wm.getMostRecentWindow("navigator:browser");
    1.61 +    browser.AlertsHelper.showAlertNotification(aImageUrl, aTitle, aText,
    1.62 +                                               aTextClickable, aCookie,
    1.63 +                                               aAlertListener, aName, aBidi,
    1.64 +                                               aLang);
    1.65 +  },
    1.66 +
    1.67 +  closeAlert: function(aName) {
    1.68 +    let browser = Services.wm.getMostRecentWindow("navigator:browser");
    1.69 +    browser.AlertsHelper.closeAlert(aName);
    1.70 +  },
    1.71 +
    1.72 +  // nsIAppNotificationService
    1.73 +  showAppNotification: function showAppNotification(aImageURL,
    1.74 +                                                    aTitle,
    1.75 +                                                    aText,
    1.76 +                                                    aAlertListener,
    1.77 +                                                    aDetails) {
    1.78 +    let uid = (aDetails.id == "") ?
    1.79 +          "app-notif-" + uuidGenerator.generateUUID() : aDetails.id;
    1.80 +
    1.81 +    this._listeners[uid] = {
    1.82 +      observer: aAlertListener,
    1.83 +      title: aTitle,
    1.84 +      text: aText,
    1.85 +      manifestURL: aDetails.manifestURL,
    1.86 +      imageURL: aImageURL,
    1.87 +      lang: aDetails.lang || undefined,
    1.88 +      id: aDetails.id || undefined,
    1.89 +      dbId: aDetails.dbId || undefined,
    1.90 +      dir: aDetails.dir || undefined,
    1.91 +      tag: aDetails.tag || undefined
    1.92 +    };
    1.93 +
    1.94 +    cpmm.sendAsyncMessage("app-notification-send", {
    1.95 +      imageURL: aImageURL,
    1.96 +      title: aTitle,
    1.97 +      text: aText,
    1.98 +      uid: uid,
    1.99 +      details: aDetails
   1.100 +    });
   1.101 +  },
   1.102 +
   1.103 +  // AlertsService.js custom implementation
   1.104 +  _listeners: [],
   1.105 +
   1.106 +  receiveMessage: function receiveMessage(aMessage) {
   1.107 +    let data = aMessage.data;
   1.108 +    let listener = this._listeners[data.uid];
   1.109 +    if (aMessage.name !== "app-notification-return" || !listener) {
   1.110 +      return;
   1.111 +    }
   1.112 +
   1.113 +    let topic = data.topic;
   1.114 +
   1.115 +    try {
   1.116 +      listener.observer.observe(null, topic, null);
   1.117 +    } catch (e) {
   1.118 +      // It seems like there is no callbacks anymore, forward the click on
   1.119 +      // notification via a system message containing the title/text/icon of
   1.120 +      // the notification so the app get a change to react.
   1.121 +      if (data.target) {
   1.122 +        gSystemMessenger.sendMessage("notification", {
   1.123 +            title: listener.title,
   1.124 +            body: listener.text,
   1.125 +            imageURL: listener.imageURL,
   1.126 +            lang: listener.lang,
   1.127 +            dir: listener.dir,
   1.128 +            id: listener.id,
   1.129 +            tag: listener.tag,
   1.130 +            dbId: listener.dbId
   1.131 +          },
   1.132 +          Services.io.newURI(data.target, null, null),
   1.133 +          Services.io.newURI(listener.manifestURL, null, null));
   1.134 +      }
   1.135 +    }
   1.136 +
   1.137 +    // we're done with this notification
   1.138 +    if (topic === "alertfinished") {
   1.139 +      if (listener.dbId) {
   1.140 +        notificationStorage.delete(listener.manifestURL, listener.dbId);
   1.141 +      }
   1.142 +      delete this._listeners[data.uid];
   1.143 +    }
   1.144 +  }
   1.145 +};
   1.146 +
   1.147 +this.NSGetFactory = XPCOMUtils.generateNSGetFactory([AlertsService]);

mercurial