core_location_provider.cc 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. #include <Security/Security.h>
  5. #include "services/device/geolocation/core_location_provider.h"
  6. #include "base/mac/scoped_cftyperef.h"
  7. #include "services/device/public/cpp/device_features.h"
  8. #include "services/device/public/cpp/geolocation/location_system_permission_status.h"
  9. namespace device {
  10. CoreLocationProvider::CoreLocationProvider(
  11. scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
  12. GeolocationManager* geolocation_manager)
  13. : geolocation_manager_(geolocation_manager),
  14. permission_observers_(geolocation_manager->GetObserverList()),
  15. position_observers_(geolocation_manager->GetPositionObserverList()) {
  16. permission_observers_->AddObserver(this);
  17. main_task_runner->PostTaskAndReplyWithResult(
  18. FROM_HERE,
  19. base::BindOnce(&GeolocationManager::GetSystemPermission,
  20. base::Unretained(geolocation_manager_)),
  21. base::BindOnce(&CoreLocationProvider::OnSystemPermissionUpdated,
  22. weak_ptr_factory_.GetWeakPtr()));
  23. }
  24. CoreLocationProvider::~CoreLocationProvider() {
  25. permission_observers_->RemoveObserver(this);
  26. StopProvider();
  27. }
  28. void CoreLocationProvider::SetUpdateCallback(
  29. const LocationProviderUpdateCallback& callback) {
  30. callback_ = callback;
  31. }
  32. void CoreLocationProvider::StartProvider(bool high_accuracy) {
  33. high_accuracy_ = high_accuracy;
  34. // macOS guarantees that didChangeAuthorization will be called at least once
  35. // with the initial authorization status. Therefore this variable will be
  36. // updated regardless of whether that authorization status has recently
  37. // changed.
  38. if (has_permission_) {
  39. StartWatching();
  40. } else {
  41. provider_start_attemped_ = true;
  42. }
  43. }
  44. void CoreLocationProvider::StartWatching() {
  45. position_observers_->AddObserver(this);
  46. geolocation_manager_->StartWatchingPosition(high_accuracy_);
  47. }
  48. void CoreLocationProvider::StopProvider() {
  49. position_observers_->RemoveObserver(this);
  50. geolocation_manager_->StopWatchingPosition();
  51. }
  52. const mojom::Geoposition& CoreLocationProvider::GetPosition() {
  53. return last_position_;
  54. }
  55. void CoreLocationProvider::OnPermissionGranted() {
  56. // Nothing to do here.
  57. }
  58. void CoreLocationProvider::OnSystemPermissionUpdated(
  59. LocationSystemPermissionStatus new_status) {
  60. has_permission_ = new_status == LocationSystemPermissionStatus::kAllowed;
  61. if (provider_start_attemped_ && has_permission_) {
  62. StartWatching();
  63. provider_start_attemped_ = false;
  64. }
  65. }
  66. void CoreLocationProvider::OnPositionUpdated(
  67. const mojom::Geoposition& location) {
  68. last_position_ = location;
  69. callback_.Run(this, last_position_);
  70. }
  71. std::unique_ptr<LocationProvider> NewSystemLocationProvider(
  72. scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
  73. GeolocationManager* geolocation_manager) {
  74. if (!base::FeatureList::IsEnabled(features::kMacCoreLocationBackend)) {
  75. return nullptr;
  76. }
  77. return std::make_unique<CoreLocationProvider>(std::move(main_task_runner),
  78. geolocation_manager);
  79. }
  80. } // namespace device