-
Notifications
You must be signed in to change notification settings - Fork 261
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Additional metrics for breakage testing (#2918)
* Extract messaging into a component. * Pipe popup interactions to a/b metrics * Implement refresh metrics * Move all metrics to the same file * Replace PixelMetric for broken site reports with dashboard use version. * Define a set of metrics for breakage experiments (https://app.asana.com/0/1163321984198618/1208737552969233/f) * Revert config URL * Delay TDS override check to guarantee that config has been processed. * Lint fix * Fix cyclic dependency. We need to keep the popup-messaging file to handle static access to the messaging port. * Fix merge * Docs * RefreshMetric tests and fixes
- Loading branch information
1 parent
b98103c
commit 7c9aa12
Showing
14 changed files
with
449 additions
and
195 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import browser from 'webextension-polyfill'; | ||
|
||
import messageHandlers from '../message-handlers'; | ||
import { getExtensionId } from '../wrapper'; | ||
import { getBrowserName } from '../utils'; | ||
import { getActivePort, setActivePort } from '../popup-messaging'; | ||
|
||
/** | ||
* @typedef {import('webextension-polyfill').Runtime.Port} Port | ||
* @typedef {import('@duckduckgo/privacy-dashboard/schema/__generated__/schema.types').IncomingExtensionMessage} OutgoingPopupMessage | ||
*/ | ||
|
||
export class MessageReceivedEvent extends CustomEvent { | ||
constructor(msg) { | ||
super('messageReceived', { detail: msg }); | ||
} | ||
|
||
get messageType() { | ||
return this.detail.messageType; | ||
} | ||
} | ||
|
||
export default class MessageRouter extends EventTarget { | ||
constructor({ tabManager }) { | ||
super(); | ||
const browserName = getBrowserName(); | ||
// Handle popup UI (aka privacy dashboard) messaging. | ||
browser.runtime.onConnect.addListener((port) => { | ||
if (port.name === 'privacy-dashboard') { | ||
this.popupConnectionOpened(port); | ||
} | ||
}); | ||
|
||
// Handle any messages that come from content/UI scripts | ||
browser.runtime.onMessage.addListener((req, sender) => { | ||
if (sender.id !== getExtensionId()) return; | ||
|
||
// TODO clean up message passing | ||
const legacyMessageTypes = [ | ||
'addUserData', | ||
'getUserData', | ||
'removeUserData', | ||
'getEmailProtectionCapabilities', | ||
'getAddresses', | ||
'refreshAlias', | ||
'debuggerMessage', | ||
]; | ||
for (const legacyMessageType of legacyMessageTypes) { | ||
if (legacyMessageType in req) { | ||
req.messageType = legacyMessageType; | ||
req.options = req[legacyMessageType]; | ||
} | ||
} | ||
|
||
if (req.registeredTempAutofillContentScript) { | ||
req.messageType = 'registeredContentScript'; | ||
} | ||
|
||
if (req.messageType && req.messageType in messageHandlers) { | ||
this.dispatchEvent(new MessageReceivedEvent(req)); | ||
return Promise.resolve(messageHandlers[req.messageType](req.options, sender, req)); | ||
} | ||
|
||
// Count refreshes per page | ||
if (req.pageReloaded && sender.tab !== undefined) { | ||
const tab = tabManager.get({ tabId: sender.tab.id }); | ||
if (tab) { | ||
tab.userRefreshCount += 1; | ||
} | ||
return; | ||
} | ||
|
||
// TODO clean up legacy onboarding messaging | ||
if (browserName === 'chrome') { | ||
if (req === 'healthCheckRequest' || req === 'rescheduleCounterMessagingRequest') { | ||
return; | ||
} | ||
} | ||
|
||
console.error('Unrecognized message to background:', req, sender); | ||
}); | ||
} | ||
|
||
/** | ||
* Set up the messaging connection with the popup UI. | ||
* | ||
* Note: If the ServiceWorker dies while there is an open connection, the popup | ||
* will take care of re-opening the connection. | ||
* | ||
* @param {Port} port | ||
*/ | ||
popupConnectionOpened(port) { | ||
setActivePort(port); | ||
port.onDisconnect.addListener(() => { | ||
if (getActivePort() === port) { | ||
setActivePort(null); | ||
} | ||
}); | ||
|
||
port.onMessage.addListener(async (message) => { | ||
const messageType = message?.messageType; | ||
|
||
if (!messageType || !(messageType in messageHandlers)) { | ||
console.error('Unrecognized message (privacy-dashboard -> background):', message); | ||
return; | ||
} | ||
this.dispatchEvent(new MessageReceivedEvent(message)); | ||
|
||
const response = await messageHandlers[messageType](message?.options, port, message); | ||
if (typeof message?.id === 'number') { | ||
port.postMessage({ | ||
id: message.id, | ||
messageType: 'response', | ||
options: response, | ||
}); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.