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
3 // found in the LICENSE file.
5 #include "content/browser/geolocation/geolocation_service_impl.h"
8 #include "base/metrics/histogram.h"
9 #include "content/browser/geolocation/geolocation_service_context.h"
10 #include "content/public/common/mojo_geoposition.mojom.h"
16 // Geoposition error codes for reporting in UMA.
17 enum GeopositionErrorCode
{
18 // NOTE: Do not renumber these as that would confuse interpretation of
19 // previously logged data. When making changes, also update the enum list
20 // in tools/metrics/histograms/histograms.xml to keep it in sync.
22 // There was no error.
23 GEOPOSITION_ERROR_CODE_NONE
= 0,
25 // User denied use of geolocation.
26 GEOPOSITION_ERROR_CODE_PERMISSION_DENIED
= 1,
28 // Geoposition could not be determined.
29 GEOPOSITION_ERROR_CODE_POSITION_UNAVAILABLE
= 2,
32 GEOPOSITION_ERROR_CODE_TIMEOUT
= 3,
34 // NOTE: Add entries only immediately above this line.
35 GEOPOSITION_ERROR_CODE_COUNT
= 4
38 void RecordGeopositionErrorCode(Geoposition::ErrorCode error_code
) {
39 GeopositionErrorCode code
= GEOPOSITION_ERROR_CODE_NONE
;
41 case Geoposition::ERROR_CODE_NONE
:
42 code
= GEOPOSITION_ERROR_CODE_NONE
;
44 case Geoposition::ERROR_CODE_PERMISSION_DENIED
:
45 code
= GEOPOSITION_ERROR_CODE_PERMISSION_DENIED
;
47 case Geoposition::ERROR_CODE_POSITION_UNAVAILABLE
:
48 code
= GEOPOSITION_ERROR_CODE_POSITION_UNAVAILABLE
;
50 case Geoposition::ERROR_CODE_TIMEOUT
:
51 code
= GEOPOSITION_ERROR_CODE_TIMEOUT
;
54 UMA_HISTOGRAM_ENUMERATION("Geolocation.LocationUpdate.ErrorCode",
56 GEOPOSITION_ERROR_CODE_COUNT
);
61 GeolocationServiceImpl::GeolocationServiceImpl(
62 GeolocationServiceContext
* context
,
63 const base::Closure
& update_callback
)
65 update_callback_(update_callback
),
66 high_accuracy_(false),
67 has_position_to_report_(false) {
71 GeolocationServiceImpl::~GeolocationServiceImpl() {
72 // Make sure to respond to any pending callback even without a valid position.
73 if (!position_callback_
.is_null()) {
74 if (!current_position_
.valid
) {
75 current_position_
.error_code
= MojoGeoposition::ErrorCode(
76 GEOPOSITION_ERROR_CODE_POSITION_UNAVAILABLE
);
77 current_position_
.error_message
= mojo::String("");
79 ReportCurrentPosition();
83 void GeolocationServiceImpl::PauseUpdates() {
84 geolocation_subscription_
.reset();
87 void GeolocationServiceImpl::ResumeUpdates() {
88 if (position_override_
.Validate()) {
89 OnLocationUpdate(position_override_
);
93 StartListeningForUpdates();
96 void GeolocationServiceImpl::StartListeningForUpdates() {
97 geolocation_subscription_
=
98 GeolocationProvider::GetInstance()->AddLocationUpdateCallback(
99 base::Bind(&GeolocationServiceImpl::OnLocationUpdate
,
100 base::Unretained(this)),
104 void GeolocationServiceImpl::SetHighAccuracy(bool high_accuracy
) {
105 UMA_HISTOGRAM_BOOLEAN(
106 "Geolocation.GeolocationDispatcherHostImpl.EnableHighAccuracy",
108 high_accuracy_
= high_accuracy
;
110 if (position_override_
.Validate()) {
111 OnLocationUpdate(position_override_
);
115 StartListeningForUpdates();
118 void GeolocationServiceImpl::QueryNextPosition(
119 const PositionCallback
& callback
) {
120 if (!position_callback_
.is_null()) {
121 DVLOG(1) << "Overlapped call to QueryNextPosition!";
122 OnConnectionError(); // Simulate a connection error.
126 position_callback_
= callback
;
128 if (has_position_to_report_
)
129 ReportCurrentPosition();
132 void GeolocationServiceImpl::SetOverride(const Geoposition
& position
) {
133 position_override_
= position
;
134 if (!position_override_
.Validate()) {
138 geolocation_subscription_
.reset();
140 OnLocationUpdate(position_override_
);
143 void GeolocationServiceImpl::ClearOverride() {
144 position_override_
= Geoposition();
145 StartListeningForUpdates();
148 void GeolocationServiceImpl::OnConnectionError() {
149 context_
->ServiceHadConnectionError(this);
151 // The above call deleted this instance, so the only safe thing to do is
155 void GeolocationServiceImpl::OnLocationUpdate(const Geoposition
& position
) {
156 RecordGeopositionErrorCode(position
.error_code
);
159 if (context_
->paused())
162 update_callback_
.Run();
164 current_position_
.valid
= position
.Validate();
165 current_position_
.latitude
= position
.latitude
;
166 current_position_
.longitude
= position
.longitude
;
167 current_position_
.altitude
= position
.altitude
;
168 current_position_
.accuracy
= position
.accuracy
;
169 current_position_
.altitude_accuracy
= position
.altitude_accuracy
;
170 current_position_
.heading
= position
.heading
;
171 current_position_
.speed
= position
.speed
;
172 current_position_
.timestamp
= position
.timestamp
.ToDoubleT();
173 current_position_
.error_code
=
174 MojoGeoposition::ErrorCode(position
.error_code
);
175 current_position_
.error_message
= position
.error_message
;
177 has_position_to_report_
= true;
179 if (!position_callback_
.is_null())
180 ReportCurrentPosition();
183 void GeolocationServiceImpl::ReportCurrentPosition() {
184 position_callback_
.Run(current_position_
.Clone());
185 position_callback_
.reset();
186 has_position_to_report_
= false;
189 } // namespace content