Skip to content

Commit

Permalink
Revert "feat: implement dedicated workers" (#1570)
Browse files Browse the repository at this point in the history
Reverts #1565

e2e tests started constantly timing out.
  • Loading branch information
sadym-chromium authored Nov 17, 2023
1 parent 705365b commit cd7fa0b
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 235 deletions.
54 changes: 23 additions & 31 deletions src/bidiMapper/domains/context/BrowsingContextImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -447,44 +447,32 @@ export class BrowsingContextImpl {
this.#cdpTarget.cdpClient.on(
'Runtime.executionContextCreated',
(params: Protocol.Runtime.ExecutionContextCreatedEvent) => {
const {auxData, name, uniqueId, id} = params.context;
if (!auxData || auxData.frameId !== this.id) {
if (params.context.auxData.frameId !== this.id) {
return;
}

let origin: string;
let sandbox: string | undefined;
// Only these execution contexts are supported for now.
switch (auxData.type) {
case 'isolated':
sandbox = name;
// Sandbox should have the same origin as the context itself, but in CDP
// it has an empty one.
origin = this.#defaultRealm.origin;
break;
case 'default':
origin = serializeOrigin(params.context.origin);
break;
default:
return;
// Only this execution contexts are supported for now.
if (!['default', 'isolated'].includes(params.context.auxData.type)) {
return;
}
const realm = new Realm(
this.#realmStorage,
this.#browsingContextStorage,
uniqueId,
params.context.uniqueId,
this.id,
id,
origin,
params.context.id,
this.#getOrigin(params),
// XXX: differentiate types.
'window',
// Sandbox name for isolated world.
sandbox,
params.context.auxData.type === 'isolated'
? params.context.name
: undefined,
this.#cdpTarget.cdpClient,
this.#eventManager,
this.#logger
);

if (auxData.isDefault) {
if (params.context.auxData.isDefault) {
this.#maybeDefaultRealm = realm;

// Initialize ChannelProxy listeners for all the channels of all the
Expand Down Expand Up @@ -553,6 +541,18 @@ export class BrowsingContextImpl {
});
}

#getOrigin(params: Protocol.Runtime.ExecutionContextCreatedEvent) {
if (params.context.auxData.type === 'isolated') {
// Sandbox should have the same origin as the context itself, but in CDP
// it has an empty one.
return this.#defaultRealm.origin;
}
// https://html.spec.whatwg.org/multipage/origin.html#ascii-serialisation-of-an-origin
return ['://', ''].includes(params.context.origin)
? 'null'
: params.context.origin;
}

#documentChanged(loaderId?: Protocol.Network.LoaderId) {
// Same document navigation.
if (loaderId === undefined || this.#loaderId === loaderId) {
Expand Down Expand Up @@ -1001,14 +1001,6 @@ export class BrowsingContextImpl {
}
}

export function serializeOrigin(origin: string) {
// https://html.spec.whatwg.org/multipage/origin.html#ascii-serialisation-of-an-origin
if (['://', ''].includes(origin)) {
origin = 'null';
}
return origin;
}

function getImageFormatParameters(
params: Readonly<BrowsingContext.CaptureScreenshotParameters>
) {
Expand Down
170 changes: 61 additions & 109 deletions src/bidiMapper/domains/context/BrowsingContextProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,17 @@ import type {ICdpClient} from '../../../cdp/CdpClient.js';
import type {ICdpConnection} from '../../../cdp/CdpConnection.js';
import {
BrowsingContext,
InvalidArgumentException,
type EmptyResult,
InvalidArgumentException,
} from '../../../protocol/protocol.js';
import {CdpErrorConstants} from '../../../utils/CdpErrorConstants.js';
import {LogType, type LoggerFn} from '../../../utils/log.js';
import {type LoggerFn, LogType} from '../../../utils/log.js';
import type {EventManager} from '../events/EventManager.js';
import type {NetworkStorage} from '../network/NetworkStorage.js';
import type {PreloadScriptStorage} from '../script/PreloadScriptStorage.js';
import {Realm} from '../script/Realm.js';
import type {RealmStorage} from '../script/RealmStorage.js';

import {BrowsingContextImpl, serializeOrigin} from './BrowsingContextImpl.js';
import {BrowsingContextImpl} from './BrowsingContextImpl.js';
import type {BrowsingContextStorage} from './BrowsingContextStorage.js';
import {CdpTarget} from './CdpTarget.js';

Expand Down Expand Up @@ -336,136 +335,89 @@ export class BrowsingContextProcessor {
parentSessionCdpClient: ICdpClient
) {
const {sessionId, targetInfo} = params;

const targetCdpClient = this.#cdpConnection.getCdpClient(sessionId);

if (!this.#isValidTarget(targetInfo)) {
// DevTools or some other not supported by BiDi target. Just release
// debugger and ignore them.
targetCdpClient
.sendCommand('Runtime.runIfWaitingForDebugger')
.then(() =>
parentSessionCdpClient.sendCommand('Target.detachFromTarget', params)
)
.catch((error) => this.#logger?.(LogType.debugError, error));
return;
}

this.#logger?.(
LogType.debugInfo,
'AttachedToTarget event received:',
params
);

switch (targetInfo.type) {
case 'page':
case 'iframe': {
if (targetInfo.targetId === this.#selfTargetId) {
break;
}

this.#setEventListeners(targetCdpClient);

const cdpTarget = CdpTarget.create(
targetInfo.targetId,
targetCdpClient,
this.#browserCdpClient,
sessionId,
this.#realmStorage,
this.#eventManager,
this.#preloadScriptStorage,
this.#networkStorage,
this.#acceptInsecureCerts
);
this.#setEventListeners(targetCdpClient);

const maybeContext = this.#browsingContextStorage.findContext(
targetInfo.targetId
);
if (maybeContext) {
// OOPiF.
maybeContext.updateCdpTarget(cdpTarget);
} else {
// New context.
BrowsingContextImpl.create(
cdpTarget,
this.#realmStorage,
targetInfo.targetId,
null,
this.#eventManager,
this.#browsingContextStorage,
this.#logger
);
}
return;
}
case 'worker': {
this.#setEventListeners(targetCdpClient);

const cdpTarget = CdpTarget.create(
targetInfo.targetId,
targetCdpClient,
this.#browserCdpClient,
sessionId,
this.#realmStorage,
this.#eventManager,
this.#preloadScriptStorage,
this.#networkStorage,
this.#acceptInsecureCerts
);

this.#createWorkerRealm(cdpTarget);
return;
}
}
const maybeContext = this.#browsingContextStorage.findContext(
targetInfo.targetId
);

// DevTools or some other not supported by BiDi target. Just release
// debugger and ignore them.
targetCdpClient
.sendCommand('Runtime.runIfWaitingForDebugger')
.then(() =>
parentSessionCdpClient.sendCommand('Target.detachFromTarget', params)
)
.catch((error) => this.#logger?.(LogType.debugError, error));
}
const cdpTarget = CdpTarget.create(
targetInfo.targetId,
targetCdpClient,
this.#browserCdpClient,
sessionId,
this.#realmStorage,
this.#eventManager,
this.#preloadScriptStorage,
this.#networkStorage,
this.#acceptInsecureCerts
);

#workers = new Map<string, Realm>();
#createWorkerRealm(cdpTarget: CdpTarget) {
cdpTarget.cdpClient.on('Runtime.executionContextCreated', (params) => {
const {uniqueId, id, origin} = params.context;
const realm = new Realm(
if (maybeContext) {
// OOPiF.
maybeContext.updateCdpTarget(cdpTarget);
} else {
// New context.
BrowsingContextImpl.create(
cdpTarget,
this.#realmStorage,
this.#browsingContextStorage,
uniqueId,
cdpTarget.targetId,
id,
serializeOrigin(origin),
'dedicated-worker',
undefined,
cdpTarget.cdpClient,
targetInfo.targetId,
null,
this.#eventManager,
this.#browsingContextStorage,
this.#logger
);
this.#workers.set(cdpTarget.cdpSessionId, realm);
});
}
}

#handleDetachedFromTargetEvent(
params: Protocol.Target.DetachedFromTargetEvent
) {
const context = this.#browsingContextStorage
.getAllContexts()
.find((context) => context.cdpTarget.cdpSessionId === params.sessionId);
if (context) {
context.dispose();
this.#preloadScriptStorage
.find({targetId: context.id})
.map((preloadScript) => preloadScript.dispose(context.id));
return;
}

const worker = this.#workers.get(params.sessionId);
if (worker) {
this.#realmStorage.deleteRealms({
cdpSessionId: worker.cdpClient.sessionId,
});
}
// XXX: params.targetId is deprecated. Update this class to track using
// params.sessionId instead.
// https://github.com/GoogleChromeLabs/chromium-bidi/issues/60
const contextId = params.targetId!;
this.#browsingContextStorage.findContext(contextId)?.dispose();

this.#preloadScriptStorage
.find({targetId: contextId})
.map((preloadScript) => preloadScript.dispose(contextId));
}

#handleTargetInfoChangedEvent(
params: Protocol.Target.TargetInfoChangedEvent
) {
const context = this.#browsingContextStorage.findContext(
params.targetInfo.targetId
);
if (context) {
context.onTargetInfoChanged(params);
const contextId = params.targetInfo.targetId;
this.#browsingContextStorage
.findContext(contextId)
?.onTargetInfoChanged(params);
}

#isValidTarget(target: Protocol.Target.TargetInfo) {
if (target.targetId === this.#selfTargetId) {
return false;
}
return ['page', 'iframe'].includes(target.type);
}
}
Loading

0 comments on commit cd7fa0b

Please sign in to comment.