ASRouterDefaultConfig.sys.mjs (2734B)
1 /* vim: set ts=2 sw=2 sts=2 et tw=80: */ 2 /* This Source Code Form is subject to the terms of the Mozilla Public 3 * License, v. 2.0. If a copy of the MPL was not distributed with this 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 5 6 import { ASRouter } from "resource:///modules/asrouter/ASRouter.sys.mjs"; 7 import { ASRouterParentProcessMessageHandler } from "resource:///modules/asrouter/ASRouterParentProcessMessageHandler.sys.mjs"; 8 import { ASRouterTelemetry } from "resource:///modules/asrouter/ASRouterTelemetry.sys.mjs"; 9 10 // We use importESModule here instead of static import so that 11 // the Karma test environment won't choke on this module. This 12 // is because the Karma test environment does not actually rely 13 // on SpecialMessageActions, and overrides importESModule to be 14 // a no-op (which can't be done for a static import statement). 15 16 // eslint-disable-next-line mozilla/use-static-import 17 const { SpecialMessageActions } = ChromeUtils.importESModule( 18 "resource://messaging-system/lib/SpecialMessageActions.sys.mjs" 19 ); 20 21 import { ASRouterPreferences } from "resource:///modules/asrouter/ASRouterPreferences.sys.mjs"; 22 import { QueryCache } from "resource:///modules/asrouter/ASRouterTargeting.sys.mjs"; 23 import { ASRouterStorage } from "resource:///modules/asrouter/ASRouterStorage.sys.mjs"; 24 25 const createStorage = async telemetryFeed => { 26 // "snippets" is the name of one storage space, but these days it is used 27 // not for snippet-related data (snippets were removed in bug 1715158), 28 // but storage for impression or session data for all ASRouter messages. 29 // 30 // We keep the name "snippets" to avoid having to do an IndexedDB database 31 // migration. 32 const dbStore = new ASRouterStorage({ 33 storeNames: ["snippets"], 34 telemetry: { 35 handleUndesiredEvent: e => telemetryFeed.SendASRouterUndesiredEvent(e), 36 }, 37 }); 38 // Accessing the db causes the object stores to be created / migrated. 39 // This needs to happen before other instances try to access the db, which 40 // would update only a subset of the stores to the latest version. 41 try { 42 await dbStore.db; // eslint-disable-line no-unused-expressions 43 } catch (e) { 44 return Promise.reject(e); 45 } 46 return dbStore.getDbTable("snippets"); 47 }; 48 49 export const ASRouterDefaultConfig = () => { 50 const router = ASRouter; 51 const telemetry = new ASRouterTelemetry(); 52 const messageHandler = new ASRouterParentProcessMessageHandler({ 53 router, 54 preferences: ASRouterPreferences, 55 specialMessageActions: SpecialMessageActions, 56 queryCache: QueryCache, 57 sendTelemetry: telemetry.onAction.bind(telemetry), 58 }); 59 return { 60 router, 61 messageHandler, 62 createStorage: createStorage.bind(null, telemetry), 63 }; 64 };