NotificationStorage.sys.mjs (2339B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 const lazy = {}; 6 7 ChromeUtils.defineLazyGetter(lazy, "console", () => { 8 return console.createInstance({ 9 prefix: "NotificationStorage", 10 maxLogLevelPref: "dom.webnotifications.loglevel", 11 }); 12 }); 13 14 /** @import {NotificationDB} from "./NotificationDB.sys.mjs" */ 15 16 export class NotificationStorage { 17 /** @type {NotificationDB} */ 18 db = ChromeUtils.importESModule( 19 "moz-src:///dom/notification/NotificationDB.sys.mjs" 20 ).db; 21 22 async put(aOrigin, aEntry, aScope) { 23 lazy.console.debug(`PUT: ${aOrigin} ${aEntry.id}: ${aEntry.title}`); 24 let notification = { 25 ...aEntry, 26 27 // Storing QueryInterface confuses XPCOM to think it's passing an XPCOM object 28 actions: aEntry.actions.map(rawAction => { 29 let actionEntry = { ...rawAction }; 30 delete actionEntry.QueryInterface; 31 return actionEntry; 32 }), 33 timestamp: new Date().getTime(), 34 serviceWorkerRegistrationScope: aScope, 35 }; 36 delete notification.QueryInterface; 37 38 await this.db.queueTask("save", { 39 origin: aOrigin, 40 notification, 41 }); 42 } 43 44 async get(origin, scope, tag, callback) { 45 lazy.console.debug(`GET: ${origin} ${tag}`); 46 let notifications = await this.db.queueTask("getall", { 47 origin, 48 scope, 49 tag, 50 }); 51 callback.done(notifications); 52 } 53 54 /** 55 * @param {string} origin The site origin 56 * @param {string} id The notification ID 57 */ 58 async getById(origin, id) { 59 lazy.console.debug(`GETBYID: ${origin} ${id}`); 60 61 return await this.db.queueTask("get", { origin, id }); 62 } 63 64 async delete(origin, id) { 65 lazy.console.debug(`DELETE: ${id}`); 66 await this.db.queueTask("delete", { 67 origin, 68 id, 69 }); 70 } 71 72 async deleteAllExcept(ids) { 73 lazy.console.debug(`DELETEALLEXCEPT: ${ids}`); 74 await this.db.queueTask("deleteAllExcept", { ids }); 75 } 76 77 QueryInterface = ChromeUtils.generateQI(["nsINotificationStorage"]); 78 } 79 80 export class MemoryNotificationStorage extends NotificationStorage { 81 db = new (ChromeUtils.importESModule( 82 "moz-src:///dom/notification/MemoryNotificationDB.sys.mjs" 83 ).MemoryNotificationDB)(); 84 }