DevTools: cut host and port from webSocketDebuggerUrl in addition to ws:// prefix
[chromium-blink-merge.git] / content / browser / geolocation / geolocation_service_impl.cc
blob6c3a5c8b6b7d4481d4ab1295d49e8c1cd65c6c17
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"
7 #include "base/bind.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"
12 namespace content {
14 namespace {
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,
31 // Timeout.
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;
40 switch (error_code) {
41 case Geoposition::ERROR_CODE_NONE:
42 code = GEOPOSITION_ERROR_CODE_NONE;
43 break;
44 case Geoposition::ERROR_CODE_PERMISSION_DENIED:
45 code = GEOPOSITION_ERROR_CODE_PERMISSION_DENIED;
46 break;
47 case Geoposition::ERROR_CODE_POSITION_UNAVAILABLE:
48 code = GEOPOSITION_ERROR_CODE_POSITION_UNAVAILABLE;
49 break;
50 case Geoposition::ERROR_CODE_TIMEOUT:
51 code = GEOPOSITION_ERROR_CODE_TIMEOUT;
52 break;
54 UMA_HISTOGRAM_ENUMERATION("Geolocation.LocationUpdate.ErrorCode",
55 code,
56 GEOPOSITION_ERROR_CODE_COUNT);
59 } // namespace
61 GeolocationServiceImpl::GeolocationServiceImpl(
62 mojo::InterfaceRequest<GeolocationService> request,
63 GeolocationServiceContext* context,
64 const base::Closure& update_callback)
65 : binding_(this, request.Pass()),
66 context_(context),
67 update_callback_(update_callback),
68 high_accuracy_(false),
69 has_position_to_report_(false) {
70 DCHECK(context_);
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_);
93 return;
96 StartListeningForUpdates();
99 void GeolocationServiceImpl::StartListeningForUpdates() {
100 geolocation_subscription_ =
101 GeolocationProvider::GetInstance()->AddLocationUpdateCallback(
102 base::Bind(&GeolocationServiceImpl::OnLocationUpdate,
103 base::Unretained(this)),
104 high_accuracy_);
107 void GeolocationServiceImpl::SetHighAccuracy(bool high_accuracy) {
108 UMA_HISTOGRAM_BOOLEAN(
109 "Geolocation.GeolocationDispatcherHostImpl.EnableHighAccuracy",
110 high_accuracy);
111 high_accuracy_ = high_accuracy;
113 if (position_override_.Validate()) {
114 OnLocationUpdate(position_override_);
115 return;
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.
126 return;
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()) {
138 ResumeUpdates();
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
155 // return.
158 void GeolocationServiceImpl::OnLocationUpdate(const Geoposition& position) {
159 RecordGeopositionErrorCode(position.error_code);
160 DCHECK(context_);
162 if (context_->paused())
163 return;
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