public_ip_address_location_notifier.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright 2017 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_PUBLIC_IP_ADDRESS_LOCATION_NOTIFIER_H_
  5. #define SERVICES_DEVICE_GEOLOCATION_PUBLIC_IP_ADDRESS_LOCATION_NOTIFIER_H_
  6. #include <memory>
  7. #include "base/callback.h"
  8. #include "base/callback_list.h"
  9. #include "base/cancelable_callback.h"
  10. #include "base/memory/raw_ptr.h"
  11. #include "base/time/time.h"
  12. #include "net/traffic_annotation/network_traffic_annotation.h"
  13. #include "services/device/geolocation/geolocation_provider.h"
  14. #include "services/device/geolocation/network_location_request.h"
  15. #include "services/device/public/mojom/geoposition.mojom.h"
  16. #include "services/network/public/cpp/network_connection_tracker.h"
  17. #include "third_party/abseil-cpp/absl/types/optional.h"
  18. namespace device {
  19. class NetworkLocationRequest;
  20. struct WifiData;
  21. // Provides subscribers with updates of the device's approximate geographic
  22. // location inferred from its publicly-visible IP address.
  23. // Sequencing:
  24. // * Must be created, used, and destroyed on the same sequence.
  25. class PublicIpAddressLocationNotifier
  26. : public network::NetworkConnectionTracker::NetworkConnectionObserver {
  27. public:
  28. // Creates a notifier that uses the specified Google |api_key| and
  29. // |url_loader_factory| for network location requests.
  30. PublicIpAddressLocationNotifier(
  31. scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
  32. network::NetworkConnectionTracker* network_connection_tracker,
  33. const std::string& api_key);
  34. PublicIpAddressLocationNotifier(const PublicIpAddressLocationNotifier&) =
  35. delete;
  36. PublicIpAddressLocationNotifier& operator=(
  37. const PublicIpAddressLocationNotifier&) = delete;
  38. ~PublicIpAddressLocationNotifier() override;
  39. using QueryNextPositionCallback =
  40. base::OnceCallback<void(const mojom::Geoposition&)>;
  41. // Requests a callback with the next Geoposition obtained later than
  42. // |time_of_prev_position|.
  43. // Specifically:
  44. // * If a position has been obtained subsequent to |time_of_prev_position|,
  45. // returns it.
  46. // * Otherwise, returns an updated position once a network change has
  47. // occurred.
  48. // * Note that it is possible for |callback| to never be called if no network
  49. // change ever occurs after |time_of_prev_position|.
  50. void QueryNextPosition(base::Time time_of_prev_position,
  51. const net::PartialNetworkTrafficAnnotationTag& tag,
  52. QueryNextPositionCallback callback);
  53. private:
  54. // Sequence checker for all methods.
  55. SEQUENCE_CHECKER(sequence_checker_);
  56. // NetworkConnectionTracker::NetworkConnectionObserver:
  57. // Network change notifications tend to come in a cluster in a short time, so
  58. // this just sets a task to run ReactToNetworkChange after a short time.
  59. void OnConnectionChanged(network::mojom::ConnectionType type) override;
  60. // Actually react to a network change, starting a network geolocation request
  61. // if any clients are waiting.
  62. void ReactToNetworkChange();
  63. // Creates |network_location_request_| and starts the network request, which
  64. // will invoke OnNetworkLocationResponse when done.
  65. void MakeNetworkLocationRequest();
  66. // Completion callback for network_location_request_.
  67. void OnNetworkLocationResponse(const mojom::Geoposition& position,
  68. bool server_error,
  69. const WifiData& wifi_data);
  70. // Cancelable closure to absorb overlapping delayed calls to
  71. // ReactToNetworkChange.
  72. base::CancelableOnceClosure react_to_network_change_closure_;
  73. // Whether we have been notified of a network change since the last network
  74. // location request was sent.
  75. bool network_changed_since_last_request_;
  76. // The geoposition as of the latest network change, if it has been obtained.
  77. absl::optional<mojom::Geoposition> latest_geoposition_;
  78. // Google API key for network geolocation requests.
  79. const std::string api_key_;
  80. // SharedURLLoaderFactory for network geolocation requests.
  81. const scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_;
  82. // Used to listen to network connection changes.
  83. // Must outlive this object.
  84. raw_ptr<network::NetworkConnectionTracker> network_connection_tracker_;
  85. // Used to make calls to the Maps geolocate API.
  86. // Empty unless a call is currently in progress.
  87. std::unique_ptr<NetworkLocationRequest> network_location_request_;
  88. // Clients waiting for an updated geoposition.
  89. std::vector<QueryNextPositionCallback> callbacks_;
  90. // The most recent PartialNetworkTrafficAnnotationTag provided by a client.
  91. std::unique_ptr<const net::PartialNetworkTrafficAnnotationTag>
  92. network_traffic_annotation_tag_;
  93. // Weak references to |this| for posted tasks.
  94. base::WeakPtrFactory<PublicIpAddressLocationNotifier> weak_ptr_factory_{this};
  95. };
  96. } // namespace device
  97. #endif // SERVICES_DEVICE_GEOLOCATION_PUBLIC_IP_ADDRESS_LOCATION_NOTIFIER_H_