-
Notifications
You must be signed in to change notification settings - Fork 248
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update privacy-dashboard dependency to 6.0.0 (#2726)
With this update, the way privacy-dashboard communicates with the extension has changed. Instead of sending messages using the chrome.runtime.sendMessage API, the chrome.runtime.connect API is used instead. In the future, that will allow us to listen for the disconnect event as a way to know when the popup UI has been closed by the browser (e.g. the user clicked away).
- Loading branch information
Showing
9 changed files
with
81 additions
and
23 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,58 @@ | ||
/** | ||
* @typedef {import('webextension-polyfill').Runtime.Port} Port | ||
* @typedef {import('@duckduckgo/privacy-dashboard/schema/__generated__/schema.types').IncomingExtensionMessage} OutgoingPopupMessage | ||
*/ | ||
|
||
// Messaging connection with the popup UI (when active). | ||
/** @type {Port?} */ | ||
let activePort = null | ||
|
||
/** | ||
* 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 | ||
* @param {Record< | ||
* string, | ||
* (options: any, sender: any, message: any) => any | ||
* >} messageHandlers | ||
*/ | ||
export function popupConnectionOpened (port, messageHandlers) { | ||
activePort = port | ||
port.onDisconnect.addListener(() => { | ||
if (activePort === port) { | ||
activePort = null | ||
} | ||
}) | ||
|
||
port.onMessage.addListener(async message => { | ||
const messageType = message?.messageType | ||
|
||
if (!messageType || !(messageType in messageHandlers)) { | ||
console.error( | ||
'Unrecognized message (privacy-dashboard -> background):', message | ||
) | ||
return | ||
} | ||
|
||
const response = await messageHandlers[messageType](message?.options, port, message) | ||
if (typeof message?.id === 'number') { | ||
port.postMessage({ | ||
id: message.id, | ||
messageType: 'response', | ||
options: response | ||
}) | ||
} | ||
}) | ||
} | ||
|
||
/** | ||
* Post a message to the popup UI, if it's open. | ||
* | ||
* @param {OutgoingPopupMessage} message | ||
*/ | ||
export function postPopupMessage (message) { | ||
activePort?.postMessage(message) | ||
} |
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