refactor: fail early for features that require CDP (#10723)

This commit is contained in:
Alex Rudenko 2023-08-11 09:36:32 +02:00 committed by GitHub
parent 6d7ea9e105
commit c6e5bb2e40
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 6 deletions

View File

@ -179,7 +179,11 @@ export class Browser extends BrowserBase {
} }
#onContextCreated(event: Bidi.BrowsingContext.ContextCreated['params']) { #onContextCreated(event: Bidi.BrowsingContext.ContextCreated['params']) {
const context = new BrowsingContext(this.#connection, event); const context = new BrowsingContext(
this.#connection,
event,
this.#browserName
);
this.#connection.registerBrowsingContexts(context); this.#connection.registerBrowsingContexts(context);
// TODO: once more browsing context types are supported, this should be // TODO: once more browsing context types are supported, this should be
// updated to support those. Currently, all top-level contexts are treated // updated to support those. Currently, all top-level contexts are treated

View File

@ -51,6 +51,9 @@ export class CDPSessionWrapper extends CDPSession {
constructor(context: BrowsingContext, sessionId?: string) { constructor(context: BrowsingContext, sessionId?: string) {
super(); super();
this.#context = context; this.#context = context;
if (!this.#context.supportsCDP()) {
return;
}
if (sessionId) { if (sessionId) {
this.#sessionId.resolve(sessionId); this.#sessionId.resolve(sessionId);
cdpSessions.set(sessionId, this); cdpSessions.set(sessionId, this);
@ -77,6 +80,11 @@ export class CDPSessionWrapper extends CDPSession {
method: T, method: T,
...paramArgs: ProtocolMapping.Commands[T]['paramsType'] ...paramArgs: ProtocolMapping.Commands[T]['paramsType']
): Promise<ProtocolMapping.Commands[T]['returnType']> { ): Promise<ProtocolMapping.Commands[T]['returnType']> {
if (!this.#context.supportsCDP()) {
throw new Error(
'CDP support is required for this feature. The current browser does not support CDP.'
);
}
if (this.#detached) { if (this.#detached) {
throw new TargetCloseError( throw new TargetCloseError(
`Protocol error (${method}): Session closed. Most likely the page has been closed.` `Protocol error (${method}): Session closed. Most likely the page has been closed.`
@ -93,9 +101,11 @@ export class CDPSessionWrapper extends CDPSession {
override async detach(): Promise<void> { override async detach(): Promise<void> {
cdpSessions.delete(this.id()); cdpSessions.delete(this.id());
if (this.#context.supportsCDP()) {
await this.#context.cdpSession.send('Target.detachFromTarget', { await this.#context.cdpSession.send('Target.detachFromTarget', {
sessionId: this.id(), sessionId: this.id(),
}); });
}
this.#detached = true; this.#detached = true;
} }
@ -130,19 +140,29 @@ export class BrowsingContext extends Realm {
#url: string; #url: string;
#cdpSession: CDPSession; #cdpSession: CDPSession;
#parent?: string | null; #parent?: string | null;
#browserName = '';
constructor(connection: Connection, info: Bidi.BrowsingContext.Info) { constructor(
connection: Connection,
info: Bidi.BrowsingContext.Info,
browserName: string
) {
super(connection, info.context); super(connection, info.context);
this.connection = connection; this.connection = connection;
this.#id = info.context; this.#id = info.context;
this.#url = info.url; this.#url = info.url;
this.#parent = info.parent; this.#parent = info.parent;
this.#cdpSession = new CDPSessionWrapper(this); this.#browserName = browserName;
this.#cdpSession = new CDPSessionWrapper(this, undefined);
this.on('browsingContext.domContentLoaded', this.#updateUrl.bind(this)); this.on('browsingContext.domContentLoaded', this.#updateUrl.bind(this));
this.on('browsingContext.load', this.#updateUrl.bind(this)); this.on('browsingContext.load', this.#updateUrl.bind(this));
} }
supportsCDP(): boolean {
return !this.#browserName.toLowerCase().includes('firefox');
}
#updateUrl(info: Bidi.BrowsingContext.NavigationInfo) { #updateUrl(info: Bidi.BrowsingContext.NavigationInfo) {
this.url = info.url; this.url = info.url;
} }