core_location_provider.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef SERVICES_DEVICE_GEOLOCATION_CORE_LOCATION_PROVIDER_H_
  5. #define SERVICES_DEVICE_GEOLOCATION_CORE_LOCATION_PROVIDER_H_
  6. #include "base/memory/raw_ptr.h"
  7. #include "services/device/public/cpp/geolocation/geolocation_manager.h"
  8. #include "services/device/public/cpp/geolocation/location_provider.h"
  9. #include "services/device/public/mojom/geoposition.mojom.h"
  10. namespace device {
  11. // Location provider for macOS using the platform's Core Location API.
  12. class CoreLocationProvider : public LocationProvider,
  13. public GeolocationManager::PermissionObserver,
  14. public GeolocationManager::PositionObserver {
  15. public:
  16. CoreLocationProvider(
  17. scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
  18. GeolocationManager* geolocation_manager);
  19. CoreLocationProvider(const CoreLocationProvider&) = delete;
  20. CoreLocationProvider& operator=(const CoreLocationProvider&) = delete;
  21. ~CoreLocationProvider() override;
  22. // LocationProvider implementation.
  23. void SetUpdateCallback(
  24. const LocationProviderUpdateCallback& callback) override;
  25. void StartProvider(bool high_accuracy) override;
  26. void StopProvider() override;
  27. const mojom::Geoposition& GetPosition() override;
  28. void OnPermissionGranted() override;
  29. private:
  30. void StartWatching();
  31. // GeolocationManager::PositionObserver implementation.
  32. void OnPositionUpdated(const mojom::Geoposition& location) override;
  33. // GeolocationManager::PermissionObserver implementation.
  34. void OnSystemPermissionUpdated(
  35. LocationSystemPermissionStatus new_status) override;
  36. raw_ptr<GeolocationManager> geolocation_manager_;
  37. // References to the observer lists are kept to ensure their lifetime as the
  38. // BrowserProcess may destroy its reference on the UI Thread before we
  39. // destroy this provider.
  40. scoped_refptr<GeolocationManager::PermissionObserverList>
  41. permission_observers_;
  42. scoped_refptr<GeolocationManager::PositionObserverList> position_observers_;
  43. mojom::Geoposition last_position_;
  44. LocationProviderUpdateCallback callback_;
  45. bool has_permission_ = false;
  46. bool provider_start_attemped_ = false;
  47. bool high_accuracy_ = false;
  48. base::WeakPtrFactory<CoreLocationProvider> weak_ptr_factory_{this};
  49. };
  50. } // namespace device
  51. #endif // SERVICES_DEVICE_GEOLOCATION_CORE_LOCATION_PROVIDER_H_