ASRouterParentProcessMessageHandler.sys.mjs (5467B)
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 { ASRouterPreferences } from "resource:///modules/asrouter/ASRouterPreferences.sys.mjs"; 7 import { MESSAGE_TYPE_HASH as msg } from "resource:///modules/asrouter/ActorConstants.mjs"; 8 9 export class ASRouterParentProcessMessageHandler { 10 constructor({ 11 router, 12 preferences, 13 specialMessageActions, 14 queryCache, 15 sendTelemetry, 16 }) { 17 this._router = router; 18 this._preferences = preferences; 19 this._specialMessageActions = specialMessageActions; 20 this._queryCache = queryCache; 21 this.handleTelemetry = sendTelemetry; 22 this.handleMessage = this.handleMessage.bind(this); 23 this.handleCFRAction = this.handleCFRAction.bind(this); 24 } 25 26 handleCFRAction({ type, data }, browser) { 27 switch (type) { 28 case msg.INFOBAR_TELEMETRY: 29 case msg.TOOLBAR_BADGE_TELEMETRY: 30 case msg.MOMENTS_PAGE_TELEMETRY: 31 case msg.DOORHANGER_TELEMETRY: 32 case msg.SPOTLIGHT_TELEMETRY: 33 case msg.MENU_MESSAGE_TELEMETRY: 34 case msg.NEWTAB_MESSAGE_TELEMETRY: 35 case msg.TOAST_NOTIFICATION_TELEMETRY: { 36 return this.handleTelemetry({ type, data }); 37 } 38 default: { 39 return this.handleMessage(type, data, { browser }); 40 } 41 } 42 } 43 44 handleMessage(name, data, { browser } = { browser: null }) { 45 switch (name) { 46 case msg.AS_ROUTER_TELEMETRY_USER_EVENT: 47 return this.handleTelemetry({ 48 type: msg.AS_ROUTER_TELEMETRY_USER_EVENT, 49 data, 50 }); 51 case msg.BLOCK_MESSAGE_BY_ID: { 52 ASRouterPreferences.console.debug( 53 "handleMesssage(): about to block, data = ", 54 data 55 ); 56 ASRouterPreferences.console.trace(); 57 58 // Block the message but don't dismiss it in case the action taken has 59 // another state that needs to be visible 60 return this._router 61 .blockMessageById(data.id) 62 .then(() => !data.preventDismiss); 63 } 64 case msg.USER_ACTION: { 65 return this._specialMessageActions.handleAction(data, browser); 66 } 67 case msg.IMPRESSION: { 68 return this._router.addImpression(data); 69 } 70 case msg.TRIGGER: { 71 return this._router.sendTriggerMessage({ 72 ...(data && data.trigger), 73 browser, 74 }); 75 } 76 case msg.PBNEWTAB_MESSAGE_REQUEST: { 77 return this._router.sendPBNewTabMessage({ 78 ...data, 79 browser, 80 }); 81 } 82 83 // ADMIN Messages 84 case msg.ADMIN_CONNECT_STATE: { 85 if (data && data.endpoint) { 86 return this._router.loadMessagesFromAllProviders(); 87 } 88 return this._router.updateTargetingParameters(); 89 } 90 case msg.UNBLOCK_MESSAGE_BY_ID: { 91 return this._router.unblockMessageById(data.id); 92 } 93 case msg.UNBLOCK_ALL: { 94 return this._router.unblockAll(); 95 } 96 case msg.BLOCK_BUNDLE: { 97 return this._router.blockMessageById(data.bundle.map(b => b.id)); 98 } 99 case msg.UNBLOCK_BUNDLE: { 100 return this._router.setState(state => { 101 const messageBlockList = [...state.messageBlockList]; 102 for (let message of data.bundle) { 103 messageBlockList.splice(messageBlockList.indexOf(message.id), 1); 104 } 105 this._router._storage.set("messageBlockList", messageBlockList); 106 return { messageBlockList }; 107 }); 108 } 109 case msg.DISABLE_PROVIDER: { 110 this._preferences.enableOrDisableProvider(data, false); 111 return Promise.resolve(); 112 } 113 case msg.ENABLE_PROVIDER: { 114 this._preferences.enableOrDisableProvider(data, true); 115 return Promise.resolve(); 116 } 117 case msg.EVALUATE_JEXL_EXPRESSION: { 118 return this._router.evaluateExpression(data); 119 } 120 case msg.EXPIRE_QUERY_CACHE: { 121 this._queryCache.expireAll(); 122 return Promise.resolve(); 123 } 124 case msg.FORCE_ATTRIBUTION: { 125 return this._router.forceAttribution(data); 126 } 127 case msg.FORCE_PRIVATE_BROWSING_WINDOW: { 128 return this._router.forcePBWindow(browser, data.message); 129 } 130 case msg.MODIFY_MESSAGE_JSON: { 131 return this._router.routeCFRMessage(data.content, browser, data, true); 132 } 133 case msg.OVERRIDE_MESSAGE: { 134 return this._router.setMessageById(data, true, browser); 135 } 136 case msg.RESET_PROVIDER_PREF: { 137 this._preferences.resetProviderPref(); 138 return Promise.resolve(); 139 } 140 case msg.SET_PROVIDER_USER_PREF: { 141 this._preferences.setUserPreference(data.id, data.value); 142 return Promise.resolve(); 143 } 144 case msg.RESET_GROUPS_STATE: { 145 return this._router 146 .resetGroupsState(data) 147 .then(() => this._router.loadMessagesFromAllProviders()); 148 } 149 case msg.RESET_MESSAGE_STATE: { 150 return this._router.resetMessageState(); 151 } 152 case msg.RESET_SCREEN_IMPRESSIONS: { 153 return this._router.resetScreenImpressions(); 154 } 155 case msg.EDIT_STATE: { 156 const [[key, value]] = Object.entries(data); 157 return this._router.editState(key, value); 158 } 159 default: { 160 return Promise.reject(new Error(`Unknown message received: ${name}`)); 161 } 162 } 163 } 164 }