OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "content/browser/geolocation/win7_location_provider_win.h" | |
6 | |
7 #include <algorithm> | |
8 #include <cmath> | |
9 | |
10 #include "base/bind.h" | |
11 #include "base/compiler_specific.h" | |
12 #include "base/logging.h" | |
13 #include "base/message_loop/message_loop.h" | |
14 | |
15 namespace content { | |
16 namespace { | |
17 | |
18 const int kPollPeriodMovingMillis = 500; | |
19 // Poll less frequently whilst stationary. | |
20 const int kPollPeriodStationaryMillis = kPollPeriodMovingMillis * 3; | |
21 // Reading must differ by more than this amount to be considered movement. | |
22 const int kMovementThresholdMeters = 20; | |
23 | |
24 // This algorithm is reused from the corresponding code in the Gears project | |
25 // and is also used in gps_location_provider_linux.cc | |
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 | |
28 // remove GPS location jitter noise. | |
29 bool PositionsDifferSiginificantly(const Geoposition& position_1, | |
30 const Geoposition& position_2) { | |
31 const bool pos_1_valid = position_1.Validate(); | |
32 if (pos_1_valid != position_2.Validate()) | |
33 return true; | |
34 if (!pos_1_valid) { | |
35 DCHECK(!position_2.Validate()); | |
36 return false; | |
37 } | |
38 double delta = std::sqrt( | |
39 std::pow(std::fabs(position_1.latitude - position_2.latitude), 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 | |
42 // equator) is 1 nautical mile or 1852m. | |
43 delta *= 60 * 1852; | |
44 return delta > kMovementThresholdMeters; | |
45 } | |
46 } | |
47 | |
48 Win7LocationProvider::Win7LocationProvider(Win7LocationApi* api) | |
49 : weak_factory_(this) { | |
50 DCHECK(api != NULL); | |
51 api_.reset(api); | |
52 } | |
53 | |
54 Win7LocationProvider::~Win7LocationProvider() { | |
55 api_.reset(); | |
56 } | |
57 | |
58 bool Win7LocationProvider::StartProvider(bool high_accuracy){ | |
59 if (api_ == NULL) | |
60 return false; | |
61 api_->SetHighAccuracy(high_accuracy); | |
62 if (!weak_factory_.HasWeakPtrs()) | |
63 ScheduleNextPoll(0); | |
64 return true; | |
65 } | |
66 | |
67 void Win7LocationProvider::StopProvider() { | |
68 weak_factory_.InvalidateWeakPtrs(); | |
69 } | |
70 | |
71 void Win7LocationProvider::GetPosition(Geoposition* position) { | |
72 DCHECK(position); | |
73 *position = position_; | |
74 } | |
75 | |
76 void Win7LocationProvider::RequestRefresh() { | |
77 ScheduleNextPoll(0); | |
78 } | |
79 | |
80 void Win7LocationProvider::OnPermissionGranted() { | |
81 } | |
82 | |
83 void Win7LocationProvider::DoPollTask() { | |
84 Geoposition new_position; | |
85 api_->GetPosition(&new_position); | |
86 const bool differ = PositionsDifferSiginificantly(position_, new_position); | |
87 ScheduleNextPoll(differ ? kPollPeriodMovingMillis : | |
88 kPollPeriodStationaryMillis); | |
89 if (differ || new_position.error_code != Geoposition::ERROR_CODE_NONE) { | |
90 // Update if the new location is interesting or we have an error to report | |
91 position_ = new_position; | |
92 NotifyCallback(position_); | |
93 } | |
94 } | |
95 | |
96 void Win7LocationProvider::ScheduleNextPoll(int interval) { | |
97 base::MessageLoop::current()->PostDelayedTask( | |
98 FROM_HERE, | |
99 base::Bind(&Win7LocationProvider::DoPollTask, weak_factory_.GetWeakPtr()), | |
100 base::TimeDelta::FromMilliseconds(interval)); | |
101 } | |
102 | |
103 LocationProvider* NewSystemLocationProvider() { | |
104 Win7LocationApi* api = Win7LocationApi::Create(); | |
105 if (api == NULL) | |
106 return NULL; // API not supported on this machine. | |
107 return new Win7LocationProvider(api); | |
108 } | |
109 | |
110 } // namespace content | |
OLD | NEW |