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");
|
|
|
|
}
|
|
|
|
|
2018-01-30 02:20:18 +03:00
|
|
|
ChromeUtils.defineModuleGetter(
|
|
|
|
this,
|
|
|
|
"Services",
|
|
|
|
"resource://gre/modules/Services.jsm"
|
|
|
|
);
|
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,
|
2019-02-16 17:15:39 +03:00
|
|
|
kMessageNotificationDeleteKo,
|
2014-06-09 10:34:00 +04:00
|
|
|
];
|
2013-10-03 05:27:53 +04:00
|
|
|
|
|
|
|
function NotificationStorage() {
|
|
|
|
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 = {
|
2019-02-16 17:15:39 +03:00
|
|
|
registerListeners() {
|
2014-06-09 10:34:00 +04:00
|
|
|
for (let message of kMessages) {
|
2018-03-01 22:19:56 +03:00
|
|
|
Services.cpmm.addMessageListener(message, this);
|
2014-06-09 10:34:00 +04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-02-16 17:15:39 +03:00
|
|
|
unregisterListeners() {
|
2014-06-09 10:34:00 +04:00
|
|
|
for (let message of kMessages) {
|
2018-03-01 22:19:56 +03:00
|
|
|
Services.cpmm.removeMessageListener(message, this);
|
2014-06-09 10:34:00 +04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-02-16 17:15:39 +03:00
|
|
|
observe(aSubject, aTopic, aData) {
|
2014-06-09 10:34:00 +04:00
|
|
|
if (DEBUG) {
|
|
|
|
debug("Topic: " + aTopic);
|
2019-07-05 11:44:55 +03:00
|
|
|
}
|
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();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-02-16 17:15:39 +03:00
|
|
|
put(
|
|
|
|
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 = {
|
2019-02-16 17:15:39 +03:00
|
|
|
id,
|
|
|
|
title,
|
|
|
|
dir,
|
|
|
|
lang,
|
|
|
|
body,
|
|
|
|
tag,
|
|
|
|
icon,
|
|
|
|
alertName,
|
2014-04-24 18:14:09 +04:00
|
|
|
timestamp: new Date().getTime(),
|
2019-02-16 17:15:39 +03:00
|
|
|
origin,
|
|
|
|
data,
|
2015-06-26 04:50:25 +03:00
|
|
|
mozbehavior: behavior,
|
2019-02-16 17:15:39 +03:00
|
|
|
serviceWorkerRegistrationScope,
|
2013-10-03 05:27:53 +04:00
|
|
|
};
|
|
|
|
|
2018-12-18 01:18:59 +03:00
|
|
|
Services.cpmm.sendAsyncMessage("Notification:Save", {
|
2019-02-16 17:15:39 +03:00
|
|
|
origin,
|
|
|
|
notification,
|
2018-12-18 01:18:59 +03:00
|
|
|
});
|
2013-10-03 05:27:53 +04:00
|
|
|
},
|
|
|
|
|
2019-02-16 17:15:39 +03:00
|
|
|
get(origin, tag, callback) {
|
2014-04-24 18:14:09 +04:00
|
|
|
if (DEBUG) {
|
|
|
|
debug("GET: " + origin + " " + tag);
|
|
|
|
}
|
2018-12-18 01:18:59 +03:00
|
|
|
this._fetchFromDB(origin, tag, callback);
|
2013-10-03 05:27:53 +04:00
|
|
|
},
|
|
|
|
|
2019-02-16 17:15:39 +03:00
|
|
|
getByID(origin, id, callback) {
|
2015-06-26 04:50:25 +03:00
|
|
|
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));
|
|
|
|
},
|
|
|
|
|
2019-02-16 17:15:39 +03:00
|
|
|
delete(origin, id) {
|
2013-10-03 05:27:53 +04:00
|
|
|
if (DEBUG) {
|
|
|
|
debug("DELETE: " + id);
|
|
|
|
}
|
2018-03-01 22:19:56 +03:00
|
|
|
Services.cpmm.sendAsyncMessage("Notification:Delete", {
|
2019-02-16 17:15:39 +03:00
|
|
|
origin,
|
|
|
|
id,
|
2013-10-03 05:27:53 +04:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2019-02-16 17:15:39 +03:00
|
|
|
receiveMessage(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];
|
2018-12-18 01:18:59 +03:00
|
|
|
this._returnNotifications(
|
|
|
|
message.data.notifications,
|
|
|
|
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);
|
2019-07-05 11:44:55 +03:00
|
|
|
}
|
2013-10-03 05:27:53 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-02-16 17:15:39 +03:00
|
|
|
_fetchFromDB(origin, tag, callback) {
|
2013-10-03 05:27:53 +04:00
|
|
|
var request = {
|
2018-12-18 01:18:59 +03:00
|
|
|
origin,
|
|
|
|
tag,
|
|
|
|
callback,
|
2013-10-03 05:27:53 +04:00
|
|
|
};
|
|
|
|
var requestID = this._requestCount++;
|
|
|
|
this._requests[requestID] = request;
|
2018-03-01 22:19:56 +03:00
|
|
|
Services.cpmm.sendAsyncMessage("Notification:GetAll", {
|
2018-12-18 01:18:59 +03:00
|
|
|
origin,
|
|
|
|
tag,
|
|
|
|
requestID,
|
2013-10-03 05:27:53 +04:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2019-02-16 17:15:39 +03:00
|
|
|
_returnNotifications(notifications, origin, tag, callback) {
|
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
|
2019-07-05 11:44:55 +03:00
|
|
|
)
|
2017-04-14 19:29:12 +03:00
|
|
|
);
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-04-23 06:55:06 +03:00
|
|
|
QueryInterface: ChromeUtils.generateQI([Ci.nsINotificationStorage]),
|
2013-10-03 05:27:53 +04:00
|
|
|
};
|
|
|
|
|
2019-01-30 22:27:51 +03:00
|
|
|
var EXPORTED_SYMBOLS = ["NotificationStorage"];
|