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/win7_location_provider_win.h" | 5 #include "content/browser/geolocation/win7_location_provider_win.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 | 14 |
15 namespace{ | 15 namespace{ |
16 const int kPollPeriodMovingMillis = 500; | 16 const int kPollPeriodMovingMillis = 500; |
17 // Poll less frequently whilst stationary. | 17 // Poll less frequently whilst stationary. |
18 const int kPollPeriodStationaryMillis = kPollPeriodMovingMillis * 3; | 18 const int kPollPeriodStationaryMillis = kPollPeriodMovingMillis * 3; |
19 // Reading must differ by more than this amount to be considered movement. | 19 // Reading must differ by more than this amount to be considered movement. |
20 const int kMovementThresholdMeters = 20; | 20 const int kMovementThresholdMeters = 20; |
21 | 21 |
22 // This algorithm is reused from the corresponding code in the Gears project | 22 // This algorithm is reused from the corresponding code in the Gears project |
23 // and is also used in gps_location_provider_linux.cc | 23 // and is also used in gps_location_provider_linux.cc |
24 // The arbitrary delta is decreased (Gears used 100 meters); if we need to | 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 | 25 // decrease it any further we'll likely want to do some smarter filtering to |
26 // remove GPS location jitter noise. | 26 // remove GPS location jitter noise. |
27 bool PositionsDifferSiginificantly(const Geoposition& position_1, | 27 bool PositionsDifferSiginificantly(const content::Geoposition& position_1, |
28 const Geoposition& position_2) { | 28 const content::Geoposition& position_2) { |
29 const bool pos_1_valid = position_1.IsValidFix(); | 29 const bool pos_1_valid = position_1.Validate(); |
30 if (pos_1_valid != position_2.IsValidFix()) | 30 if (pos_1_valid != position_2.Validate()) |
31 return true; | 31 return true; |
32 if (!pos_1_valid) { | 32 if (!pos_1_valid) { |
33 DCHECK(!position_2.IsValidFix()); | 33 DCHECK(!position_2.Validate()); |
34 return false; | 34 return false; |
35 } | 35 } |
36 double delta = std::sqrt( | 36 double delta = std::sqrt( |
37 std::pow(std::fabs(position_1.latitude - position_2.latitude), 2) + | 37 std::pow(std::fabs(position_1.latitude - position_2.latitude), 2) + |
38 std::pow(std::fabs(position_1.longitude - position_2.longitude), 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 | 39 // Convert to meters. 1 minute of arc of latitude (or longitude at the |
40 // equator) is 1 nautical mile or 1852m. | 40 // equator) is 1 nautical mile or 1852m. |
41 delta *= 60 * 1852; | 41 delta *= 60 * 1852; |
42 return delta > kMovementThresholdMeters; | 42 return delta > kMovementThresholdMeters; |
43 } | 43 } |
(...skipping 15 matching lines...) Expand all Loading... |
59 api_->SetHighAccuracy(high_accuracy); | 59 api_->SetHighAccuracy(high_accuracy); |
60 if (!weak_factory_.HasWeakPtrs()) | 60 if (!weak_factory_.HasWeakPtrs()) |
61 ScheduleNextPoll(0); | 61 ScheduleNextPoll(0); |
62 return true; | 62 return true; |
63 } | 63 } |
64 | 64 |
65 void Win7LocationProvider::StopProvider() { | 65 void Win7LocationProvider::StopProvider() { |
66 weak_factory_.InvalidateWeakPtrs(); | 66 weak_factory_.InvalidateWeakPtrs(); |
67 } | 67 } |
68 | 68 |
69 void Win7LocationProvider::GetPosition(Geoposition* position) { | 69 void Win7LocationProvider::GetPosition(content::Geoposition* position) { |
70 DCHECK(position); | 70 DCHECK(position); |
71 *position = position_; | 71 *position = position_; |
72 } | 72 } |
73 | 73 |
74 void Win7LocationProvider::UpdatePosition() { | 74 void Win7LocationProvider::UpdatePosition() { |
75 ScheduleNextPoll(0); | 75 ScheduleNextPoll(0); |
76 } | 76 } |
77 | 77 |
78 void Win7LocationProvider::DoPollTask() { | 78 void Win7LocationProvider::DoPollTask() { |
79 Geoposition new_position; | 79 content::Geoposition new_position; |
80 api_->GetPosition(&new_position); | 80 api_->GetPosition(&new_position); |
81 const bool differ = PositionsDifferSiginificantly(position_, new_position); | 81 const bool differ = PositionsDifferSiginificantly(position_, new_position); |
82 ScheduleNextPoll(differ ? kPollPeriodMovingMillis : | 82 ScheduleNextPoll(differ ? kPollPeriodMovingMillis : |
83 kPollPeriodStationaryMillis); | 83 kPollPeriodStationaryMillis); |
84 if (differ || new_position.error_code != Geoposition::ERROR_CODE_NONE) { | 84 if (differ || |
| 85 new_position.error_code != content::Geoposition::ERROR_CODE_NONE) { |
85 // Update if the new location is interesting or we have an error to report | 86 // Update if the new location is interesting or we have an error to report |
86 position_ = new_position; | 87 position_ = new_position; |
87 UpdateListeners(); | 88 UpdateListeners(); |
88 } | 89 } |
89 } | 90 } |
90 | 91 |
91 void Win7LocationProvider::ScheduleNextPoll(int interval) { | 92 void Win7LocationProvider::ScheduleNextPoll(int interval) { |
92 MessageLoop::current()->PostDelayedTask( | 93 MessageLoop::current()->PostDelayedTask( |
93 FROM_HERE, | 94 FROM_HERE, |
94 base::Bind(&Win7LocationProvider::DoPollTask, weak_factory_.GetWeakPtr()), | 95 base::Bind(&Win7LocationProvider::DoPollTask, weak_factory_.GetWeakPtr()), |
95 base::TimeDelta::FromMilliseconds(interval)); | 96 base::TimeDelta::FromMilliseconds(interval)); |
96 } | 97 } |
97 | 98 |
98 LocationProviderBase* NewSystemLocationProvider() { | 99 LocationProviderBase* NewSystemLocationProvider() { |
99 Win7LocationApi* api = Win7LocationApi::Create(); | 100 Win7LocationApi* api = Win7LocationApi::Create(); |
100 if (api == NULL) | 101 if (api == NULL) |
101 return NULL; // API not supported on this machine. | 102 return NULL; // API not supported on this machine. |
102 return new Win7LocationProvider(api); | 103 return new Win7LocationProvider(api); |
103 } | 104 } |
OLD | NEW |