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";
|
|
|
|
|
|
|
|
this.EXPORTED_SYMBOLS = [];
|
|
|
|
|
|
|
|
const DEBUG = false;
|
|
|
|
function debug(s) { dump("-*- NotificationDB component: " + s + "\n"); }
|
|
|
|
|
|
|
|
const Cu = Components.utils;
|
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
Cu.import("resource://gre/modules/osfile.jsm");
|
2014-06-09 10:34:00 +04:00
|
|
|
Cu.import("resource://gre/modules/Promise.jsm");
|
2013-10-03 05:27:53 +04:00
|
|
|
|
2014-05-09 01:16:00 +04:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "Services",
|
|
|
|
"resource://gre/modules/Services.jsm");
|
|
|
|
|
2013-10-03 05:27:53 +04:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "ppmm",
|
|
|
|
"@mozilla.org/parentprocessmessagemanager;1",
|
|
|
|
"nsIMessageListenerManager");
|
|
|
|
|
2015-05-14 11:59:00 +03:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "notificationStorage",
|
|
|
|
"@mozilla.org/notificationStorage;1",
|
|
|
|
"nsINotificationStorage");
|
|
|
|
|
2013-10-03 05:27:53 +04:00
|
|
|
const NOTIFICATION_STORE_DIR = OS.Constants.Path.profileDir;
|
|
|
|
const NOTIFICATION_STORE_PATH =
|
|
|
|
OS.Path.join(NOTIFICATION_STORE_DIR, "notificationstore.json");
|
|
|
|
|
2014-05-09 01:16:00 +04:00
|
|
|
const kMessages = [
|
|
|
|
"Notification:Save",
|
|
|
|
"Notification:Delete",
|
2016-10-18 07:23:44 +03:00
|
|
|
"Notification:GetAll"
|
2014-05-09 01:16:00 +04:00
|
|
|
];
|
|
|
|
|
2015-09-15 21:19:45 +03:00
|
|
|
var NotificationDB = {
|
2014-05-09 01:16:00 +04:00
|
|
|
|
|
|
|
// Ensure we won't call init() while xpcom-shutdown is performed
|
|
|
|
_shutdownInProgress: false,
|
|
|
|
|
2013-10-03 05:27:53 +04:00
|
|
|
init: function() {
|
2014-05-09 01:16:00 +04:00
|
|
|
if (this._shutdownInProgress) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-10-03 05:27:53 +04:00
|
|
|
this.notifications = {};
|
|
|
|
this.byTag = {};
|
|
|
|
this.loaded = false;
|
|
|
|
|
|
|
|
this.tasks = []; // read/write operation queue
|
2014-06-09 10:34:00 +04:00
|
|
|
this.runningTask = null;
|
2013-10-03 05:27:53 +04:00
|
|
|
|
2017-04-14 22:51:38 +03:00
|
|
|
Services.obs.addObserver(this, "xpcom-shutdown");
|
2014-05-09 01:16:00 +04:00
|
|
|
this.registerListeners();
|
|
|
|
},
|
|
|
|
|
|
|
|
registerListeners: function() {
|
|
|
|
for (let message of kMessages) {
|
|
|
|
ppmm.addMessageListener(message, this);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
unregisterListeners: function() {
|
|
|
|
for (let message of kMessages) {
|
|
|
|
ppmm.removeMessageListener(message, this);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
observe: function(aSubject, aTopic, aData) {
|
|
|
|
if (DEBUG) debug("Topic: " + aTopic);
|
|
|
|
if (aTopic == "xpcom-shutdown") {
|
|
|
|
this._shutdownInProgress = true;
|
|
|
|
Services.obs.removeObserver(this, "xpcom-shutdown");
|
|
|
|
this.unregisterListeners();
|
|
|
|
}
|
2013-10-03 05:27:53 +04:00
|
|
|
},
|
|
|
|
|
2015-05-14 11:59:00 +03:00
|
|
|
filterNonAppNotifications: function(notifications) {
|
2016-05-12 04:28:46 +03:00
|
|
|
for (let origin in notifications) {
|
2016-10-21 17:07:45 +03:00
|
|
|
let persistentNotificationCount = 0;
|
|
|
|
for (let id in notifications[origin]) {
|
|
|
|
if (notifications[origin][id].serviceWorkerRegistrationScope) {
|
|
|
|
persistentNotificationCount++;
|
|
|
|
} else {
|
|
|
|
delete notifications[origin][id];
|
2016-05-12 04:28:46 +03:00
|
|
|
}
|
2015-05-14 11:59:00 +03:00
|
|
|
}
|
2016-10-21 17:07:45 +03:00
|
|
|
if (persistentNotificationCount == 0) {
|
|
|
|
if (DEBUG) debug("Origin " + origin + " is not linked to an app manifest, deleting.");
|
|
|
|
delete notifications[origin];
|
|
|
|
}
|
2015-05-14 11:59:00 +03:00
|
|
|
}
|
2016-10-21 17:07:45 +03:00
|
|
|
|
2015-05-14 11:59:00 +03:00
|
|
|
return notifications;
|
|
|
|
},
|
|
|
|
|
2013-10-03 05:27:53 +04:00
|
|
|
// Attempt to read notification file, if it's not there we will create it.
|
2014-06-09 10:34:00 +04:00
|
|
|
load: function() {
|
|
|
|
var promise = OS.File.read(NOTIFICATION_STORE_PATH, { encoding: "utf-8"});
|
|
|
|
return promise.then(
|
2017-04-27 01:25:45 +03:00
|
|
|
data => {
|
2014-06-14 11:52:00 +04:00
|
|
|
if (data.length > 0) {
|
2016-05-12 04:28:46 +03:00
|
|
|
// Preprocessing phase intends to cleanly separate any migration-related
|
2015-05-14 11:59:00 +03:00
|
|
|
// tasks.
|
2016-05-12 04:28:46 +03:00
|
|
|
this.notifications = this.filterNonAppNotifications(JSON.parse(data));
|
2014-06-14 11:52:00 +04:00
|
|
|
}
|
2015-05-14 11:59:00 +03:00
|
|
|
|
2014-01-24 01:19:31 +04:00
|
|
|
// populate the list of notifications by tag
|
|
|
|
if (this.notifications) {
|
|
|
|
for (var origin in this.notifications) {
|
|
|
|
this.byTag[origin] = {};
|
|
|
|
for (var id in this.notifications[origin]) {
|
|
|
|
var curNotification = this.notifications[origin][id];
|
|
|
|
if (curNotification.tag) {
|
|
|
|
this.byTag[origin][curNotification.tag] = curNotification;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-14 11:59:00 +03:00
|
|
|
|
2013-10-03 05:27:53 +04:00
|
|
|
this.loaded = true;
|
2017-04-27 01:25:45 +03:00
|
|
|
},
|
2013-10-03 05:27:53 +04:00
|
|
|
|
|
|
|
// If read failed, we assume we have no notifications to load.
|
2017-04-27 01:25:45 +03:00
|
|
|
reason => {
|
2013-10-03 05:27:53 +04:00
|
|
|
this.loaded = true;
|
2014-06-09 10:34:00 +04:00
|
|
|
return this.createStore();
|
2017-04-27 01:25:45 +03:00
|
|
|
}
|
2013-10-03 05:27:53 +04:00
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
// Creates the notification directory.
|
2014-06-09 10:34:00 +04:00
|
|
|
createStore: function() {
|
2013-10-03 05:27:53 +04:00
|
|
|
var promise = OS.File.makeDir(NOTIFICATION_STORE_DIR, {
|
|
|
|
ignoreExisting: true
|
|
|
|
});
|
2014-06-09 10:34:00 +04:00
|
|
|
return promise.then(
|
|
|
|
this.createFile.bind(this)
|
2013-10-03 05:27:53 +04:00
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
// Creates the notification file once the directory is created.
|
2014-06-09 10:34:00 +04:00
|
|
|
createFile: function() {
|
|
|
|
return OS.File.writeAtomic(NOTIFICATION_STORE_PATH, "");
|
2013-10-03 05:27:53 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
// Save current notifications to the file.
|
2014-06-09 10:34:00 +04:00
|
|
|
save: function() {
|
|
|
|
var data = JSON.stringify(this.notifications);
|
|
|
|
return OS.File.writeAtomic(NOTIFICATION_STORE_PATH, data, { encoding: "utf-8"});
|
2013-10-03 05:27:53 +04:00
|
|
|
},
|
|
|
|
|
2014-06-09 10:34:00 +04:00
|
|
|
// Helper function: promise will be resolved once file exists and/or is loaded.
|
|
|
|
ensureLoaded: function() {
|
2013-10-03 05:27:53 +04:00
|
|
|
if (!this.loaded) {
|
2014-06-09 10:34:00 +04:00
|
|
|
return this.load();
|
2013-10-03 05:27:53 +04:00
|
|
|
} else {
|
2014-06-09 10:34:00 +04:00
|
|
|
return Promise.resolve();
|
2013-10-03 05:27:53 +04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
receiveMessage: function(message) {
|
|
|
|
if (DEBUG) { debug("Received message:" + message.name); }
|
|
|
|
|
2014-01-24 02:20:31 +04:00
|
|
|
// sendAsyncMessage can fail if the child process exits during a
|
|
|
|
// notification storage operation, so always wrap it in a try/catch.
|
|
|
|
function returnMessage(name, data) {
|
|
|
|
try {
|
|
|
|
message.target.sendAsyncMessage(name, data);
|
|
|
|
} catch (e) {
|
|
|
|
if (DEBUG) { debug("Return message failed, " + name); }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-03 05:27:53 +04:00
|
|
|
switch (message.name) {
|
|
|
|
case "Notification:GetAll":
|
2014-06-09 10:34:00 +04:00
|
|
|
this.queueTask("getall", message.data).then(function(notifications) {
|
2014-01-24 02:20:31 +04:00
|
|
|
returnMessage("Notification:GetAll:Return:OK", {
|
2013-10-03 05:27:53 +04:00
|
|
|
requestID: message.data.requestID,
|
2014-04-24 18:14:09 +04:00
|
|
|
origin: message.data.origin,
|
2013-10-03 05:27:53 +04:00
|
|
|
notifications: notifications
|
|
|
|
});
|
2014-06-09 10:34:00 +04:00
|
|
|
}).catch(function(error) {
|
|
|
|
returnMessage("Notification:GetAll:Return:KO", {
|
|
|
|
requestID: message.data.requestID,
|
|
|
|
origin: message.data.origin,
|
|
|
|
errorMsg: error
|
|
|
|
});
|
2013-10-03 05:27:53 +04:00
|
|
|
});
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "Notification:Save":
|
2014-06-09 10:34:00 +04:00
|
|
|
this.queueTask("save", message.data).then(function() {
|
2014-01-24 02:20:31 +04:00
|
|
|
returnMessage("Notification:Save:Return:OK", {
|
2013-10-03 05:27:53 +04:00
|
|
|
requestID: message.data.requestID
|
|
|
|
});
|
2014-06-09 10:34:00 +04:00
|
|
|
}).catch(function(error) {
|
|
|
|
returnMessage("Notification:Save:Return:KO", {
|
|
|
|
requestID: message.data.requestID,
|
|
|
|
errorMsg: error
|
|
|
|
});
|
2013-10-03 05:27:53 +04:00
|
|
|
});
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "Notification:Delete":
|
2014-06-09 10:34:00 +04:00
|
|
|
this.queueTask("delete", message.data).then(function() {
|
2014-01-24 02:20:31 +04:00
|
|
|
returnMessage("Notification:Delete:Return:OK", {
|
2013-10-03 05:27:53 +04:00
|
|
|
requestID: message.data.requestID
|
|
|
|
});
|
2014-06-09 10:34:00 +04:00
|
|
|
}).catch(function(error) {
|
|
|
|
returnMessage("Notification:Delete:Return:KO", {
|
|
|
|
requestID: message.data.requestID,
|
|
|
|
errorMsg: error
|
|
|
|
});
|
2013-10-03 05:27:53 +04:00
|
|
|
});
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
if (DEBUG) { debug("Invalid message name" + message.name); }
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// We need to make sure any read/write operations are atomic,
|
|
|
|
// so use a queue to run each operation sequentially.
|
2014-06-09 10:34:00 +04:00
|
|
|
queueTask: function(operation, data) {
|
2013-10-03 05:27:53 +04:00
|
|
|
if (DEBUG) { debug("Queueing task: " + operation); }
|
2014-06-09 10:34:00 +04:00
|
|
|
|
|
|
|
var defer = {};
|
|
|
|
|
2013-10-03 05:27:53 +04:00
|
|
|
this.tasks.push({
|
|
|
|
operation: operation,
|
|
|
|
data: data,
|
2014-06-09 10:34:00 +04:00
|
|
|
defer: defer
|
|
|
|
});
|
|
|
|
|
|
|
|
var promise = new Promise(function(resolve, reject) {
|
|
|
|
defer.resolve = resolve;
|
|
|
|
defer.reject = reject;
|
2013-10-03 05:27:53 +04:00
|
|
|
});
|
|
|
|
|
|
|
|
// Only run immediately if we aren't currently running another task.
|
|
|
|
if (!this.runningTask) {
|
2014-05-09 01:16:00 +04:00
|
|
|
if (DEBUG) { debug("Task queue was not running, starting now..."); }
|
2013-10-03 05:27:53 +04:00
|
|
|
this.runNextTask();
|
|
|
|
}
|
2014-06-09 10:34:00 +04:00
|
|
|
|
|
|
|
return promise;
|
2013-10-03 05:27:53 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
runNextTask: function() {
|
|
|
|
if (this.tasks.length === 0) {
|
2014-05-09 01:16:00 +04:00
|
|
|
if (DEBUG) { debug("No more tasks to run, queue depleted"); }
|
2014-06-09 10:34:00 +04:00
|
|
|
this.runningTask = null;
|
2013-10-03 05:27:53 +04:00
|
|
|
return;
|
|
|
|
}
|
2014-06-09 10:34:00 +04:00
|
|
|
this.runningTask = this.tasks.shift();
|
2013-10-03 05:27:53 +04:00
|
|
|
|
|
|
|
// Always make sure we are loaded before performing any read/write tasks.
|
2014-06-09 10:34:00 +04:00
|
|
|
this.ensureLoaded()
|
2017-04-27 01:25:45 +03:00
|
|
|
.then(() => {
|
2014-06-09 10:34:00 +04:00
|
|
|
var task = this.runningTask;
|
2013-10-03 05:27:53 +04:00
|
|
|
|
|
|
|
switch (task.operation) {
|
|
|
|
case "getall":
|
2014-06-09 10:34:00 +04:00
|
|
|
return this.taskGetAll(task.data);
|
2013-10-03 05:27:53 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case "save":
|
2014-06-09 10:34:00 +04:00
|
|
|
return this.taskSave(task.data);
|
2013-10-03 05:27:53 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case "delete":
|
2014-06-09 10:34:00 +04:00
|
|
|
return this.taskDelete(task.data);
|
2013-10-03 05:27:53 +04:00
|
|
|
break;
|
|
|
|
}
|
2014-06-09 10:34:00 +04:00
|
|
|
|
2017-04-27 01:25:45 +03:00
|
|
|
})
|
|
|
|
.then(payload => {
|
2014-06-09 10:34:00 +04:00
|
|
|
if (DEBUG) {
|
|
|
|
debug("Finishing task: " + this.runningTask.operation);
|
|
|
|
}
|
|
|
|
this.runningTask.defer.resolve(payload);
|
2017-04-27 01:25:45 +03:00
|
|
|
})
|
|
|
|
.catch(err => {
|
2014-06-09 10:34:00 +04:00
|
|
|
if (DEBUG) {
|
|
|
|
debug("Error while running " + this.runningTask.operation + ": " + err);
|
|
|
|
}
|
|
|
|
this.runningTask.defer.reject(new String(err));
|
2017-04-27 01:25:45 +03:00
|
|
|
})
|
|
|
|
.then(() => {
|
2014-06-09 10:34:00 +04:00
|
|
|
this.runNextTask();
|
2017-04-27 01:25:45 +03:00
|
|
|
});
|
2013-10-03 05:27:53 +04:00
|
|
|
},
|
|
|
|
|
2014-06-09 10:34:00 +04:00
|
|
|
taskGetAll: function(data) {
|
2013-10-03 05:27:53 +04:00
|
|
|
if (DEBUG) { debug("Task, getting all"); }
|
|
|
|
var origin = data.origin;
|
|
|
|
var notifications = [];
|
|
|
|
// Grab only the notifications for specified origin.
|
2014-09-23 16:02:00 +04:00
|
|
|
if (this.notifications[origin]) {
|
|
|
|
for (var i in this.notifications[origin]) {
|
|
|
|
notifications.push(this.notifications[origin][i]);
|
|
|
|
}
|
2013-10-03 05:27:53 +04:00
|
|
|
}
|
2014-06-09 10:34:00 +04:00
|
|
|
return Promise.resolve(notifications);
|
2013-10-03 05:27:53 +04:00
|
|
|
},
|
|
|
|
|
2014-06-09 10:34:00 +04:00
|
|
|
taskSave: function(data) {
|
2013-10-03 05:27:53 +04:00
|
|
|
if (DEBUG) { debug("Task, saving"); }
|
|
|
|
var origin = data.origin;
|
|
|
|
var notification = data.notification;
|
|
|
|
if (!this.notifications[origin]) {
|
|
|
|
this.notifications[origin] = {};
|
|
|
|
this.byTag[origin] = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
// We might have existing notification with this tag,
|
|
|
|
// if so we need to remove it before saving the new one.
|
2014-08-12 18:15:00 +04:00
|
|
|
if (notification.tag) {
|
2013-10-03 05:27:53 +04:00
|
|
|
var oldNotification = this.byTag[origin][notification.tag];
|
2014-08-12 18:15:00 +04:00
|
|
|
if (oldNotification) {
|
|
|
|
delete this.notifications[origin][oldNotification.id];
|
|
|
|
}
|
2013-10-03 05:27:53 +04:00
|
|
|
this.byTag[origin][notification.tag] = notification;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.notifications[origin][notification.id] = notification;
|
2014-06-09 10:34:00 +04:00
|
|
|
return this.save();
|
2013-10-03 05:27:53 +04:00
|
|
|
},
|
|
|
|
|
2014-06-09 10:34:00 +04:00
|
|
|
taskDelete: function(data) {
|
2013-10-03 05:27:53 +04:00
|
|
|
if (DEBUG) { debug("Task, deleting"); }
|
|
|
|
var origin = data.origin;
|
|
|
|
var id = data.id;
|
|
|
|
if (!this.notifications[origin]) {
|
|
|
|
if (DEBUG) { debug("No notifications found for origin: " + origin); }
|
2014-06-09 10:34:00 +04:00
|
|
|
return Promise.resolve();
|
2013-10-03 05:27:53 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure we can find the notification to delete.
|
|
|
|
var oldNotification = this.notifications[origin][id];
|
|
|
|
if (!oldNotification) {
|
|
|
|
if (DEBUG) { debug("No notification found with id: " + id); }
|
2014-06-09 10:34:00 +04:00
|
|
|
return Promise.resolve();
|
2013-10-03 05:27:53 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (oldNotification.tag) {
|
|
|
|
delete this.byTag[origin][oldNotification.tag];
|
|
|
|
}
|
|
|
|
delete this.notifications[origin][id];
|
2014-06-09 10:34:00 +04:00
|
|
|
return this.save();
|
2013-10-03 05:27:53 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
NotificationDB.init();
|