Chromium Code Reviews| Index: content/browser/geolocation/geolocation_service_impl.cc |
| diff --git a/content/browser/geolocation/geolocation_service_impl.cc b/content/browser/geolocation/geolocation_service_impl.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..47b634bbbf8354c2072bff24f2686db31464059d |
| --- /dev/null |
| +++ b/content/browser/geolocation/geolocation_service_impl.cc |
| @@ -0,0 +1,132 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "content/browser/geolocation/geolocation_service_impl.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/metrics/histogram.h" |
| +#include "content/browser/geolocation/geolocation_service_impl_context.h" |
| +#include "content/public/common/mojo_geoposition.mojom.h" |
| + |
| +namespace content { |
| + |
| +namespace { |
| + |
| +// Geoposition error codes for reporting in UMA. |
| +enum GeopositionErrorCode { |
| + // NOTE: Do not renumber these as that would confuse interpretation of |
| + // previously logged data. When making changes, also update the enum list |
| + // in tools/metrics/histograms/histograms.xml to keep it in sync. |
| + |
| + // There was no error. |
| + GEOPOSITION_ERROR_CODE_NONE = 0, |
| + |
| + // User denied use of geolocation. |
| + GEOPOSITION_ERROR_CODE_PERMISSION_DENIED = 1, |
| + |
| + // Geoposition could not be determined. |
| + GEOPOSITION_ERROR_CODE_POSITION_UNAVAILABLE = 2, |
| + |
| + // Timeout. |
| + GEOPOSITION_ERROR_CODE_TIMEOUT = 3, |
| + |
| + // NOTE: Add entries only immediately above this line. |
| + GEOPOSITION_ERROR_CODE_COUNT = 4 |
| +}; |
| + |
| +void RecordGeopositionErrorCode(Geoposition::ErrorCode error_code) { |
| + GeopositionErrorCode code = GEOPOSITION_ERROR_CODE_NONE; |
| + switch (error_code) { |
| + case Geoposition::ERROR_CODE_NONE: |
| + code = GEOPOSITION_ERROR_CODE_NONE; |
| + break; |
| + case Geoposition::ERROR_CODE_PERMISSION_DENIED: |
| + code = GEOPOSITION_ERROR_CODE_PERMISSION_DENIED; |
| + break; |
| + case Geoposition::ERROR_CODE_POSITION_UNAVAILABLE: |
| + code = GEOPOSITION_ERROR_CODE_POSITION_UNAVAILABLE; |
| + break; |
| + case Geoposition::ERROR_CODE_TIMEOUT: |
| + code = GEOPOSITION_ERROR_CODE_TIMEOUT; |
| + break; |
| + } |
| + UMA_HISTOGRAM_ENUMERATION("Geolocation.LocationUpdate.ErrorCode", |
| + code, |
| + GEOPOSITION_ERROR_CODE_COUNT); |
| +} |
| + |
| +} // namespace |
| + |
| +// static |
| +void GeolocationServiceImpl::Create( |
| + GeolocationServiceImplContext* context, |
| + const base::Closure& on_location_update_callback, |
| + mojo::InterfaceRequest<GeolocationService> request) { |
| + GeolocationServiceImpl* service = |
| + new GeolocationServiceImpl(context, on_location_update_callback); |
| + BindToRequest(service, &request); |
| +} |
| + |
| +GeolocationServiceImpl::GeolocationServiceImpl( |
| + GeolocationServiceImplContext* context, |
| + const base::Closure& on_location_update_callback) |
| + : context_(context), |
| + on_location_update_callback_(on_location_update_callback) { |
| + context_->AddService(this); |
| +} |
| + |
| +GeolocationServiceImpl::~GeolocationServiceImpl() { |
| + context_->RemoveService(this); |
| +} |
| + |
| +void GeolocationServiceImpl::PauseUpdates() { |
| + geolocation_subscription_.reset(); |
| +} |
| + |
| +void GeolocationServiceImpl::ResumeUpdates() { |
| + StartListeningForUpdates(); |
| +} |
| + |
| +void GeolocationServiceImpl::StartListeningForUpdates() { |
| + geolocation_subscription_ = |
| + GeolocationProvider::GetInstance()->AddLocationUpdateCallback( |
| + base::Bind(&GeolocationServiceImpl::OnLocationUpdate, |
| + base::Unretained(this)), |
| + high_accuracy_); |
| +} |
| + |
| +void GeolocationServiceImpl::StartUpdating(bool high_accuracy) { |
| + // TODO(blundell): What does this comment even mean? |
|
Michael van Ouwerkerk
2014/10/08 14:13:07
I was thinking the same. The purpose of this histo
blundell
2014/10/09 08:32:52
I'm not sure what you're suggesting I do with the
|
| + // StartUpdating() can be invoked as a result of high-accuracy mode |
| + // being enabled / disabled. No need to record the dispatcher again. |
| + UMA_HISTOGRAM_BOOLEAN( |
| + "Geolocation.GeolocationDispatcherHostImpl.EnableHighAccuracy", |
|
Michael van Ouwerkerk
2014/10/08 14:13:08
I've chosen a terrible name for this histogram. St
blundell
2014/10/09 08:32:52
Acknowledged.
|
| + high_accuracy); |
| + high_accuracy_ = high_accuracy; |
| + StartListeningForUpdates(); |
| +} |
| + |
| +void GeolocationServiceImpl::OnLocationUpdate(const Geoposition& position) { |
| + RecordGeopositionErrorCode(position.error_code); |
| + if (context_->paused()) |
| + return; |
| + |
| + MojoGeopositionPtr geoposition(MojoGeoposition::New()); |
| + geoposition->valid = position.Validate(); |
| + geoposition->latitude = position.latitude; |
| + geoposition->longitude = position.longitude; |
| + geoposition->altitude = position.altitude; |
| + geoposition->accuracy = position.accuracy; |
| + geoposition->altitude_accuracy = position.altitude_accuracy; |
| + geoposition->heading = position.heading; |
| + geoposition->speed = position.speed; |
| + geoposition->timestamp = position.timestamp.ToDoubleT(); |
| + geoposition->error_code = MojoGeoposition::ErrorCode(position.error_code); |
| + geoposition->error_message = position.error_message; |
| + |
| + on_location_update_callback_.Run(); |
| + client()->OnLocationUpdate(geoposition.Pass()); |
| +} |
| + |
| +} // namespace content |