Chromium Code Reviews| 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_process_id_(render_frame_host->GetProcess()->GetID()), |
| 67 update_callback_(update_callback), | 70 render_frame_id_(render_frame_host->GetRoutingID()), |
| 68 high_accuracy_(false), | 71 high_accuracy_(false) { |
| 69 has_position_to_report_(false) { | |
| 70 DCHECK(context_); | 72 DCHECK(context_); |
| 71 binding_.set_connection_error_handler( | |
| 72 base::Bind(&GeolocationServiceImpl::OnConnectionError, | |
| 73 base::Unretained(this))); | |
| 74 } | 73 } |
| 75 | 74 |
| 76 GeolocationServiceImpl::~GeolocationServiceImpl() { | 75 GeolocationServiceImpl::~GeolocationServiceImpl() = default; |
| 77 // Make sure to respond to any pending callback even without a valid position. | |
|
Michael van Ouwerkerk
2015/11/11 11:20:56
Why is this no longer needed?
Sam McNally
2015/11/13 00:49:59
There are two reasons this might be needed:
(1) Th
| |
| 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 | 76 |
| 88 void GeolocationServiceImpl::PauseUpdates() { | 77 void GeolocationServiceImpl::PauseUpdates() { |
| 89 geolocation_subscription_.reset(); | 78 geolocation_subscription_.reset(); |
| 90 } | 79 } |
| 91 | 80 |
| 92 void GeolocationServiceImpl::ResumeUpdates() { | 81 void GeolocationServiceImpl::ResumeUpdates() { |
| 93 if (position_override_.Validate()) { | 82 if (context_->position_override() && |
| 94 OnLocationUpdate(position_override_); | 83 context_->position_override()->Validate()) { |
| 84 OnLocationUpdate(*context_->position_override()); | |
| 95 return; | 85 return; |
| 96 } | 86 } |
| 97 | 87 |
| 98 StartListeningForUpdates(); | 88 StartListeningForUpdates(); |
| 99 } | 89 } |
| 100 | 90 |
| 101 void GeolocationServiceImpl::StartListeningForUpdates() { | 91 void GeolocationServiceImpl::StartListeningForUpdates() { |
| 102 geolocation_subscription_ = | 92 geolocation_subscription_ = |
| 103 GeolocationProvider::GetInstance()->AddLocationUpdateCallback( | 93 GeolocationProvider::GetInstance()->AddLocationUpdateCallback( |
| 104 base::Bind(&GeolocationServiceImpl::OnLocationUpdate, | 94 base::Bind(&GeolocationServiceImpl::OnLocationUpdate, |
| 105 base::Unretained(this)), | 95 base::Unretained(this)), |
| 106 high_accuracy_); | 96 high_accuracy_); |
| 107 } | 97 } |
| 108 | 98 |
| 109 void GeolocationServiceImpl::SetHighAccuracy(bool high_accuracy) { | 99 void GeolocationServiceImpl::SetHighAccuracy(bool high_accuracy) { |
| 110 UMA_HISTOGRAM_BOOLEAN( | 100 UMA_HISTOGRAM_BOOLEAN( |
| 111 "Geolocation.GeolocationDispatcherHostImpl.EnableHighAccuracy", | 101 "Geolocation.GeolocationDispatcherHostImpl.EnableHighAccuracy", |
| 112 high_accuracy); | 102 high_accuracy); |
| 113 high_accuracy_ = high_accuracy; | 103 high_accuracy_ = high_accuracy; |
| 114 | 104 |
| 115 if (position_override_.Validate()) { | 105 if (context_->position_override() && |
| 116 OnLocationUpdate(position_override_); | 106 context_->position_override()->Validate()) { |
| 107 OnLocationUpdate(*context_->position_override()); | |
| 117 return; | 108 return; |
| 118 } | 109 } |
| 119 | 110 |
| 120 StartListeningForUpdates(); | 111 StartListeningForUpdates(); |
| 121 } | 112 } |
| 122 | 113 |
| 123 void GeolocationServiceImpl::QueryNextPosition( | 114 void GeolocationServiceImpl::QueryNextPosition( |
| 124 const PositionCallback& callback) { | 115 const PositionCallback& callback) { |
| 125 if (!position_callback_.is_null()) { | 116 if (!position_callback_.is_null()) { |
| 126 DVLOG(1) << "Overlapped call to QueryNextPosition!"; | 117 DVLOG(1) << "Overlapped call to QueryNextPosition!"; |
| 127 OnConnectionError(); // Simulate a connection error. | 118 context_->DestroyService(this); |
| 128 return; | 119 return; |
| 129 } | 120 } |
| 130 | 121 |
| 131 position_callback_ = callback; | 122 position_callback_ = callback; |
| 132 | 123 |
| 133 if (has_position_to_report_) | 124 if (current_position_) |
| 134 ReportCurrentPosition(); | 125 ReportCurrentPosition(); |
| 135 } | 126 } |
| 136 | 127 |
| 137 void GeolocationServiceImpl::SetOverride(const Geoposition& position) { | 128 void GeolocationServiceImpl::OnOverrideSet() { |
| 138 position_override_ = position; | 129 if (!context_->position_override()->Validate()) { |
| 139 if (!position_override_.Validate()) { | |
| 140 ResumeUpdates(); | 130 ResumeUpdates(); |
| 141 } | 131 } |
| 142 | 132 |
| 143 geolocation_subscription_.reset(); | 133 geolocation_subscription_.reset(); |
| 144 | 134 |
| 145 OnLocationUpdate(position_override_); | 135 OnLocationUpdate(*context_->position_override()); |
| 146 } | 136 } |
| 147 | 137 |
| 148 void GeolocationServiceImpl::ClearOverride() { | 138 void GeolocationServiceImpl::ClearOverride() { |
| 149 position_override_ = Geoposition(); | |
| 150 StartListeningForUpdates(); | 139 StartListeningForUpdates(); |
| 151 } | 140 } |
| 152 | 141 |
| 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) { | 142 void GeolocationServiceImpl::OnLocationUpdate(const Geoposition& position) { |
| 161 RecordGeopositionErrorCode(position.error_code); | 143 RecordGeopositionErrorCode(position.error_code); |
| 162 DCHECK(context_); | 144 DCHECK(context_); |
| 163 | 145 |
| 164 if (context_->paused()) | 146 if (context_->paused()) |
| 165 return; | 147 return; |
| 166 | 148 |
| 167 update_callback_.Run(); | 149 ReportPermissionUsage(); |
| 168 | 150 |
| 169 current_position_.valid = position.Validate(); | 151 if (!current_position_) |
| 170 current_position_.latitude = position.latitude; | 152 current_position_ = MojoGeoposition::New(); |
| 171 current_position_.longitude = position.longitude; | 153 current_position_->valid = position.Validate(); |
| 172 current_position_.altitude = position.altitude; | 154 current_position_->latitude = position.latitude; |
| 173 current_position_.accuracy = position.accuracy; | 155 current_position_->longitude = position.longitude; |
| 174 current_position_.altitude_accuracy = position.altitude_accuracy; | 156 current_position_->altitude = position.altitude; |
| 175 current_position_.heading = position.heading; | 157 current_position_->accuracy = position.accuracy; |
| 176 current_position_.speed = position.speed; | 158 current_position_->altitude_accuracy = position.altitude_accuracy; |
| 177 current_position_.timestamp = position.timestamp.ToDoubleT(); | 159 current_position_->heading = position.heading; |
| 178 current_position_.error_code = | 160 current_position_->speed = position.speed; |
| 161 current_position_->timestamp = position.timestamp.ToDoubleT(); | |
| 162 current_position_->error_code = | |
| 179 MojoGeoposition::ErrorCode(position.error_code); | 163 MojoGeoposition::ErrorCode(position.error_code); |
| 180 current_position_.error_message = position.error_message; | 164 current_position_->error_message = position.error_message; |
| 181 | |
| 182 has_position_to_report_ = true; | |
| 183 | 165 |
| 184 if (!position_callback_.is_null()) | 166 if (!position_callback_.is_null()) |
| 185 ReportCurrentPosition(); | 167 ReportCurrentPosition(); |
| 186 } | 168 } |
| 187 | 169 |
| 188 void GeolocationServiceImpl::ReportCurrentPosition() { | 170 void GeolocationServiceImpl::ReportCurrentPosition() { |
| 189 position_callback_.Run(current_position_.Clone()); | 171 position_callback_.Run(current_position_.Pass()); |
| 190 position_callback_.reset(); | 172 position_callback_.reset(); |
| 191 has_position_to_report_ = false; | 173 } |
| 174 | |
| 175 void GeolocationServiceImpl::ReportPermissionUsage() { | |
| 176 RenderFrameHost* render_frame_host = | |
| 177 RenderFrameHost::FromID(render_process_id_, render_frame_id_); | |
| 178 if (!render_frame_host) | |
| 179 return; | |
| 180 | |
| 181 PermissionManager* permission_manager = render_frame_host->GetProcess() | |
| 182 ->GetBrowserContext() | |
| 183 ->GetPermissionManager(); | |
| 184 if (!permission_manager) | |
| 185 return; | |
| 186 | |
| 187 permission_manager->RegisterPermissionUsage( | |
| 188 PermissionType::GEOLOCATION, | |
| 189 render_frame_host->GetLastCommittedURL().GetOrigin(), | |
| 190 WebContents::FromRenderFrameHost(render_frame_host) | |
| 191 ->GetMainFrame() | |
| 192 ->GetLastCommittedURL() | |
| 193 .GetOrigin()); | |
| 192 } | 194 } |
| 193 | 195 |
| 194 } // namespace content | 196 } // namespace content |
| OLD | NEW |