diff options
Diffstat (limited to 'dom/notification')
-rw-r--r-- | dom/notification/NotificationStorage.sys.mjs | 121 | ||||
-rw-r--r-- | dom/notification/old/NotificationDB.sys.mjs | 84 |
2 files changed, 82 insertions, 123 deletions
diff --git a/dom/notification/NotificationStorage.sys.mjs b/dom/notification/NotificationStorage.sys.mjs index 46c9e2485c..e33277b598 100644 --- a/dom/notification/NotificationStorage.sys.mjs +++ b/dom/notification/NotificationStorage.sys.mjs @@ -2,10 +2,14 @@ * 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/. */ -const DEBUG = false; -function debug(s) { - dump("-*- NotificationStorage.js: " + s + "\n"); -} +const lazy = {}; + +ChromeUtils.defineLazyGetter(lazy, "console", () => { + return console.createInstance({ + prefix: "NotificationStorage", + maxLogLevelPref: "dom.webnotifications.loglevel", + }); +}); const kMessageNotificationGetAllOk = "Notification:GetAll:Return:OK"; const kMessageNotificationGetAllKo = "Notification:GetAll:Return:KO"; @@ -19,38 +23,36 @@ const kMessages = [ kMessageNotificationDeleteKo, ]; -export function NotificationStorage() { - this._requests = {}; - this._requestCount = 0; +export class NotificationStorage { + #requests = {}; + #requestCount = 0; - Services.obs.addObserver(this, "xpcom-shutdown"); + constructor() { + Services.obs.addObserver(this, "xpcom-shutdown"); - // Register for message listeners. - this.registerListeners(); -} + // Register for message listeners. + this.registerListeners(); + } -NotificationStorage.prototype = { registerListeners() { for (let message of kMessages) { Services.cpmm.addMessageListener(message, this); } - }, + } unregisterListeners() { for (let message of kMessages) { Services.cpmm.removeMessageListener(message, this); } - }, + } observe(aSubject, aTopic) { - if (DEBUG) { - debug("Topic: " + aTopic); - } + lazy.console.debug(`Topic: ${aTopic}`); if (aTopic === "xpcom-shutdown") { Services.obs.removeObserver(this, "xpcom-shutdown"); this.unregisterListeners(); } - }, + } put( origin, @@ -66,9 +68,7 @@ NotificationStorage.prototype = { behavior, serviceWorkerRegistrationScope ) { - if (DEBUG) { - debug("PUT: " + origin + " " + id + ": " + title); - } + lazy.console.debug(`PUT: ${origin} ${id}: ${title}`); var notification = { id, title, @@ -89,83 +89,74 @@ NotificationStorage.prototype = { origin, notification, }); - }, + } get(origin, tag, callback) { - if (DEBUG) { - debug("GET: " + origin + " " + tag); - } - this._fetchFromDB(origin, tag, callback); - }, + lazy.console.debug(`GET: ${origin} ${tag}`); + this.#fetchFromDB(origin, tag, callback); + } delete(origin, id) { - if (DEBUG) { - debug("DELETE: " + id); - } + lazy.console.debug(`DELETE: ${id}`); Services.cpmm.sendAsyncMessage("Notification:Delete", { origin, id, }); - }, + } receiveMessage(message) { - var request = this._requests[message.data.requestID]; + var request = this.#requests[message.data.requestID]; switch (message.name) { case kMessageNotificationGetAllOk: - delete this._requests[message.data.requestID]; - this._returnNotifications( - message.data.notifications, - request.origin, - request.tag, - request.callback - ); + delete this.#requests[message.data.requestID]; + this.#returnNotifications(message.data.notifications, request.callback); break; case kMessageNotificationGetAllKo: - delete this._requests[message.data.requestID]; + delete this.#requests[message.data.requestID]; try { request.callback.done(); } catch (e) { - debug("Error calling callback done: " + e); + lazy.console.debug(`Error calling callback done: ${e}`); } break; case kMessageNotificationSaveKo: case kMessageNotificationDeleteKo: - if (DEBUG) { - debug( - "Error received when treating: '" + - message.name + - "': " + - message.data.errorMsg - ); - } + lazy.console.debug( + `Error received when treating: '${message.name}': ${message.data.errorMsg}` + ); break; default: - if (DEBUG) { - debug("Unrecognized message: " + message.name); - } + lazy.console.debug(`Unrecognized message: ${message.name}`); break; } - }, + } + + #getUniqueRequestID() { + // This assumes the count will never go above MAX_SAFE_INTEGER, as + // notifications are not supposed to happen that frequently. + this.#requestCount += 1; + return this.#requestCount; + } - _fetchFromDB(origin, tag, callback) { + #fetchFromDB(origin, tag, callback) { var request = { origin, tag, callback, }; - var requestID = this._requestCount++; - this._requests[requestID] = request; + var requestID = this.#getUniqueRequestID(); + this.#requests[requestID] = request; Services.cpmm.sendAsyncMessage("Notification:GetAll", { origin, tag, requestID, }); - }, + } - _returnNotifications(notifications, origin, tag, callback) { + #returnNotifications(notifications, callback) { // Pass each notification back separately. // The callback is called asynchronously to match the behaviour when // fetching from the database. @@ -187,19 +178,15 @@ NotificationStorage.prototype = { ) ); } catch (e) { - if (DEBUG) { - debug("Error calling callback handle: " + e); - } + lazy.console.debug(`Error calling callback handle: ${e}`); } }); try { Services.tm.dispatchToMainThread(callback.done); } catch (e) { - if (DEBUG) { - debug("Error calling callback done: " + e); - } + lazy.console.debug(`Error calling callback done: ${e}`); } - }, + } - QueryInterface: ChromeUtils.generateQI(["nsINotificationStorage"]), -}; + QueryInterface = ChromeUtils.generateQI(["nsINotificationStorage"]); +} diff --git a/dom/notification/old/NotificationDB.sys.mjs b/dom/notification/old/NotificationDB.sys.mjs index 7de9734450..8cd2bff8f6 100644 --- a/dom/notification/old/NotificationDB.sys.mjs +++ b/dom/notification/old/NotificationDB.sys.mjs @@ -2,17 +2,19 @@ * 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/. */ -const DEBUG = false; -function debug(s) { - dump("-*- NotificationDB component: " + s + "\n"); -} - const lazy = {}; ChromeUtils.defineESModuleGetters(lazy, { AsyncShutdown: "resource://gre/modules/AsyncShutdown.sys.mjs", }); +ChromeUtils.defineLazyGetter(lazy, "console", () => { + return console.createInstance({ + prefix: "NotificationDB", + maxLogLevelPref: "dom.webnotifications.loglevel", + }); +}); + const NOTIFICATION_STORE_DIR = PathUtils.profileDir; const NOTIFICATION_STORE_PATH = PathUtils.join( NOTIFICATION_STORE_DIR, @@ -70,9 +72,7 @@ var NotificationDB = { }, observe(aSubject, aTopic) { - if (DEBUG) { - debug("Topic: " + aTopic); - } + lazy.console.debug(`Topic: ${aTopic}`); if (aTopic == "xpcom-shutdown") { this._shutdownInProgress = true; Services.obs.removeObserver(this, "xpcom-shutdown"); @@ -92,11 +92,9 @@ var NotificationDB = { } } if (persistentNotificationCount == 0) { - if (DEBUG) { - debug( - "Origin " + origin + " is not linked to an app manifest, deleting." - ); - } + lazy.console.debug( + `Origin ${origin} is not linked to an app manifest, deleting.` + ); delete result[origin]; } } @@ -171,9 +169,7 @@ var NotificationDB = { }, receiveMessage(message) { - if (DEBUG) { - debug("Received message:" + message.name); - } + lazy.console.debug(`Received message: ${message.name}`); // sendAsyncMessage can fail if the child process exits during a // notification storage operation, so always wrap it in a try/catch. @@ -181,9 +177,7 @@ var NotificationDB = { try { message.target.sendAsyncMessage(name, data); } catch (e) { - if (DEBUG) { - debug("Return message failed, " + name); - } + lazy.console.debug(`Return message failed, ${name}`); } } @@ -237,18 +231,14 @@ var NotificationDB = { break; default: - if (DEBUG) { - debug("Invalid message name" + message.name); - } + lazy.console.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. queueTask(operation, data) { - if (DEBUG) { - debug("Queueing task: " + operation); - } + lazy.console.debug(`Queueing task: ${operation}`); var defer = {}; @@ -265,9 +255,7 @@ var NotificationDB = { // Only run immediately if we aren't currently running another task. if (!this.runningTask) { - if (DEBUG) { - debug("Task queue was not running, starting now..."); - } + lazy.console.debug("Task queue was not running, starting now..."); this.runNextTask(); this._queueDrainedPromise = new Promise(resolve => { this._queueDrainedPromiseResolve = resolve; @@ -279,14 +267,12 @@ var NotificationDB = { runNextTask() { if (this.tasks.length === 0) { - if (DEBUG) { - debug("No more tasks to run, queue depleted"); - } + lazy.console.debug("No more tasks to run, queue depleted"); this.runningTask = null; if (this._queueDrainedPromiseResolve) { this._queueDrainedPromiseResolve(); - } else if (DEBUG) { - debug( + } else { + lazy.console.debug( "_queueDrainedPromiseResolve was null somehow, no promise to resolve" ); } @@ -316,17 +302,13 @@ var NotificationDB = { } }) .then(payload => { - if (DEBUG) { - debug("Finishing task: " + this.runningTask.operation); - } + lazy.console.debug(`Finishing task: ${this.runningTask.operation}`); this.runningTask.defer.resolve(payload); }) .catch(err => { - if (DEBUG) { - debug( - "Error while running " + this.runningTask.operation + ": " + err - ); - } + lazy.console.debug( + `Error while running ${this.runningTask.operation}: ${err}` + ); this.runningTask.defer.reject(err); }) .then(() => { @@ -335,9 +317,7 @@ var NotificationDB = { }, taskGetAll(data) { - if (DEBUG) { - debug("Task, getting all"); - } + lazy.console.debug("Task, getting all"); var origin = data.origin; var notifications = []; // Grab only the notifications for specified origin. @@ -357,9 +337,7 @@ var NotificationDB = { }, taskSave(data) { - if (DEBUG) { - debug("Task, saving"); - } + lazy.console.debug("Task, saving"); var origin = data.origin; var notification = data.notification; if (!this.notifications[origin]) { @@ -382,24 +360,18 @@ var NotificationDB = { }, taskDelete(data) { - if (DEBUG) { - debug("Task, deleting"); - } + lazy.console.debug("Task, deleting"); var origin = data.origin; var id = data.id; if (!this.notifications[origin]) { - if (DEBUG) { - debug("No notifications found for origin: " + origin); - } + lazy.console.debug(`No notifications found for origin: ${origin}`); return Promise.resolve(); } // 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); - } + lazy.console.debug(`No notification found with id: ${id}`); return Promise.resolve(); } |