| OLD | NEW |
| (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_impl_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 // static |
| 62 void GeolocationServiceImpl::Create( |
| 63 GeolocationServiceImplContext* context, |
| 64 const base::Closure& on_location_update_callback, |
| 65 mojo::InterfaceRequest<GeolocationService> request) { |
| 66 GeolocationServiceImpl* service = |
| 67 new GeolocationServiceImpl(context, on_location_update_callback); |
| 68 BindToRequest(service, &request); |
| 69 } |
| 70 |
| 71 GeolocationServiceImpl::GeolocationServiceImpl( |
| 72 GeolocationServiceImplContext* context, |
| 73 const base::Closure& on_location_update_callback) |
| 74 : context_(context), |
| 75 on_location_update_callback_(on_location_update_callback) { |
| 76 context_->AddService(this); |
| 77 } |
| 78 |
| 79 GeolocationServiceImpl::~GeolocationServiceImpl() { |
| 80 context_->RemoveService(this); |
| 81 } |
| 82 |
| 83 void GeolocationServiceImpl::PauseUpdates() { |
| 84 geolocation_subscription_.reset(); |
| 85 } |
| 86 |
| 87 void GeolocationServiceImpl::ResumeUpdates() { |
| 88 StartListeningForUpdates(); |
| 89 } |
| 90 |
| 91 void GeolocationServiceImpl::StartListeningForUpdates() { |
| 92 geolocation_subscription_ = |
| 93 GeolocationProvider::GetInstance()->AddLocationUpdateCallback( |
| 94 base::Bind(&GeolocationServiceImpl::OnLocationUpdate, |
| 95 base::Unretained(this)), |
| 96 high_accuracy_); |
| 97 } |
| 98 |
| 99 void GeolocationServiceImpl::StartUpdating(bool high_accuracy) { |
| 100 // TODO(blundell): What does this comment even mean? |
| 101 // StartUpdating() can be invoked as a result of high-accuracy mode |
| 102 // being enabled / disabled. No need to record the dispatcher again. |
| 103 UMA_HISTOGRAM_BOOLEAN( |
| 104 "Geolocation.GeolocationDispatcherHostImpl.EnableHighAccuracy", |
| 105 high_accuracy); |
| 106 high_accuracy_ = high_accuracy; |
| 107 StartListeningForUpdates(); |
| 108 } |
| 109 |
| 110 void GeolocationServiceImpl::OnLocationUpdate(const Geoposition& position) { |
| 111 RecordGeopositionErrorCode(position.error_code); |
| 112 if (context_->paused()) |
| 113 return; |
| 114 |
| 115 MojoGeopositionPtr geoposition(MojoGeoposition::New()); |
| 116 geoposition->valid = position.Validate(); |
| 117 geoposition->latitude = position.latitude; |
| 118 geoposition->longitude = position.longitude; |
| 119 geoposition->altitude = position.altitude; |
| 120 geoposition->accuracy = position.accuracy; |
| 121 geoposition->altitude_accuracy = position.altitude_accuracy; |
| 122 geoposition->heading = position.heading; |
| 123 geoposition->speed = position.speed; |
| 124 geoposition->timestamp = position.timestamp.ToDoubleT(); |
| 125 geoposition->error_code = MojoGeoposition::ErrorCode(position.error_code); |
| 126 geoposition->error_message = position.error_message; |
| 127 |
| 128 on_location_update_callback_.Run(); |
| 129 client()->OnLocationUpdate(geoposition.Pass()); |
| 130 } |
| 131 |
| 132 } // namespace content |
| OLD | NEW |