2013-10-02 18:27:53 -07: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";
|
|
|
|
|
|
|
|
const DEBUG = false;
|
|
|
|
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-08 23:34:00 -07:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "Services",
|
|
|
|
"resource://gre/modules/Services.jsm");
|
|
|
|
|
2013-10-02 18:27:53 -07:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "cpmm",
|
|
|
|
"@mozilla.org/childprocessmessagemanager;1",
|
|
|
|
"nsIMessageSender");
|
|
|
|
|
2014-06-08 23:34:00 -07: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-02 18:27:53 -07:00
|
|
|
|
|
|
|
function NotificationStorage() {
|
|
|
|
// cache objects
|
|
|
|
this._notifications = {};
|
|
|
|
this._byTag = {};
|
|
|
|
this._cached = false;
|
|
|
|
|
|
|
|
this._requests = {};
|
|
|
|
this._requestCount = 0;
|
|
|
|
|
2014-06-08 23:34:00 -07:00
|
|
|
Services.obs.addObserver(this, "xpcom-shutdown", false);
|
2013-10-02 18:27:53 -07:00
|
|
|
// Register for message listeners.
|
2014-06-08 23:34:00 -07:00
|
|
|
this.registerListeners();
|
2013-10-02 18:27:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NotificationStorage.prototype = {
|
|
|
|
|
2014-06-08 23:34:00 -07: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);
|
|
|
|
if (aTopic == "xpcom-shutdown") {
|
|
|
|
Services.obs.removeObserver(this, "xpcom-shutdown");
|
|
|
|
this.unregisterListeners();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-08-20 17:56:12 -07:00
|
|
|
put: function(origin, id, title, dir, lang, body, tag, icon, alertName,
|
2014-09-16 14:12:00 -07:00
|
|
|
data, behavior) {
|
2013-10-02 18:27:53 -07:00
|
|
|
if (DEBUG) { debug("PUT: " + id + ": " + title); }
|
|
|
|
var notification = {
|
|
|
|
id: id,
|
|
|
|
title: title,
|
|
|
|
dir: dir,
|
|
|
|
lang: lang,
|
|
|
|
body: body,
|
|
|
|
tag: tag,
|
2014-05-08 14:16:00 -07:00
|
|
|
icon: icon,
|
|
|
|
alertName: alertName,
|
2014-04-24 07:14:09 -07:00
|
|
|
timestamp: new Date().getTime(),
|
2014-08-20 17:56:12 -07:00
|
|
|
origin: origin,
|
2014-09-16 14:12:00 -07:00
|
|
|
data: data,
|
2014-09-19 12:35:00 -07:00
|
|
|
mozbehavior: behavior
|
2013-10-02 18:27:53 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
this._notifications[id] = notification;
|
|
|
|
if (tag) {
|
2014-04-24 07:14:09 -07:00
|
|
|
if (!this._byTag[origin]) {
|
|
|
|
this._byTag[origin] = {};
|
|
|
|
}
|
|
|
|
|
2013-10-02 18:27:53 -07:00
|
|
|
// We might have existing notification with this tag,
|
|
|
|
// if so we need to remove it from our cache.
|
2014-04-24 07:14:09 -07:00
|
|
|
if (this._byTag[origin][tag]) {
|
|
|
|
var oldNotification = this._byTag[origin][tag];
|
2013-10-02 18:27:53 -07:00
|
|
|
delete this._notifications[oldNotification.id];
|
|
|
|
}
|
|
|
|
|
2014-04-24 07:14:09 -07:00
|
|
|
this._byTag[origin][tag] = notification;
|
2013-10-02 18:27:53 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
cpmm.sendAsyncMessage("Notification:Save", {
|
|
|
|
origin: origin,
|
|
|
|
notification: notification
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
get: function(origin, tag, callback) {
|
2014-04-24 07:14:09 -07:00
|
|
|
if (DEBUG) { debug("GET: " + origin + " " + tag); }
|
2013-10-02 18:27:53 -07:00
|
|
|
if (this._cached) {
|
2014-04-24 07:14:09 -07:00
|
|
|
this._fetchFromCache(origin, tag, callback);
|
2013-10-02 18:27:53 -07:00
|
|
|
} else {
|
|
|
|
this._fetchFromDB(origin, tag, callback);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
delete: function(origin, id) {
|
|
|
|
if (DEBUG) { debug("DELETE: " + id); }
|
|
|
|
var notification = this._notifications[id];
|
|
|
|
if (notification) {
|
|
|
|
if (notification.tag) {
|
2014-04-24 07:14:09 -07:00
|
|
|
delete this._byTag[origin][notification.tag];
|
2013-10-02 18:27:53 -07:00
|
|
|
}
|
|
|
|
delete this._notifications[id];
|
|
|
|
}
|
|
|
|
|
|
|
|
cpmm.sendAsyncMessage("Notification:Delete", {
|
|
|
|
origin: origin,
|
|
|
|
id: id
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
receiveMessage: function(message) {
|
2014-06-08 23:34:00 -07:00
|
|
|
var request = this._requests[message.data.requestID];
|
|
|
|
|
2013-10-02 18:27:53 -07:00
|
|
|
switch (message.name) {
|
2014-06-08 23:34:00 -07:00
|
|
|
case kMessageNotificationGetAllOk:
|
2013-10-02 18:27:53 -07:00
|
|
|
delete this._requests[message.data.requestID];
|
|
|
|
this._populateCache(message.data.notifications);
|
2014-04-24 07:14:09 -07:00
|
|
|
this._fetchFromCache(request.origin, request.tag, request.callback);
|
2013-10-02 18:27:53 -07:00
|
|
|
break;
|
|
|
|
|
2014-06-08 23:34:00 -07:00
|
|
|
case kMessageNotificationGetAllKo:
|
|
|
|
delete this._requests[message.data.requestID];
|
|
|
|
try {
|
|
|
|
request.callback.done();
|
|
|
|
} catch (e) {
|
|
|
|
debug("Error calling callback done: " + e);
|
|
|
|
}
|
2014-06-14 00:52:00 -07:00
|
|
|
case kMessageNotificationSaveKo:
|
|
|
|
case kMessageNotificationDeleteKo:
|
|
|
|
if (DEBUG) {
|
|
|
|
debug("Error received when treating: '" + message.name +
|
|
|
|
"': " + message.data.errorMsg);
|
|
|
|
}
|
2014-06-08 23:34:00 -07:00
|
|
|
break;
|
|
|
|
|
2013-10-02 18:27:53 -07: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 07:14:09 -07:00
|
|
|
_fetchFromCache: function(origin, tag, callback) {
|
2013-10-02 18:27:53 -07: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 07:14:09 -07:00
|
|
|
if (tag && this._byTag[origin] && this._byTag[origin][tag]) {
|
|
|
|
notifications.push(this._byTag[origin][tag]);
|
2013-10-02 18:27:53 -07:00
|
|
|
} else if (!tag) {
|
|
|
|
for (var id in this._notifications) {
|
2014-04-24 07:14:09 -07:00
|
|
|
if (this._notifications[id].origin === origin) {
|
|
|
|
notifications.push(this._notifications[id]);
|
|
|
|
}
|
2013-10-02 18:27:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pass each notification back separately.
|
|
|
|
notifications.forEach(function(notification) {
|
|
|
|
try {
|
|
|
|
callback.handle(notification.id,
|
|
|
|
notification.title,
|
|
|
|
notification.dir,
|
|
|
|
notification.lang,
|
|
|
|
notification.body,
|
|
|
|
notification.tag,
|
2014-08-20 17:56:12 -07:00
|
|
|
notification.icon,
|
2014-09-16 14:12:00 -07:00
|
|
|
notification.data,
|
2014-09-19 12:35:00 -07:00
|
|
|
notification.mozbehavior);
|
2013-10-02 18:27:53 -07:00
|
|
|
} catch (e) {
|
|
|
|
if (DEBUG) { debug("Error calling callback handle: " + e); }
|
|
|
|
}
|
|
|
|
});
|
|
|
|
try {
|
|
|
|
callback.done();
|
|
|
|
} catch (e) {
|
|
|
|
if (DEBUG) { debug("Error calling callback done: " + e); }
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_populateCache: function(notifications) {
|
|
|
|
notifications.forEach(function(notification) {
|
|
|
|
this._notifications[notification.id] = notification;
|
2014-04-24 07:14:09 -07: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-02 18:27:53 -07:00
|
|
|
}
|
|
|
|
}.bind(this));
|
|
|
|
this._cached = true;
|
|
|
|
},
|
|
|
|
|
|
|
|
classID : Components.ID(NOTIFICATIONSTORAGE_CID),
|
|
|
|
contractID : NOTIFICATIONSTORAGE_CONTRACTID,
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsINotificationStorage,
|
|
|
|
Ci.nsIMessageListener]),
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([NotificationStorage]);
|