chore: align crServiceWorker with crPage (#30367)

Simplify network-related methods because crNetworkManager already
handles initial/non-initial calls.
This commit is contained in:
Dmitry Gozman 2024-04-15 11:09:38 -07:00 committed by GitHub
parent 950e2af44c
commit a6a44a07a7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 17 additions and 33 deletions

View File

@ -459,7 +459,7 @@ export class CRBrowserContext extends BrowserContext {
for (const page of this.pages()) for (const page of this.pages())
await (page._delegate as CRPage).updateExtraHTTPHeaders(); await (page._delegate as CRPage).updateExtraHTTPHeaders();
for (const sw of this.serviceWorkers()) for (const sw of this.serviceWorkers())
await (sw as CRServiceWorker).updateExtraHTTPHeaders(false); await (sw as CRServiceWorker).updateExtraHTTPHeaders();
} }
async setUserAgent(userAgent: string | undefined): Promise<void> { async setUserAgent(userAgent: string | undefined): Promise<void> {
@ -474,7 +474,7 @@ export class CRBrowserContext extends BrowserContext {
for (const page of this.pages()) for (const page of this.pages())
await (page._delegate as CRPage).updateOffline(); await (page._delegate as CRPage).updateOffline();
for (const sw of this.serviceWorkers()) for (const sw of this.serviceWorkers())
await (sw as CRServiceWorker).updateOffline(false); await (sw as CRServiceWorker).updateOffline();
} }
async doSetHTTPCredentials(httpCredentials?: types.Credentials): Promise<void> { async doSetHTTPCredentials(httpCredentials?: types.Credentials): Promise<void> {
@ -482,7 +482,7 @@ export class CRBrowserContext extends BrowserContext {
for (const page of this.pages()) for (const page of this.pages())
await (page._delegate as CRPage).updateHttpCredentials(); await (page._delegate as CRPage).updateHttpCredentials();
for (const sw of this.serviceWorkers()) for (const sw of this.serviceWorkers())
await (sw as CRServiceWorker).updateHttpCredentials(false); await (sw as CRServiceWorker).updateHttpCredentials();
} }
async doAddInitScript(source: string) { async doAddInitScript(source: string) {

View File

@ -16,18 +16,15 @@
import { Worker } from '../page'; import { Worker } from '../page';
import type { CRBrowserContext } from './crBrowser'; import type { CRBrowserContext } from './crBrowser';
import type { CRSession } from './crConnection'; import type { CRSession } from './crConnection';
import type * as types from '../types';
import { CRExecutionContext } from './crExecutionContext'; import { CRExecutionContext } from './crExecutionContext';
import { CRNetworkManager } from './crNetworkManager'; import { CRNetworkManager } from './crNetworkManager';
import * as network from '../network'; import * as network from '../network';
import { BrowserContext } from '../browserContext'; import { BrowserContext } from '../browserContext';
import { headersArrayToObject } from '../../utils';
export class CRServiceWorker extends Worker { export class CRServiceWorker extends Worker {
readonly _browserContext: CRBrowserContext; readonly _browserContext: CRBrowserContext;
readonly _networkManager?: CRNetworkManager; readonly _networkManager?: CRNetworkManager;
private _session: CRSession; private _session: CRSession;
private _extraHTTPHeaders: types.HeadersArray | null = null;
constructor(browserContext: CRBrowserContext, session: CRSession, url: string) { constructor(browserContext: CRBrowserContext, session: CRSession, url: string) {
super(browserContext, url); super(browserContext, url);
@ -40,11 +37,11 @@ export class CRServiceWorker extends Worker {
}); });
if (this._networkManager && this._isNetworkInspectionEnabled()) { if (this._networkManager && this._isNetworkInspectionEnabled()) {
this._networkManager.addSession(session, undefined, true /* isMain */).catch(() => {});
this.updateRequestInterception(); this.updateRequestInterception();
this.updateExtraHTTPHeaders(true); this.updateExtraHTTPHeaders();
this.updateHttpCredentials(true); this.updateHttpCredentials();
this.updateOffline(true); this.updateOffline();
this._networkManager.addSession(session, undefined, true /* isMain */).catch(() => {});
} }
session.send('Runtime.enable', {}).catch(e => { }); session.send('Runtime.enable', {}).catch(e => { });
@ -61,41 +58,28 @@ export class CRServiceWorker extends Worker {
super.didClose(); super.didClose();
} }
async updateOffline(initial: boolean): Promise<void> { async updateOffline(): Promise<void> {
if (!this._isNetworkInspectionEnabled()) if (!this._isNetworkInspectionEnabled())
return; return;
await this._networkManager?.setOffline(!!this._browserContext._options.offline).catch(() => {});
const offline = !!this._browserContext._options.offline;
if (!initial || offline)
await this._networkManager?.setOffline(offline);
} }
async updateHttpCredentials(initial: boolean): Promise<void> { async updateHttpCredentials(): Promise<void> {
if (!this._isNetworkInspectionEnabled()) if (!this._isNetworkInspectionEnabled())
return; return;
await this._networkManager?.authenticate(this._browserContext._options.httpCredentials || null).catch(() => {});
const credentials = this._browserContext._options.httpCredentials || null;
if (!initial || credentials)
await this._networkManager?.authenticate(credentials);
} }
async updateExtraHTTPHeaders(initial: boolean): Promise<void> { async updateExtraHTTPHeaders(): Promise<void> {
if (!this._isNetworkInspectionEnabled()) if (!this._isNetworkInspectionEnabled())
return; return;
await this._networkManager?.setExtraHTTPHeaders(this._browserContext._options.extraHTTPHeaders || []).catch(() => {});
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 */) });
} }
updateRequestInterception(): Promise<void> { async updateRequestInterception(): Promise<void> {
if (!this._networkManager || !this._isNetworkInspectionEnabled()) if (!this._isNetworkInspectionEnabled())
return Promise.resolve(); return;
await this._networkManager?.setRequestInterception(this.needsRequestInterception()).catch(() => {});
return this._networkManager.setRequestInterception(this.needsRequestInterception()).catch(e => { });
} }
needsRequestInterception(): boolean { needsRequestInterception(): boolean {