Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scope approvals to documentId #49

Merged
merged 3 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/giant-otters-whisper.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'chrome-extension': patch
---

scope init to documentId
225 changes: 0 additions & 225 deletions apps/extension/src/approve-origin.test.ts

This file was deleted.

77 changes: 0 additions & 77 deletions apps/extension/src/approve-origin.ts

This file was deleted.

20 changes: 17 additions & 3 deletions apps/extension/src/content-scripts/injected-connection-port.ts
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

elaborated some of the comments and parameter naming in various places

Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,28 @@ import { PraxMessage } from './message-event';
import { CRSessionClient } from '@penumbra-zone/transport-chrome/session-client';
import { PraxConnection } from '../message/prax';

// this inits the session client that transports messages on the DOM channel through the Chrome runtime
const initOnce = (req: unknown, _sender: chrome.runtime.MessageSender, respond: () => void) => {
// content script unconditionally announces itself to extension background.
void chrome.runtime.sendMessage(PraxConnection.Init);

// listen for init command from background. this may arrive soon after announce,
turbocrime marked this conversation as resolved.
Show resolved Hide resolved
// or much later, after a request is made. this activates the channel session
// that transports messages from the DOM channel into the Chrome runtime
const initOnce = (
req: unknown,
// in a content script, sender is always an extension background script
_: chrome.runtime.MessageSender,
// this handler will only ever send an empty response
emptyResponse: (no?: never) => void,
) => {
if (req !== PraxConnection.Init) return false;

chrome.runtime.onMessage.removeListener(initOnce);

// 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]);
respond();

emptyResponse();
return true;
};

Expand Down
17 changes: 5 additions & 12 deletions apps/extension/src/content-scripts/injected-penumbra-global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,12 @@ const requestResponseListener = (msg: MessageEvent<unknown>) => {
if (msg.origin === window.origin) {
if (isPraxFailureMessageEvent(msg)) {
// @ts-expect-error - ts can't understand the injected string
const status = msg.data[PRAX] as PraxConnection;
const status: unknown = msg.data[PRAX];
const failure = new Error('Connection request failed');
switch (status) {
case PraxConnection.Denied:
failure.cause = PenumbraRequestFailure.Denied;
break;
case PraxConnection.NeedsLogin:
failure.cause = PenumbraRequestFailure.NeedsLogin;
break;
default:
failure.cause = 'Unknown';
break;
}
failure.cause =
typeof status === 'string' && status in PenumbraRequestFailure
? status
: `Unknown failure: ${String(status)}`;
request.reject(failure);
}
}
Expand Down
19 changes: 9 additions & 10 deletions apps/extension/src/content-scripts/injected-request-listener.ts
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is no logic or behavior change in this code, but its behavior is clearer now.

Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
import { PraxMessage, isPraxRequestMessageEvent } from './message-event';
import { PraxConnection } from '../message/prax';
import { PenumbraRequestFailure } from '@penumbra-zone/client';

const handleRequest = (ev: MessageEvent<unknown>) => {
if (ev.origin === window.origin && isPraxRequestMessageEvent(ev)) {
void (async () => {
window.removeEventListener('message', handleRequest);
const result = await chrome.runtime.sendMessage<

// any response to this message only indicates failure. success is
// resolved upon successful connection, and those messages are handled by
// the script in injected-connection-port
const failure = await chrome.runtime.sendMessage<
PraxConnection,
Exclude<PraxConnection, PraxConnection.Request>
undefined | PenumbraRequestFailure
>(PraxConnection.Request);
// init is handled by injected-connection-port
if (result !== PraxConnection.Init)
window.postMessage(
{ [PRAX]: result } satisfies PraxMessage<
PraxConnection.Denied | PraxConnection.NeedsLogin
>,
'/',
);
if (failure)
window.postMessage({ [PRAX]: failure } satisfies PraxMessage<PenumbraRequestFailure>, '/');
})();
}
};
Expand Down
Loading
Loading