Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: do not emit initial navigation events #2796

Merged
merged 7 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 97 additions & 45 deletions src/bidiMapper/modules/context/BrowsingContextImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {assert} from '../../../utils/assert.js';
import {Deferred} from '../../../utils/Deferred.js';
import {type LoggerFn, LogType} from '../../../utils/log.js';
import {inchesFromCm} from '../../../utils/unitConversions.js';
import {urlMatchesAboutBlank} from '../../../utils/UrlHelpers.js';
import {uuidv4} from '../../../utils/uuid.js';
import type {CdpTarget} from '../cdp/CdpTarget.js';
import type {Realm} from '../script/Realm.js';
Expand Down Expand Up @@ -98,6 +99,11 @@ export class BrowsingContextImpl {
// Set if there is a pending navigation initiated by `BrowsingContext.navigate` command.
// The promise is resolved when the navigation is finished or rejected when canceled.
#pendingCommandNavigation: Deferred<void> | undefined;
// Flags if the initial navigation to `about:blank` is in progress.
#initialNavigation = true;
// Flags if the navigation is initiated by `browsingContext.navigate` or
// `browsingContext.reload` command.
#navigationInitiatedByCommand = false;

#originalOpener?: string;

Expand Down Expand Up @@ -408,7 +414,6 @@ export class BrowsingContextImpl {
onTargetInfoChanged(params: Protocol.Target.TargetInfoChangedEvent) {
this.#url = params.targetInfo.url;
}

#initListeners() {
this.#cdpTarget.cdpClient.on('Page.frameNavigated', (params) => {
if (this.id !== params.frame.id) {
Expand Down Expand Up @@ -468,27 +473,34 @@ export class BrowsingContextImpl {
if (this.id !== params.frameId) {
return;
}
// Use `pendingNavigationId` if navigation initiated by BiDi
// `BrowsingContext.navigate` or generate a new navigation id.
this.#navigationId = this.#pendingNavigationId ?? uuidv4();
this.#pendingNavigationId = undefined;
this.#eventManager.registerEvent(
{
type: 'event',
method: ChromiumBidi.BrowsingContext.EventNames.NavigationStarted,
params: {
context: this.id,
navigation: this.#navigationId,
timestamp: BrowsingContextImpl.getTimestamp(),
// The URL of the navigation that is currently in progress. Although the URL
// is not yet known in case of user-initiated navigations, it is possible to
// provide the URL in case of BiDi-initiated navigations.
// TODO: provide proper URL in case of user-initiated navigations.
url: this.#pendingNavigationUrl ?? 'UNKNOWN',

if (this.#navigationInitiatedByCommand) {
// In case of the navigation is initiated by `browsingContext.navigate` or
// `browsingContext.reload` commands, the `Page.frameRequestedNavigation` is not
// emitted, which means the `NavigationStarted` is not emitted.
// TODO: consider emit it right after the CDP command `navigate` or `reload` is finished.

// The URL of the navigation that is currently in progress. Although the URL
// is not yet known in case of user-initiated navigations, it is possible to
// provide the URL in case of BiDi-initiated navigations.
// TODO: provide proper URL in case of user-initiated navigations.
const url = this.#pendingNavigationUrl ?? 'UNKNOWN';
this.#navigationId = this.#pendingNavigationId ?? uuidv4();
this.#pendingNavigationId = undefined;
this.#eventManager.registerEvent(
{
type: 'event',
method: ChromiumBidi.BrowsingContext.EventNames.NavigationStarted,
params: {
context: this.id,
navigation: this.#navigationId,
timestamp: BrowsingContextImpl.getTimestamp(),
url,
},
},
},
this.id,
);
this.id,
);
}
});

// TODO: don't use deprecated `Page.frameScheduledNavigation` event.
Expand Down Expand Up @@ -523,7 +535,34 @@ export class BrowsingContextImpl {
new UnknownErrorException('navigation aborted'),
);
this.#pendingCommandNavigation = undefined;
this.#navigationInitiatedByCommand = false;
}
if (!urlMatchesAboutBlank(params.url)) {
// If the url does not match about:blank, do not consider it is an initial
// navigation and emit all the required events.
// https://github.com/GoogleChromeLabs/chromium-bidi/issues/2793.
this.#initialNavigation = false;
}

if (!this.#initialNavigation) {
// Do not emit the event for the initial navigation to `about:blank`.
this.#navigationId = this.#pendingNavigationId ?? uuidv4();
this.#pendingNavigationId = undefined;
this.#eventManager.registerEvent(
{
type: 'event',
method: ChromiumBidi.BrowsingContext.EventNames.NavigationStarted,
params: {
context: this.id,
navigation: this.#navigationId,
timestamp: BrowsingContextImpl.getTimestamp(),
url: params.url,
},
},
this.id,
);
}

this.#pendingNavigationUrl = params.url;
});

Expand Down Expand Up @@ -558,36 +597,45 @@ export class BrowsingContextImpl {

switch (params.name) {
case 'DOMContentLoaded':
this.#eventManager.registerEvent(
{
type: 'event',
method: ChromiumBidi.BrowsingContext.EventNames.DomContentLoaded,
params: {
context: this.id,
navigation: this.#navigationId,
timestamp,
url: this.#url,
if (!this.#initialNavigation) {
// Do not emit for the initial navigation.
this.#eventManager.registerEvent(
{
type: 'event',
method:
ChromiumBidi.BrowsingContext.EventNames.DomContentLoaded,
params: {
context: this.id,
navigation: this.#navigationId,
timestamp,
url: this.#url,
},
},
},
this.id,
);
this.id,
);
}
this.#lifecycle.DOMContentLoaded.resolve();
break;

case 'load':
this.#eventManager.registerEvent(
{
type: 'event',
method: ChromiumBidi.BrowsingContext.EventNames.Load,
params: {
context: this.id,
navigation: this.#navigationId,
timestamp,
url: this.#url,
if (!this.#initialNavigation) {
// Do not emit for the initial navigation.
this.#eventManager.registerEvent(
{
type: 'event',
method: ChromiumBidi.BrowsingContext.EventNames.Load,
params: {
context: this.id,
navigation: this.#navigationId,
timestamp,
url: this.#url,
},
},
},
this.id,
);
this.id,
);
}
// The initial navigation is finished.
this.#initialNavigation = false;
this.#lifecycle.load.resolve();
break;
}
Expand Down Expand Up @@ -884,6 +932,7 @@ export class BrowsingContextImpl {
const navigationId = uuidv4();
this.#pendingNavigationId = navigationId;
this.#pendingCommandNavigation = new Deferred<void>();
this.#navigationInitiatedByCommand = true;

// Navigate and wait for the result. If the navigation fails, the error event is
// emitted and the promise is rejected.
Expand Down Expand Up @@ -949,6 +998,7 @@ export class BrowsingContextImpl {

// `#pendingCommandNavigation` can be already rejected and set to undefined.
this.#pendingCommandNavigation?.resolve();
this.#navigationInitiatedByCommand = false;
this.#pendingCommandNavigation = undefined;
return {
navigation: navigationId,
Expand Down Expand Up @@ -986,6 +1036,8 @@ export class BrowsingContextImpl {

this.#resetLifecycleIfFinished();

this.#navigationInitiatedByCommand = true;

await this.#cdpTarget.cdpClient.sendCommand('Page.reload', {
ignoreCache,
});
Expand Down
89 changes: 89 additions & 0 deletions src/utils/UrlHelpers.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/**
* 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.
*/

import {expect} from 'chai';

import {urlMatchesAboutBlank} from './UrlHelpers.js';

describe('BrowsingContextStorage', () => {
describe('urlMatchesAboutBlank', () => {
describe('should return true for matching urls', () => {
it('"about:blank"', () => {
expect(urlMatchesAboutBlank('about:blank')).to.be.true;
});

it('"about:blank?foo=bar"', () => {
expect(urlMatchesAboutBlank('about:blank?foo=bar')).to.be.true;
});

it('"about:blank#foo"', () => {
expect(urlMatchesAboutBlank('about:blank#foo')).to.be.true;
});

it('"about:Blank"', () => {
expect(urlMatchesAboutBlank('about:Blank')).to.be.true;
});

it('empty string', () => {
expect(urlMatchesAboutBlank('')).to.be.true;
});
});

describe('should return false for not matching urls', () => {
it('"http://example.com"', () => {
expect(urlMatchesAboutBlank('http://example.com')).to.be.false;
});

it('"about:blanka"', () => {
expect(urlMatchesAboutBlank('about:blanka')).to.be.false;
});

it('"about:blank/foo"', () => {
expect(urlMatchesAboutBlank('about:blank/foo')).to.be.false;
});

it('"about://blank"', () => {
expect(urlMatchesAboutBlank('about://blank')).to.be.false;
});

it('"about: blank"', () => {
expect(urlMatchesAboutBlank('about: blank')).to.be.false;
});

it('username and password', () => {
expect(urlMatchesAboutBlank('about:username:password@blank')).to.be
.false;
});

it('null', () => {
expect(urlMatchesAboutBlank(null as any)).to.be.false;
});

it('undefined', () => {
expect(urlMatchesAboutBlank(undefined as any)).to.be.false;
});

it('"blob:http://example.com"', () => {
expect(urlMatchesAboutBlank('blob:http://example.com')).to.be.false;
});

it('"about:srcdoc"', () => {
expect(urlMatchesAboutBlank('about:srcdoc')).to.be.false;
});
});
});
});
51 changes: 51 additions & 0 deletions src/utils/UrlHelpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* 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.
*
*/

/**
* A URL matches about:blank if its scheme is "about", its path contains a single string
* "blank", its username and password are the empty string, and its host is null.
* https://html.spec.whatwg.org/multipage/urls-and-fetching.html#matches-about:blank
* @param {string} url
* @return {boolean}
*/
export function urlMatchesAboutBlank(url: string): boolean {
// An empty string is a special case, and considered to be about:blank.
// https://html.spec.whatwg.org/multipage/nav-history-apis.html#window-open-steps
if (url === '') {
return true;
}

try {
const parsedUrl = new URL(url);
const schema = parsedUrl.protocol.replace(/:$/, '');
return (
schema.toLowerCase() === 'about' &&
parsedUrl.pathname.toLowerCase() === 'blank' &&
parsedUrl.username === '' &&
parsedUrl.password === '' &&
parsedUrl.host === ''
);
} catch (err) {
// Wrong URL considered do not match about:blank.
if (err instanceof TypeError) {
return false;
}
// Re-throw other unexpected errors.
throw err;
}
}
Loading
Loading