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/browser/browser_context.h" |
| 11 #include "content/public/browser/geolocation_provider.h" |
| 12 #include "content/public/browser/permission_manager.h" |
| 13 #include "content/public/browser/permission_type.h" |
| 14 #include "content/public/browser/render_frame_host.h" |
| 15 #include "content/public/browser/render_process_host.h" |
| 16 #include "content/public/common/geoposition.h" |
11 | 17 |
12 namespace content { | 18 namespace content { |
13 | |
14 namespace { | 19 namespace { |
15 | 20 |
16 // Geoposition error codes for reporting in UMA. | 21 // Geoposition error codes for reporting in UMA. |
17 enum GeopositionErrorCode { | 22 enum GeopositionErrorCode { |
18 // NOTE: Do not renumber these as that would confuse interpretation of | 23 // NOTE: Do not renumber these as that would confuse interpretation of |
19 // previously logged data. When making changes, also update the enum list | 24 // previously logged data. When making changes, also update the enum list |
20 // in tools/metrics/histograms/histograms.xml to keep it in sync. | 25 // in tools/metrics/histograms/histograms.xml to keep it in sync. |
21 | 26 |
22 // There was no error. | 27 // There was no error. |
23 GEOPOSITION_ERROR_CODE_NONE = 0, | 28 GEOPOSITION_ERROR_CODE_NONE = 0, |
(...skipping 20 matching lines...) Expand all Loading... |
44 case Geoposition::ERROR_CODE_PERMISSION_DENIED: | 49 case Geoposition::ERROR_CODE_PERMISSION_DENIED: |
45 code = GEOPOSITION_ERROR_CODE_PERMISSION_DENIED; | 50 code = GEOPOSITION_ERROR_CODE_PERMISSION_DENIED; |
46 break; | 51 break; |
47 case Geoposition::ERROR_CODE_POSITION_UNAVAILABLE: | 52 case Geoposition::ERROR_CODE_POSITION_UNAVAILABLE: |
48 code = GEOPOSITION_ERROR_CODE_POSITION_UNAVAILABLE; | 53 code = GEOPOSITION_ERROR_CODE_POSITION_UNAVAILABLE; |
49 break; | 54 break; |
50 case Geoposition::ERROR_CODE_TIMEOUT: | 55 case Geoposition::ERROR_CODE_TIMEOUT: |
51 code = GEOPOSITION_ERROR_CODE_TIMEOUT; | 56 code = GEOPOSITION_ERROR_CODE_TIMEOUT; |
52 break; | 57 break; |
53 } | 58 } |
54 UMA_HISTOGRAM_ENUMERATION("Geolocation.LocationUpdate.ErrorCode", | 59 UMA_HISTOGRAM_ENUMERATION("Geolocation.LocationUpdate.ErrorCode", code, |
55 code, | |
56 GEOPOSITION_ERROR_CODE_COUNT); | 60 GEOPOSITION_ERROR_CODE_COUNT); |
57 } | 61 } |
58 | 62 |
59 } // namespace | 63 } // namespace |
60 | 64 |
61 GeolocationServiceImpl::GeolocationServiceImpl( | 65 GeolocationServiceImpl::GeolocationServiceImpl( |
62 mojo::InterfaceRequest<GeolocationService> request, | |
63 GeolocationServiceContext* context, | 66 GeolocationServiceContext* context, |
64 const base::Closure& update_callback) | 67 RenderFrameHost* render_frame_host) |
65 : binding_(this, request.Pass()), | 68 : context_(context), |
66 context_(context), | 69 render_frame_host_(render_frame_host), |
67 update_callback_(update_callback), | 70 high_accuracy_(false) { |
68 high_accuracy_(false), | |
69 has_position_to_report_(false) { | |
70 DCHECK(context_); | 71 DCHECK(context_); |
71 binding_.set_connection_error_handler( | |
72 base::Bind(&GeolocationServiceImpl::OnConnectionError, | |
73 base::Unretained(this))); | |
74 } | 72 } |
75 | 73 |
76 GeolocationServiceImpl::~GeolocationServiceImpl() { | 74 GeolocationServiceImpl::~GeolocationServiceImpl() = default; |
77 // Make sure to respond to any pending callback even without a valid position. | |
78 if (!position_callback_.is_null()) { | |
79 if (!current_position_.valid) { | |
80 current_position_.error_code = MojoGeoposition::ErrorCode( | |
81 GEOPOSITION_ERROR_CODE_POSITION_UNAVAILABLE); | |
82 current_position_.error_message = mojo::String(""); | |
83 } | |
84 ReportCurrentPosition(); | |
85 } | |
86 } | |
87 | 75 |
88 void GeolocationServiceImpl::PauseUpdates() { | 76 void GeolocationServiceImpl::PauseUpdates() { |
89 geolocation_subscription_.reset(); | 77 geolocation_subscription_.reset(); |
90 } | 78 } |
91 | 79 |
92 void GeolocationServiceImpl::ResumeUpdates() { | 80 void GeolocationServiceImpl::ResumeUpdates() { |
93 if (position_override_.Validate()) { | 81 if (context_->geoposition_override_ && |
94 OnLocationUpdate(position_override_); | 82 context_->geoposition_override_->Validate()) { |
| 83 OnLocationUpdate(*context_->geoposition_override_); |
95 return; | 84 return; |
96 } | 85 } |
97 | 86 |
98 StartListeningForUpdates(); | 87 StartListeningForUpdates(); |
99 } | 88 } |
100 | 89 |
101 void GeolocationServiceImpl::StartListeningForUpdates() { | 90 void GeolocationServiceImpl::StartListeningForUpdates() { |
102 geolocation_subscription_ = | 91 geolocation_subscription_ = |
103 GeolocationProvider::GetInstance()->AddLocationUpdateCallback( | 92 GeolocationProvider::GetInstance()->AddLocationUpdateCallback( |
104 base::Bind(&GeolocationServiceImpl::OnLocationUpdate, | 93 base::Bind(&GeolocationServiceImpl::OnLocationUpdate, |
105 base::Unretained(this)), | 94 base::Unretained(this)), |
106 high_accuracy_); | 95 high_accuracy_); |
107 } | 96 } |
108 | 97 |
109 void GeolocationServiceImpl::SetHighAccuracy(bool high_accuracy) { | 98 void GeolocationServiceImpl::SetHighAccuracy(bool high_accuracy) { |
110 UMA_HISTOGRAM_BOOLEAN( | 99 UMA_HISTOGRAM_BOOLEAN( |
111 "Geolocation.GeolocationDispatcherHostImpl.EnableHighAccuracy", | 100 "Geolocation.GeolocationDispatcherHostImpl.EnableHighAccuracy", |
112 high_accuracy); | 101 high_accuracy); |
113 high_accuracy_ = high_accuracy; | 102 high_accuracy_ = high_accuracy; |
114 | 103 |
115 if (position_override_.Validate()) { | 104 if (context_->geoposition_override_ && |
116 OnLocationUpdate(position_override_); | 105 context_->geoposition_override_->Validate()) { |
| 106 OnLocationUpdate(*context_->geoposition_override_); |
117 return; | 107 return; |
118 } | 108 } |
119 | 109 |
120 StartListeningForUpdates(); | 110 StartListeningForUpdates(); |
121 } | 111 } |
122 | 112 |
123 void GeolocationServiceImpl::QueryNextPosition( | 113 void GeolocationServiceImpl::QueryNextPosition( |
124 const PositionCallback& callback) { | 114 const PositionCallback& callback) { |
125 if (!position_callback_.is_null()) { | 115 if (!position_callback_.is_null()) { |
126 DVLOG(1) << "Overlapped call to QueryNextPosition!"; | 116 DVLOG(1) << "Overlapped call to QueryNextPosition!"; |
127 OnConnectionError(); // Simulate a connection error. | 117 context_->services_.EraseService(this); |
128 return; | 118 return; |
129 } | 119 } |
130 | 120 |
131 position_callback_ = callback; | 121 position_callback_ = callback; |
132 | 122 |
133 if (has_position_to_report_) | 123 if (current_position_) |
134 ReportCurrentPosition(); | 124 ReportCurrentPosition(); |
135 } | 125 } |
136 | 126 |
137 void GeolocationServiceImpl::SetOverride(const Geoposition& position) { | 127 void GeolocationServiceImpl::OnOverrideSet() { |
138 position_override_ = position; | 128 if (!context_->geoposition_override_->Validate()) { |
139 if (!position_override_.Validate()) { | |
140 ResumeUpdates(); | 129 ResumeUpdates(); |
141 } | 130 } |
142 | 131 |
143 geolocation_subscription_.reset(); | 132 geolocation_subscription_.reset(); |
144 | 133 |
145 OnLocationUpdate(position_override_); | 134 OnLocationUpdate(*context_->geoposition_override_); |
146 } | 135 } |
147 | 136 |
148 void GeolocationServiceImpl::ClearOverride() { | 137 void GeolocationServiceImpl::ClearOverride() { |
149 position_override_ = Geoposition(); | |
150 StartListeningForUpdates(); | 138 StartListeningForUpdates(); |
151 } | 139 } |
152 | 140 |
153 void GeolocationServiceImpl::OnConnectionError() { | |
154 context_->ServiceHadConnectionError(this); | |
155 | |
156 // The above call deleted this instance, so the only safe thing to do is | |
157 // return. | |
158 } | |
159 | |
160 void GeolocationServiceImpl::OnLocationUpdate(const Geoposition& position) { | 141 void GeolocationServiceImpl::OnLocationUpdate(const Geoposition& position) { |
161 RecordGeopositionErrorCode(position.error_code); | 142 RecordGeopositionErrorCode(position.error_code); |
162 DCHECK(context_); | 143 DCHECK(context_); |
163 | 144 |
164 if (context_->paused()) | 145 if (context_->paused()) |
165 return; | 146 return; |
166 | 147 |
167 update_callback_.Run(); | 148 PermissionManager* permission_manager = render_frame_host_->GetProcess() |
| 149 ->GetBrowserContext() |
| 150 ->GetPermissionManager(); |
| 151 if (permission_manager) { |
| 152 permission_manager->RegisterPermissionUsage( |
| 153 PermissionType::GEOLOCATION, |
| 154 render_frame_host_->GetLastCommittedURL().GetOrigin(), |
| 155 WebContents::FromRenderFrameHost(render_frame_host_) |
| 156 ->GetMainFrame() |
| 157 ->GetLastCommittedURL() |
| 158 .GetOrigin()); |
| 159 } |
168 | 160 |
169 current_position_.valid = position.Validate(); | 161 if (!current_position_) |
170 current_position_.latitude = position.latitude; | 162 current_position_ = MojoGeoposition::New(); |
171 current_position_.longitude = position.longitude; | 163 current_position_->valid = position.Validate(); |
172 current_position_.altitude = position.altitude; | 164 current_position_->latitude = position.latitude; |
173 current_position_.accuracy = position.accuracy; | 165 current_position_->longitude = position.longitude; |
174 current_position_.altitude_accuracy = position.altitude_accuracy; | 166 current_position_->altitude = position.altitude; |
175 current_position_.heading = position.heading; | 167 current_position_->accuracy = position.accuracy; |
176 current_position_.speed = position.speed; | 168 current_position_->altitude_accuracy = position.altitude_accuracy; |
177 current_position_.timestamp = position.timestamp.ToDoubleT(); | 169 current_position_->heading = position.heading; |
178 current_position_.error_code = | 170 current_position_->speed = position.speed; |
| 171 current_position_->timestamp = position.timestamp.ToDoubleT(); |
| 172 current_position_->error_code = |
179 MojoGeoposition::ErrorCode(position.error_code); | 173 MojoGeoposition::ErrorCode(position.error_code); |
180 current_position_.error_message = position.error_message; | 174 current_position_->error_message = position.error_message; |
181 | |
182 has_position_to_report_ = true; | |
183 | 175 |
184 if (!position_callback_.is_null()) | 176 if (!position_callback_.is_null()) |
185 ReportCurrentPosition(); | 177 ReportCurrentPosition(); |
186 } | 178 } |
187 | 179 |
188 void GeolocationServiceImpl::ReportCurrentPosition() { | 180 void GeolocationServiceImpl::ReportCurrentPosition() { |
189 position_callback_.Run(current_position_.Clone()); | 181 position_callback_.Run(current_position_.Pass()); |
190 position_callback_.reset(); | 182 position_callback_.reset(); |
191 has_position_to_report_ = false; | |
192 } | 183 } |
193 | 184 |
194 } // namespace content | 185 } // namespace content |
OLD | NEW |