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/. */
9 dump("-*- NotificationStorage.js: " + s + "\n");
12 const kMessageNotificationGetAllOk = "Notification:GetAll:Return:OK";
13 const kMessageNotificationGetAllKo = "Notification:GetAll:Return:KO";
14 const kMessageNotificationSaveKo = "Notification:Save:Return:KO";
15 const kMessageNotificationDeleteKo = "Notification:Delete:Return:KO";
18 kMessageNotificationGetAllOk,
19 kMessageNotificationGetAllKo,
20 kMessageNotificationSaveKo,
21 kMessageNotificationDeleteKo,
24 function NotificationStorage() {
26 this._requestCount = 0;
28 Services.obs.addObserver(this, "xpcom-shutdown");
30 // Register for message listeners.
31 this.registerListeners();
34 NotificationStorage.prototype = {
36 for (let message of kMessages) {
37 Services.cpmm.addMessageListener(message, this);
41 unregisterListeners() {
42 for (let message of kMessages) {
43 Services.cpmm.removeMessageListener(message, this);
47 observe(aSubject, aTopic, aData) {
49 debug("Topic: " + aTopic);
51 if (aTopic === "xpcom-shutdown") {
52 Services.obs.removeObserver(this, "xpcom-shutdown");
53 this.unregisterListeners();
69 serviceWorkerRegistrationScope
72 debug("PUT: " + origin + " " + id + ": " + title);
83 timestamp: new Date().getTime(),
86 mozbehavior: behavior,
87 serviceWorkerRegistrationScope,
90 Services.cpmm.sendAsyncMessage("Notification:Save", {
96 get(origin, tag, callback) {
98 debug("GET: " + origin + " " + tag);
100 this._fetchFromDB(origin, tag, callback);
103 getByID(origin, id, callback) {
105 debug("GETBYID: " + origin + " " + id);
107 var GetByIDProxyCallback = function(id, originalCallback) {
109 this.originalCallback = originalCallback;
111 this.handle = function(
121 serviceWorkerRegistrationScope
123 if (id == this.searchID) {
124 self.originalCallback.handle(
134 serviceWorkerRegistrationScope
138 this.done = function() {
139 self.originalCallback.done();
143 return this.get(origin, "", new GetByIDProxyCallback(id, callback));
148 debug("DELETE: " + id);
150 Services.cpmm.sendAsyncMessage("Notification:Delete", {
156 receiveMessage(message) {
157 var request = this._requests[message.data.requestID];
159 switch (message.name) {
160 case kMessageNotificationGetAllOk:
161 delete this._requests[message.data.requestID];
162 this._returnNotifications(
163 message.data.notifications,
170 case kMessageNotificationGetAllKo:
171 delete this._requests[message.data.requestID];
173 request.callback.done();
175 debug("Error calling callback done: " + e);
178 case kMessageNotificationSaveKo:
179 case kMessageNotificationDeleteKo:
182 "Error received when treating: '" +
185 message.data.errorMsg
192 debug("Unrecognized message: " + message.name);
198 _fetchFromDB(origin, tag, callback) {
204 var requestID = this._requestCount++;
205 this._requests[requestID] = request;
206 Services.cpmm.sendAsyncMessage("Notification:GetAll", {
213 _returnNotifications(notifications, origin, tag, callback) {
214 // Pass each notification back separately.
215 // The callback is called asynchronously to match the behaviour when
216 // fetching from the database.
217 notifications.forEach(function(notification) {
219 Services.tm.dispatchToMainThread(
220 callback.handle.bind(
230 notification.mozbehavior,
231 notification.serviceWorkerRegistrationScope
236 debug("Error calling callback handle: " + e);
241 Services.tm.dispatchToMainThread(callback.done);
244 debug("Error calling callback done: " + e);
249 QueryInterface: ChromeUtils.generateQI(["nsINotificationStorage"]),
252 var EXPORTED_SYMBOLS = ["NotificationStorage"];