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