123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265 |
- // Copyright (c) 2012 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 "services/device/geolocation/geolocation_provider_impl.h"
- #include <utility>
- #include "base/bind.h"
- #include "base/callback.h"
- #include "base/callback_helpers.h"
- #include "base/check.h"
- #include "base/lazy_instance.h"
- #include "base/location.h"
- #include "base/memory/ptr_util.h"
- #include "base/memory/singleton.h"
- #include "base/notreached.h"
- #include "base/task/single_thread_task_runner.h"
- #include "base/threading/thread_task_runner_handle.h"
- #include "base/time/default_tick_clock.h"
- #include "build/build_config.h"
- #include "net/base/network_change_notifier.h"
- #include "services/device/geolocation/location_arbitrator.h"
- #include "services/device/geolocation/position_cache_impl.h"
- #include "services/device/public/cpp/geolocation/geoposition.h"
- #include "services/network/public/cpp/shared_url_loader_factory.h"
- #if BUILDFLAG(IS_ANDROID)
- #include "base/android/jni_android.h"
- #include "services/device/geolocation/geolocation_jni_headers/LocationProviderFactory_jni.h"
- #endif
- namespace device {
- namespace {
- base::LazyInstance<CustomLocationProviderCallback>::Leaky
- g_custom_location_provider_callback = LAZY_INSTANCE_INITIALIZER;
- base::LazyInstance<std::unique_ptr<network::PendingSharedURLLoaderFactory>>::
- Leaky g_pending_url_loader_factory = LAZY_INSTANCE_INITIALIZER;
- base::LazyInstance<std::string>::Leaky g_api_key = LAZY_INSTANCE_INITIALIZER;
- GeolocationManager* g_geolocation_manager;
- } // namespace
- // static
- GeolocationProvider* GeolocationProvider::GetInstance() {
- return GeolocationProviderImpl::GetInstance();
- }
- // static
- void GeolocationProviderImpl::SetGeolocationConfiguration(
- scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
- const std::string& api_key,
- const CustomLocationProviderCallback& custom_location_provider_getter,
- GeolocationManager* geolocation_manager,
- bool use_gms_core_location_provider) {
- if (url_loader_factory)
- g_pending_url_loader_factory.Get() = url_loader_factory->Clone();
- g_api_key.Get() = api_key;
- g_custom_location_provider_callback.Get() = custom_location_provider_getter;
- g_geolocation_manager = geolocation_manager;
- if (use_gms_core_location_provider) {
- #if BUILDFLAG(IS_ANDROID)
- JNIEnv* env = base::android::AttachCurrentThread();
- Java_LocationProviderFactory_useGmsCoreLocationProvider(env);
- #else
- NOTREACHED() << "GMS core location provider is only available for Android";
- #endif
- }
- }
- base::CallbackListSubscription
- GeolocationProviderImpl::AddLocationUpdateCallback(
- const LocationUpdateCallback& callback,
- bool enable_high_accuracy) {
- DCHECK(main_task_runner_->BelongsToCurrentThread());
- base::CallbackListSubscription subscription;
- if (enable_high_accuracy) {
- subscription = high_accuracy_callbacks_.Add(callback);
- } else {
- subscription = low_accuracy_callbacks_.Add(callback);
- }
- OnClientsChanged();
- if (ValidateGeoposition(position_) ||
- position_.error_code != mojom::Geoposition::ErrorCode::NONE) {
- callback.Run(position_);
- }
- return subscription;
- }
- bool GeolocationProviderImpl::HighAccuracyLocationInUse() {
- return !high_accuracy_callbacks_.empty();
- }
- void GeolocationProviderImpl::OverrideLocationForTesting(
- const mojom::Geoposition& position) {
- DCHECK(main_task_runner_->BelongsToCurrentThread());
- ignore_location_updates_ = true;
- NotifyClients(position);
- }
- void GeolocationProviderImpl::OnLocationUpdate(
- const LocationProvider* provider,
- const mojom::Geoposition& position) {
- DCHECK(OnGeolocationThread());
- // Will be true only in testing.
- if (ignore_location_updates_)
- return;
- main_task_runner_->PostTask(
- FROM_HERE, base::BindOnce(&GeolocationProviderImpl::NotifyClients,
- base::Unretained(this), position));
- }
- // static
- GeolocationProviderImpl* GeolocationProviderImpl::GetInstance() {
- return base::Singleton<GeolocationProviderImpl>::get();
- }
- void GeolocationProviderImpl::BindGeolocationControlReceiver(
- mojo::PendingReceiver<mojom::GeolocationControl> receiver) {
- // The |receiver_| has been bound already here means that more than one
- // GeolocationPermissionContext in chrome tried to bind to Device Service.
- // We only bind the first receiver. See more info in
- // geolocation_control.mojom.
- if (!receiver_.is_bound())
- receiver_.Bind(std::move(receiver));
- }
- void GeolocationProviderImpl::UserDidOptIntoLocationServices() {
- DCHECK(main_task_runner_->BelongsToCurrentThread());
- bool was_permission_granted = user_did_opt_into_location_services_;
- user_did_opt_into_location_services_ = true;
- if (IsRunning() && !was_permission_granted)
- InformProvidersPermissionGranted();
- }
- GeolocationProviderImpl::GeolocationProviderImpl()
- : base::Thread("Geolocation"),
- user_did_opt_into_location_services_(false),
- ignore_location_updates_(false),
- main_task_runner_(base::ThreadTaskRunnerHandle::Get()) {
- DCHECK(main_task_runner_->BelongsToCurrentThread());
- high_accuracy_callbacks_.set_removal_callback(base::BindRepeating(
- &GeolocationProviderImpl::OnClientsChanged, base::Unretained(this)));
- low_accuracy_callbacks_.set_removal_callback(base::BindRepeating(
- &GeolocationProviderImpl::OnClientsChanged, base::Unretained(this)));
- }
- GeolocationProviderImpl::~GeolocationProviderImpl() {
- Stop();
- DCHECK(!arbitrator_);
- }
- void GeolocationProviderImpl::SetArbitratorForTesting(
- std::unique_ptr<LocationProvider> arbitrator) {
- arbitrator_ = std::move(arbitrator);
- }
- bool GeolocationProviderImpl::OnGeolocationThread() const {
- return task_runner()->BelongsToCurrentThread();
- }
- void GeolocationProviderImpl::OnClientsChanged() {
- DCHECK(main_task_runner_->BelongsToCurrentThread());
- base::OnceClosure task;
- if (high_accuracy_callbacks_.empty() && low_accuracy_callbacks_.empty()) {
- DCHECK(IsRunning());
- if (!ignore_location_updates_) {
- // We have no more observers, so we clear the cached geoposition so that
- // when the next observer is added we will not provide a stale position.
- position_ = mojom::Geoposition();
- }
- task = base::BindOnce(&GeolocationProviderImpl::StopProviders,
- base::Unretained(this));
- } else {
- if (!IsRunning()) {
- base::Thread::Options options;
- #if BUILDFLAG(IS_MAC)
- options.message_pump_type = base::MessagePumpType::NS_RUNLOOP;
- #endif
- StartWithOptions(std::move(options));
- if (user_did_opt_into_location_services_)
- InformProvidersPermissionGranted();
- }
- // Determine a set of options that satisfies all clients.
- bool enable_high_accuracy = !high_accuracy_callbacks_.empty();
- // Send the current options to the providers as they may have changed.
- task = base::BindOnce(&GeolocationProviderImpl::StartProviders,
- base::Unretained(this), enable_high_accuracy);
- }
- task_runner()->PostTask(FROM_HERE, std::move(task));
- }
- void GeolocationProviderImpl::StopProviders() {
- DCHECK(OnGeolocationThread());
- DCHECK(arbitrator_);
- arbitrator_->StopProvider();
- }
- void GeolocationProviderImpl::StartProviders(bool enable_high_accuracy) {
- DCHECK(OnGeolocationThread());
- DCHECK(arbitrator_);
- arbitrator_->StartProvider(enable_high_accuracy);
- }
- void GeolocationProviderImpl::InformProvidersPermissionGranted() {
- DCHECK(IsRunning());
- if (!OnGeolocationThread()) {
- task_runner()->PostTask(
- FROM_HERE,
- base::BindOnce(
- &GeolocationProviderImpl::InformProvidersPermissionGranted,
- base::Unretained(this)));
- return;
- }
- DCHECK(OnGeolocationThread());
- DCHECK(arbitrator_);
- arbitrator_->OnPermissionGranted();
- }
- void GeolocationProviderImpl::NotifyClients(
- const mojom::Geoposition& position) {
- DCHECK(main_task_runner_->BelongsToCurrentThread());
- DCHECK(ValidateGeoposition(position) ||
- position.error_code != mojom::Geoposition::ErrorCode::NONE);
- position_ = position;
- high_accuracy_callbacks_.Notify(position_);
- low_accuracy_callbacks_.Notify(position_);
- }
- void GeolocationProviderImpl::Init() {
- DCHECK(OnGeolocationThread());
- if (arbitrator_)
- return;
- LocationProvider::LocationProviderUpdateCallback callback =
- base::BindRepeating(&GeolocationProviderImpl::OnLocationUpdate,
- base::Unretained(this));
- scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory;
- if (g_pending_url_loader_factory.Get()) {
- url_loader_factory = network::SharedURLLoaderFactory::Create(
- std::move(g_pending_url_loader_factory.Get()));
- }
- DCHECK(!net::NetworkChangeNotifier::CreateIfNeeded())
- << "PositionCacheImpl needs a global NetworkChangeNotifier";
- arbitrator_ = std::make_unique<LocationArbitrator>(
- g_custom_location_provider_callback.Get(), g_geolocation_manager,
- main_task_runner_, std::move(url_loader_factory), g_api_key.Get(),
- std::make_unique<PositionCacheImpl>(
- base::DefaultTickClock::GetInstance()));
- arbitrator_->SetUpdateCallback(callback);
- }
- void GeolocationProviderImpl::CleanUp() {
- DCHECK(OnGeolocationThread());
- arbitrator_.reset();
- }
- } // namespace device
|