network_location_provider.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright (c) 2012 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_NETWORK_LOCATION_PROVIDER_H_
  5. #define SERVICES_DEVICE_GEOLOCATION_NETWORK_LOCATION_PROVIDER_H_
  6. #include <stddef.h>
  7. #include <map>
  8. #include <memory>
  9. #include <string>
  10. #include "base/memory/raw_ptr.h"
  11. #include "base/memory/ref_counted.h"
  12. #include "base/memory/weak_ptr.h"
  13. #include "base/scoped_observation.h"
  14. #include "base/threading/thread.h"
  15. #include "base/threading/thread_checker.h"
  16. #include "base/time/time.h"
  17. #include "build/build_config.h"
  18. #include "services/device/geolocation/network_location_request.h"
  19. #include "services/device/geolocation/wifi_data_provider_handle.h"
  20. #include "services/device/public/cpp/geolocation/geolocation_manager.h"
  21. #include "services/device/public/cpp/geolocation/location_provider.h"
  22. #include "services/device/public/mojom/geoposition.mojom.h"
  23. namespace device {
  24. class PositionCache;
  25. class NetworkLocationProvider : public LocationProvider,
  26. public GeolocationManager::PermissionObserver {
  27. public:
  28. NetworkLocationProvider(
  29. scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
  30. GeolocationManager* geolocation_manager,
  31. const scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
  32. const std::string& api_key,
  33. PositionCache* position_cache);
  34. NetworkLocationProvider(const NetworkLocationProvider&) = delete;
  35. NetworkLocationProvider& operator=(const NetworkLocationProvider&) = delete;
  36. ~NetworkLocationProvider() override;
  37. // LocationProvider implementation
  38. void SetUpdateCallback(const LocationProviderUpdateCallback& cb) override;
  39. void StartProvider(bool high_accuracy) override;
  40. void StopProvider() override;
  41. const mojom::Geoposition& GetPosition() override;
  42. void OnPermissionGranted() override;
  43. // GeolocationPermissionObserver implementation.
  44. void OnSystemPermissionUpdated(
  45. LocationSystemPermissionStatus new_status) override;
  46. private:
  47. // Tries to update |position_| request from cache or network.
  48. void RequestPosition();
  49. // Gets called when new wifi data is available, either via explicit request to
  50. // or callback from |wifi_data_provider_handle_|.
  51. void OnWifiDataUpdate();
  52. bool IsStarted() const;
  53. void OnLocationResponse(const mojom::Geoposition& position,
  54. bool server_error,
  55. const WifiData& wifi_data);
  56. // The wifi data provider, acquired via global factories. Valid between
  57. // StartProvider() and StopProvider(), and checked via IsStarted().
  58. std::unique_ptr<WifiDataProviderHandle> wifi_data_provider_handle_;
  59. WifiDataProviderHandle::WifiDataUpdateCallback wifi_data_update_callback_;
  60. #if BUILDFLAG(IS_MAC)
  61. // Used to keep track of macOS System Permission changes. Also, ensures
  62. // lifetime of PermissionObserverList as the BrowserProcess may destroy its
  63. // reference on the UI Thread before we destroy this provider.
  64. scoped_refptr<GeolocationManager::PermissionObserverList>
  65. permission_observers_;
  66. raw_ptr<GeolocationManager> geolocation_manager_;
  67. #endif
  68. // The wifi data and a flag to indicate if the data set is complete.
  69. WifiData wifi_data_;
  70. bool is_wifi_data_complete_;
  71. // The timestamp for the latest wifi data update.
  72. base::Time wifi_timestamp_;
  73. const raw_ptr<PositionCache> position_cache_;
  74. LocationProvider::LocationProviderUpdateCallback
  75. location_provider_update_callback_;
  76. // Whether permission has been granted for the provider to operate.
  77. bool is_permission_granted_;
  78. bool is_new_data_available_;
  79. // The network location request object.
  80. const std::unique_ptr<NetworkLocationRequest> request_;
  81. base::ThreadChecker thread_checker_;
  82. bool is_system_permission_granted_ = false;
  83. bool is_awaiting_initial_permission_status_ = true;
  84. base::WeakPtrFactory<NetworkLocationProvider> weak_factory_{this};
  85. };
  86. } // namespace device
  87. #endif // SERVICES_DEVICE_GEOLOCATION_NETWORK_LOCATION_PROVIDER_H_