OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/geolocation/geolocation_service_impl.h" | 5 #include "content/browser/geolocation/geolocation_service_impl.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
9 #include "content/browser/geolocation/geolocation_service_context.h" | 9 #include "content/browser/geolocation/geolocation_service_context.h" |
10 #include "content/public/common/mojo_geoposition.mojom.h" | 10 #include "content/public/common/mojo_geoposition.mojom.h" |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 GEOPOSITION_ERROR_CODE_COUNT); | 56 GEOPOSITION_ERROR_CODE_COUNT); |
57 } | 57 } |
58 | 58 |
59 } // namespace | 59 } // namespace |
60 | 60 |
61 GeolocationServiceImpl::GeolocationServiceImpl( | 61 GeolocationServiceImpl::GeolocationServiceImpl( |
62 GeolocationServiceContext* context, | 62 GeolocationServiceContext* context, |
63 const base::Closure& update_callback) | 63 const base::Closure& update_callback) |
64 : context_(context), | 64 : context_(context), |
65 update_callback_(update_callback), | 65 update_callback_(update_callback), |
66 high_accuracy_(false) { | 66 high_accuracy_(false), |
| 67 has_position_to_report_(false) { |
67 DCHECK(context_); | 68 DCHECK(context_); |
68 } | 69 } |
69 | 70 |
70 GeolocationServiceImpl::~GeolocationServiceImpl() { | 71 GeolocationServiceImpl::~GeolocationServiceImpl() { |
71 } | 72 } |
72 | 73 |
73 void GeolocationServiceImpl::PauseUpdates() { | 74 void GeolocationServiceImpl::PauseUpdates() { |
74 geolocation_subscription_.reset(); | 75 geolocation_subscription_.reset(); |
75 } | 76 } |
76 | 77 |
(...skipping 21 matching lines...) Expand all Loading... |
98 high_accuracy_ = high_accuracy; | 99 high_accuracy_ = high_accuracy; |
99 | 100 |
100 if (position_override_.Validate()) { | 101 if (position_override_.Validate()) { |
101 OnLocationUpdate(position_override_); | 102 OnLocationUpdate(position_override_); |
102 return; | 103 return; |
103 } | 104 } |
104 | 105 |
105 StartListeningForUpdates(); | 106 StartListeningForUpdates(); |
106 } | 107 } |
107 | 108 |
| 109 void GeolocationServiceImpl::QueryNextPosition( |
| 110 const PositionCallback& callback) { |
| 111 position_callbacks_.push_back(callback); |
| 112 |
| 113 if (has_position_to_report_) |
| 114 ReportCurrentPosition(); |
| 115 } |
| 116 |
108 void GeolocationServiceImpl::SetOverride(const Geoposition& position) { | 117 void GeolocationServiceImpl::SetOverride(const Geoposition& position) { |
109 position_override_ = position; | 118 position_override_ = position; |
110 if (!position_override_.Validate()) { | 119 if (!position_override_.Validate()) { |
111 ResumeUpdates(); | 120 ResumeUpdates(); |
112 } | 121 } |
113 | 122 |
114 geolocation_subscription_.reset(); | 123 geolocation_subscription_.reset(); |
115 | 124 |
116 OnLocationUpdate(position_override_); | 125 OnLocationUpdate(position_override_); |
117 } | 126 } |
(...skipping 10 matching lines...) Expand all Loading... |
128 // return. | 137 // return. |
129 } | 138 } |
130 | 139 |
131 void GeolocationServiceImpl::OnLocationUpdate(const Geoposition& position) { | 140 void GeolocationServiceImpl::OnLocationUpdate(const Geoposition& position) { |
132 RecordGeopositionErrorCode(position.error_code); | 141 RecordGeopositionErrorCode(position.error_code); |
133 DCHECK(context_); | 142 DCHECK(context_); |
134 | 143 |
135 if (context_->paused()) | 144 if (context_->paused()) |
136 return; | 145 return; |
137 | 146 |
138 MojoGeopositionPtr geoposition(MojoGeoposition::New()); | 147 update_callback_.Run(); |
139 geoposition->valid = position.Validate(); | |
140 geoposition->latitude = position.latitude; | |
141 geoposition->longitude = position.longitude; | |
142 geoposition->altitude = position.altitude; | |
143 geoposition->accuracy = position.accuracy; | |
144 geoposition->altitude_accuracy = position.altitude_accuracy; | |
145 geoposition->heading = position.heading; | |
146 geoposition->speed = position.speed; | |
147 geoposition->timestamp = position.timestamp.ToDoubleT(); | |
148 geoposition->error_code = MojoGeoposition::ErrorCode(position.error_code); | |
149 geoposition->error_message = position.error_message; | |
150 | 148 |
151 update_callback_.Run(); | 149 current_position_.valid = position.Validate(); |
152 client()->OnLocationUpdate(geoposition.Pass()); | 150 current_position_.latitude = position.latitude; |
| 151 current_position_.longitude = position.longitude; |
| 152 current_position_.altitude = position.altitude; |
| 153 current_position_.accuracy = position.accuracy; |
| 154 current_position_.altitude_accuracy = position.altitude_accuracy; |
| 155 current_position_.heading = position.heading; |
| 156 current_position_.speed = position.speed; |
| 157 current_position_.timestamp = position.timestamp.ToDoubleT(); |
| 158 current_position_.error_code = |
| 159 MojoGeoposition::ErrorCode(position.error_code); |
| 160 current_position_.error_message = position.error_message; |
| 161 |
| 162 has_position_to_report_ = true; |
| 163 |
| 164 if (!position_callbacks_.empty()) |
| 165 ReportCurrentPosition(); |
| 166 } |
| 167 |
| 168 void GeolocationServiceImpl::ReportCurrentPosition() { |
| 169 for (const auto& callback : position_callbacks_) |
| 170 callback.Run(current_position_.Clone()); |
| 171 position_callbacks_.clear(); |
| 172 has_position_to_report_ = false; |
153 } | 173 } |
154 | 174 |
155 } // namespace content | 175 } // namespace content |
OLD | NEW |