-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
85d685e
commit 1133bcb
Showing
4 changed files
with
75 additions
and
39 deletions.
There are no files selected for viewing
34 changes: 24 additions & 10 deletions
34
apps/extension/src/content-scripts/injected-connection-port.ts
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 |
---|---|---|
@@ -1,30 +1,44 @@ | ||
import { PraxMessage } from './message-event'; | ||
import { CRSessionClient } from '@penumbra-zone/transport-chrome/session-client'; | ||
import { PraxConnection } from '../message/prax'; | ||
import { PraxMessage } from './message-event'; | ||
|
||
// content script unconditionally announces itself to extension background. | ||
void chrome.runtime.sendMessage(PraxConnection.Init); | ||
// this script will init the page session upon instruction from an extension | ||
// worker. init does not arrive in direct response to an emitted message, it is | ||
// independent. | ||
|
||
// listen for init command from background. this may arrive soon after announce, | ||
// or much later, after a request is made. this activates the channel session | ||
// that transports messages from the DOM channel into the Chrome runtime | ||
// handler to listen for init command for this document | ||
const initOnce = ( | ||
req: unknown, | ||
// in a content script, sender is always an extension background script | ||
_: chrome.runtime.MessageSender, | ||
// content script message handlers are activated only by another | ||
// script in the same extension using chrome.tabs.sendMessage | ||
sender: chrome.runtime.MessageSender, | ||
// this handler will only ever send an empty response | ||
emptyResponse: (no?: never) => void, | ||
) => { | ||
if (req !== PraxConnection.Init) return false; | ||
if (req !== PraxConnection.Init) { | ||
// boolean return in handlers signals intent to respond | ||
return false; | ||
} | ||
|
||
chrome.runtime.onMessage.removeListener(initOnce); | ||
|
||
// create session, post port to window where the injected global can catch it | ||
if (sender.id !== PRAX) { | ||
throw new Error(`Unexpected sender ${sender.id}`); | ||
} | ||
|
||
// create session, post port to window where the injected global can catch it. | ||
const port = CRSessionClient.init(PRAX); | ||
window.postMessage({ [PRAX]: port } satisfies PraxMessage<MessagePort>, '/', [port]); | ||
|
||
// handler is done | ||
emptyResponse(); | ||
|
||
// boolean return in handlers signals intent to respond | ||
return true; | ||
}; | ||
|
||
// attach handler | ||
chrome.runtime.onMessage.addListener(initOnce); | ||
|
||
// announce | ||
void chrome.runtime.sendMessage(PraxConnection.Init); |
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 |
---|---|---|
@@ -1,22 +1,37 @@ | ||
import { alreadyApprovedOrigin } from '../origins/approve-origin'; | ||
import { PraxConnection } from '../message/prax'; | ||
import { alreadyApprovedOrigin } from '../origins/approve-origin'; | ||
import { assertValidSender } from '../origins/valid-sender'; | ||
|
||
// trigger injected-connection-port when a known page inits. | ||
// listen for page init | ||
chrome.runtime.onMessage.addListener( | ||
(req: unknown, sender, emptyResponse: (no?: never) => void) => { | ||
if (req !== PraxConnection.Init) return false; | ||
emptyResponse(); | ||
( | ||
req, | ||
unvalidatedSender, | ||
// this handler will only ever send an empty response | ||
emptyResponse: (no?: never) => void, | ||
) => { | ||
if (req !== PraxConnection.Init) { | ||
// boolean return in handlers signals intent to respond | ||
return false; | ||
} | ||
|
||
const validSender = assertValidSender(unvalidatedSender); | ||
|
||
void (async () => { | ||
const validSender = assertValidSender(sender); | ||
const alreadyApproved = await alreadyApprovedOrigin(validSender.origin); | ||
if (alreadyApproved) | ||
if (alreadyApproved) { | ||
void chrome.tabs.sendMessage(validSender.tab.id, PraxConnection.Init, { | ||
// init only the specific document | ||
frameId: validSender.frameId, | ||
documentId: validSender.documentId, | ||
}); | ||
} | ||
})(); | ||
|
||
// handler is done | ||
emptyResponse(); | ||
|
||
// boolean return in handlers signals intent to respond | ||
return true; | ||
}, | ||
); |
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