Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/submodules/wpt-d484471
Browse files Browse the repository at this point in the history
  • Loading branch information
sadym-chromium authored Dec 17, 2024
2 parents 66cf784 + 960531f commit d7c4d1d
Show file tree
Hide file tree
Showing 30 changed files with 2,318 additions and 729 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/labeler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
permissions:
pull-requests: write
steps:
- run: gh pr edit "$NUMBER" --add-label "puppeteer"
- run: gh pr edit "$NUMBER" --add-label "puppeteer" --add-label "puppeteer-required"
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GH_REPO: ${{ github.repository }}
Expand Down
35 changes: 27 additions & 8 deletions .github/workflows/puppeteer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,33 @@ concurrency:

jobs:
puppeteer-test:
name: Run Puppeteer tests
name: Run Puppeteer tests (${{ matrix.shard }})
runs-on: ubuntu-latest
if: contains(github.event.pull_request.labels.*.name, 'puppeteer')
strategy:
fail-fast: false
matrix:
shard:
- 1-4
- 2-4
- 3-4
- 4-4
steps:
- name: Checkout Chromium-BiDi
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
with:
path: 'bidi'
- name: Checkout
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
with:
repository: 'puppeteer/puppeteer'
path: 'puppeteer'
- name: Set up Node.js
uses: actions/setup-node@39370e3970a6d050c480ffad4ff0ed4d3fdee5af # v4.1.0
with:
node-version-file: './bidi/.nvmrc'
cache-dependency-path: 'puppeteer/package-lock.json,bidi/package-lock.json'
- uses: google/wireit@83d7f8bed70b7bcfc40f4b9f54f4b7485753991b # setup-github-actions-caching/v2.0.1
- name: Install Chromium-BiDi dependencies
working-directory: bidi
run: npm ci
Expand All @@ -46,12 +61,6 @@ jobs:
- name: Link Chromium-BiDi
working-directory: bidi
run: npm link

- name: Checkout
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
with:
repository: 'puppeteer/puppeteer'
path: 'puppeteer'
- name: Install Puppeteer dependencies
working-directory: puppeteer
run: npm ci
Expand Down Expand Up @@ -80,4 +89,14 @@ jobs:
working-directory: puppeteer
env:
PUPPETEER_EXECUTABLE_PATH: ${{ steps.browser.outputs.executablePath }}
run: xvfb-run --auto-servernum npm run test:chrome:bidi
run: xvfb-run --auto-servernum npm run test:chrome:bidi -- --shard '${{ matrix.shard }}'

puppeteer-test-required:
name: '[Required] Puppeteer tests'
needs: [puppeteer-test]
runs-on: ubuntu-latest
if: ${{ always() }}
steps:
- if: ${{ needs.puppeteer-test.result != 'success' && contains(github.event.pull_request.labels.*.name, 'puppeteer-required') }}
run: 'exit 1'
- run: 'exit 0'
2 changes: 1 addition & 1 deletion .release-please-manifest.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
".": "0.10.1"
".": "0.10.2"
}
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

All notable changes to this project will be documented in this file.

## [0.10.2](https://github.com/GoogleChromeLabs/chromium-bidi/compare/chromium-bidi-v0.10.1...chromium-bidi-v0.10.2) (2024-12-11)


### Bug Fixes

* avoid extra getFrameOwner call ([#2839](https://github.com/GoogleChromeLabs/chromium-bidi/issues/2839)) ([0ff2876](https://github.com/GoogleChromeLabs/chromium-bidi/commit/0ff28760906e411635c9685cf4a6ba34fdd02183))
* implement the pattern matching according to the spec ([#2832](https://github.com/GoogleChromeLabs/chromium-bidi/issues/2832)) ([4563b2b](https://github.com/GoogleChromeLabs/chromium-bidi/commit/4563b2b1a365c2518f61fc2e2922ccd099b5c238))
* stop calling bringToFront before taking screenshots ([#2830](https://github.com/GoogleChromeLabs/chromium-bidi/issues/2830)) ([6017898](https://github.com/GoogleChromeLabs/chromium-bidi/commit/6017898ea4d06a5531ddf690bedbaa25c2147f0e))

## [0.10.1](https://github.com/GoogleChromeLabs/chromium-bidi/compare/chromium-bidi-v0.10.0...chromium-bidi-v0.10.1) (2024-11-25)


Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "chromium-bidi",
"version": "0.10.1",
"version": "0.10.2",
"description": "An implementation of the WebDriver BiDi protocol for Chromium implemented as a JavaScript layer translating between BiDi and CDP, running inside a Chrome tab.",
"scripts": {
"build": "wireit",
Expand Down
78 changes: 29 additions & 49 deletions src/bidiMapper/modules/cdp/CdpTarget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import type {CdpClient} from '../../../cdp/CdpClient.js';
import {BiDiModule} from '../../../protocol/chromium-bidi.js';
import type {ChromiumBidi, Session} from '../../../protocol/protocol.js';
import {Deferred} from '../../../utils/Deferred.js';
import {EventEmitter} from '../../../utils/EventEmitter.js';
import type {LoggerFn} from '../../../utils/log.js';
import {LogType} from '../../../utils/log.js';
import type {Result} from '../../../utils/result.js';
Expand All @@ -33,12 +34,15 @@ import type {PreloadScriptStorage} from '../script/PreloadScriptStorage.js';
import type {RealmStorage} from '../script/RealmStorage.js';
import type {EventManager} from '../session/EventManager.js';

import {type TargetEventMap, TargetEvents} from './TargetEvents.js';

interface FetchStages {
request: boolean;
response: boolean;
auth: boolean;
}
export class CdpTarget {

export class CdpTarget extends EventEmitter<TargetEventMap> {
readonly #id: Protocol.Target.TargetID;
readonly #cdpClient: CdpClient;
readonly #browserCdpClient: CdpClient;
Expand All @@ -57,7 +61,6 @@ export class CdpTarget {

#deviceAccessEnabled = false;
#cacheDisableState = false;
#networkDomainEnabled = false;
#fetchDomainStages: FetchStages = {
request: false,
response: false,
Expand Down Expand Up @@ -118,6 +121,7 @@ export class CdpTarget {
unhandledPromptBehavior?: Session.UserPromptHandler,
logger?: LoggerFn,
) {
super();
this.#id = targetId;
this.#cdpClient = cdpClient;
this.#browserCdpClient = browserCdpClient;
Expand Down Expand Up @@ -192,7 +196,11 @@ export class CdpTarget {
// prerendered pages. Generic catch, as the error can vary between CdpClient
// implementations: Tab vs Puppeteer.
}),
this.toggleNetworkIfNeeded(),
// Enabling CDP Network domain is required for navigation detection:
// https://github.com/GoogleChromeLabs/chromium-bidi/issues/2856.
this.#cdpClient
.sendCommand('Network.enable')
.then(() => this.toggleNetworkIfNeeded()),
this.#cdpClient.sendCommand('Target.setAutoAttach', {
autoAttach: true,
waitForDebuggerOnStart: true,
Expand Down Expand Up @@ -265,11 +273,9 @@ export class CdpTarget {
const stages = this.#networkStorage.getInterceptionStages(this.topLevelId);

if (
// Only toggle interception when Network is enabled
!this.#networkDomainEnabled ||
(this.#fetchDomainStages.request === stages.request &&
this.#fetchDomainStages.response === stages.response &&
this.#fetchDomainStages.auth === stages.auth)
this.#fetchDomainStages.request === stages.request &&
this.#fetchDomainStages.response === stages.response &&
this.#fetchDomainStages.auth === stages.auth
) {
return;
}
Expand Down Expand Up @@ -317,25 +323,18 @@ export class CdpTarget {
}

/**
* Toggles both Network and Fetch domains.
* Toggles CDP "Fetch" domain and enable/disable network cache.
*/
async toggleNetworkIfNeeded(): Promise<void> {
const enabled = this.isSubscribedTo(BiDiModule.Network);
if (enabled === this.#networkDomainEnabled) {
return;
}

this.#networkDomainEnabled = enabled;
// Although the Network domain remains active, Fetch domain activation and caching
// settings should be managed dynamically.
try {
await Promise.all([
this.#cdpClient
.sendCommand(enabled ? 'Network.enable' : 'Network.disable')
.then(async () => await this.toggleSetCacheDisabled()),
this.toggleSetCacheDisabled(),
this.toggleFetchIfNeeded(),
]);
} catch (err) {
this.#logger?.(LogType.debugError, err);
this.#networkDomainEnabled = !enabled;
if (!this.#isExpectedError(err)) {
throw err;
}
Expand All @@ -347,10 +346,7 @@ export class CdpTarget {
this.#networkStorage.defaultCacheBehavior === 'bypass';
const cacheDisabled = disable ?? defaultCacheDisabled;

if (
!this.#networkDomainEnabled ||
this.#cacheDisableState === cacheDisabled
) {
if (this.#cacheDisableState === cacheDisabled) {
return;
}
this.#cacheDisableState = cacheDisabled;
Expand Down Expand Up @@ -401,6 +397,15 @@ export class CdpTarget {
}

#setEventListeners() {
this.#cdpClient.on('Network.requestWillBeSent', (eventParams) => {
if (eventParams.loaderId === eventParams.requestId) {
this.emit(TargetEvents.FrameStartedNavigating, {
loaderId: eventParams.loaderId,
url: eventParams.request.url,
frameId: eventParams.frameId,
});
}
});
this.#cdpClient.on('*', (event, params) => {
// We may encounter uses for EventEmitter other than CDP events,
// which we want to skip.
Expand Down Expand Up @@ -436,17 +441,6 @@ export class CdpTarget {
});
}

async #toggleNetwork(enable: boolean): Promise<void> {
this.#networkDomainEnabled = enable;
try {
await this.#cdpClient.sendCommand(
enable ? 'Network.enable' : 'Network.disable',
);
} catch {
this.#networkDomainEnabled = !enable;
}
}

async #enableFetch(stages: FetchStages) {
const patterns: Protocol.Fetch.EnableRequest['patterns'] = [];

Expand All @@ -463,11 +457,7 @@ export class CdpTarget {
requestStage: 'Response',
});
}
if (
// Only enable interception when Network is enabled
this.#networkDomainEnabled &&
patterns.length
) {
if (patterns.length) {
const oldStages = this.#fetchDomainStages;
this.#fetchDomainStages = stages;
try {
Expand Down Expand Up @@ -503,29 +493,19 @@ export class CdpTarget {
this.#fetchDomainStages.request !== stages.request ||
this.#fetchDomainStages.response !== stages.response ||
this.#fetchDomainStages.auth !== stages.auth;
const networkEnable = this.isSubscribedTo(BiDiModule.Network);
const networkChanged = this.#networkDomainEnabled !== networkEnable;

this.#logger?.(
LogType.debugInfo,
'Toggle Network',
`Fetch (${fetchEnable}) ${fetchChanged}`,
`Network (${networkEnable}) ${networkChanged}`,
);

if (networkEnable && networkChanged) {
await this.#toggleNetwork(true);
}
if (fetchEnable && fetchChanged) {
await this.#enableFetch(stages);
}
if (!fetchEnable && fetchChanged) {
await this.#disableFetch();
}

if (!networkEnable && networkChanged && !fetchEnable && !fetchChanged) {
await this.#toggleNetwork(false);
}
}

/**
Expand Down
36 changes: 36 additions & 0 deletions src/bidiMapper/modules/cdp/TargetEvents.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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.
*
*/

/**
* `FrameStartedNavigating` event addressing lack of such an event in CDP. It is emitted
* on CdpTarget before each `Network.requestWillBeSent` event. Note that there can be
* several `Network.requestWillBeSent` events for a single navigation e.g. on redirection,
* so the `FrameStartedNavigating` can be duplicated as well.
* http://go/webdriver:detect-navigation-started#bookmark=id.64balpqrmadv
*/
export const enum TargetEvents {
FrameStartedNavigating = 'frameStartedNavigating',
}

export type TargetEventMap = {
[TargetEvents.FrameStartedNavigating]: {
loaderId: string;
url: string;
frameId: string | undefined;
};
};
Loading

0 comments on commit d7c4d1d

Please sign in to comment.