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.
5 #include "content/browser/geolocation/win7_location_provider_win.h"
10 #include "base/bind.h"
11 #include "base/compiler_specific.h"
12 #include "base/logging.h"
13 #include "base/message_loop.h"
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;
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())
35 DCHECK(!position_2
.Validate());
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.
44 return delta
> kMovementThresholdMeters
;
48 Win7LocationProvider::Win7LocationProvider(Win7LocationApi
* api
)
49 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {
54 Win7LocationProvider::~Win7LocationProvider() {
58 bool Win7LocationProvider::StartProvider(bool high_accuracy
){
61 api_
->SetHighAccuracy(high_accuracy
);
62 if (!weak_factory_
.HasWeakPtrs())
67 void Win7LocationProvider::StopProvider() {
68 weak_factory_
.InvalidateWeakPtrs();
71 void Win7LocationProvider::GetPosition(Geoposition
* position
) {
73 *position
= position_
;
76 void Win7LocationProvider::UpdatePosition() {
80 void Win7LocationProvider::DoPollTask() {
81 Geoposition new_position
;
82 api_
->GetPosition(&new_position
);
83 const bool differ
= PositionsDifferSiginificantly(position_
, new_position
);
84 ScheduleNextPoll(differ
? kPollPeriodMovingMillis
:
85 kPollPeriodStationaryMillis
);
86 if (differ
|| new_position
.error_code
!= Geoposition::ERROR_CODE_NONE
) {
87 // Update if the new location is interesting or we have an error to report
88 position_
= new_position
;
93 void Win7LocationProvider::ScheduleNextPoll(int interval
) {
94 MessageLoop::current()->PostDelayedTask(
96 base::Bind(&Win7LocationProvider::DoPollTask
, weak_factory_
.GetWeakPtr()),
97 base::TimeDelta::FromMilliseconds(interval
));
100 LocationProviderBase
* NewSystemLocationProvider() {
101 Win7LocationApi
* api
= Win7LocationApi::Create();
103 return NULL
; // API not supported on this machine.
104 return new Win7LocationProvider(api
);
107 } // namespace content