12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- // Copyright 2020 The Chromium Authors. All rights reserved.
- // Use of this source code is governed by a BSD-style license that can be
- // found in the LICENSE file.
- #include <Security/Security.h>
- #include "services/device/geolocation/core_location_provider.h"
- #include "base/mac/scoped_cftyperef.h"
- #include "services/device/public/cpp/device_features.h"
- #include "services/device/public/cpp/geolocation/location_system_permission_status.h"
- namespace device {
- CoreLocationProvider::CoreLocationProvider(
- scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
- GeolocationManager* geolocation_manager)
- : geolocation_manager_(geolocation_manager),
- permission_observers_(geolocation_manager->GetObserverList()),
- position_observers_(geolocation_manager->GetPositionObserverList()) {
- permission_observers_->AddObserver(this);
- main_task_runner->PostTaskAndReplyWithResult(
- FROM_HERE,
- base::BindOnce(&GeolocationManager::GetSystemPermission,
- base::Unretained(geolocation_manager_)),
- base::BindOnce(&CoreLocationProvider::OnSystemPermissionUpdated,
- weak_ptr_factory_.GetWeakPtr()));
- }
- CoreLocationProvider::~CoreLocationProvider() {
- permission_observers_->RemoveObserver(this);
- StopProvider();
- }
- void CoreLocationProvider::SetUpdateCallback(
- const LocationProviderUpdateCallback& callback) {
- callback_ = callback;
- }
- void CoreLocationProvider::StartProvider(bool high_accuracy) {
- high_accuracy_ = high_accuracy;
- // macOS guarantees that didChangeAuthorization will be called at least once
- // with the initial authorization status. Therefore this variable will be
- // updated regardless of whether that authorization status has recently
- // changed.
- if (has_permission_) {
- StartWatching();
- } else {
- provider_start_attemped_ = true;
- }
- }
- void CoreLocationProvider::StartWatching() {
- position_observers_->AddObserver(this);
- geolocation_manager_->StartWatchingPosition(high_accuracy_);
- }
- void CoreLocationProvider::StopProvider() {
- position_observers_->RemoveObserver(this);
- geolocation_manager_->StopWatchingPosition();
- }
- const mojom::Geoposition& CoreLocationProvider::GetPosition() {
- return last_position_;
- }
- void CoreLocationProvider::OnPermissionGranted() {
- // Nothing to do here.
- }
- void CoreLocationProvider::OnSystemPermissionUpdated(
- LocationSystemPermissionStatus new_status) {
- has_permission_ = new_status == LocationSystemPermissionStatus::kAllowed;
- if (provider_start_attemped_ && has_permission_) {
- StartWatching();
- provider_start_attemped_ = false;
- }
- }
- void CoreLocationProvider::OnPositionUpdated(
- const mojom::Geoposition& location) {
- last_position_ = location;
- callback_.Run(this, last_position_);
- }
- std::unique_ptr<LocationProvider> NewSystemLocationProvider(
- scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
- GeolocationManager* geolocation_manager) {
- if (!base::FeatureList::IsEnabled(features::kMacCoreLocationBackend)) {
- return nullptr;
- }
- return std::make_unique<CoreLocationProvider>(std::move(main_task_runner),
- geolocation_manager);
- }
- } // namespace device
|