Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(374)

Side by Side Diff: content/browser/geolocation/geolocation_service_impl.cc

Issue 628773003: Partially convert geolocation IPC to Mojo. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Response to reviews, port override impl Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 #include "content/browser/geolocation/geolocation_service_impl.h"
6
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"
11
12 namespace content {
13
14 namespace {
15
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.
21
22 // There was no error.
23 GEOPOSITION_ERROR_CODE_NONE = 0,
24
25 // User denied use of geolocation.
26 GEOPOSITION_ERROR_CODE_PERMISSION_DENIED = 1,
27
28 // Geoposition could not be determined.
29 GEOPOSITION_ERROR_CODE_POSITION_UNAVAILABLE = 2,
30
31 // Timeout.
32 GEOPOSITION_ERROR_CODE_TIMEOUT = 3,
33
34 // NOTE: Add entries only immediately above this line.
35 GEOPOSITION_ERROR_CODE_COUNT = 4
36 };
37
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;
53 }
54 UMA_HISTOGRAM_ENUMERATION("Geolocation.LocationUpdate.ErrorCode",
55 code,
56 GEOPOSITION_ERROR_CODE_COUNT);
57 }
58
59 } // namespace
60
61 GeolocationServiceImpl::GeolocationServiceImpl(
62 GeolocationServiceContext* context,
63 const base::Closure& update_callback)
64 : context_(context),
65 update_callback_(update_callback),
66 client_asked_for_updates_(false) {
67 DCHECK(context_);
68 }
69
70 GeolocationServiceImpl::~GeolocationServiceImpl() {
71 }
72
73 void GeolocationServiceImpl::PauseUpdates() {
74 geolocation_subscription_.reset();
75 }
76
77 void GeolocationServiceImpl::ResumeUpdates() {
78 if (position_override_.Validate()) {
79 OnLocationUpdate(position_override_);
80 return;
81 }
82
83 if (client_asked_for_updates_)
84 StartListeningForUpdates();
85 }
86
87 void GeolocationServiceImpl::StartListeningForUpdates() {
88 geolocation_subscription_ =
89 GeolocationProvider::GetInstance()->AddLocationUpdateCallback(
90 base::Bind(&GeolocationServiceImpl::OnLocationUpdate,
91 base::Unretained(this)),
92 high_accuracy_);
93 }
94
95 void GeolocationServiceImpl::StartUpdating(bool high_accuracy) {
96 UMA_HISTOGRAM_BOOLEAN(
97 "Geolocation.GeolocationDispatcherHostImpl.EnableHighAccuracy",
98 high_accuracy);
99 high_accuracy_ = high_accuracy;
100 client_asked_for_updates_ = true;
101
102 if (position_override_.Validate()) {
103 OnLocationUpdate(position_override_);
104 return;
105 }
106
107 StartListeningForUpdates();
108 }
109
110 void GeolocationServiceImpl::SetOverride(const Geoposition& position) {
111 position_override_ = position;
112 if (!position_override_.Validate()) {
113 ResumeUpdates();
114 }
115
116 geolocation_subscription_.reset();
117
118 if (client_asked_for_updates_)
119 OnLocationUpdate(position_override_);
120 }
121
122 void GeolocationServiceImpl::ClearOverride() {
123 position_override_ = Geoposition();
124 ResumeUpdates();
125 }
126
127 void GeolocationServiceImpl::OnConnectionError() {
128 context_->ServiceHadConnectionError(this);
129
130 // The above call deleted this instance, so the only safe thing to do is
131 // return.
132 }
133
134 void GeolocationServiceImpl::OnLocationUpdate(const Geoposition& position) {
135 RecordGeopositionErrorCode(position.error_code);
136 DCHECK(context_);
137 DCHECK(client_asked_for_updates_);
138
139 if (context_->paused())
140 return;
141
142 MojoGeopositionPtr geoposition(MojoGeoposition::New());
143 geoposition->valid = position.Validate();
144 geoposition->latitude = position.latitude;
145 geoposition->longitude = position.longitude;
146 geoposition->altitude = position.altitude;
147 geoposition->accuracy = position.accuracy;
148 geoposition->altitude_accuracy = position.altitude_accuracy;
149 geoposition->heading = position.heading;
150 geoposition->speed = position.speed;
151 geoposition->timestamp = position.timestamp.ToDoubleT();
152 geoposition->error_code = MojoGeoposition::ErrorCode(position.error_code);
153 geoposition->error_message = position.error_message;
154
155 update_callback_.Run();
156 client()->OnLocationUpdate(geoposition.Pass());
157 }
158
159 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698