#ifndef SERVICES_DEVICE_GEOLOCATION_GEOLOCATION_PROVIDER_IMPL_H_
#define SERVICES_DEVICE_GEOLOCATION_GEOLOCATION_PROVIDER_IMPL_H_
#include <memory>
#include <string>
#include <vector>
#include "base/compiler_specific.h"
#include "base/functional/callback_forward.h"
#include "base/threading/thread.h"
#include "build/build_config.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "services/device/geolocation/geolocation_provider.h"
#include "services/device/public/cpp/geolocation/location_provider.h"
#include "services/device/public/mojom/geolocation_control.mojom.h"
#include "services/device/public/mojom/geoposition.mojom.h"
namespace base {
template <typename Type>
struct DefaultSingletonTraits;
class SingleThreadTaskRunner;
}
namespace network {
class SharedURLLoaderFactory;
}
namespace device {
class GeolocationManager;
using CustomLocationProviderCallback =
base::RepeatingCallback<std::unique_ptr<LocationProvider>()>;
class GeolocationProviderImpl : public GeolocationProvider,
public mojom::GeolocationControl,
public base::Thread {
public:
base::CallbackListSubscription AddLocationUpdateCallback(
const LocationUpdateCallback& callback,
bool enable_high_accuracy) override;
bool HighAccuracyLocationInUse() override;
void OverrideLocationForTesting(mojom::GeopositionResultPtr result) override;
void OnLocationUpdate(const LocationProvider* provider,
mojom::GeopositionResultPtr result);
static GeolocationProviderImpl* GetInstance();
GeolocationProviderImpl(const GeolocationProviderImpl&) = delete;
GeolocationProviderImpl& operator=(const GeolocationProviderImpl&) = delete;
static void 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);
void BindGeolocationControlReceiver(
mojo::PendingReceiver<mojom::GeolocationControl> receiver);
void UserDidOptIntoLocationServices() override;
bool user_did_opt_into_location_services_for_testing() {
return user_did_opt_into_location_services_;
}
void clear_user_did_opt_into_location_services_for_testing() {
user_did_opt_into_location_services_ = false;
}
void SetArbitratorForTesting(std::unique_ptr<LocationProvider> arbitrator);
private:
friend struct base::DefaultSingletonTraits<GeolocationProviderImpl>;
GeolocationProviderImpl();
~GeolocationProviderImpl() override;
bool OnGeolocationThread() const;
void OnClientsChanged();
void StopProviders();
void StartProviders(bool enable_high_accuracy);
void InformProvidersPermissionGranted();
void NotifyClients(mojom::GeopositionResultPtr result);
void Init() override;
void CleanUp() override;
base::RepeatingCallbackList<void(const mojom::GeopositionResult&)>
high_accuracy_callbacks_;
base::RepeatingCallbackList<void(const mojom::GeopositionResult&)>
low_accuracy_callbacks_;
bool user_did_opt_into_location_services_ = false;
mojom::GeopositionResultPtr result_;
bool ignore_location_updates_ = false;
const scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_;
std::unique_ptr<LocationProvider> arbitrator_;
mojo::Receiver<mojom::GeolocationControl> receiver_{this};
};
}
#endif