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