Add Brave CDP automation, replace Oracle browser mode

Connects to user's running Brave via Chrome DevTools Protocol
to automate ChatGPT interaction. Uses puppeteer-core to open a
tab, send the prompt, wait for response, and extract the result.

No cookies, no separate profiles, no copy/paste. Just connects
to the browser where the user is already logged in.

One-time setup: relaunch Brave with --remote-debugging-port=9222

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Taylor Eernisse
2026-02-07 16:16:41 -05:00
parent d776a266a8
commit e7882b917b
4163 changed files with 782828 additions and 148 deletions

View File

@@ -0,0 +1,30 @@
import { Script } from '../../../protocol/protocol.js';
import { type LoggerFn } from '../../../utils/log.js';
import type { EventManager } from '../session/EventManager.js';
import type { Realm } from './Realm.js';
/**
* Used to send messages from realm to BiDi user.
*/
export declare class ChannelProxy {
#private;
constructor(channel: Script.ChannelProperties, logger?: LoggerFn);
/**
* Creates a channel proxy in the given realm, initialises listener and
* returns a handle to `sendMessage` delegate.
*/
init(realm: Realm, eventManager: EventManager): Promise<Script.Handle>;
/** Gets a ChannelProxy from window and returns its handle. */
startListenerFromWindow(realm: Realm, eventManager: EventManager): Promise<void>;
/**
* String to be evaluated to create a ProxyChannel and put it to window.
* Returns the delegate `sendMessage`. Used to provide an argument for preload
* script. Does the following:
* 1. Creates a ChannelProxy.
* 2. Puts the ChannelProxy to window['${this.#id}'] or resolves the promise
* by calling delegate stored in window['${this.#id}'].
* This is needed because `#getHandleFromWindow` can be called before or
* after this method.
* 3. Returns the delegate `sendMessage` of the created ChannelProxy.
*/
getEvalInWindowStr(): string;
}

View File

@@ -0,0 +1,231 @@
/*
* Copyright 2023 Google LLC.
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
import { ChromiumBidi } from '../../../protocol/protocol.js';
import { LogType } from '../../../utils/log.js';
import { uuidv4 } from '../../../utils/uuid.js';
/**
* Used to send messages from realm to BiDi user.
*/
export class ChannelProxy {
#properties;
#id = uuidv4();
#logger;
constructor(channel, logger) {
this.#properties = channel;
this.#logger = logger;
}
/**
* Creates a channel proxy in the given realm, initialises listener and
* returns a handle to `sendMessage` delegate.
*/
async init(realm, eventManager) {
const channelHandle = await ChannelProxy.#createAndGetHandleInRealm(realm);
const sendMessageHandle = await ChannelProxy.#createSendMessageHandle(realm, channelHandle);
void this.#startListener(realm, channelHandle, eventManager);
return sendMessageHandle;
}
/** Gets a ChannelProxy from window and returns its handle. */
async startListenerFromWindow(realm, eventManager) {
try {
const channelHandle = await this.#getHandleFromWindow(realm);
void this.#startListener(realm, channelHandle, eventManager);
}
catch (error) {
this.#logger?.(LogType.debugError, error);
}
}
/**
* Evaluation string which creates a ChannelProxy object on the client side.
*/
static #createChannelProxyEvalStr() {
const functionStr = String(() => {
const queue = [];
let queueNonEmptyResolver = null;
return {
/**
* Gets a promise, which is resolved as soon as a message occurs
* in the queue.
*/
async getMessage() {
const onMessage = queue.length > 0
? Promise.resolve()
: new Promise((resolve) => {
queueNonEmptyResolver = resolve;
});
await onMessage;
return queue.shift();
},
/**
* Adds a message to the queue.
* Resolves the pending promise if needed.
*/
sendMessage(message) {
queue.push(message);
if (queueNonEmptyResolver !== null) {
queueNonEmptyResolver();
queueNonEmptyResolver = null;
}
},
};
});
return `(${functionStr})()`;
}
/** Creates a ChannelProxy in the given realm. */
static async #createAndGetHandleInRealm(realm) {
const createChannelHandleResult = await realm.cdpClient.sendCommand('Runtime.evaluate', {
expression: this.#createChannelProxyEvalStr(),
contextId: realm.executionContextId,
serializationOptions: {
serialization: "idOnly" /* Protocol.Runtime.SerializationOptionsSerialization.IdOnly */,
},
});
if (createChannelHandleResult.exceptionDetails ||
createChannelHandleResult.result.objectId === undefined) {
throw new Error(`Cannot create channel`);
}
return createChannelHandleResult.result.objectId;
}
/** Gets a handle to `sendMessage` delegate from the ChannelProxy handle. */
static async #createSendMessageHandle(realm, channelHandle) {
const sendMessageArgResult = await realm.cdpClient.sendCommand('Runtime.callFunctionOn', {
functionDeclaration: String((channelHandle) => {
return channelHandle.sendMessage;
}),
arguments: [{ objectId: channelHandle }],
executionContextId: realm.executionContextId,
serializationOptions: {
serialization: "idOnly" /* Protocol.Runtime.SerializationOptionsSerialization.IdOnly */,
},
});
// TODO: check for exceptionDetails.
return sendMessageArgResult.result.objectId;
}
/** Starts listening for the channel events of the provided ChannelProxy. */
async #startListener(realm, channelHandle, eventManager) {
// noinspection InfiniteLoopJS
for (;;) {
try {
const message = await realm.cdpClient.sendCommand('Runtime.callFunctionOn', {
functionDeclaration: String(async (channelHandle) => await channelHandle.getMessage()),
arguments: [
{
objectId: channelHandle,
},
],
awaitPromise: true,
executionContextId: realm.executionContextId,
serializationOptions: {
serialization: "deep" /* Protocol.Runtime.SerializationOptionsSerialization.Deep */,
maxDepth: this.#properties.serializationOptions?.maxObjectDepth ??
undefined,
},
});
if (message.exceptionDetails) {
throw new Error('Runtime.callFunctionOn in ChannelProxy', {
cause: message.exceptionDetails,
});
}
for (const browsingContext of realm.associatedBrowsingContexts) {
eventManager.registerEvent({
type: 'event',
method: ChromiumBidi.Script.EventNames.Message,
params: {
channel: this.#properties.channel,
data: realm.cdpToBidiValue(message, this.#properties.ownership ?? "none" /* Script.ResultOwnership.None */),
source: realm.source,
},
}, browsingContext.id);
}
}
catch (error) {
// If an error is thrown, then the channel is permanently broken, so we
// exit the loop.
this.#logger?.(LogType.debugError, error);
break;
}
}
}
/**
* Returns a handle of ChannelProxy from window's property which was set there
* by `getEvalInWindowStr`. If window property is not set yet, sets a promise
* resolver to the window property, so that `getEvalInWindowStr` can resolve
* the promise later on with the channel.
* This is needed because `getEvalInWindowStr` can be called before or
* after this method.
*/
async #getHandleFromWindow(realm) {
const channelHandleResult = await realm.cdpClient.sendCommand('Runtime.callFunctionOn', {
functionDeclaration: String((id) => {
const w = window;
if (w[id] === undefined) {
// The channelProxy is not created yet. Create a promise, put the
// resolver to window property and return the promise.
// `getEvalInWindowStr` will resolve the promise later.
return new Promise((resolve) => (w[id] = resolve));
}
// The channelProxy is already created by `getEvalInWindowStr` and
// is set into window property. Return it.
const channelProxy = w[id];
delete w[id];
return channelProxy;
}),
arguments: [{ value: this.#id }],
executionContextId: realm.executionContextId,
awaitPromise: true,
serializationOptions: {
serialization: "idOnly" /* Protocol.Runtime.SerializationOptionsSerialization.IdOnly */,
},
});
if (channelHandleResult.exceptionDetails !== undefined ||
channelHandleResult.result.objectId === undefined) {
throw new Error(`ChannelHandle not found in window["${this.#id}"]`);
}
return channelHandleResult.result.objectId;
}
/**
* String to be evaluated to create a ProxyChannel and put it to window.
* Returns the delegate `sendMessage`. Used to provide an argument for preload
* script. Does the following:
* 1. Creates a ChannelProxy.
* 2. Puts the ChannelProxy to window['${this.#id}'] or resolves the promise
* by calling delegate stored in window['${this.#id}'].
* This is needed because `#getHandleFromWindow` can be called before or
* after this method.
* 3. Returns the delegate `sendMessage` of the created ChannelProxy.
*/
getEvalInWindowStr() {
const delegate = String((id, channelProxy) => {
const w = window;
if (w[id] === undefined) {
// `#getHandleFromWindow` is not initialized yet, and will get the
// channelProxy later.
w[id] = channelProxy;
}
else {
// `#getHandleFromWindow` is already set a delegate to window property
// and is waiting for it to be called with the channelProxy.
w[id](channelProxy);
delete w[id];
}
return channelProxy.sendMessage;
});
const channelProxyEval = ChannelProxy.#createChannelProxyEvalStr();
return `(${delegate})('${this.#id}',${channelProxyEval})`;
}
}
//# sourceMappingURL=ChannelProxy.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,43 @@
import type { Protocol } from 'devtools-protocol';
import type { Browser, BrowsingContext, Script } from '../../../protocol/protocol.js';
import type { LoggerFn } from '../../../utils/log.js';
import type { CdpTarget } from '../cdp/CdpTarget.js';
import { ChannelProxy } from './ChannelProxy.js';
/**
* BiDi IDs are generated by the server and are unique within contexts.
*
* CDP preload script IDs are generated by the client and are unique
* within sessions.
*
* The mapping between BiDi and CDP preload script IDs is 1:many.
* BiDi IDs are needed by the mapper to keep track of potential multiple CDP IDs
* in the client.
*/
export declare class PreloadScript {
#private;
get id(): string;
get targetIds(): Set<Protocol.Target.TargetID>;
constructor(params: Script.AddPreloadScriptParameters, logger?: LoggerFn);
/** Channels of the preload script. */
get channels(): ChannelProxy[];
/** Contexts of the preload script, if any */
get contexts(): BrowsingContext.BrowsingContext[] | undefined;
/** UserContexts of the preload script, if any */
get userContexts(): Browser.UserContext[] | undefined;
/**
* Adds the script to the given CDP targets by calling the
* `Page.addScriptToEvaluateOnNewDocument` command.
*/
initInTargets(cdpTargets: Iterable<CdpTarget>, runImmediately: boolean): Promise<void>;
/**
* Adds the script to the given CDP target by calling the
* `Page.addScriptToEvaluateOnNewDocument` command.
*/
initInTarget(cdpTarget: CdpTarget, runImmediately: boolean): Promise<void>;
/**
* Removes this script from all CDP targets.
*/
remove(): Promise<void>;
/** Removes the provided cdp target from the list of cdp preload scripts. */
dispose(cdpTargetId: Protocol.Target.TargetID): void;
}

View File

@@ -0,0 +1,129 @@
/*
* Copyright 2023 Google LLC.
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
import { uuidv4 } from '../../../utils/uuid.js';
import { ChannelProxy } from './ChannelProxy.js';
/**
* BiDi IDs are generated by the server and are unique within contexts.
*
* CDP preload script IDs are generated by the client and are unique
* within sessions.
*
* The mapping between BiDi and CDP preload script IDs is 1:many.
* BiDi IDs are needed by the mapper to keep track of potential multiple CDP IDs
* in the client.
*/
export class PreloadScript {
/** BiDi ID, an automatically generated UUID. */
#id = uuidv4();
/** CDP preload scripts. */
#cdpPreloadScripts = [];
/** The script itself, in a format expected by the spec i.e. a function. */
#functionDeclaration;
/** Targets, in which the preload script is initialized. */
#targetIds = new Set();
/** Channels to be added as arguments to functionDeclaration. */
#channels;
/** The script sandbox / world name. */
#sandbox;
/** The browsing contexts to execute the preload scripts in, if any. */
#contexts;
/** The browsing contexts to execute the preload scripts in, if any. */
#userContexts;
get id() {
return this.#id;
}
get targetIds() {
return this.#targetIds;
}
constructor(params, logger) {
this.#channels =
params.arguments?.map((a) => new ChannelProxy(a.value, logger)) ?? [];
this.#functionDeclaration = params.functionDeclaration;
this.#sandbox = params.sandbox;
this.#contexts = params.contexts;
this.#userContexts = params.userContexts;
}
/** Channels of the preload script. */
get channels() {
return this.#channels;
}
/** Contexts of the preload script, if any */
get contexts() {
return this.#contexts;
}
/** UserContexts of the preload script, if any */
get userContexts() {
return this.#userContexts;
}
/**
* String to be evaluated. Wraps user-provided function so that the following
* steps are run:
* 1. Create channels.
* 2. Store the created channels in window.
* 3. Call the user-provided function with channels as arguments.
*/
#getEvaluateString() {
const channelsArgStr = `[${this.channels
.map((c) => c.getEvalInWindowStr())
.join(', ')}]`;
return `(()=>{(${this.#functionDeclaration})(...${channelsArgStr})})()`;
}
/**
* Adds the script to the given CDP targets by calling the
* `Page.addScriptToEvaluateOnNewDocument` command.
*/
async initInTargets(cdpTargets, runImmediately) {
await Promise.all(Array.from(cdpTargets).map((cdpTarget) => this.initInTarget(cdpTarget, runImmediately)));
}
/**
* Adds the script to the given CDP target by calling the
* `Page.addScriptToEvaluateOnNewDocument` command.
*/
async initInTarget(cdpTarget, runImmediately) {
const addCdpPreloadScriptResult = await cdpTarget.cdpClient.sendCommand('Page.addScriptToEvaluateOnNewDocument', {
source: this.#getEvaluateString(),
worldName: this.#sandbox,
runImmediately,
});
this.#cdpPreloadScripts.push({
target: cdpTarget,
preloadScriptId: addCdpPreloadScriptResult.identifier,
});
this.#targetIds.add(cdpTarget.id);
}
/**
* Removes this script from all CDP targets.
*/
async remove() {
await Promise.all([
this.#cdpPreloadScripts.map(async (cdpPreloadScript) => {
const cdpTarget = cdpPreloadScript.target;
const cdpPreloadScriptId = cdpPreloadScript.preloadScriptId;
return await cdpTarget.cdpClient.sendCommand('Page.removeScriptToEvaluateOnNewDocument', {
identifier: cdpPreloadScriptId,
});
}),
]);
}
/** Removes the provided cdp target from the list of cdp preload scripts. */
dispose(cdpTargetId) {
this.#cdpPreloadScripts = this.#cdpPreloadScripts.filter((cdpPreloadScript) => cdpPreloadScript.target?.id !== cdpTargetId);
this.#targetIds.delete(cdpTargetId);
}
}
//# sourceMappingURL=PreloadScript.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"PreloadScript.js","sourceRoot":"","sources":["../../../../../src/bidiMapper/modules/script/PreloadScript.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAUH,OAAO,EAAC,MAAM,EAAC,MAAM,wBAAwB,CAAC;AAG9C,OAAO,EAAC,YAAY,EAAC,MAAM,mBAAmB,CAAC;AAS/C;;;;;;;;;GASG;AACH,MAAM,OAAO,aAAa;IACxB,gDAAgD;IACvC,GAAG,GAAW,MAAM,EAAE,CAAC;IAChC,2BAA2B;IAC3B,kBAAkB,GAAuB,EAAE,CAAC;IAC5C,2EAA2E;IAClE,oBAAoB,CAAS;IACtC,2DAA2D;IAClD,UAAU,GAAG,IAAI,GAAG,EAA4B,CAAC;IAC1D,gEAAgE;IACvD,SAAS,CAAiB;IACnC,uCAAuC;IAC9B,QAAQ,CAAU;IAC3B,uEAAuE;IAC9D,SAAS,CAAqC;IACvD,uEAAuE;IAC9D,aAAa,CAAyB;IAE/C,IAAI,EAAE;QACJ,OAAO,IAAI,CAAC,GAAG,CAAC;IAClB,CAAC;IAED,IAAI,SAAS;QACX,OAAO,IAAI,CAAC,UAAU,CAAC;IACzB,CAAC;IAED,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';
import type { PreloadScript } from './PreloadScript.js';
/** PreloadScripts can be filtered by BiDi ID or target ID. */
export interface PreloadScriptFilter {
targetId: CdpTarget['id'];
}
/**
* Container class for preload scripts.
*/
export declare class PreloadScriptStorage {
#private;
/**
* Finds all entries that match the given filter (OR logic).
*/
find(filter?: PreloadScriptFilter): PreloadScript[];
add(preloadScript: PreloadScript): void;
/** Deletes all BiDi preload script entries that match the given filter. */
remove(id: string): void;
/** Gets the preload script with the given ID, if any, otherwise throws. */
getPreloadScript(id: string): PreloadScript;
onCdpTargetCreated(targetId: string, userContext: Browser.UserContext): void;
}

View File

@@ -0,0 +1,75 @@
/*
* Copyright 2023 Google LLC.
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { NoSuchScriptException } from '../../../protocol/ErrorResponse.js';
/**
* Container class for preload scripts.
*/
export class PreloadScriptStorage {
/** Tracks all BiDi preload scripts. */
#scripts = new Set();
/**
* Finds all entries that match the given filter (OR logic).
*/
find(filter) {
if (!filter) {
return [...this.#scripts];
}
return [...this.#scripts].filter((script) => {
// Global scripts have no contexts or userContext
if (script.contexts === undefined && script.userContexts === undefined) {
return true;
}
if (filter.targetId !== undefined &&
script.targetIds.has(filter.targetId)) {
return true;
}
return false;
});
}
add(preloadScript) {
this.#scripts.add(preloadScript);
}
/** Deletes all BiDi preload script entries that match the given filter. */
remove(id) {
const script = [...this.#scripts].find((script) => script.id === id);
if (script === undefined) {
throw new NoSuchScriptException(`No preload script with id '${id}'`);
}
this.#scripts.delete(script);
}
/** Gets the preload script with the given ID, if any, otherwise throws. */
getPreloadScript(id) {
const script = [...this.#scripts].find((script) => script.id === id);
if (script === undefined) {
throw new NoSuchScriptException(`No preload script with id '${id}'`);
}
return script;
}
onCdpTargetCreated(targetId, userContext) {
const scriptInUserContext = [...this.#scripts].filter((script) => {
// Global scripts
if (!script.userContexts && !script.contexts) {
return true;
}
return script.userContexts?.includes(userContext);
});
for (const script of scriptInUserContext) {
script.targetIds.add(targetId);
}
}
}
//# sourceMappingURL=PreloadScriptStorage.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"PreloadScriptStorage.js","sourceRoot":"","sources":["../../../../../src/bidiMapper/modules/script/PreloadScriptStorage.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AACH,OAAO,EAAC,qBAAqB,EAAC,MAAM,oCAAoC,CAAC;AAWzE;;GAEG;AACH,MAAM,OAAO,oBAAoB;IAC/B,wCAAwC;IAC/B,QAAQ,GAAG,IAAI,GAAG,EAAiB,CAAC;IAE7C;;OAEG;IACH,IAAI,CAAC,MAA4B;QAC/B,IAAI,CAAC,MAAM,EAAE,CAAC;YACZ,OAAO,CAAC,GAAG,IAAI,CAAC,QAAQ,CAAC,CAAC;QAC5B,CAAC;QAED,OAAO,CAAC,GAAG,IAAI,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,CAAC,MAAM,EAAE,EAAE;YAC1C,iDAAiD;YACjD,IAAI,MAAM,CAAC,QAAQ,KAAK,SAAS,IAAI,MAAM,CAAC,YAAY,KAAK,SAAS,EAAE,CAAC;gBACvE,OAAO,IAAI,CAAC;YACd,CAAC;YAED,IACE,MAAM,CAAC,QAAQ,KAAK,SAAS;gBAC7B,MAAM,CAAC,SAAS,CAAC,GAAG,CAAC,MAAM,CAAC,QAAQ,CAAC,EACrC,CAAC;gBACD,OAAO,IAAI,CAAC;YACd,CAAC;YAED,OAAO,KAAK,CAAC;QACf,CAAC,CAAC,CAAC;IACL,CAAC;IAED,GAAG,CAAC,aAA4B;QAC9B,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC;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ACH,CAAC;CACF"}

View File

@@ -0,0 +1,67 @@
/**
* Copyright 2022 Google LLC.
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Protocol } from 'devtools-protocol';
import type { CdpClient } from '../../../cdp/CdpClient.js';
import { Script } from '../../../protocol/protocol.js';
import { type LoggerFn } from '../../../utils/log.js';
import type { BrowsingContextImpl } from '../context/BrowsingContextImpl.js';
import type { EventManager } from '../session/EventManager.js';
import type { RealmStorage } from './RealmStorage.js';
export declare abstract class Realm {
#private;
protected realmStorage: RealmStorage;
constructor(cdpClient: CdpClient, eventManager: EventManager, executionContextId: Protocol.Runtime.ExecutionContextId, logger: LoggerFn | undefined, origin: string, realmId: Script.Realm, realmStorage: RealmStorage);
cdpToBidiValue(cdpValue: Protocol.Runtime.CallFunctionOnResponse | Protocol.Runtime.EvaluateResponse, resultOwnership: Script.ResultOwnership): Script.RemoteValue;
isHidden(): boolean;
/**
* Relies on the CDP to implement proper BiDi serialization, except:
* * CDP integer property `backendNodeId` is replaced with `sharedId` of
* `{documentId}_element_{backendNodeId}`;
* * CDP integer property `weakLocalObjectReference` is replaced with UUID `internalId`
* using unique-per serialization `internalIdMap`.
* * CDP type `platformobject` is replaced with `object`.
* @param deepSerializedValue - CDP value to be converted to BiDi.
* @param internalIdMap - Map from CDP integer `weakLocalObjectReference` to BiDi UUID
* `internalId`.
*/
protected serializeForBiDi(deepSerializedValue: Protocol.Runtime.DeepSerializedValue, internalIdMap: Map<number, string>): Script.RemoteValue;
get realmId(): Script.Realm;
get executionContextId(): Protocol.Runtime.ExecutionContextId;
get origin(): string;
get source(): Script.Source;
get cdpClient(): CdpClient;
abstract get associatedBrowsingContexts(): BrowsingContextImpl[];
abstract get realmType(): Script.RealmType;
protected get baseInfo(): Script.BaseRealmInfo;
abstract get realmInfo(): Script.RealmInfo;
evaluate(expression: string, awaitPromise: boolean, resultOwnership?: Script.ResultOwnership, serializationOptions?: Script.SerializationOptions, userActivation?: boolean, includeCommandLineApi?: boolean): Promise<Script.EvaluateResult>;
protected initialize(): void;
/**
* Serializes a given CDP object into BiDi, keeping references in the
* target's `globalThis`.
*/
serializeCdpObject(cdpRemoteObject: Protocol.Runtime.RemoteObject, resultOwnership: Script.ResultOwnership): Promise<Script.RemoteValue>;
/**
* Gets the string representation of an object. This is equivalent to
* calling `toString()` on the object value.
*/
stringifyObject(cdpRemoteObject: Protocol.Runtime.RemoteObject): Promise<string>;
callFunction(functionDeclaration: string, awaitPromise: boolean, thisLocalValue?: Script.LocalValue, argumentsLocalValues?: Script.LocalValue[], resultOwnership?: Script.ResultOwnership, serializationOptions?: Script.SerializationOptions, userActivation?: boolean): Promise<Script.EvaluateResult>;
deserializeForCdp(localValue: Script.LocalValue): Promise<Protocol.Runtime.CallArgument>;
disown(handle: Script.Handle): Promise<void>;
dispose(): void;
}

View File

@@ -0,0 +1,481 @@
import { ChromiumBidi, NoSuchHandleException, } from '../../../protocol/protocol.js';
import { LogType } from '../../../utils/log.js';
import { uuidv4 } from '../../../utils/uuid.js';
import { ChannelProxy } from './ChannelProxy.js';
export class Realm {
#cdpClient;
#eventManager;
#executionContextId;
#logger;
#origin;
#realmId;
realmStorage;
constructor(cdpClient, eventManager, executionContextId, logger, origin, realmId, realmStorage) {
this.#cdpClient = cdpClient;
this.#eventManager = eventManager;
this.#executionContextId = executionContextId;
this.#logger = logger;
this.#origin = origin;
this.#realmId = realmId;
this.realmStorage = realmStorage;
this.realmStorage.addRealm(this);
}
cdpToBidiValue(cdpValue, resultOwnership) {
const bidiValue = this.serializeForBiDi(cdpValue.result.deepSerializedValue, new Map());
if (cdpValue.result.objectId) {
const objectId = cdpValue.result.objectId;
if (resultOwnership === "root" /* Script.ResultOwnership.Root */) {
// Extend BiDi value with `handle` based on required `resultOwnership`
// and CDP response but not on the actual BiDi type.
bidiValue.handle = objectId;
// Remember all the handles sent to client.
this.realmStorage.knownHandlesToRealmMap.set(objectId, this.realmId);
}
else {
// No need to await for the object to be released.
void this.#releaseObject(objectId).catch((error) => this.#logger?.(LogType.debugError, error));
}
}
return bidiValue;
}
isHidden() {
return false;
}
/**
* Relies on the CDP to implement proper BiDi serialization, except:
* * CDP integer property `backendNodeId` is replaced with `sharedId` of
* `{documentId}_element_{backendNodeId}`;
* * CDP integer property `weakLocalObjectReference` is replaced with UUID `internalId`
* using unique-per serialization `internalIdMap`.
* * CDP type `platformobject` is replaced with `object`.
* @param deepSerializedValue - CDP value to be converted to BiDi.
* @param internalIdMap - Map from CDP integer `weakLocalObjectReference` to BiDi UUID
* `internalId`.
*/
serializeForBiDi(deepSerializedValue, internalIdMap) {
if (Object.hasOwn(deepSerializedValue, 'weakLocalObjectReference')) {
const weakLocalObjectReference = deepSerializedValue.weakLocalObjectReference;
if (!internalIdMap.has(weakLocalObjectReference)) {
internalIdMap.set(weakLocalObjectReference, uuidv4());
}
deepSerializedValue.internalId = internalIdMap.get(weakLocalObjectReference);
delete deepSerializedValue['weakLocalObjectReference'];
}
if (deepSerializedValue.type === 'node' &&
deepSerializedValue.value &&
Object.hasOwn(deepSerializedValue.value, 'frameId')) {
// `frameId` is not needed in BiDi as it is not yet specified.
delete deepSerializedValue.value['frameId'];
}
// Platform object is a special case. It should have only `{type: object}`
// without `value` field.
if (deepSerializedValue.type === 'platformobject') {
return { type: 'object' };
}
const bidiValue = deepSerializedValue.value;
if (bidiValue === undefined) {
return deepSerializedValue;
}
// Recursively update the nested values.
if (['array', 'set', 'htmlcollection', 'nodelist'].includes(deepSerializedValue.type)) {
for (const i in bidiValue) {
bidiValue[i] = this.serializeForBiDi(bidiValue[i], internalIdMap);
}
}
if (['object', 'map'].includes(deepSerializedValue.type)) {
for (const i in bidiValue) {
bidiValue[i] = [
this.serializeForBiDi(bidiValue[i][0], internalIdMap),
this.serializeForBiDi(bidiValue[i][1], internalIdMap),
];
}
}
return deepSerializedValue;
}
get realmId() {
return this.#realmId;
}
get executionContextId() {
return this.#executionContextId;
}
get origin() {
return this.#origin;
}
get source() {
return {
realm: this.realmId,
};
}
get cdpClient() {
return this.#cdpClient;
}
get baseInfo() {
return {
realm: this.realmId,
origin: this.origin,
};
}
async evaluate(expression, awaitPromise, resultOwnership = "none" /* Script.ResultOwnership.None */, serializationOptions = {}, userActivation = false, includeCommandLineApi = false) {
const cdpEvaluateResult = await this.cdpClient.sendCommand('Runtime.evaluate', {
contextId: this.executionContextId,
expression,
awaitPromise,
serializationOptions: Realm.#getSerializationOptions("deep" /* Protocol.Runtime.SerializationOptionsSerialization.Deep */, serializationOptions),
userGesture: userActivation,
includeCommandLineAPI: includeCommandLineApi,
});
if (cdpEvaluateResult.exceptionDetails) {
return await this.#getExceptionResult(cdpEvaluateResult.exceptionDetails, 0, resultOwnership);
}
return {
realm: this.realmId,
result: this.cdpToBidiValue(cdpEvaluateResult, resultOwnership),
type: 'success',
};
}
#registerEvent(event) {
if (this.associatedBrowsingContexts.length === 0) {
this.#eventManager.registerGlobalEvent(event);
}
else {
for (const browsingContext of this.associatedBrowsingContexts) {
this.#eventManager.registerEvent(event, browsingContext.id);
}
}
}
initialize() {
if (!this.isHidden()) {
// Report only not-hidden realms.
this.#registerEvent({
type: 'event',
method: ChromiumBidi.Script.EventNames.RealmCreated,
params: this.realmInfo,
});
}
}
/**
* Serializes a given CDP object into BiDi, keeping references in the
* target's `globalThis`.
*/
async serializeCdpObject(cdpRemoteObject, resultOwnership) {
// TODO: if the object is a primitive, return it directly without CDP roundtrip.
const argument = Realm.#cdpRemoteObjectToCallArgument(cdpRemoteObject);
const cdpValue = await this.cdpClient.sendCommand('Runtime.callFunctionOn', {
functionDeclaration: String((remoteObject) => remoteObject),
awaitPromise: false,
arguments: [argument],
serializationOptions: {
serialization: "deep" /* Protocol.Runtime.SerializationOptionsSerialization.Deep */,
},
executionContextId: this.executionContextId,
});
return this.cdpToBidiValue(cdpValue, resultOwnership);
}
static #cdpRemoteObjectToCallArgument(cdpRemoteObject) {
if (cdpRemoteObject.objectId !== undefined) {
return { objectId: cdpRemoteObject.objectId };
}
if (cdpRemoteObject.unserializableValue !== undefined) {
return { unserializableValue: cdpRemoteObject.unserializableValue };
}
return { value: cdpRemoteObject.value };
}
/**
* Gets the string representation of an object. This is equivalent to
* calling `toString()` on the object value.
*/
async stringifyObject(cdpRemoteObject) {
const { result } = await this.cdpClient.sendCommand('Runtime.callFunctionOn', {
functionDeclaration: String((remoteObject) => String(remoteObject)),
awaitPromise: false,
arguments: [cdpRemoteObject],
returnByValue: true,
executionContextId: this.executionContextId,
});
return result.value;
}
async #flattenKeyValuePairs(mappingLocalValue) {
const keyValueArray = await Promise.all(mappingLocalValue.map(async ([key, value]) => {
let keyArg;
if (typeof key === 'string') {
// Key is a string.
keyArg = { value: key };
}
else {
// Key is a serialized value.
keyArg = await this.deserializeForCdp(key);
}
const valueArg = await this.deserializeForCdp(value);
return [keyArg, valueArg];
}));
return keyValueArray.flat();
}
async #flattenValueList(listLocalValue) {
return await Promise.all(listLocalValue.map((localValue) => this.deserializeForCdp(localValue)));
}
async #serializeCdpExceptionDetails(cdpExceptionDetails, lineOffset, resultOwnership) {
const callFrames = cdpExceptionDetails.stackTrace?.callFrames.map((frame) => ({
url: frame.url,
functionName: frame.functionName,
lineNumber: frame.lineNumber - lineOffset,
columnNumber: frame.columnNumber,
})) ?? [];
// Exception should always be there.
const exception = cdpExceptionDetails.exception;
return {
exception: await this.serializeCdpObject(exception, resultOwnership),
columnNumber: cdpExceptionDetails.columnNumber,
lineNumber: cdpExceptionDetails.lineNumber - lineOffset,
stackTrace: {
callFrames,
},
text: (await this.stringifyObject(exception)) || cdpExceptionDetails.text,
};
}
async callFunction(functionDeclaration, awaitPromise, thisLocalValue = {
type: 'undefined',
}, argumentsLocalValues = [], resultOwnership = "none" /* Script.ResultOwnership.None */, serializationOptions = {}, userActivation = false) {
const callFunctionAndSerializeScript = `(...args) => {
function callFunction(f, args) {
const deserializedThis = args.shift();
const deserializedArgs = args;
return f.apply(deserializedThis, deserializedArgs);
}
return callFunction((
${functionDeclaration}
), args);
}`;
const thisAndArgumentsList = [
await this.deserializeForCdp(thisLocalValue),
...(await Promise.all(argumentsLocalValues.map(async (argumentLocalValue) => await this.deserializeForCdp(argumentLocalValue)))),
];
let cdpCallFunctionResult;
try {
cdpCallFunctionResult = await this.cdpClient.sendCommand('Runtime.callFunctionOn', {
functionDeclaration: callFunctionAndSerializeScript,
awaitPromise,
arguments: thisAndArgumentsList,
serializationOptions: Realm.#getSerializationOptions("deep" /* Protocol.Runtime.SerializationOptionsSerialization.Deep */, serializationOptions),
executionContextId: this.executionContextId,
userGesture: userActivation,
});
}
catch (error) {
// Heuristic to determine if the problem is in the argument.
// The check can be done on the `deserialization` step, but this approach
// helps to save round-trips.
if (error.code === -32000 /* CdpErrorConstants.GENERIC_ERROR */ &&
[
'Could not find object with given id',
'Argument should belong to the same JavaScript world as target object',
'Invalid remote object id',
].includes(error.message)) {
throw new NoSuchHandleException('Handle was not found.');
}
throw error;
}
if (cdpCallFunctionResult.exceptionDetails) {
return await this.#getExceptionResult(cdpCallFunctionResult.exceptionDetails, 1, resultOwnership);
}
return {
type: 'success',
result: this.cdpToBidiValue(cdpCallFunctionResult, resultOwnership),
realm: this.realmId,
};
}
async deserializeForCdp(localValue) {
if ('handle' in localValue && localValue.handle) {
return { objectId: localValue.handle };
// We tried to find a handle value but failed
// This allows us to have exhaustive switch on `localValue.type`
}
else if ('handle' in localValue || 'sharedId' in localValue) {
throw new NoSuchHandleException('Handle was not found.');
}
switch (localValue.type) {
case 'undefined':
return { unserializableValue: 'undefined' };
case 'null':
return { unserializableValue: 'null' };
case 'string':
return { value: localValue.value };
case 'number':
if (localValue.value === 'NaN') {
return { unserializableValue: 'NaN' };
}
else if (localValue.value === '-0') {
return { unserializableValue: '-0' };
}
else if (localValue.value === 'Infinity') {
return { unserializableValue: 'Infinity' };
}
else if (localValue.value === '-Infinity') {
return { unserializableValue: '-Infinity' };
}
return {
value: localValue.value,
};
case 'boolean':
return { value: Boolean(localValue.value) };
case 'bigint':
return {
unserializableValue: `BigInt(${JSON.stringify(localValue.value)})`,
};
case 'date':
return {
unserializableValue: `new Date(Date.parse(${JSON.stringify(localValue.value)}))`,
};
case 'regexp':
return {
unserializableValue: `new RegExp(${JSON.stringify(localValue.value.pattern)}, ${JSON.stringify(localValue.value.flags)})`,
};
case 'map': {
// TODO: If none of the nested keys and values has a remote
// reference, serialize to `unserializableValue` without CDP roundtrip.
const keyValueArray = await this.#flattenKeyValuePairs(localValue.value);
const { result } = await this.cdpClient.sendCommand('Runtime.callFunctionOn', {
functionDeclaration: String((...args) => {
const result = new Map();
for (let i = 0; i < args.length; i += 2) {
result.set(args[i], args[i + 1]);
}
return result;
}),
awaitPromise: false,
arguments: keyValueArray,
returnByValue: false,
executionContextId: this.executionContextId,
});
// TODO(#375): Release `result.objectId` after using.
return { objectId: result.objectId };
}
case 'object': {
// TODO: If none of the nested keys and values has a remote
// reference, serialize to `unserializableValue` without CDP roundtrip.
const keyValueArray = await this.#flattenKeyValuePairs(localValue.value);
const { result } = await this.cdpClient.sendCommand('Runtime.callFunctionOn', {
functionDeclaration: String((...args) => {
const result = {};
for (let i = 0; i < args.length; i += 2) {
// Key should be either `string`, `number`, or `symbol`.
const key = args[i];
result[key] = args[i + 1];
}
return result;
}),
awaitPromise: false,
arguments: keyValueArray,
returnByValue: false,
executionContextId: this.executionContextId,
});
// TODO(#375): Release `result.objectId` after using.
return { objectId: result.objectId };
}
case 'array': {
// TODO: If none of the nested items has a remote reference,
// serialize to `unserializableValue` without CDP roundtrip.
const args = await this.#flattenValueList(localValue.value);
const { result } = await this.cdpClient.sendCommand('Runtime.callFunctionOn', {
functionDeclaration: String((...args) => args),
awaitPromise: false,
arguments: args,
returnByValue: false,
executionContextId: this.executionContextId,
});
// TODO(#375): Release `result.objectId` after using.
return { objectId: result.objectId };
}
case 'set': {
// TODO: if none of the nested items has a remote reference,
// serialize to `unserializableValue` without CDP roundtrip.
const args = await this.#flattenValueList(localValue.value);
const { result } = await this.cdpClient.sendCommand('Runtime.callFunctionOn', {
functionDeclaration: String((...args) => new Set(args)),
awaitPromise: false,
arguments: args,
returnByValue: false,
executionContextId: this.executionContextId,
});
// TODO(#375): Release `result.objectId` after using.
return { objectId: result.objectId };
}
case 'channel': {
const channelProxy = new ChannelProxy(localValue.value, this.#logger);
const channelProxySendMessageHandle = await channelProxy.init(this, this.#eventManager);
return { objectId: channelProxySendMessageHandle };
}
// TODO(#375): Dispose of nested objects.
}
// Intentionally outside to handle unknown types
throw new Error(`Value ${JSON.stringify(localValue)} is not deserializable.`);
}
async #getExceptionResult(exceptionDetails, lineOffset, resultOwnership) {
return {
exceptionDetails: await this.#serializeCdpExceptionDetails(exceptionDetails, lineOffset, resultOwnership),
realm: this.realmId,
type: 'exception',
};
}
static #getSerializationOptions(serialization, serializationOptions) {
return {
serialization,
additionalParameters: Realm.#getAdditionalSerializationParameters(serializationOptions),
...Realm.#getMaxObjectDepth(serializationOptions),
};
}
static #getAdditionalSerializationParameters(serializationOptions) {
const additionalParameters = {};
if (serializationOptions.maxDomDepth !== undefined) {
additionalParameters['maxNodeDepth'] =
serializationOptions.maxDomDepth === null
? 1000
: serializationOptions.maxDomDepth;
}
if (serializationOptions.includeShadowTree !== undefined) {
additionalParameters['includeShadowTree'] =
serializationOptions.includeShadowTree;
}
return additionalParameters;
}
static #getMaxObjectDepth(serializationOptions) {
return serializationOptions.maxObjectDepth === undefined ||
serializationOptions.maxObjectDepth === null
? {}
: { maxDepth: serializationOptions.maxObjectDepth };
}
async #releaseObject(handle) {
try {
await this.cdpClient.sendCommand('Runtime.releaseObject', {
objectId: handle,
});
}
catch (error) {
// Heuristic to determine if the problem is in the unknown handler.
// Ignore the error if so.
if (!(error.code === -32000 /* CdpErrorConstants.GENERIC_ERROR */ &&
error.message === 'Invalid remote object id')) {
throw error;
}
}
}
async disown(handle) {
// Disowning an object from different realm does nothing.
if (this.realmStorage.knownHandlesToRealmMap.get(handle) !== this.realmId) {
return;
}
await this.#releaseObject(handle);
this.realmStorage.knownHandlesToRealmMap.delete(handle);
}
dispose() {
if (!this.isHidden()) {
this.#registerEvent({
type: 'event',
method: ChromiumBidi.Script.EventNames.RealmDestroyed,
params: {
realm: this.realmId,
},
});
}
}
}
//# sourceMappingURL=Realm.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,45 @@
/**
* Copyright 2023 Google LLC.
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import type { Protocol } from 'devtools-protocol';
import { type BrowsingContext, type Script } from '../../../protocol/protocol.js';
import type { Realm } from './Realm.js';
interface RealmFilter {
realmId?: Script.Realm;
browsingContextId?: BrowsingContext.BrowsingContext;
executionContextId?: Protocol.Runtime.ExecutionContextId;
origin?: string;
type?: Script.RealmType;
sandbox?: string | null;
cdpSessionId?: Protocol.Target.SessionID;
isHidden?: boolean;
}
/** Container class for browsing realms. */
export declare class RealmStorage {
#private;
/** List of the internal sandboxed realms which should not be reported to the user. */
readonly hiddenSandboxes: Set<string | undefined>;
get knownHandlesToRealmMap(): Map<string, string>;
addRealm(realm: Realm): void;
/** Finds all realms that match the given filter. */
findRealms(filter: RealmFilter): Realm[];
findRealm(filter: RealmFilter): Realm | undefined;
/** Gets the only realm that matches the given filter, if any, otherwise throws. */
getRealm(filter: RealmFilter): Realm;
/** Deletes all realms that match the given filter. */
deleteRealms(filter: RealmFilter): void;
}
export {};

View File

@@ -0,0 +1,78 @@
import { NoSuchFrameException, } from '../../../protocol/protocol.js';
import { WindowRealm } from './WindowRealm.js';
/** Container class for browsing realms. */
export class RealmStorage {
/** Tracks handles and their realms sent to the client. */
#knownHandlesToRealmMap = new Map();
/** Map from realm ID to Realm. */
#realmMap = new Map();
/** List of the internal sandboxed realms which should not be reported to the user. */
hiddenSandboxes = new Set();
get knownHandlesToRealmMap() {
return this.#knownHandlesToRealmMap;
}
addRealm(realm) {
this.#realmMap.set(realm.realmId, realm);
}
/** Finds all realms that match the given filter. */
findRealms(filter) {
const sandboxFilterValue = filter.sandbox === null ? undefined : filter.sandbox;
return Array.from(this.#realmMap.values()).filter((realm) => {
if (filter.realmId !== undefined && filter.realmId !== realm.realmId) {
return false;
}
if (filter.browsingContextId !== undefined &&
!realm.associatedBrowsingContexts
.map((browsingContext) => browsingContext.id)
.includes(filter.browsingContextId)) {
return false;
}
if (filter.sandbox !== undefined &&
(!(realm instanceof WindowRealm) ||
sandboxFilterValue !== realm.sandbox)) {
return false;
}
if (filter.executionContextId !== undefined &&
filter.executionContextId !== realm.executionContextId) {
return false;
}
if (filter.origin !== undefined && filter.origin !== realm.origin) {
return false;
}
if (filter.type !== undefined && filter.type !== realm.realmType) {
return false;
}
if (filter.cdpSessionId !== undefined &&
filter.cdpSessionId !== realm.cdpClient.sessionId) {
return false;
}
if (filter.isHidden !== undefined &&
filter.isHidden !== realm.isHidden()) {
return false;
}
return true;
});
}
findRealm(filter) {
return this.findRealms(filter)[0];
}
/** Gets the only realm that matches the given filter, if any, otherwise throws. */
getRealm(filter) {
const maybeRealm = this.findRealm(filter);
if (maybeRealm === undefined) {
throw new NoSuchFrameException(`Realm ${JSON.stringify(filter)} not found`);
}
return maybeRealm;
}
/** Deletes all realms that match the given filter. */
deleteRealms(filter) {
this.findRealms(filter).map((realm) => {
realm.dispose();
this.#realmMap.delete(realm.realmId);
Array.from(this.knownHandlesToRealmMap.entries())
.filter(([, r]) => r === realm.realmId)
.map(([handle]) => this.knownHandlesToRealmMap.delete(handle));
});
}
}
//# sourceMappingURL=RealmStorage.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"RealmStorage.js","sourceRoot":"","sources":["../../../../../src/bidiMapper/modules/script/RealmStorage.ts"],"names":[],"mappings":"AAkBA,OAAO,EACL,oBAAoB,GAGrB,MAAM,+BAA+B,CAAC;AAGvC,OAAO,EAAC,WAAW,EAAC,MAAM,kBAAkB,CAAC;AAc7C,2CAA2C;AAC3C,MAAM,OAAO,YAAY;IACvB,0DAA0D;IACjD,uBAAuB,GAAG,IAAI,GAAG,EAGvC,CAAC;IAEJ,kCAAkC;IACzB,SAAS,GAAG,IAAI,GAAG,EAAuB,CAAC;IACpD,sFAAsF;IAC7E,eAAe,GAAG,IAAI,GAAG,EAAsB,CAAC;IAEzD,IAAI,sBAAsB;QACxB,OAAO,IAAI,CAAC,uBAAuB,CAAC;IACtC,CAAC;IAED,QAAQ,CAAC,KAAY;QACnB,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,KAAK,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;IAC3C,CAAC;IAED,oDAAoD;IACpD,UAAU,CAAC,MAAmB;QAC5B,MAAM,kBAAkB,GACtB,MAAM,CAAC,OAAO,KAAK,IAAI,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC;QACvD,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,MAAM,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YAC1D,IAAI,MAAM,CAAC,OAAO,KAAK,SAAS,IAAI,MAAM,CAAC,OAAO,KAAK,KAAK,CAAC,OAAO,EAAE,CAAC;gBACrE,OAAO,KAAK,CAAC;YACf,CAAC;YACD,IACE,MAAM,CAAC,iBAAiB,KAAK,SAAS;gBACtC,CAAC,KAAK,CAAC,0BAA0B;qBAC9B,GAA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;IACnF,QAAQ,CAAC,MAAmB;QAC1B,MAAM,UAAU,GAAG,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC;QAC1C,IAAI,UAAU,KAAK,SAAS,EAAE,CAAC;YAC7B,MAAM,IAAI,oBAAoB,CAC5B,SAAS,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,YAAY,CAC5C,CAAC;QACJ,CAAC;QACD,OAAO,UAAU,CAAC;IACpB,CAAC;IAED,sDAAsD;IACtD,YAAY,CAAC,MAAmB;QAC9B,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE;YACpC,KAAK,CAAC,OAAO,EAAE,CAAC;YAChB,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;YACrC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,sBAAsB,CAAC,OAAO,EAAE,CAAC;iBAC9C,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,KAAK,KAAK,CAAC,OAAO,CAAC;iBACtC,GAAG,CAAC,CAAC,CAAC,MAAM,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC,sBAAsB,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC;QACnE,CAAC,CAAC,CAAC;IACL,CAAC;CACF"}

View File

@@ -0,0 +1,33 @@
/**
* Copyright 2023 Google LLC.
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { type EmptyResult, type Script } from '../../../protocol/protocol.js';
import type { LoggerFn } from '../../../utils/log.js';
import type { UserContextStorage } from '../browser/UserContextStorage.js';
import type { BrowsingContextStorage } from '../context/BrowsingContextStorage.js';
import type { EventManager } from '../session/EventManager.js';
import type { PreloadScriptStorage } from './PreloadScriptStorage.js';
import type { RealmStorage } from './RealmStorage.js';
export declare class ScriptProcessor {
#private;
constructor(eventManager: EventManager, browsingContextStorage: BrowsingContextStorage, realmStorage: RealmStorage, preloadScriptStorage: PreloadScriptStorage, userContextStorage: UserContextStorage, logger?: LoggerFn);
addPreloadScript(params: Script.AddPreloadScriptParameters): Promise<Script.AddPreloadScriptResult>;
removePreloadScript(params: Script.RemovePreloadScriptParameters): Promise<EmptyResult>;
callFunction(params: Script.CallFunctionParameters): Promise<Script.EvaluateResult>;
evaluate(params: Script.EvaluateParameters): Promise<Script.EvaluateResult>;
disown(params: Script.DisownParameters): Promise<EmptyResult>;
getRealms(params: Script.GetRealmsParameters): Script.GetRealmsResult;
}

View File

@@ -0,0 +1,132 @@
/**
* Copyright 2023 Google LLC.
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { ChromiumBidi, InvalidArgumentException, } from '../../../protocol/protocol.js';
import { PreloadScript } from './PreloadScript.js';
export class ScriptProcessor {
#eventManager;
#browsingContextStorage;
#realmStorage;
#preloadScriptStorage;
#userContextStorage;
#logger;
constructor(eventManager, browsingContextStorage, realmStorage, preloadScriptStorage, userContextStorage, logger) {
this.#browsingContextStorage = browsingContextStorage;
this.#realmStorage = realmStorage;
this.#preloadScriptStorage = preloadScriptStorage;
this.#userContextStorage = userContextStorage;
this.#logger = logger;
this.#eventManager = eventManager;
this.#eventManager.addSubscribeHook(ChromiumBidi.Script.EventNames.RealmCreated, this.#onRealmCreatedSubscribeHook.bind(this));
}
#onRealmCreatedSubscribeHook(contextId) {
const context = this.#browsingContextStorage.getContext(contextId);
const contextsToReport = [
context,
...this.#browsingContextStorage.getContext(contextId).allChildren,
];
const realms = new Set();
for (const reportContext of contextsToReport) {
const realmsForContext = this.#realmStorage.findRealms({
browsingContextId: reportContext.id,
});
for (const realm of realmsForContext) {
realms.add(realm);
}
}
for (const realm of realms) {
this.#eventManager.registerEvent({
type: 'event',
method: ChromiumBidi.Script.EventNames.RealmCreated,
params: realm.realmInfo,
}, context.id);
}
return Promise.resolve();
}
async addPreloadScript(params) {
if (params.userContexts?.length && params.contexts?.length) {
throw new InvalidArgumentException('Both userContexts and contexts cannot be specified.');
}
const userContexts = await this.#userContextStorage.verifyUserContextIdList(params.userContexts ?? []);
const browsingContexts = this.#browsingContextStorage.verifyTopLevelContextsList(params.contexts);
const preloadScript = new PreloadScript(params, this.#logger);
this.#preloadScriptStorage.add(preloadScript);
let contextsToRunIn = [];
if (userContexts.size) {
contextsToRunIn = this.#browsingContextStorage
.getTopLevelContexts()
.filter((context) => {
return userContexts.has(context.userContext);
});
}
else if (browsingContexts.size) {
contextsToRunIn = [...browsingContexts.values()];
}
else {
contextsToRunIn = this.#browsingContextStorage.getTopLevelContexts();
}
const cdpTargets = new Set(contextsToRunIn.map((context) => context.cdpTarget));
await preloadScript.initInTargets(cdpTargets, false);
return {
script: preloadScript.id,
};
}
async removePreloadScript(params) {
const { script: id } = params;
const script = this.#preloadScriptStorage.getPreloadScript(id);
await script.remove();
this.#preloadScriptStorage.remove(id);
return {};
}
async callFunction(params) {
const realm = await this.#getRealm(params.target);
return await realm.callFunction(params.functionDeclaration, params.awaitPromise, params.this, params.arguments, params.resultOwnership, params.serializationOptions, params.userActivation);
}
async evaluate(params) {
const realm = await this.#getRealm(params.target);
return await realm.evaluate(params.expression, params.awaitPromise, params.resultOwnership, params.serializationOptions, params.userActivation);
}
async disown(params) {
const realm = await this.#getRealm(params.target);
await Promise.all(params.handles.map(async (handle) => await realm.disown(handle)));
return {};
}
getRealms(params) {
if (params.context !== undefined) {
// Make sure the context is known.
this.#browsingContextStorage.getContext(params.context);
}
const realms = this.#realmStorage
.findRealms({
browsingContextId: params.context,
type: params.type,
isHidden: false,
})
.map((realm) => realm.realmInfo);
return { realms };
}
async #getRealm(target) {
if ('context' in target) {
const context = this.#browsingContextStorage.getContext(target.context);
return await context.getOrCreateUserSandbox(target.sandbox);
}
return this.#realmStorage.getRealm({
realmId: target.realm,
isHidden: false,
});
}
}
//# sourceMappingURL=ScriptProcessor.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"ScriptProcessor.js","sourceRoot":"","sources":["../../../../../src/bidiMapper/modules/script/ScriptProcessor.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,OAAO,EAEL,YAAY,EAEZ,wBAAwB,GAEzB,MAAM,+BAA+B,CAAC;AAOvC,OAAO,EAAC,aAAa,EAAC,MAAM,oBAAoB,CAAC;AAKjD,MAAM,OAAO,eAAe;IACjB,aAAa,CAAe;IAC5B,uBAAuB,CAAyB;IAChD,aAAa,CAAe;IAC5B,qBAAqB,CAAC;IACtB,mBAAmB,CAAqB;IACxC,OAAO,CAAY;IAE5B,YACE,YAA0B,EAC1B,sBAA8C,EAC9C,YAA0B,EAC1B,oBAA0C,EAC1C,kBAAsC,EACtC,MAAiB;QAEjB,IAAI,CAAC,uBAAuB,GAAG,sBAAsB,CAAC;QACtD,IAAI,CAAC,aAAa,GAAG,YAAY,CAAC;QAClC,IAAI,CAAC,qBAAqB,GAAG,oBAAoB,CAAC;QAClD,IAAI,CAAC,mBAAmB,GAAG,kBAAkB,CAAC;QAC9C,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC;QAEtB,IAAI,CAAC,aAAa,GAAG,YAAY,CAAC;QAClC,IAAI,CAAC,aAAa,CAAC,gBAAgB,CACjC,YAAY,CAAC,MAAM,CAAC,UAAU,CAAC,YA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,CAAC;YACjC,kCAAkC;YAClC,IAAI,CAAC,uBAAuB,CAAC,UAAU,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;QAC1D,CAAC;QACD,MAAM,MAAM,GAAG,IAAI,CAAC,aAAa;aAC9B,UAAU,CAAC;YACV,iBAAiB,EAAE,MAAM,CAAC,OAAO;YACjC,IAAI,EAAE,MAAM,CAAC,IAAI;YACjB,QAAQ,EAAE,KAAK;SAChB,CAAC;aACD,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC;QACnC,OAAO,EAAC,MAAM,EAAC,CAAC;IAClB,CAAC;IAED,KAAK,CAAC,SAAS,CAAC,MAAqB;QACnC,IAAI,SAAS,IAAI,MAAM,EAAE,CAAC;YACxB,MAAM,OAAO,GAAG,IAAI,CAAC,uBAAuB,CAAC,UAAU,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;YACxE,OAAO,MAAM,OAAO,CAAC,sBAAsB,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;QAC9D,CAAC;QACD,OAAO,IAAI,CAAC,aAAa,CAAC,QAAQ,CAAC;YACjC,OAAO,EAAE,MAAM,CAAC,KAAK;YACrB,QAAQ,EAAE,KAAK;SAChB,CAAC,CAAC;IACL,CAAC;CACF"}

View File

@@ -0,0 +1,6 @@
export declare function getSharedId(frameId: string, documentId: string, backendNodeId: number): string;
export declare function parseSharedId(sharedId: string): {
frameId: string | undefined;
documentId: string;
backendNodeId: number;
} | null;

View File

@@ -0,0 +1,70 @@
/*
* Copyright 2023 Google LLC.
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const SHARED_ID_DIVIDER = '_element_';
export function getSharedId(frameId, documentId, backendNodeId) {
return `f.${frameId}.d.${documentId}.e.${backendNodeId}`;
}
function parseLegacySharedId(sharedId) {
const match = sharedId.match(new RegExp(`(.*)${SHARED_ID_DIVIDER}(.*)`));
if (!match) {
// SharedId is incorrectly formatted.
return null;
}
const documentId = match[1];
const elementId = match[2];
if (documentId === undefined || elementId === undefined) {
return null;
}
const backendNodeId = parseInt(elementId ?? '');
if (isNaN(backendNodeId)) {
return null;
}
return {
documentId,
backendNodeId,
};
}
export function parseSharedId(sharedId) {
// TODO: remove legacy check once ChromeDriver provides sharedId in the new format.
const legacyFormattedSharedId = parseLegacySharedId(sharedId);
if (legacyFormattedSharedId !== null) {
return { ...legacyFormattedSharedId, frameId: undefined };
}
const match = sharedId.match(/f\.(.*)\.d\.(.*)\.e\.([0-9]*)/);
if (!match) {
// SharedId is incorrectly formatted.
return null;
}
const frameId = match[1];
const documentId = match[2];
const elementId = match[3];
if (frameId === undefined ||
documentId === undefined ||
elementId === undefined) {
return null;
}
const backendNodeId = parseInt(elementId ?? '');
if (isNaN(backendNodeId)) {
return null;
}
return {
frameId,
documentId,
backendNodeId,
};
}
//# sourceMappingURL=SharedId.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"SharedId.js","sourceRoot":"","sources":["../../../../../src/bidiMapper/modules/script/SharedId.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,MAAM,iBAAiB,GAAG,WAAW,CAAC;AAEtC,MAAM,UAAU,WAAW,CACzB,OAAe,EACf,UAAkB,EAClB,aAAqB;IAErB,OAAO,KAAK,OAAO,MAAM,UAAU,MAAM,aAAa,EAAE,CAAC;AAC3D,CAAC;AAED,SAAS,mBAAmB,CAAC,QAAgB;IAI3C,MAAM,KAAK,GAAG,QAAQ,CAAC,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,iBAAiB,MAAM,CAAC,CAAC,CAAC;IACzE,IAAI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AC;IAChD,IAAI,KAAK,CAAC,aAAa,CAAC,EAAE,CAAC;QACzB,OAAO,IAAI,CAAC;IACd,CAAC;IAED,OAAO;QACL,OAAO;QACP,UAAU;QACV,aAAa;KACd,CAAC;AACJ,CAAC"}

View File

@@ -0,0 +1,43 @@
/**
* Copyright 2024 Google LLC.
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import type { Protocol } from 'devtools-protocol';
import type { CdpClient } from '../../../cdp/CdpClient.js';
import { type BrowsingContext, type Script } from '../../../protocol/protocol.js';
import type { LoggerFn } from '../../../utils/log.js';
import type { BrowsingContextImpl } from '../context/BrowsingContextImpl.js';
import type { BrowsingContextStorage } from '../context/BrowsingContextStorage.js';
import type { EventManager } from '../session/EventManager.js';
import { Realm } from './Realm.js';
import type { RealmStorage } from './RealmStorage.js';
export declare class WindowRealm extends Realm {
#private;
readonly sandbox: string | undefined;
constructor(browsingContextId: BrowsingContext.BrowsingContext, browsingContextStorage: BrowsingContextStorage, cdpClient: CdpClient, eventManager: EventManager, executionContextId: Protocol.Runtime.ExecutionContextId, logger: LoggerFn | undefined, origin: string, realmId: Script.Realm, realmStorage: RealmStorage, sandbox: string | undefined);
get browsingContext(): BrowsingContextImpl;
/**
* Do not expose to user hidden realms.
*/
isHidden(): boolean;
get associatedBrowsingContexts(): [BrowsingContextImpl];
get realmType(): 'window';
get realmInfo(): Script.WindowRealmInfo;
get source(): Script.Source;
serializeForBiDi(deepSerializedValue: Protocol.Runtime.DeepSerializedValue, internalIdMap: Map<number, string>): Script.RemoteValue;
deserializeForCdp(localValue: Script.LocalValue): Promise<Protocol.Runtime.CallArgument>;
evaluate(expression: string, awaitPromise: boolean, resultOwnership: Script.ResultOwnership, serializationOptions: Script.SerializationOptions, userActivation?: boolean, includeCommandLineApi?: boolean): Promise<Script.EvaluateResult>;
callFunction(functionDeclaration: string, awaitPromise: boolean, thisLocalValue: Script.LocalValue, argumentsLocalValues: Script.LocalValue[], resultOwnership: Script.ResultOwnership, serializationOptions: Script.SerializationOptions, userActivation?: boolean): Promise<Script.EvaluateResult>;
}

View File

@@ -0,0 +1,142 @@
/**
* Copyright 2024 Google LLC.
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { NoSuchNodeException, UnknownErrorException, } from '../../../protocol/protocol.js';
import { Realm } from './Realm.js';
import { getSharedId, parseSharedId } from './SharedId.js';
export class WindowRealm extends Realm {
#browsingContextId;
#browsingContextStorage;
sandbox;
constructor(browsingContextId, browsingContextStorage, cdpClient, eventManager, executionContextId, logger, origin, realmId, realmStorage, sandbox) {
super(cdpClient, eventManager, executionContextId, logger, origin, realmId, realmStorage);
this.#browsingContextId = browsingContextId;
this.#browsingContextStorage = browsingContextStorage;
this.sandbox = sandbox;
this.initialize();
}
#getBrowsingContextId(navigableId) {
const maybeBrowsingContext = this.#browsingContextStorage
.getAllContexts()
.find((context) => context.navigableId === navigableId);
return maybeBrowsingContext?.id ?? 'UNKNOWN';
}
get browsingContext() {
return this.#browsingContextStorage.getContext(this.#browsingContextId);
}
/**
* Do not expose to user hidden realms.
*/
isHidden() {
return this.realmStorage.hiddenSandboxes.has(this.sandbox);
}
get associatedBrowsingContexts() {
return [this.browsingContext];
}
get realmType() {
return 'window';
}
get realmInfo() {
return {
...this.baseInfo,
type: this.realmType,
context: this.#browsingContextId,
sandbox: this.sandbox,
};
}
get source() {
return {
realm: this.realmId,
context: this.browsingContext.id,
};
}
serializeForBiDi(deepSerializedValue, internalIdMap) {
const bidiValue = deepSerializedValue.value;
if (deepSerializedValue.type === 'node' && bidiValue !== undefined) {
if (Object.hasOwn(bidiValue, 'backendNodeId')) {
let navigableId = this.browsingContext.navigableId ?? 'UNKNOWN';
if (Object.hasOwn(bidiValue, 'loaderId')) {
// `loaderId` should be always there after ~2024-03-05, when
// https://crrev.com/c/5116240 reaches stable.
// TODO: remove the check after the date.
navigableId = bidiValue.loaderId;
delete bidiValue['loaderId'];
}
deepSerializedValue.sharedId =
getSharedId(this.#getBrowsingContextId(navigableId), navigableId, bidiValue.backendNodeId);
delete bidiValue['backendNodeId'];
}
if (Object.hasOwn(bidiValue, 'children')) {
for (const i in bidiValue.children) {
bidiValue.children[i] = this.serializeForBiDi(bidiValue.children[i], internalIdMap);
}
}
if (Object.hasOwn(bidiValue, 'shadowRoot') &&
bidiValue.shadowRoot !== null) {
bidiValue.shadowRoot = this.serializeForBiDi(bidiValue.shadowRoot, internalIdMap);
}
// `namespaceURI` can be is either `null` or non-empty string.
if (bidiValue.namespaceURI === '') {
bidiValue.namespaceURI = null;
}
}
return super.serializeForBiDi(deepSerializedValue, internalIdMap);
}
async deserializeForCdp(localValue) {
if ('sharedId' in localValue && localValue.sharedId) {
const parsedSharedId = parseSharedId(localValue.sharedId);
if (parsedSharedId === null) {
throw new NoSuchNodeException(`SharedId "${localValue.sharedId}" was not found.`);
}
const { documentId, backendNodeId } = parsedSharedId;
// TODO: add proper validation if the element is accessible from the current realm.
if (this.browsingContext.navigableId !== documentId) {
throw new NoSuchNodeException(`SharedId "${localValue.sharedId}" belongs to different document. Current document is ${this.browsingContext.navigableId}.`);
}
try {
const { object } = await this.cdpClient.sendCommand('DOM.resolveNode', {
backendNodeId,
executionContextId: this.executionContextId,
});
// TODO(#375): Release `obj.object.objectId` after using.
return { objectId: object.objectId };
}
catch (error) {
// Heuristic to detect "no such node" exception. Based on the specific
// CDP implementation.
if (error.code === -32000 /* CdpErrorConstants.GENERIC_ERROR */ &&
error.message === 'No node with given id found') {
throw new NoSuchNodeException(`SharedId "${localValue.sharedId}" was not found.`);
}
throw new UnknownErrorException(error.message, error.stack);
}
}
return await super.deserializeForCdp(localValue);
}
async evaluate(expression, awaitPromise, resultOwnership, serializationOptions, userActivation, includeCommandLineApi) {
await this.#browsingContextStorage
.getContext(this.#browsingContextId)
.targetUnblockedOrThrow();
return await super.evaluate(expression, awaitPromise, resultOwnership, serializationOptions, userActivation, includeCommandLineApi);
}
async callFunction(functionDeclaration, awaitPromise, thisLocalValue, argumentsLocalValues, resultOwnership, serializationOptions, userActivation) {
await this.#browsingContextStorage
.getContext(this.#browsingContextId)
.targetUnblockedOrThrow();
return await super.callFunction(functionDeclaration, awaitPromise, thisLocalValue, argumentsLocalValues, resultOwnership, serializationOptions, userActivation);
}
}
//# sourceMappingURL=WindowRealm.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"WindowRealm.js","sourceRoot":"","sources":["../../../../../src/bidiMapper/modules/script/WindowRealm.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAKH,OAAO,EACL,mBAAmB,EACnB,qBAAqB,GAGtB,MAAM,+BAA+B,CAAC;AAOvC,OAAO,EAAC,KAAK,EAAC,MAAM,YAAY,CAAC;AAEjC,OAAO,EAAC,WAAW,EAAE,aAAa,EAAC,MAAM,eAAe,CAAC;AAEzD,MAAM,OAAO,WAAY,SAAQ,KAAK;IAC3B,kBAAkB,CAAkC;IACpD,uBAAuB,CAAyB;IAChD,OAAO,CAAqB;IAErC,YACE,iBAAkD,EAClD,sBAA8C,EAC9C,SAAoB,EACpB,YAA0B,EAC1B,kBAAuD,EACvD,MAA4B,EAC5B,MAAc,EACd,OAAqB,EACrB,YAA0B,EAC1B,OAA2B;QAE3B,KAAK,CACH,SAAS,EACT,YAAY,EACZ,kBAAkB,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CAC3B,aAAa,UAAU,CAAC,QAAQ,kBAAkB,CACnD,CAAC;gBACJ,CAAC;gBACD,MAAM,IAAI,qBAAqB,CAAC,KAAK,CAAC,OAAO,EAAE,KAAK,CAAC,KAAK,CAAC,CAAC;YAC9D,CAAC;QACH,CAAC;QACD,OAAO,MAAM,KAAK,CAAC,iBAAiB,CAAC,UAAU,CAAC,CAAC;IACnD,CAAC;IAEQ,KAAK,CAAC,QAAQ,CACrB,UAAkB,EAClB,YAAqB,EACrB,eAAuC,EACvC,oBAAiD,EACjD,cAAwB,EACxB,qBAA+B;QAE/B,MAAM,IAAI,CAAC,uBAAuB;aAC/B,UAAU,CAAC,IAAI,CAAC,kBAAkB,CAAC;aACnC,sBAAsB,EAAE,CAAC;QAE5B,OAAO,MAAM,KAAK,CAAC,QAAQ,CACzB,UAAU,EACV,YAAY,EACZ,eAAe,EACf,oBAAoB,EACpB,cAAc,EACd,qBAAqB,CACtB,CAAC;IACJ,CAAC;IAEQ,KAAK,CAAC,YAAY,CACzB,mBAA2B,EAC3B,YAAqB,EACrB,cAAiC,EACjC,oBAAyC,EACzC,eAAuC,EACvC,oBAAiD,EACjD,cAAwB;QAExB,MAAM,IAAI,CAAC,uBAAuB;aAC/B,UAAU,CAAC,IAAI,CAAC,kBAAkB,CAAC;aACnC,sBAAsB,EAAE,CAAC;QAE5B,OAAO,MAAM,KAAK,CAAC,YAAY,CAC7B,mBAAmB,EACnB,YAAY,EACZ,cAAc,EACd,oBAAoB,EACpB,eAAe,EACf,oBAAoB,EACpB,cAAc,CACf,CAAC;IACJ,CAAC;CACF"}

View File

@@ -0,0 +1,33 @@
/**
* Copyright 2024 Google LLC.
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import type { Protocol } from 'devtools-protocol';
import type { CdpClient } from '../../../cdp/CdpClient.js';
import type { Script } from '../../../protocol/protocol.js';
import type { LoggerFn } from '../../../utils/log.js';
import type { BrowsingContextImpl } from '../context/BrowsingContextImpl.js';
import type { EventManager } from '../session/EventManager.js';
import { Realm } from './Realm.js';
import type { RealmStorage } from './RealmStorage.js';
export type WorkerRealmType = Pick<Script.SharedWorkerRealmInfo | Script.DedicatedWorkerRealmInfo | Script.ServiceWorkerRealmInfo, 'type'>['type'];
export declare class WorkerRealm extends Realm {
#private;
constructor(cdpClient: CdpClient, eventManager: EventManager, executionContextId: Protocol.Runtime.ExecutionContextId, logger: LoggerFn | undefined, origin: string, ownerRealms: Realm[], realmId: Script.Realm, realmStorage: RealmStorage, realmType: WorkerRealmType);
get associatedBrowsingContexts(): BrowsingContextImpl[];
get realmType(): WorkerRealmType;
get source(): Script.Source;
get realmInfo(): Script.RealmInfo;
}

View File

@@ -0,0 +1,66 @@
/**
* Copyright 2024 Google LLC.
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Realm } from './Realm.js';
export class WorkerRealm extends Realm {
#realmType;
#ownerRealms;
constructor(cdpClient, eventManager, executionContextId, logger, origin, ownerRealms, realmId, realmStorage, realmType) {
super(cdpClient, eventManager, executionContextId, logger, origin, realmId, realmStorage);
this.#ownerRealms = ownerRealms;
this.#realmType = realmType;
this.initialize();
}
get associatedBrowsingContexts() {
return this.#ownerRealms.flatMap((realm) => realm.associatedBrowsingContexts);
}
get realmType() {
return this.#realmType;
}
get source() {
return {
realm: this.realmId,
// This is a hack to make Puppeteer able to track workers.
// TODO: remove after Puppeteer tracks workers by owners and use the base version.
context: this.associatedBrowsingContexts[0]?.id,
};
}
get realmInfo() {
const owners = this.#ownerRealms.map((realm) => realm.realmId);
const { realmType } = this;
switch (realmType) {
case 'dedicated-worker': {
const owner = owners[0];
if (owner === undefined || owners.length !== 1) {
throw new Error('Dedicated worker must have exactly one owner');
}
return {
...this.baseInfo,
type: realmType,
owners: [owner],
};
}
case 'service-worker':
case 'shared-worker': {
return {
...this.baseInfo,
type: realmType,
};
}
}
}
}
//# sourceMappingURL=WorkerRealm.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"WorkerRealm.js","sourceRoot":"","sources":["../../../../../src/bidiMapper/modules/script/WorkerRealm.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAUH,OAAO,EAAC,KAAK,EAAC,MAAM,YAAY,CAAC;AAUjC,MAAM,OAAO,WAAY,SAAQ,KAAK;IAC3B,UAAU,CAAkB;IAC5B,YAAY,CAAU;IAE/B,YACE,SAAoB,EACpB,YAA0B,EAC1B,kBAAuD,EACvD,MAA4B,EAC5B,MAAc,EACd,WAAoB,EACpB,OAAqB,EACrB,YAA0B,EAC1B,SAA0B;QAE1B,KAAK,CACH,SAAS,EACT,YAAY,EACZ,kBAAkB,EAClB,MAAM,EACN,MAAM,EACN,OAAO,EACP,YAAY,CACb,CAAC;QAEF,IAAI,CAAC,YAAY,GAAG,WAAW,CAAC;QAChC,IAAI,CAAC,UAAU,GAAG,SAAS,CAAC;QAE5B,IAAI,CAAC,UAAU,EAAE,CAAC;IACpB,CAAC;IAED,IAAa,0BAA0B;QACrC,OAAO,IAAI,CAAC,YAAY,CAAC,OAAO,CAC9B,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,0BAA0B,CAC5C,CAAC;IACJ,CAAC;IAED,IAAa,SAAS;QACpB,OAAO,IAAI,CAAC,UAAU,CAAC;IACzB,CA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