public_ip_address_geolocator.cc 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. #include "services/device/geolocation/public_ip_address_geolocator.h"
  5. #include "base/bind.h"
  6. #include "net/traffic_annotation/network_traffic_annotation.h"
  7. #include "services/device/geolocation/public_ip_address_location_notifier.h"
  8. namespace device {
  9. PublicIpAddressGeolocator::PublicIpAddressGeolocator(
  10. const net::PartialNetworkTrafficAnnotationTag tag,
  11. PublicIpAddressLocationNotifier* const notifier,
  12. BadMessageCallback callback)
  13. : last_updated_timestamp_(),
  14. notifier_(notifier),
  15. network_traffic_annotation_tag_(
  16. std::make_unique<const net::PartialNetworkTrafficAnnotationTag>(tag)),
  17. bad_message_callback_(callback) {}
  18. PublicIpAddressGeolocator::~PublicIpAddressGeolocator() {}
  19. void PublicIpAddressGeolocator::QueryNextPosition(
  20. QueryNextPositionCallback callback) {
  21. if (query_next_position_callback_) {
  22. bad_message_callback_.Run(
  23. "Overlapping calls to QueryNextPosition are prohibited.");
  24. return;
  25. }
  26. DCHECK(notifier_);
  27. // Request the next position after the latest one we received.
  28. notifier_->QueryNextPosition(
  29. last_updated_timestamp_, *network_traffic_annotation_tag_,
  30. base::BindOnce(&PublicIpAddressGeolocator::OnPositionUpdate,
  31. base::Unretained(this)));
  32. // Retain the callback to use if/when we get a new position.
  33. query_next_position_callback_ = std::move(callback);
  34. }
  35. // Low/high accuracy toggle is ignored by this implementation.
  36. void PublicIpAddressGeolocator::SetHighAccuracy(bool /* high_accuracy */) {}
  37. void PublicIpAddressGeolocator::OnPositionUpdate(
  38. const mojom::Geoposition& position) {
  39. last_updated_timestamp_ = position.timestamp;
  40. // Use Clone since query_next_position_callback_ needs an
  41. // device::mojom::GeopositionPtr.
  42. std::move(query_next_position_callback_).Run(position.Clone());
  43. }
  44. } // namespace device