2013-10-03 05:27:53 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2015-09-15 02:08:38 +03:00
|
|
|
const DEBUG = false;
|
2013-10-03 05:27:53 +04:00
|
|
|
function debug(s) { dump("-*- NotificationStorage.js: " + s + "\n"); }
|
|
|
|
|
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cu = Components.utils;
|
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
|
|
|
|
const NOTIFICATIONSTORAGE_CID = "{37f819b0-0b5c-11e3-8ffd-0800200c9a66}";
|
|
|
|
const NOTIFICATIONSTORAGE_CONTRACTID = "@mozilla.org/notificationStorage;1";
|
|
|
|
|
2014-06-09 10:34:00 +04:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "Services",
|
|
|
|
"resource://gre/modules/Services.jsm");
|
|
|
|
|
2013-10-03 05:27:53 +04:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "cpmm",
|
|
|
|
"@mozilla.org/childprocessmessagemanager;1",
|
|
|
|
"nsIMessageSender");
|
|
|
|
|
2014-06-09 10:34:00 +04:00
|
|
|
const kMessageNotificationGetAllOk = "Notification:GetAll:Return:OK";
|
|
|
|
const kMessageNotificationGetAllKo = "Notification:GetAll:Return:KO";
|
|
|
|
const kMessageNotificationSaveKo = "Notification:Save:Return:KO";
|
|
|
|
const kMessageNotificationDeleteKo = "Notification:Delete:Return:KO";
|
|
|
|
|
|
|
|
const kMessages = [
|
|
|
|
kMessageNotificationGetAllOk,
|
|
|
|
kMessageNotificationGetAllKo,
|
|
|
|
kMessageNotificationSaveKo,
|
|
|
|
kMessageNotificationDeleteKo
|
|
|
|
];
|
2013-10-03 05:27:53 +04:00
|
|
|
|
|
|
|
function NotificationStorage() {
|
|
|
|
// cache objects
|
|
|
|
this._notifications = {};
|
|
|
|
this._byTag = {};
|
|
|
|
this._cached = false;
|
|
|
|
|
|
|
|
this._requests = {};
|
|
|
|
this._requestCount = 0;
|
|
|
|
|
2017-04-14 22:51:38 +03:00
|
|
|
Services.obs.addObserver(this, "xpcom-shutdown");
|
2015-05-14 11:59:00 +03:00
|
|
|
|
2013-10-03 05:27:53 +04:00
|
|
|
// Register for message listeners.
|
2014-06-09 10:34:00 +04:00
|
|
|
this.registerListeners();
|
2013-10-03 05:27:53 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NotificationStorage.prototype = {
|
|
|
|
|
2014-06-09 10:34:00 +04:00
|
|
|
registerListeners: function() {
|
|
|
|
for (let message of kMessages) {
|
|
|
|
cpmm.addMessageListener(message, this);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
unregisterListeners: function() {
|
|
|
|
for (let message of kMessages) {
|
|
|
|
cpmm.removeMessageListener(message, this);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
observe: function(aSubject, aTopic, aData) {
|
|
|
|
if (DEBUG) debug("Topic: " + aTopic);
|
2015-05-14 11:59:00 +03:00
|
|
|
if (aTopic === "xpcom-shutdown") {
|
2014-06-09 10:34:00 +04:00
|
|
|
Services.obs.removeObserver(this, "xpcom-shutdown");
|
|
|
|
this.unregisterListeners();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-08-21 04:56:12 +04:00
|
|
|
put: function(origin, id, title, dir, lang, body, tag, icon, alertName,
|
2016-05-26 17:19:09 +03:00
|
|
|
data, behavior, serviceWorkerRegistrationScope) {
|
2015-06-26 04:50:25 +03:00
|
|
|
if (DEBUG) { debug("PUT: " + origin + " " + id + ": " + title); }
|
2013-10-03 05:27:53 +04:00
|
|
|
var notification = {
|
|
|
|
id: id,
|
|
|
|
title: title,
|
|
|
|
dir: dir,
|
|
|
|
lang: lang,
|
|
|
|
body: body,
|
|
|
|
tag: tag,
|
2014-05-09 01:16:00 +04:00
|
|
|
icon: icon,
|
|
|
|
alertName: alertName,
|
2014-04-24 18:14:09 +04:00
|
|
|
timestamp: new Date().getTime(),
|
2014-08-21 04:56:12 +04:00
|
|
|
origin: origin,
|
2014-09-17 01:12:00 +04:00
|
|
|
data: data,
|
2015-06-26 04:50:25 +03:00
|
|
|
mozbehavior: behavior,
|
2016-05-26 17:19:09 +03:00
|
|
|
serviceWorkerRegistrationScope: serviceWorkerRegistrationScope,
|
2013-10-03 05:27:53 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
this._notifications[id] = notification;
|
|
|
|
if (tag) {
|
2014-04-24 18:14:09 +04:00
|
|
|
if (!this._byTag[origin]) {
|
|
|
|
this._byTag[origin] = {};
|
|
|
|
}
|
|
|
|
|
2013-10-03 05:27:53 +04:00
|
|
|
// We might have existing notification with this tag,
|
|
|
|
// if so we need to remove it from our cache.
|
2014-04-24 18:14:09 +04:00
|
|
|
if (this._byTag[origin][tag]) {
|
|
|
|
var oldNotification = this._byTag[origin][tag];
|
2013-10-03 05:27:53 +04:00
|
|
|
delete this._notifications[oldNotification.id];
|
|
|
|
}
|
|
|
|
|
2014-04-24 18:14:09 +04:00
|
|
|
this._byTag[origin][tag] = notification;
|
2013-10-03 05:27:53 +04:00
|
|
|
};
|
|
|
|
|
2016-09-24 02:30:09 +03:00
|
|
|
if (serviceWorkerRegistrationScope) {
|
2015-05-14 11:59:00 +03:00
|
|
|
cpmm.sendAsyncMessage("Notification:Save", {
|
|
|
|
origin: origin,
|
|
|
|
notification: notification
|
|
|
|
});
|
|
|
|
}
|
2013-10-03 05:27:53 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
get: function(origin, tag, callback) {
|
2014-04-24 18:14:09 +04:00
|
|
|
if (DEBUG) { debug("GET: " + origin + " " + tag); }
|
2013-10-03 05:27:53 +04:00
|
|
|
if (this._cached) {
|
2014-04-24 18:14:09 +04:00
|
|
|
this._fetchFromCache(origin, tag, callback);
|
2013-10-03 05:27:53 +04:00
|
|
|
} else {
|
|
|
|
this._fetchFromDB(origin, tag, callback);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-06-26 04:50:25 +03:00
|
|
|
getByID: function(origin, id, callback) {
|
|
|
|
if (DEBUG) { debug("GETBYID: " + origin + " " + id); }
|
|
|
|
var GetByIDProxyCallback = function(id, originalCallback) {
|
|
|
|
this.searchID = id;
|
|
|
|
this.originalCallback = originalCallback;
|
|
|
|
var self = this;
|
2016-05-26 17:19:09 +03:00
|
|
|
this.handle = function(id, title, dir, lang, body, tag, icon, data, behavior, serviceWorkerRegistrationScope) {
|
2015-06-26 04:50:25 +03:00
|
|
|
if (id == this.searchID) {
|
2016-05-26 17:19:09 +03:00
|
|
|
self.originalCallback.handle(id, title, dir, lang, body, tag, icon, data, behavior, serviceWorkerRegistrationScope);
|
2015-06-26 04:50:25 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
this.done = function() {
|
|
|
|
self.originalCallback.done();
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
return this.get(origin, "", new GetByIDProxyCallback(id, callback));
|
|
|
|
},
|
|
|
|
|
2013-10-03 05:27:53 +04:00
|
|
|
delete: function(origin, id) {
|
|
|
|
if (DEBUG) { debug("DELETE: " + id); }
|
|
|
|
var notification = this._notifications[id];
|
|
|
|
if (notification) {
|
|
|
|
if (notification.tag) {
|
2014-04-24 18:14:09 +04:00
|
|
|
delete this._byTag[origin][notification.tag];
|
2013-10-03 05:27:53 +04:00
|
|
|
}
|
|
|
|
delete this._notifications[id];
|
|
|
|
}
|
|
|
|
|
|
|
|
cpmm.sendAsyncMessage("Notification:Delete", {
|
|
|
|
origin: origin,
|
|
|
|
id: id
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
receiveMessage: function(message) {
|
2014-06-09 10:34:00 +04:00
|
|
|
var request = this._requests[message.data.requestID];
|
|
|
|
|
2013-10-03 05:27:53 +04:00
|
|
|
switch (message.name) {
|
2014-06-09 10:34:00 +04:00
|
|
|
case kMessageNotificationGetAllOk:
|
2013-10-03 05:27:53 +04:00
|
|
|
delete this._requests[message.data.requestID];
|
|
|
|
this._populateCache(message.data.notifications);
|
2014-04-24 18:14:09 +04:00
|
|
|
this._fetchFromCache(request.origin, request.tag, request.callback);
|
2013-10-03 05:27:53 +04:00
|
|
|
break;
|
|
|
|
|
2014-06-09 10:34:00 +04:00
|
|
|
case kMessageNotificationGetAllKo:
|
|
|
|
delete this._requests[message.data.requestID];
|
|
|
|
try {
|
|
|
|
request.callback.done();
|
|
|
|
} catch (e) {
|
|
|
|
debug("Error calling callback done: " + e);
|
|
|
|
}
|
2014-06-14 11:52:00 +04:00
|
|
|
case kMessageNotificationSaveKo:
|
|
|
|
case kMessageNotificationDeleteKo:
|
|
|
|
if (DEBUG) {
|
|
|
|
debug("Error received when treating: '" + message.name +
|
|
|
|
"': " + message.data.errorMsg);
|
|
|
|
}
|
2014-06-09 10:34:00 +04:00
|
|
|
break;
|
|
|
|
|
2013-10-03 05:27:53 +04:00
|
|
|
default:
|
|
|
|
if (DEBUG) debug("Unrecognized message: " + message.name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_fetchFromDB: function(origin, tag, callback) {
|
|
|
|
var request = {
|
|
|
|
origin: origin,
|
|
|
|
tag: tag,
|
|
|
|
callback: callback
|
|
|
|
};
|
|
|
|
var requestID = this._requestCount++;
|
|
|
|
this._requests[requestID] = request;
|
|
|
|
cpmm.sendAsyncMessage("Notification:GetAll", {
|
|
|
|
origin: origin,
|
|
|
|
requestID: requestID
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2014-04-24 18:14:09 +04:00
|
|
|
_fetchFromCache: function(origin, tag, callback) {
|
2013-10-03 05:27:53 +04:00
|
|
|
var notifications = [];
|
|
|
|
// If a tag was specified and we have a notification
|
|
|
|
// with this tag, return that. If no tag was specified
|
|
|
|
// simple return all stored notifications.
|
2014-04-24 18:14:09 +04:00
|
|
|
if (tag && this._byTag[origin] && this._byTag[origin][tag]) {
|
|
|
|
notifications.push(this._byTag[origin][tag]);
|
2013-10-03 05:27:53 +04:00
|
|
|
} else if (!tag) {
|
|
|
|
for (var id in this._notifications) {
|
2014-04-24 18:14:09 +04:00
|
|
|
if (this._notifications[id].origin === origin) {
|
|
|
|
notifications.push(this._notifications[id]);
|
|
|
|
}
|
2013-10-03 05:27:53 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pass each notification back separately.
|
2015-06-26 04:50:24 +03:00
|
|
|
// The callback is called asynchronously to match the behaviour when
|
|
|
|
// fetching from the database.
|
2013-10-03 05:27:53 +04:00
|
|
|
notifications.forEach(function(notification) {
|
|
|
|
try {
|
2017-04-14 19:29:12 +03:00
|
|
|
Services.tm.dispatchToMainThread(
|
2015-06-26 04:50:24 +03:00
|
|
|
callback.handle.bind(callback,
|
|
|
|
notification.id,
|
|
|
|
notification.title,
|
|
|
|
notification.dir,
|
|
|
|
notification.lang,
|
|
|
|
notification.body,
|
|
|
|
notification.tag,
|
|
|
|
notification.icon,
|
|
|
|
notification.data,
|
2015-06-26 04:50:25 +03:00
|
|
|
notification.mozbehavior,
|
2017-04-14 19:29:12 +03:00
|
|
|
notification.serviceWorkerRegistrationScope));
|
2013-10-03 05:27:53 +04:00
|
|
|
} catch (e) {
|
|
|
|
if (DEBUG) { debug("Error calling callback handle: " + e); }
|
|
|
|
}
|
|
|
|
});
|
|
|
|
try {
|
2017-04-14 19:29:12 +03:00
|
|
|
Services.tm.dispatchToMainThread(callback.done);
|
2013-10-03 05:27:53 +04:00
|
|
|
} catch (e) {
|
|
|
|
if (DEBUG) { debug("Error calling callback done: " + e); }
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_populateCache: function(notifications) {
|
2017-04-27 01:25:45 +03:00
|
|
|
notifications.forEach(notification => {
|
2013-10-03 05:27:53 +04:00
|
|
|
this._notifications[notification.id] = notification;
|
2014-04-24 18:14:09 +04:00
|
|
|
if (notification.tag && notification.origin) {
|
|
|
|
let tag = notification.tag;
|
|
|
|
let origin = notification.origin;
|
|
|
|
if (!this._byTag[origin]) {
|
|
|
|
this._byTag[origin] = {};
|
|
|
|
}
|
|
|
|
this._byTag[origin][tag] = notification;
|
2013-10-03 05:27:53 +04:00
|
|
|
}
|
2017-04-27 01:25:45 +03:00
|
|
|
});
|
2013-10-03 05:27:53 +04:00
|
|
|
this._cached = true;
|
|
|
|
},
|
|
|
|
|
|
|
|
classID : Components.ID(NOTIFICATIONSTORAGE_CID),
|
|
|
|
contractID : NOTIFICATIONSTORAGE_CONTRACTID,
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsINotificationStorage,
|
|
|
|
Ci.nsIMessageListener]),
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([NotificationStorage]);
|