Skip to content

Commit c3ffde0

Browse files
feat: emulation.setScreenSettingsOverride (#3943)
1 parent 95f1aea commit c3ffde0

File tree

13 files changed

+379
-2
lines changed

13 files changed

+379
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@ coverage/
2323
wpt-metadata/mapper/headful/webdriver/tests/bidi/
2424
*.tsbuildinfo
2525
**.code-workspace
26+
.agent/

src/bidiMapper/BidiNoOpParser.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,11 @@ export class BidiNoOpParser implements BidiCommandParameterParser {
211211
): Emulation.SetScreenOrientationOverrideParameters {
212212
return params as Emulation.SetScreenOrientationOverrideParameters;
213213
}
214+
parseSetScreenSettingsOverrideParams(
215+
params: unknown,
216+
): Emulation.SetScreenSettingsOverrideParameters {
217+
return params as Emulation.SetScreenSettingsOverrideParameters;
218+
}
214219
parseSetScriptingEnabledParams(
215220
params: unknown,
216221
): Emulation.SetScriptingEnabledParameters {

src/bidiMapper/BidiParser.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ export interface BidiCommandParameterParser {
137137
parseSetScreenOrientationOverrideParams(
138138
params: unknown,
139139
): Emulation.SetScreenOrientationOverrideParameters;
140+
parseSetScreenSettingsOverrideParams(
141+
params: unknown,
142+
): Emulation.SetScreenSettingsOverrideParameters;
140143
parseSetScriptingEnabledParams(
141144
params: unknown,
142145
): Emulation.SetScriptingEnabledParameters;

src/bidiMapper/CommandProcessor.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,10 @@ export class CommandProcessor extends EventEmitter<CommandProcessorEventsMap> {
346346
return await this.#emulationProcessor.setScreenOrientationOverride(
347347
this.#parser.parseSetScreenOrientationOverrideParams(command.params),
348348
);
349+
case 'emulation.setScreenSettingsOverride':
350+
return await this.#emulationProcessor.setScreenSettingsOverride(
351+
this.#parser.parseSetScreenSettingsOverrideParams(command.params),
352+
);
349353
case 'emulation.setScriptingEnabled':
350354
return await this.#emulationProcessor.setScriptingEnabled(
351355
this.#parser.parseSetScriptingEnabledParams(command.params),

src/bidiMapper/modules/browser/ContextConfig.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export class ContextConfig {
4444
| null;
4545
locale?: string | null;
4646
prerenderingDisabled?: boolean;
47+
screenArea?: Emulation.ScreenArea | null;
4748
screenOrientation?: Emulation.ScreenOrientation | null;
4849
scriptingEnabled?: false | null;
4950
// Timezone is kept in CDP format with GMT prefix for offset values.

src/bidiMapper/modules/cdp/CdpTarget.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -588,11 +588,13 @@ export class CdpTarget {
588588
viewport: BrowsingContext.Viewport | null,
589589
devicePixelRatio: number | null,
590590
screenOrientation: Emulation.ScreenOrientation | null,
591+
screenArea: Emulation.ScreenArea | null,
591592
) {
592593
if (
593594
viewport === null &&
594595
devicePixelRatio === null &&
595-
screenOrientation === null
596+
screenOrientation === null &&
597+
screenArea === null
596598
) {
597599
await this.cdpClient.sendCommand('Emulation.clearDeviceMetricsOverride');
598600
return;
@@ -606,6 +608,8 @@ export class CdpTarget {
606608
screenOrientation:
607609
this.#toCdpScreenOrientationAngle(screenOrientation) ?? undefined,
608610
mobile: false,
611+
screenWidth: screenArea?.width,
612+
screenHeight: screenArea?.height,
609613
};
610614

611615
await this.cdpClient.sendCommand(
@@ -637,13 +641,15 @@ export class CdpTarget {
637641
if (
638642
config.viewport !== undefined ||
639643
config.devicePixelRatio !== undefined ||
640-
config.screenOrientation !== undefined
644+
config.screenOrientation !== undefined ||
645+
config.screenArea !== undefined
641646
) {
642647
promises.push(
643648
this.setDeviceMetricsOverride(
644649
config.viewport ?? null,
645650
config.devicePixelRatio ?? null,
646651
config.screenOrientation ?? null,
652+
config.screenArea ?? null,
647653
).catch(() => {
648654
// Ignore CDP errors, as the command is not supported by iframe targets. Generic
649655
// catch, as the error can vary between CdpClient implementations: Tab vs

src/bidiMapper/modules/context/BrowsingContextImpl.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,10 +1134,15 @@ export class BrowsingContextImpl {
11341134
screenOrientation: Emulation.ScreenOrientation | null,
11351135
) {
11361136
// Set the target's viewport.
1137+
const config = this.#configStorage.getActiveConfig(
1138+
this.id,
1139+
this.userContext,
1140+
);
11371141
await this.cdpTarget.setDeviceMetricsOverride(
11381142
viewport,
11391143
devicePixelRatio,
11401144
screenOrientation,
1145+
config.screenArea ?? null,
11411146
);
11421147
}
11431148

src/bidiMapper/modules/emulation/EmulationProcessor.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,47 @@ export class EmulationProcessor {
247247
return {};
248248
}
249249

250+
async setScreenSettingsOverride(
251+
params: Emulation.SetScreenSettingsOverrideParameters,
252+
): Promise<EmptyResult> {
253+
const browsingContexts = await this.#getRelatedTopLevelBrowsingContexts(
254+
params.contexts,
255+
params.userContexts,
256+
);
257+
258+
for (const browsingContextId of params.contexts ?? []) {
259+
this.#contextConfigStorage.updateBrowsingContextConfig(
260+
browsingContextId,
261+
{
262+
screenArea: params.screenArea,
263+
},
264+
);
265+
}
266+
for (const userContextId of params.userContexts ?? []) {
267+
this.#contextConfigStorage.updateUserContextConfig(userContextId, {
268+
screenArea: params.screenArea,
269+
});
270+
}
271+
272+
await Promise.all(
273+
browsingContexts.map(async (context) => {
274+
// Actual value can be different from the one in params, e.g. in case of already
275+
// existing more granular setting.
276+
const config = this.#contextConfigStorage.getActiveConfig(
277+
context.id,
278+
context.userContext,
279+
);
280+
281+
await context.setViewport(
282+
config.viewport ?? null,
283+
config.devicePixelRatio ?? null,
284+
config.screenOrientation ?? null,
285+
);
286+
}),
287+
);
288+
return {};
289+
}
290+
250291
/**
251292
* Returns a list of top-level browsing contexts.
252293
*/

src/bidiTab/BidiParser.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,11 @@ export class BidiParser implements BidiCommandParameterParser {
212212
): Emulation.SetScreenOrientationOverrideParameters {
213213
return Parser.Emulation.parseSetScreenOrientationOverrideParams(params);
214214
}
215+
parseSetScreenSettingsOverrideParams(
216+
params: unknown,
217+
): Emulation.SetScreenSettingsOverrideParameters {
218+
return Parser.Emulation.parseSetScreenSettingsOverrideParams(params);
219+
}
215220
parseSetScriptingEnabledParams(
216221
params: unknown,
217222
): Emulation.SetScriptingEnabledParameters {

src/protocol-parser/generated/webdriver-bidi.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1307,6 +1307,7 @@ export const EmulationCommandSchema = z.lazy(() =>
13071307
Emulation.SetLocaleOverrideSchema,
13081308
Emulation.SetNetworkConditionsSchema,
13091309
Emulation.SetScreenOrientationOverrideSchema,
1310+
Emulation.SetScreenSettingsOverrideSchema,
13101311
Emulation.SetScriptingEnabledSchema,
13111312
Emulation.SetTimezoneOverrideSchema,
13121313
Emulation.SetUserAgentOverrideSchema,

0 commit comments

Comments
 (0)