@@ -14,6 +14,8 @@ import CONFIG from '@src/CONFIG';
1414import CONST from '@src/CONST' ;
1515import IntlStore from '@src/languages/IntlStore' ;
1616import type { TranslationPaths } from '@src/languages/types' ;
17+ import type { LocationPermissionState } from '@src/libs/getCurrentPosition/locationPermission' ;
18+ import { LOCATION_PERMISSION_STATES } from '@src/libs/getCurrentPosition/locationPermission' ;
1719import type PlatformSpecificUpdater from '@src/setup/platformSetup/types' ;
1820import type { Locale } from '@src/types/onyx' ;
1921import type { CreateDownloadQueueModule , DownloadItem } from './createDownloadQueue' ;
@@ -32,11 +34,53 @@ const MAC_PERMISSION_STATUSES = {
3234 NOT_DETERMINED : 'not determined' ,
3335} as const ;
3436
35- const LOCATION_PERMISSION_STATES = {
36- GRANTED : 'granted' ,
37- DENIED : 'denied' ,
38- PROMPT : 'prompt' ,
39- } as const ;
37+ type MacPermissionsModule = {
38+ getAuthStatus ?: ( authType : AuthType ) => PermissionType | typeof MAC_PERMISSION_STATUSES . NOT_DETERMINED ;
39+ } ;
40+
41+ type MacGetAuthStatus = MacPermissionsModule [ 'getAuthStatus' ] ;
42+
43+ let macGetAuthStatusPromise : Promise < MacGetAuthStatus | undefined > | undefined ;
44+
45+ const logMacPermissionsWarning = ( message : string , error ?: unknown ) => {
46+ if ( error instanceof Error ) {
47+ log . warn ( message , error . message ) ;
48+ } else if ( typeof error === 'string' ) {
49+ log . warn ( message , error ) ;
50+ } else {
51+ log . warn ( message ) ;
52+ }
53+ } ;
54+
55+ const loadMacGetAuthStatus = async ( ) : Promise < MacGetAuthStatus | undefined > => {
56+ if ( ! macGetAuthStatusPromise ) {
57+ if ( process . platform !== 'darwin' ) {
58+ macGetAuthStatusPromise = Promise . resolve < MacGetAuthStatus | undefined > ( undefined ) ;
59+ } else {
60+ try {
61+ macGetAuthStatusPromise = Promise . resolve ( ( ( await import ( 'node-mac-permissions' ) ) as MacPermissionsModule ) . getAuthStatus ) ;
62+ } catch ( error : unknown ) {
63+ logMacPermissionsWarning ( 'node-mac-permissions not available, defaulting to denied:' , error ) ;
64+ return undefined ;
65+ }
66+ }
67+ }
68+
69+ return macGetAuthStatusPromise ;
70+ } ;
71+
72+ const resolveLocationPermissionStatus = ( status : PermissionType | typeof MAC_PERMISSION_STATUSES . NOT_DETERMINED ) : LocationPermissionState => {
73+ switch ( status ) {
74+ case MAC_PERMISSION_STATUSES . AUTHORIZED :
75+ return LOCATION_PERMISSION_STATES . GRANTED ;
76+ case MAC_PERMISSION_STATUSES . NOT_DETERMINED :
77+ return LOCATION_PERMISSION_STATES . PROMPT ;
78+ case MAC_PERMISSION_STATUSES . DENIED :
79+ case MAC_PERMISSION_STATUSES . RESTRICTED :
80+ default :
81+ return LOCATION_PERMISSION_STATES . DENIED ;
82+ }
83+ } ;
4084
4185// Setup google api key in process environment, we are setting it this way intentionally. It is required by the
4286// geolocation api (window.navigator.geolocation.getCurrentPosition) to work on desktop.
@@ -390,34 +434,16 @@ const mainWindow = (): Promise<void> => {
390434 } ) ;
391435
392436 ipcMain . handle ( ELECTRON_EVENTS . CHECK_LOCATION_PERMISSION , async ( ) => {
437+ const getAuthStatus = await loadMacGetAuthStatus ( ) ;
438+
439+ if ( ! getAuthStatus ) {
440+ return LOCATION_PERMISSION_STATES . DENIED ;
441+ }
442+
393443 try {
394- type MacPermissionsModule = {
395- getAuthStatus ?: ( authType : AuthType ) => PermissionType | 'not determined' ;
396- } ;
397- const macPermissionsModule = ( await import ( 'node-mac-permissions' ) ) as MacPermissionsModule | { default : MacPermissionsModule } ;
398- const macPermissions : MacPermissionsModule = ( 'default' in macPermissionsModule ? macPermissionsModule . default : macPermissionsModule ) ?? { } ;
399- const { getAuthStatus} = macPermissions ;
400-
401- if ( ! getAuthStatus || typeof getAuthStatus !== 'function' ) {
402- log . warn ( 'node-mac-permissions not available or invalid, defaulting to denied' ) ;
403- return LOCATION_PERMISSION_STATES . DENIED ;
404- }
405-
406- const status = getAuthStatus ( 'location' ) ;
407-
408- switch ( status ) {
409- case MAC_PERMISSION_STATUSES . AUTHORIZED :
410- return LOCATION_PERMISSION_STATES . GRANTED ;
411- case MAC_PERMISSION_STATUSES . DENIED :
412- case MAC_PERMISSION_STATUSES . RESTRICTED :
413- return LOCATION_PERMISSION_STATES . DENIED ;
414- case MAC_PERMISSION_STATUSES . NOT_DETERMINED :
415- return LOCATION_PERMISSION_STATES . PROMPT ;
416- default :
417- return LOCATION_PERMISSION_STATES . DENIED ;
418- }
444+ return resolveLocationPermissionStatus ( getAuthStatus ( 'location' ) ) ;
419445 } catch ( error ) {
420- log . warn ( 'node-mac-permissions not available , defaulting to denied:' , ( error as Error ) ?. message ) ;
446+ log . warn ( 'node-mac-permissions threw while checking location permission , defaulting to denied:' , ( error as Error ) ?. message ) ;
421447 return LOCATION_PERMISSION_STATES . DENIED ;
422448 }
423449 } ) ;
0 commit comments