NotificationStorage.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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/. */
  4. "use strict";
  5. const DEBUG = false;
  6. function debug(s) { dump("-*- NotificationStorage.js: " + s + "\n"); }
  7. const Cc = Components.classes;
  8. const Ci = Components.interfaces;
  9. const Cu = Components.utils;
  10. Cu.import("resource://gre/modules/XPCOMUtils.jsm");
  11. const NOTIFICATIONSTORAGE_CID = "{37f819b0-0b5c-11e3-8ffd-0800200c9a66}";
  12. const NOTIFICATIONSTORAGE_CONTRACTID = "@mozilla.org/notificationStorage;1";
  13. XPCOMUtils.defineLazyModuleGetter(this, "Services",
  14. "resource://gre/modules/Services.jsm");
  15. XPCOMUtils.defineLazyServiceGetter(this, "cpmm",
  16. "@mozilla.org/childprocessmessagemanager;1",
  17. "nsIMessageSender");
  18. const kMessageNotificationGetAllOk = "Notification:GetAll:Return:OK";
  19. const kMessageNotificationGetAllKo = "Notification:GetAll:Return:KO";
  20. const kMessageNotificationSaveKo = "Notification:Save:Return:KO";
  21. const kMessageNotificationDeleteKo = "Notification:Delete:Return:KO";
  22. const kMessages = [
  23. kMessageNotificationGetAllOk,
  24. kMessageNotificationGetAllKo,
  25. kMessageNotificationSaveKo,
  26. kMessageNotificationDeleteKo
  27. ];
  28. function NotificationStorage() {
  29. // cache objects
  30. this._notifications = {};
  31. this._byTag = {};
  32. this._cached = false;
  33. this._requests = {};
  34. this._requestCount = 0;
  35. Services.obs.addObserver(this, "xpcom-shutdown", false);
  36. // Register for message listeners.
  37. this.registerListeners();
  38. }
  39. NotificationStorage.prototype = {
  40. registerListeners: function() {
  41. for (let message of kMessages) {
  42. cpmm.addMessageListener(message, this);
  43. }
  44. },
  45. unregisterListeners: function() {
  46. for (let message of kMessages) {
  47. cpmm.removeMessageListener(message, this);
  48. }
  49. },
  50. observe: function(aSubject, aTopic, aData) {
  51. if (DEBUG) debug("Topic: " + aTopic);
  52. if (aTopic === "xpcom-shutdown") {
  53. Services.obs.removeObserver(this, "xpcom-shutdown");
  54. this.unregisterListeners();
  55. }
  56. },
  57. put: function(origin, id, title, dir, lang, body, tag, icon, alertName,
  58. data, behavior, serviceWorkerRegistrationScope) {
  59. if (DEBUG) { debug("PUT: " + origin + " " + id + ": " + title); }
  60. var notification = {
  61. id: id,
  62. title: title,
  63. dir: dir,
  64. lang: lang,
  65. body: body,
  66. tag: tag,
  67. icon: icon,
  68. alertName: alertName,
  69. timestamp: new Date().getTime(),
  70. origin: origin,
  71. data: data,
  72. mozbehavior: behavior,
  73. serviceWorkerRegistrationScope: serviceWorkerRegistrationScope,
  74. };
  75. this._notifications[id] = notification;
  76. if (tag) {
  77. if (!this._byTag[origin]) {
  78. this._byTag[origin] = {};
  79. }
  80. // We might have existing notification with this tag,
  81. // if so we need to remove it from our cache.
  82. if (this._byTag[origin][tag]) {
  83. var oldNotification = this._byTag[origin][tag];
  84. delete this._notifications[oldNotification.id];
  85. }
  86. this._byTag[origin][tag] = notification;
  87. };
  88. if (serviceWorkerRegistrationScope) {
  89. cpmm.sendAsyncMessage("Notification:Save", {
  90. origin: origin,
  91. notification: notification
  92. });
  93. }
  94. },
  95. get: function(origin, tag, callback) {
  96. if (DEBUG) { debug("GET: " + origin + " " + tag); }
  97. if (this._cached) {
  98. this._fetchFromCache(origin, tag, callback);
  99. } else {
  100. this._fetchFromDB(origin, tag, callback);
  101. }
  102. },
  103. getByID: function(origin, id, callback) {
  104. if (DEBUG) { debug("GETBYID: " + origin + " " + id); }
  105. var GetByIDProxyCallback = function(id, originalCallback) {
  106. this.searchID = id;
  107. this.originalCallback = originalCallback;
  108. var self = this;
  109. this.handle = function(id, title, dir, lang, body, tag, icon, data, behavior, serviceWorkerRegistrationScope) {
  110. if (id == this.searchID) {
  111. self.originalCallback.handle(id, title, dir, lang, body, tag, icon, data, behavior, serviceWorkerRegistrationScope);
  112. }
  113. };
  114. this.done = function() {
  115. self.originalCallback.done();
  116. };
  117. };
  118. return this.get(origin, "", new GetByIDProxyCallback(id, callback));
  119. },
  120. delete: function(origin, id) {
  121. if (DEBUG) { debug("DELETE: " + id); }
  122. var notification = this._notifications[id];
  123. if (notification) {
  124. if (notification.tag) {
  125. delete this._byTag[origin][notification.tag];
  126. }
  127. delete this._notifications[id];
  128. }
  129. cpmm.sendAsyncMessage("Notification:Delete", {
  130. origin: origin,
  131. id: id
  132. });
  133. },
  134. receiveMessage: function(message) {
  135. var request = this._requests[message.data.requestID];
  136. switch (message.name) {
  137. case kMessageNotificationGetAllOk:
  138. delete this._requests[message.data.requestID];
  139. this._populateCache(message.data.notifications);
  140. this._fetchFromCache(request.origin, request.tag, request.callback);
  141. break;
  142. case kMessageNotificationGetAllKo:
  143. delete this._requests[message.data.requestID];
  144. try {
  145. request.callback.done();
  146. } catch (e) {
  147. debug("Error calling callback done: " + e);
  148. }
  149. case kMessageNotificationSaveKo:
  150. case kMessageNotificationDeleteKo:
  151. if (DEBUG) {
  152. debug("Error received when treating: '" + message.name +
  153. "': " + message.data.errorMsg);
  154. }
  155. break;
  156. default:
  157. if (DEBUG) debug("Unrecognized message: " + message.name);
  158. break;
  159. }
  160. },
  161. _fetchFromDB: function(origin, tag, callback) {
  162. var request = {
  163. origin: origin,
  164. tag: tag,
  165. callback: callback
  166. };
  167. var requestID = this._requestCount++;
  168. this._requests[requestID] = request;
  169. cpmm.sendAsyncMessage("Notification:GetAll", {
  170. origin: origin,
  171. requestID: requestID
  172. });
  173. },
  174. _fetchFromCache: function(origin, tag, callback) {
  175. var notifications = [];
  176. // If a tag was specified and we have a notification
  177. // with this tag, return that. If no tag was specified
  178. // simple return all stored notifications.
  179. if (tag && this._byTag[origin] && this._byTag[origin][tag]) {
  180. notifications.push(this._byTag[origin][tag]);
  181. } else if (!tag) {
  182. for (var id in this._notifications) {
  183. if (this._notifications[id].origin === origin) {
  184. notifications.push(this._notifications[id]);
  185. }
  186. }
  187. }
  188. // Pass each notification back separately.
  189. // The callback is called asynchronously to match the behaviour when
  190. // fetching from the database.
  191. notifications.forEach(function(notification) {
  192. try {
  193. Services.tm.currentThread.dispatch(
  194. callback.handle.bind(callback,
  195. notification.id,
  196. notification.title,
  197. notification.dir,
  198. notification.lang,
  199. notification.body,
  200. notification.tag,
  201. notification.icon,
  202. notification.data,
  203. notification.mozbehavior,
  204. notification.serviceWorkerRegistrationScope),
  205. Ci.nsIThread.DISPATCH_NORMAL);
  206. } catch (e) {
  207. if (DEBUG) { debug("Error calling callback handle: " + e); }
  208. }
  209. });
  210. try {
  211. Services.tm.currentThread.dispatch(callback.done,
  212. Ci.nsIThread.DISPATCH_NORMAL);
  213. } catch (e) {
  214. if (DEBUG) { debug("Error calling callback done: " + e); }
  215. }
  216. },
  217. _populateCache: function(notifications) {
  218. notifications.forEach(function(notification) {
  219. this._notifications[notification.id] = notification;
  220. if (notification.tag && notification.origin) {
  221. let tag = notification.tag;
  222. let origin = notification.origin;
  223. if (!this._byTag[origin]) {
  224. this._byTag[origin] = {};
  225. }
  226. this._byTag[origin][tag] = notification;
  227. }
  228. }.bind(this));
  229. this._cached = true;
  230. },
  231. classID : Components.ID(NOTIFICATIONSTORAGE_CID),
  232. contractID : NOTIFICATIONSTORAGE_CONTRACTID,
  233. QueryInterface: XPCOMUtils.generateQI([Ci.nsINotificationStorage,
  234. Ci.nsIMessageListener]),
  235. };
  236. this.NSGetFactory = XPCOMUtils.generateNSGetFactory([NotificationStorage]);