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 mojo::InterfaceRequest
<GeolocationService
> request
,
63 GeolocationServiceContext
* context
,
64 const base::Closure
& update_callback
)
65 : binding_(this, request
.Pass()),
67 update_callback_(update_callback
),
68 high_accuracy_(false),
69 has_position_to_report_(false) {
71 binding_
.set_error_handler(this);
74 GeolocationServiceImpl::~GeolocationServiceImpl() {
75 // Make sure to respond to any pending callback even without a valid position.
76 if (!position_callback_
.is_null()) {
77 if (!current_position_
.valid
) {
78 current_position_
.error_code
= MojoGeoposition::ErrorCode(
79 GEOPOSITION_ERROR_CODE_POSITION_UNAVAILABLE
);
80 current_position_
.error_message
= mojo::String("");
82 ReportCurrentPosition();
86 void GeolocationServiceImpl::PauseUpdates() {
87 geolocation_subscription_
.reset();
90 void GeolocationServiceImpl::ResumeUpdates() {
91 if (position_override_
.Validate()) {
92 OnLocationUpdate(position_override_
);
96 StartListeningForUpdates();
99 void GeolocationServiceImpl::StartListeningForUpdates() {
100 geolocation_subscription_
=
101 GeolocationProvider::GetInstance()->AddLocationUpdateCallback(
102 base::Bind(&GeolocationServiceImpl::OnLocationUpdate
,
103 base::Unretained(this)),
107 void GeolocationServiceImpl::SetHighAccuracy(bool high_accuracy
) {
108 UMA_HISTOGRAM_BOOLEAN(
109 "Geolocation.GeolocationDispatcherHostImpl.EnableHighAccuracy",
111 high_accuracy_
= high_accuracy
;
113 if (position_override_
.Validate()) {
114 OnLocationUpdate(position_override_
);
118 StartListeningForUpdates();
121 void GeolocationServiceImpl::QueryNextPosition(
122 const PositionCallback
& callback
) {
123 if (!position_callback_
.is_null()) {
124 DVLOG(1) << "Overlapped call to QueryNextPosition!";
125 OnConnectionError(); // Simulate a connection error.
129 position_callback_
= callback
;
131 if (has_position_to_report_
)
132 ReportCurrentPosition();
135 void GeolocationServiceImpl::SetOverride(const Geoposition
& position
) {
136 position_override_
= position
;
137 if (!position_override_
.Validate()) {
141 geolocation_subscription_
.reset();
143 OnLocationUpdate(position_override_
);
146 void GeolocationServiceImpl::ClearOverride() {
147 position_override_
= Geoposition();
148 StartListeningForUpdates();
151 void GeolocationServiceImpl::OnConnectionError() {
152 context_
->ServiceHadConnectionError(this);
154 // The above call deleted this instance, so the only safe thing to do is
158 void GeolocationServiceImpl::OnLocationUpdate(const Geoposition
& position
) {
159 RecordGeopositionErrorCode(position
.error_code
);
162 if (context_
->paused())
165 update_callback_
.Run();
167 current_position_
.valid
= position
.Validate();
168 current_position_
.latitude
= position
.latitude
;
169 current_position_
.longitude
= position
.longitude
;
170 current_position_
.altitude
= position
.altitude
;
171 current_position_
.accuracy
= position
.accuracy
;
172 current_position_
.altitude_accuracy
= position
.altitude_accuracy
;
173 current_position_
.heading
= position
.heading
;
174 current_position_
.speed
= position
.speed
;
175 current_position_
.timestamp
= position
.timestamp
.ToDoubleT();
176 current_position_
.error_code
=
177 MojoGeoposition::ErrorCode(position
.error_code
);
178 current_position_
.error_message
= position
.error_message
;
180 has_position_to_report_
= true;
182 if (!position_callback_
.is_null())
183 ReportCurrentPosition();
186 void GeolocationServiceImpl::ReportCurrentPosition() {
187 position_callback_
.Run(current_position_
.Clone());
188 position_callback_
.reset();
189 has_position_to_report_
= false;
192 } // namespace content