From a6a44a07a7e4ed8ee385c8a8042d9b3ba1000ff7 Mon Sep 17 00:00:00 2001 From: Dmitry Gozman Date: Mon, 15 Apr 2024 11:09:38 -0700 Subject: [PATCH] chore: align crServiceWorker with crPage (#30367) Simplify network-related methods because crNetworkManager already handles initial/non-initial calls. --- .../src/server/chromium/crBrowser.ts | 6 +-- .../src/server/chromium/crServiceWorker.ts | 44 ++++++------------- 2 files changed, 17 insertions(+), 33 deletions(-) diff --git a/packages/playwright-core/src/server/chromium/crBrowser.ts b/packages/playwright-core/src/server/chromium/crBrowser.ts index a714da4883..530906c0b2 100644 --- a/packages/playwright-core/src/server/chromium/crBrowser.ts +++ b/packages/playwright-core/src/server/chromium/crBrowser.ts @@ -459,7 +459,7 @@ export class CRBrowserContext extends BrowserContext { for (const page of this.pages()) await (page._delegate as CRPage).updateExtraHTTPHeaders(); for (const sw of this.serviceWorkers()) - await (sw as CRServiceWorker).updateExtraHTTPHeaders(false); + await (sw as CRServiceWorker).updateExtraHTTPHeaders(); } async setUserAgent(userAgent: string | undefined): Promise { @@ -474,7 +474,7 @@ export class CRBrowserContext extends BrowserContext { for (const page of this.pages()) await (page._delegate as CRPage).updateOffline(); for (const sw of this.serviceWorkers()) - await (sw as CRServiceWorker).updateOffline(false); + await (sw as CRServiceWorker).updateOffline(); } async doSetHTTPCredentials(httpCredentials?: types.Credentials): Promise { @@ -482,7 +482,7 @@ export class CRBrowserContext extends BrowserContext { for (const page of this.pages()) await (page._delegate as CRPage).updateHttpCredentials(); for (const sw of this.serviceWorkers()) - await (sw as CRServiceWorker).updateHttpCredentials(false); + await (sw as CRServiceWorker).updateHttpCredentials(); } async doAddInitScript(source: string) { diff --git a/packages/playwright-core/src/server/chromium/crServiceWorker.ts b/packages/playwright-core/src/server/chromium/crServiceWorker.ts index 4de63fac55..730504866b 100644 --- a/packages/playwright-core/src/server/chromium/crServiceWorker.ts +++ b/packages/playwright-core/src/server/chromium/crServiceWorker.ts @@ -16,18 +16,15 @@ import { Worker } from '../page'; import type { CRBrowserContext } from './crBrowser'; import type { CRSession } from './crConnection'; -import type * as types from '../types'; import { CRExecutionContext } from './crExecutionContext'; import { CRNetworkManager } from './crNetworkManager'; import * as network from '../network'; import { BrowserContext } from '../browserContext'; -import { headersArrayToObject } from '../../utils'; export class CRServiceWorker extends Worker { readonly _browserContext: CRBrowserContext; readonly _networkManager?: CRNetworkManager; private _session: CRSession; - private _extraHTTPHeaders: types.HeadersArray | null = null; constructor(browserContext: CRBrowserContext, session: CRSession, url: string) { super(browserContext, url); @@ -40,11 +37,11 @@ export class CRServiceWorker extends Worker { }); if (this._networkManager && this._isNetworkInspectionEnabled()) { - this._networkManager.addSession(session, undefined, true /* isMain */).catch(() => {}); this.updateRequestInterception(); - this.updateExtraHTTPHeaders(true); - this.updateHttpCredentials(true); - this.updateOffline(true); + this.updateExtraHTTPHeaders(); + this.updateHttpCredentials(); + this.updateOffline(); + this._networkManager.addSession(session, undefined, true /* isMain */).catch(() => {}); } session.send('Runtime.enable', {}).catch(e => { }); @@ -61,41 +58,28 @@ export class CRServiceWorker extends Worker { super.didClose(); } - async updateOffline(initial: boolean): Promise { + async updateOffline(): Promise { if (!this._isNetworkInspectionEnabled()) return; - - const offline = !!this._browserContext._options.offline; - if (!initial || offline) - await this._networkManager?.setOffline(offline); + await this._networkManager?.setOffline(!!this._browserContext._options.offline).catch(() => {}); } - async updateHttpCredentials(initial: boolean): Promise { + async updateHttpCredentials(): Promise { if (!this._isNetworkInspectionEnabled()) return; - - const credentials = this._browserContext._options.httpCredentials || null; - if (!initial || credentials) - await this._networkManager?.authenticate(credentials); + await this._networkManager?.authenticate(this._browserContext._options.httpCredentials || null).catch(() => {}); } - async updateExtraHTTPHeaders(initial: boolean): Promise { + async updateExtraHTTPHeaders(): Promise { if (!this._isNetworkInspectionEnabled()) return; - - const headers = network.mergeHeaders([ - this._browserContext._options.extraHTTPHeaders, - this._extraHTTPHeaders, - ]); - if (!initial || headers.length) - await this._session.send('Network.setExtraHTTPHeaders', { headers: headersArrayToObject(headers, false /* lowerCase */) }); + await this._networkManager?.setExtraHTTPHeaders(this._browserContext._options.extraHTTPHeaders || []).catch(() => {}); } - updateRequestInterception(): Promise { - if (!this._networkManager || !this._isNetworkInspectionEnabled()) - return Promise.resolve(); - - return this._networkManager.setRequestInterception(this.needsRequestInterception()).catch(e => { }); + async updateRequestInterception(): Promise { + if (!this._isNetworkInspectionEnabled()) + return; + await this._networkManager?.setRequestInterception(this.needsRequestInterception()).catch(() => {}); } needsRequestInterception(): boolean {