| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2010 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 "chrome/browser/geolocation/gps_location_provider_linux.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 #include <cmath> | |
| 9 #include "base/compiler_specific.h" | |
| 10 #include "base/logging.h" | |
| 11 #include "base/message_loop.h" | |
| 12 #include "chrome/browser/geolocation/libgps_wrapper_linux.h" | |
| 13 | |
| 14 namespace { | |
| 15 const int kGpsdReconnectRetryIntervalMillis = 10 * 1000; | |
| 16 // As per http://gpsd.berlios.de/performance.html#id374524, poll twice per sec. | |
| 17 const int kPollPeriodMovingMillis = 500; | |
| 18 // Poll less frequently whilst stationary. | |
| 19 const int kPollPeriodStationaryMillis = kPollPeriodMovingMillis * 3; | |
| 20 // GPS reading must differ by more than this amount to be considered movement. | |
| 21 const int kMovementThresholdMeters = 20; | |
| 22 | |
| 23 // This algorithm is reused from the corresponding code in the gears project. | |
| 24 // The arbitrary delta is decreased (gears used 100 meters); if we need to | |
| 25 // decrease it any further we'll likely want to do some smarter filtering to | |
| 26 // remove GPS location jitter noise. | |
| 27 bool PositionsDifferSiginificantly(const Geoposition& position_1, | |
| 28 const Geoposition& position_2) { | |
| 29 const bool pos_1_valid = position_1.IsValidFix(); | |
| 30 if (pos_1_valid != position_2.IsValidFix()) | |
| 31 return true; | |
| 32 if (!pos_1_valid) { | |
| 33 DCHECK(!position_2.IsValidFix()); | |
| 34 return false; | |
| 35 } | |
| 36 double delta = std::sqrt( | |
| 37 std::pow(std::fabs(position_1.latitude - position_2.latitude), 2) + | |
| 38 std::pow(std::fabs(position_1.longitude - position_2.longitude), 2)); | |
| 39 // Convert to meters. 1 minute of arc of latitude (or longitude at the | |
| 40 // equator) is 1 nautical mile or 1852m. | |
| 41 delta *= 60 * 1852; | |
| 42 return delta > kMovementThresholdMeters; | |
| 43 } | |
| 44 } // namespace | |
| 45 | |
| 46 GpsLocationProviderLinux::GpsLocationProviderLinux(LibGpsFactory libgps_factory) | |
| 47 : libgps_factory_(libgps_factory), | |
| 48 ALLOW_THIS_IN_INITIALIZER_LIST(task_factory_(this)) { | |
| 49 DCHECK(libgps_factory_); | |
| 50 } | |
| 51 | |
| 52 GpsLocationProviderLinux::~GpsLocationProviderLinux() { | |
| 53 } | |
| 54 | |
| 55 bool GpsLocationProviderLinux::StartProvider(bool high_accuracy) { | |
| 56 if (!high_accuracy) { | |
| 57 StopProvider(); | |
| 58 return true; // Not an error condition, so still return true. | |
| 59 } | |
| 60 if (gps_ != NULL) { | |
| 61 DCHECK(!task_factory_.empty()); | |
| 62 return true; | |
| 63 } | |
| 64 position_.error_code = Geoposition::ERROR_CODE_POSITION_UNAVAILABLE; | |
| 65 gps_.reset(libgps_factory_()); | |
| 66 if (gps_ == NULL) { | |
| 67 DLOG(WARNING) << "libgps.so could not be loaded"; | |
| 68 return false; | |
| 69 } | |
| 70 ScheduleNextGpsPoll(0); | |
| 71 return true; | |
| 72 } | |
| 73 | |
| 74 void GpsLocationProviderLinux::StopProvider() { | |
| 75 task_factory_.RevokeAll(); | |
| 76 gps_.reset(); | |
| 77 } | |
| 78 | |
| 79 void GpsLocationProviderLinux::GetPosition(Geoposition* position) { | |
| 80 DCHECK(position); | |
| 81 *position = position_; | |
| 82 DCHECK(position->IsInitialized()); | |
| 83 } | |
| 84 | |
| 85 void GpsLocationProviderLinux::UpdatePosition() { | |
| 86 ScheduleNextGpsPoll(0); | |
| 87 } | |
| 88 | |
| 89 void GpsLocationProviderLinux::OnPermissionGranted( | |
| 90 const GURL& requesting_frame) { | |
| 91 } | |
| 92 | |
| 93 void GpsLocationProviderLinux::DoGpsPollTask() { | |
| 94 if (!gps_->Start()) { | |
| 95 DLOG(WARNING) << "Couldn't start GPS provider."; | |
| 96 ScheduleNextGpsPoll(kGpsdReconnectRetryIntervalMillis); | |
| 97 return; | |
| 98 } | |
| 99 if (!gps_->Poll()) { | |
| 100 ScheduleNextGpsPoll(kPollPeriodStationaryMillis); | |
| 101 return; | |
| 102 } | |
| 103 Geoposition new_position; | |
| 104 gps_->GetPosition(&new_position); | |
| 105 DCHECK(new_position.IsInitialized()); | |
| 106 const bool differ = PositionsDifferSiginificantly(position_, new_position); | |
| 107 ScheduleNextGpsPoll(differ ? kPollPeriodMovingMillis : | |
| 108 kPollPeriodStationaryMillis); | |
| 109 if (differ || new_position.error_code != Geoposition::ERROR_CODE_NONE) { | |
| 110 // Update if the new location is interesting or we have an error to report. | |
| 111 position_ = new_position; | |
| 112 UpdateListeners(); | |
| 113 } | |
| 114 } | |
| 115 | |
| 116 void GpsLocationProviderLinux::ScheduleNextGpsPoll(int interval) { | |
| 117 task_factory_.RevokeAll(); | |
| 118 MessageLoop::current()->PostDelayedTask( | |
| 119 FROM_HERE, | |
| 120 task_factory_.NewRunnableMethod(&GpsLocationProviderLinux::DoGpsPollTask), | |
| 121 interval); | |
| 122 } | |
| 123 | |
| 124 LocationProviderBase* NewSystemLocationProvider() { | |
| 125 return new GpsLocationProviderLinux(LibGps::New); | |
| 126 } | |
| OLD | NEW |