({
debuggerApi,
request,
scriptingApi,
tabsApi,
}: {
debuggerApi: ChromeDebuggerApi | undefined;
request: TabDebuggerRequest;
scriptingApi: BrowserScriptingApi | undefined;
tabsApi: BrowserTabsApi | undefined;
})
| 57 | ChromeDebuggerApi, |
| 58 | TabDebuggerRequest, |
| 59 | TabDebuggerResponse, |
| 60 | } from '@/src/shared/tab-debugger'; |
| 61 | import { webMcpDispatchHooks } from '@/src/shared/web-mcp-tools'; |
| 62 | |
| 63 | interface ChromeRuntimeApi { |
| 64 | readonly id?: string; |
| 65 | readonly onInstalled?: { |
| 66 | readonly addListener: (listener: () => void) => void; |
| 67 | }; |
| 68 | readonly onMessage?: { |
| 69 | readonly addListener: ( |
| 70 | listener: ( |
| 71 | message: unknown, |
| 72 | sender: unknown, |
| 73 | sendResponse: (response: TabDebuggerResponse) => void |
| 74 | ) => boolean | void |
| 75 | ) => void; |
| 76 | }; |
| 77 | } |
| 78 | |
| 79 | /* |
| 80 | * The download hold, built while this worker's module is evaluated: `onDeterminingFilename` |
| 81 | * only reaches an extension that was listening when the browser created the download, so the |
| 82 | * listener cannot wait for `defineBackground` to run or for the first message. |
| 83 | */ |
| 84 | const readChrome = ( |
| 85 | globalThis as typeof globalThis & { |
| 86 | chrome?: { |
| 87 | downloads?: BrowserDownloadsApi; |
| 88 | permissions?: DownloadHoldPermissionsApi; |
| 89 | runtime?: { getURL?: (path: string) => string }; |
| 90 | }; |
| 91 | } |
| 92 | ).chrome; |
| 93 | |
| 94 | const profileDownloadHold = startDownloadHold({ |
| 95 | downloads: () => readChrome?.downloads, |
| 96 | // `chrome-extension://` on Chrome, `moz-extension://` on Firefox: the one signal that separates a browser that |
| 97 | // Raises `onDeterminingFilename` from one that never can, before the optional permission decides anything. |
| 98 | extensionOrigin: () => readChrome?.runtime?.getURL?.(''), |
| 99 | permissions: () => readChrome?.permissions, |
| 100 | // `storage` is the whole area multiplexer; the record's key selects `session:`. |
| 101 | sessionStorage: () => storage, |
| 102 | }); |
| 103 | |
| 104 | /* |
| 105 | * Trust boundary for the eval/debugger message path. Today only the extension's own pages (the |
| 106 | * side panel) can reach this listener — there is no externally_connectable and no content script. |
| 107 | * Accept only same-extension senders whose origin is an extension page, so adding a content script |
| 108 | * later can't silently widen access to the dangerous eval path: a content script shares the |
| 109 | * extension `id` but reports the host page's web origin, while an extension page reports an |
| 110 | * extension-scheme origin (`chrome-extension://` on Chrome, `moz-extension://` on Firefox). |
| 111 | */ |
| 112 | const extensionSchemeSchema = z |
| 113 | .string() |
| 114 | .refine(value => value.startsWith('chrome-extension://') || value.startsWith('moz-extension://')); |
| 115 | |
| 116 | const isExtensionScheme = (value: unknown): boolean => |
no test coverage detected