Skip to content

Commit

Permalink
feat: implement browser.getClientWindows
Browse files Browse the repository at this point in the history
  • Loading branch information
sadym-chromium committed Nov 15, 2024
1 parent 72b06ae commit b31dcb0
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 5 deletions.
9 changes: 5 additions & 4 deletions src/bidiMapper/CommandProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,10 @@ export class CommandProcessor extends EventEmitter<CommandProcessorEventsMap> {
this.#bluetoothProcessor = bluetoothProcessor;

// keep-sorted start block=yes
this.#browserProcessor = new BrowserProcessor(browserCdpClient);
this.#browserProcessor = new BrowserProcessor(
browserCdpClient,
browsingContextStorage,
);
this.#browsingContextProcessor = new BrowsingContextProcessor(
browserCdpClient,
browsingContextStorage,
Expand Down Expand Up @@ -171,9 +174,7 @@ export class CommandProcessor extends EventEmitter<CommandProcessorEventsMap> {
case 'browser.createUserContext':
return await this.#browserProcessor.createUserContext(command.params);
case 'browser.getClientWindows':
throw new UnknownErrorException(
`Method ${command.method} is not implemented.`,
);
return await this.#browserProcessor.getClientWindows();
case 'browser.getUserContexts':
return await this.#browserProcessor.getUserContexts();
case 'browser.removeUserContext':
Expand Down
37 changes: 36 additions & 1 deletion src/bidiMapper/modules/browser/BrowserProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,18 @@ import {
NoSuchUserContextException,
} from '../../../protocol/protocol.js';
import type {CdpClient} from '../../BidiMapper.js';
import type {BrowsingContextStorage} from '../context/BrowsingContextStorage';

export class BrowserProcessor {
readonly #browserCdpClient: CdpClient;
readonly #browsingContextStorage: BrowsingContextStorage;

constructor(browserCdpClient: CdpClient) {
constructor(
browserCdpClient: CdpClient,
browsingContextStorage: BrowsingContextStorage,
) {
this.#browserCdpClient = browserCdpClient;
this.#browsingContextStorage = browsingContextStorage;
}

close(): EmptyResult {
Expand Down Expand Up @@ -100,4 +106,33 @@ export class BrowserProcessor {
],
};
}

async #getWindowInfo(targetId: string): Promise<Browser.ClientWindowInfo> {
const windowInfo = await this.#browserCdpClient.sendCommand(
'Browser.getWindowForTarget',
{targetId},
);
return {
active: false,
clientWindow: `${windowInfo.windowId}`,
state: windowInfo.bounds.windowState ?? 'normal',
height: windowInfo.bounds.height ?? 0,
width: windowInfo.bounds.width ?? 0,
x: windowInfo.bounds.left ?? 0,
y: windowInfo.bounds.top ?? 0,
};
}

async getClientWindows(): Promise<Browser.GetClientWindowsResult> {
const topLevelTargetIds = this.#browsingContextStorage
.getTopLevelContexts()
.map((b) => b.cdpTarget.id);

const clientWindows = await Promise.all(
topLevelTargetIds.map(
async (targetId) => await this.#getWindowInfo(targetId),
),
);
return {clientWindows};
}
}
40 changes: 40 additions & 0 deletions tests/browser/test_get_client_windows.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# 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 pytest
from anys import ANY_NUMBER, ANY_STR
from test_helpers import execute_command


@pytest.mark.asyncio
async def test_browser_get_client_windows(websocket, context_id):
resp = await execute_command(websocket, {
"method": "browser.getClientWindows",
"params": {}
})
assert resp == {
'clientWindows': [
{
# Not implemented yet
'active': False,
'clientWindow': ANY_STR,
'height': ANY_NUMBER,
'state': 'normal',
'width': ANY_NUMBER,
'x': ANY_NUMBER,
'y': ANY_NUMBER,
},
],
}

0 comments on commit b31dcb0

Please sign in to comment.