Title: [291496] trunk/Source
Revision
291496
Author
cdu...@apple.com
Date
2022-03-18 14:53:45 -0700 (Fri, 18 Mar 2022)

Log Message

Avoid calls to [CLLocationManager authorizationStatus] & [CLLocationManager locationServicesEnabled]
https://bugs.webkit.org/show_bug.cgi?id=237933
<rdar://89931043>

Reviewed by Geoffrey Garen.

Source/WebCore:

Avoid calls to [CLLocationManager authorizationStatus] & [CLLocationManager locationServicesEnabled]
for performance reasons since those are synchronous, potentially slow and called on the UIProcess
main thread.

Instead, rely on the fact that the locationManagerDidChangeAuthorization always gets called
asynchronously right after constructing the CLLocationManager with the actual authorization.
This simplifies our logic a bit too.

We now only call [CLLocationManager requestWhenInUseAuthorization] when locationManagerDidChangeAuthorization
gets called with kCLAuthorizationStatusNotDetermined asynchronously after construction.
If locationManagerDidChangeAuthorization gets called with an authorized enum value, we
call [CLLocationManager startUpdatingLocation] then (if the client is interested in locations, and not
merely in the authorization).

* platform/cocoa/CoreLocationGeolocationProvider.h:
* platform/cocoa/CoreLocationGeolocationProvider.mm:
(-[WebCLLocationManager initWithWebsiteIdentifier:client:mode:]):
(-[WebCLLocationManager locationManagerDidChangeAuthorization:]):
(WebCore::CoreLocationGeolocationProvider::CoreLocationGeolocationProvider):
(isAuthorizationGranted): Deleted.
(-[WebCLLocationManager initWithWebsiteIdentifier:client:]): Deleted.
(-[WebCLLocationManager start]): Deleted.
(-[WebCLLocationManager requestGeolocationAuthorization]): Deleted.
(WebCore::CoreLocationGeolocationProvider::start): Deleted.
(WebCore::CoreLocationGeolocationProvider::stop): Deleted.

Source/WebKit:

Minor changes to reflect API changes for our CoreLocation location manager.

* UIProcess/WebGeolocationManagerProxy.cpp:
(WebKit::WebGeolocationManagerProxy::providerStartUpdating):
(WebKit::WebGeolocationManagerProxy::providerStopUpdating):

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (291495 => 291496)


--- trunk/Source/WebCore/ChangeLog	2022-03-18 20:09:08 UTC (rev 291495)
+++ trunk/Source/WebCore/ChangeLog	2022-03-18 21:53:45 UTC (rev 291496)
@@ -1,5 +1,39 @@
 2022-03-18  Chris Dumez  <cdu...@apple.com>
 
+        Avoid calls to [CLLocationManager authorizationStatus] & [CLLocationManager locationServicesEnabled]
+        https://bugs.webkit.org/show_bug.cgi?id=237933
+        <rdar://89931043>
+
+        Reviewed by Geoffrey Garen.
+
+        Avoid calls to [CLLocationManager authorizationStatus] & [CLLocationManager locationServicesEnabled]
+        for performance reasons since those are synchronous, potentially slow and called on the UIProcess
+        main thread.
+
+        Instead, rely on the fact that the locationManagerDidChangeAuthorization always gets called
+        asynchronously right after constructing the CLLocationManager with the actual authorization.
+        This simplifies our logic a bit too.
+
+        We now only call [CLLocationManager requestWhenInUseAuthorization] when locationManagerDidChangeAuthorization
+        gets called with kCLAuthorizationStatusNotDetermined asynchronously after construction.
+        If locationManagerDidChangeAuthorization gets called with an authorized enum value, we
+        call [CLLocationManager startUpdatingLocation] then (if the client is interested in locations, and not
+        merely in the authorization).
+
+        * platform/cocoa/CoreLocationGeolocationProvider.h:
+        * platform/cocoa/CoreLocationGeolocationProvider.mm:
+        (-[WebCLLocationManager initWithWebsiteIdentifier:client:mode:]):
+        (-[WebCLLocationManager locationManagerDidChangeAuthorization:]):
+        (WebCore::CoreLocationGeolocationProvider::CoreLocationGeolocationProvider):
+        (isAuthorizationGranted): Deleted.
+        (-[WebCLLocationManager initWithWebsiteIdentifier:client:]): Deleted.
+        (-[WebCLLocationManager start]): Deleted.
+        (-[WebCLLocationManager requestGeolocationAuthorization]): Deleted.
+        (WebCore::CoreLocationGeolocationProvider::start): Deleted.
+        (WebCore::CoreLocationGeolocationProvider::stop): Deleted.
+
+2022-03-18  Chris Dumez  <cdu...@apple.com>
+
         Avoid extra pointer dereference in EventListenerMap::m_entries
         https://bugs.webkit.org/show_bug.cgi?id=238075
 

Modified: trunk/Source/WebCore/platform/cocoa/CoreLocationGeolocationProvider.h (291495 => 291496)


--- trunk/Source/WebCore/platform/cocoa/CoreLocationGeolocationProvider.h	2022-03-18 20:09:08 UTC (rev 291495)
+++ trunk/Source/WebCore/platform/cocoa/CoreLocationGeolocationProvider.h	2022-03-18 21:53:45 UTC (rev 291496)
@@ -51,12 +51,10 @@
         virtual void resetGeolocation(const String& websiteIdentifier) = 0;
     };
 
-    CoreLocationGeolocationProvider(const RegistrableDomain&, Client&);
+    enum class Mode : bool { AuthorizationOnly, AuthorizationAndLocationUpdates };
+    CoreLocationGeolocationProvider(const RegistrableDomain&, Client&, Mode = Mode::AuthorizationAndLocationUpdates);
     ~CoreLocationGeolocationProvider();
 
-    void start();
-    void stop();
-
     void setEnableHighAccuracy(bool);
 
     static void requestAuthorization(const RegistrableDomain&, CompletionHandler<void(bool)>&&);

Modified: trunk/Source/WebCore/platform/cocoa/CoreLocationGeolocationProvider.mm (291495 => 291496)


--- trunk/Source/WebCore/platform/cocoa/CoreLocationGeolocationProvider.mm	2022-03-18 20:09:08 UTC (rev 291495)
+++ trunk/Source/WebCore/platform/cocoa/CoreLocationGeolocationProvider.mm	2022-03-18 21:53:45 UTC (rev 291496)
@@ -49,15 +49,6 @@
 #define kCLLocationAccuracyBest getkCLLocationAccuracyBest()
 #define kCLLocationAccuracyHundredMeters getkCLLocationAccuracyHundredMeters()
 
-static bool isAuthorizationGranted(CLAuthorizationStatus status)
-{
-    return status == kCLAuthorizationStatusAuthorizedAlways
-#if HAVE(CORE_LOCATION_AUTHORIZED_WHEN_IN_USE)
-        || status == kCLAuthorizationStatusAuthorizedWhenInUse
-#endif
-        ;
-}
-
 @interface WebCLLocationManager : NSObject<CLLocationManagerDelegate>
 @end
 
@@ -66,14 +57,18 @@
     WebCore::CoreLocationGeolocationProvider::Client* _client;
     String _websiteIdentifier;
     BOOL _isWaitingForAuthorization;
+    WebCore::CoreLocationGeolocationProvider::Mode _mode;
 }
 
-- (instancetype)initWithWebsiteIdentifier:(const String&)websiteIdentifier client:(WebCore::CoreLocationGeolocationProvider::Client&)client
+- (instancetype)initWithWebsiteIdentifier:(const String&)websiteIdentifier client:(WebCore::CoreLocationGeolocationProvider::Client&)client mode:(WebCore::CoreLocationGeolocationProvider::Mode)mode
 {
     self = [super init];
     if (!self)
         return nil;
 
+    _isWaitingForAuthorization = YES;
+    _mode = mode;
+
 #if USE(APPLE_INTERNAL_SDK) && HAVE(CORE_LOCATION_WEBSITE_IDENTIFIERS) && defined(CL_HAS_RADAR_88834301)
     if (!websiteIdentifier.isEmpty())
         _locationManager = adoptNS([allocCLLocationManagerInstance() initWithWebsiteIdentifier:websiteIdentifier]);
@@ -94,17 +89,6 @@
     [super dealloc];
 }
 
-- (void)start
-{
-    if (![getCLLocationManagerClass() locationServicesEnabled] || !isAuthorizationGranted([_locationManager authorizationStatus])) {
-        [_locationManager stopUpdatingLocation];
-        _client->resetGeolocation(_websiteIdentifier);
-        return;
-    }
-
-    [_locationManager startUpdatingLocation];
-}
-
 - (void)stop
 {
     [_locationManager stopUpdatingLocation];
@@ -115,34 +99,6 @@
     [_locationManager setDesiredAccuracy:highAccuracyEnabled ? kCLLocationAccuracyBest : kCLLocationAccuracyHundredMeters];
 }
 
-- (void)requestGeolocationAuthorization
-{
-    if (![getCLLocationManagerClass() locationServicesEnabled]) {
-        _client->geolocationAuthorizationDenied(_websiteIdentifier);
-        return;
-    }
-
-    switch ([_locationManager authorizationStatus]) {
-    case kCLAuthorizationStatusNotDetermined: {
-        if (!_isWaitingForAuthorization) {
-            _isWaitingForAuthorization = YES;
-            [_locationManager requestWhenInUseAuthorization];
-        }
-        break;
-    }
-    case kCLAuthorizationStatusAuthorizedAlways:
-#if HAVE(CORE_LOCATION_AUTHORIZED_WHEN_IN_USE)
-    case kCLAuthorizationStatusAuthorizedWhenInUse:
-#endif
-        _client->geolocationAuthorizationGranted(_websiteIdentifier);
-        break;
-    case kCLAuthorizationStatusRestricted:
-    case kCLAuthorizationStatusDenied:
-        _client->geolocationAuthorizationDenied(_websiteIdentifier);
-        break;
-    }
-}
-
 - (void)locationManagerDidChangeAuthorization:(CLLocationManager *)manager
 {
     auto status = [_locationManager authorizationStatus];
@@ -149,7 +105,7 @@
     if (_isWaitingForAuthorization) {
         switch (status) {
         case kCLAuthorizationStatusNotDetermined:
-            // This can happen after resume if the user has still not answered the dialog. We just have to wait for the permission.
+            [_locationManager requestWhenInUseAuthorization];
             break;
         case kCLAuthorizationStatusDenied:
         case kCLAuthorizationStatusRestricted:
@@ -162,6 +118,8 @@
 #endif
             _isWaitingForAuthorization = NO;
             _client->geolocationAuthorizationGranted(_websiteIdentifier);
+            if (_mode != WebCore::CoreLocationGeolocationProvider::Mode::AuthorizationOnly)
+                [_locationManager startUpdatingLocation];
             break;
         }
     } else {
@@ -197,11 +155,9 @@
 
 namespace WebCore {
 
-CoreLocationGeolocationProvider::CoreLocationGeolocationProvider(const RegistrableDomain& registrableDomain, Client& client)
-    : m_locationManager(adoptNS([[WebCLLocationManager alloc] initWithWebsiteIdentifier:registrableDomain.string() client:client]))
+CoreLocationGeolocationProvider::CoreLocationGeolocationProvider(const RegistrableDomain& registrableDomain, Client& client, Mode mode)
+    : m_locationManager(adoptNS([[WebCLLocationManager alloc] initWithWebsiteIdentifier:registrableDomain.string() client:client mode:mode]))
 {
-    // Request permission for the app to use geolocation (if it doesn't already).
-    [m_locationManager requestGeolocationAuthorization];
 }
 
 CoreLocationGeolocationProvider::~CoreLocationGeolocationProvider()
@@ -209,16 +165,6 @@
     [m_locationManager stop];
 }
 
-void CoreLocationGeolocationProvider::start()
-{
-    [m_locationManager start];
-}
-
-void CoreLocationGeolocationProvider::stop()
-{
-    [m_locationManager stop];
-}
-
 void CoreLocationGeolocationProvider::setEnableHighAccuracy(bool highAccuracyEnabled)
 {
     [m_locationManager setEnableHighAccuracy:highAccuracyEnabled];
@@ -234,7 +180,7 @@
     void check(const RegistrableDomain& registrableDomain, CompletionHandler<void(bool)>&& completionHandler)
     {
         m_completionHandler = WTFMove(completionHandler);
-        m_provider = makeUnique<CoreLocationGeolocationProvider>(registrableDomain, *this);
+        m_provider = makeUnique<CoreLocationGeolocationProvider>(registrableDomain, *this, CoreLocationGeolocationProvider::Mode::AuthorizationOnly);
     }
 
 private:

Modified: trunk/Source/WebKit/ChangeLog (291495 => 291496)


--- trunk/Source/WebKit/ChangeLog	2022-03-18 20:09:08 UTC (rev 291495)
+++ trunk/Source/WebKit/ChangeLog	2022-03-18 21:53:45 UTC (rev 291496)
@@ -1,3 +1,17 @@
+2022-03-18  Chris Dumez  <cdu...@apple.com>
+
+        Avoid calls to [CLLocationManager authorizationStatus] & [CLLocationManager locationServicesEnabled]
+        https://bugs.webkit.org/show_bug.cgi?id=237933
+        <rdar://89931043>
+
+        Reviewed by Geoffrey Garen.
+
+        Minor changes to reflect API changes for our CoreLocation location manager.
+
+        * UIProcess/WebGeolocationManagerProxy.cpp:
+        (WebKit::WebGeolocationManagerProxy::providerStartUpdating):
+        (WebKit::WebGeolocationManagerProxy::providerStopUpdating):
+
 2022-03-18  Ben Nham  <n...@apple.com>
 
         Remove push subscriptions when associated service worker registrations are removed

Modified: trunk/Source/WebKit/UIProcess/WebGeolocationManagerProxy.cpp (291495 => 291496)


--- trunk/Source/WebKit/UIProcess/WebGeolocationManagerProxy.cpp	2022-03-18 20:09:08 UTC (rev 291495)
+++ trunk/Source/WebKit/UIProcess/WebGeolocationManagerProxy.cpp	2022-03-18 21:53:45 UTC (rev 291496)
@@ -256,7 +256,6 @@
         ASSERT(!perDomainData.provider);
         perDomainData.provider = makeUnique<WebCore::CoreLocationGeolocationProvider>(registrableDomain, *this);
         perDomainData.provider->setEnableHighAccuracy(!perDomainData.watchersNeedingHighAccuracy.computesEmpty());
-        perDomainData.provider->start();
         return;
     }
 #else
@@ -273,7 +272,7 @@
 {
 #if PLATFORM(IOS_FAMILY)
     if (!m_clientProvider) {
-        perDomainData.provider->stop();
+        perDomainData.provider = nullptr;
         return;
     }
 #else
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to