@@ -24,6 +24,7 @@ import {
2424 type ChromiumBidi ,
2525 type Emulation ,
2626 Session ,
27+ UnknownErrorException ,
2728 UnsupportedOperationException ,
2829} from '../../../protocol/protocol.js' ;
2930import { Deferred } from '../../../utils/Deferred.js' ;
@@ -636,13 +637,11 @@ export class CdpTarget {
636637 }
637638
638639 if (
639- this . #userContextConfig. emulatedGeolocation !== undefined &&
640- this . #userContextConfig. emulatedGeolocation !== null
640+ this . #userContextConfig. geolocation !== undefined &&
641+ this . #userContextConfig. geolocation !== null
641642 ) {
642643 promises . push (
643- this . setGeolocationOverride (
644- this . #userContextConfig. emulatedGeolocation ,
645- ) ,
644+ this . setGeolocationOverride ( this . #userContextConfig. geolocation ) ,
646645 ) ;
647646 }
648647
@@ -672,21 +671,38 @@ export class CdpTarget {
672671 }
673672
674673 async setGeolocationOverride (
675- coordinates : Emulation . GeolocationCoordinates | null ,
674+ geolocation :
675+ | Emulation . GeolocationCoordinates
676+ | Emulation . GeolocationPositionError
677+ | null ,
676678 ) : Promise < void > {
677- if ( coordinates === null ) {
679+ if ( geolocation === null ) {
678680 await this . cdpClient . sendCommand ( 'Emulation.clearGeolocationOverride' ) ;
679- } else {
681+ } else if ( 'type' in geolocation ) {
682+ if ( geolocation . type !== 'positionUnavailable' ) {
683+ // Unreachable. Handled by params parser.
684+ throw new UnknownErrorException (
685+ `Unknown geolocation error ${ geolocation . type } ` ,
686+ ) ;
687+ }
688+ // Omitting latitude, longitude or accuracy emulates position unavailable.
689+ await this . cdpClient . sendCommand ( 'Emulation.setGeolocationOverride' , { } ) ;
690+ } else if ( 'latitude' in geolocation ) {
680691 await this . cdpClient . sendCommand ( 'Emulation.setGeolocationOverride' , {
681- latitude : coordinates . latitude ,
682- longitude : coordinates . longitude ,
683- accuracy : coordinates . accuracy ?? 1 ,
692+ latitude : geolocation . latitude ,
693+ longitude : geolocation . longitude ,
694+ accuracy : geolocation . accuracy ?? 1 ,
684695 // `null` value is treated as "missing".
685- altitude : coordinates . altitude ?? undefined ,
686- altitudeAccuracy : coordinates . altitudeAccuracy ?? undefined ,
687- heading : coordinates . heading ?? undefined ,
688- speed : coordinates . speed ?? undefined ,
696+ altitude : geolocation . altitude ?? undefined ,
697+ altitudeAccuracy : geolocation . altitudeAccuracy ?? undefined ,
698+ heading : geolocation . heading ?? undefined ,
699+ speed : geolocation . speed ?? undefined ,
689700 } ) ;
701+ } else {
702+ // Unreachable. Handled by params parser.
703+ throw new UnknownErrorException (
704+ 'Unexpected geolocation coordinates value' ,
705+ ) ;
690706 }
691707 }
692708}
0 commit comments