By moving the call to Load() up in SearchProvider::Start(), we are giving a chance...
[chromium-blink-merge.git] / content / browser / geolocation / win7_location_provider_win.cc
blobfa7122de5493ae5c5fc35002b386c2db38e76356
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"
7 #include <algorithm>
8 #include <cmath>
10 #include "base/bind.h"
11 #include "base/compiler_specific.h"
12 #include "base/logging.h"
13 #include "base/message_loop.h"
15 namespace content {
16 namespace {
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())
33 return true;
34 if (!pos_1_valid) {
35 DCHECK(!position_2.Validate());
36 return false;
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;
48 Win7LocationProvider::Win7LocationProvider(Win7LocationApi* api)
49 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {
50 DCHECK(api != NULL);
51 api_.reset(api);
54 Win7LocationProvider::~Win7LocationProvider() {
55 api_.reset();
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;
67 void Win7LocationProvider::StopProvider() {
68 weak_factory_.InvalidateWeakPtrs();
71 void Win7LocationProvider::GetPosition(Geoposition* position) {
72 DCHECK(position);
73 *position = position_;
76 void Win7LocationProvider::UpdatePosition() {
77 ScheduleNextPoll(0);
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;
89 UpdateListeners();
93 void Win7LocationProvider::ScheduleNextPoll(int interval) {
94 MessageLoop::current()->PostDelayedTask(
95 FROM_HERE,
96 base::Bind(&Win7LocationProvider::DoPollTask, weak_factory_.GetWeakPtr()),
97 base::TimeDelta::FromMilliseconds(interval));
100 LocationProviderBase* NewSystemLocationProvider() {
101 Win7LocationApi* api = Win7LocationApi::Create();
102 if (api == NULL)
103 return NULL; // API not supported on this machine.
104 return new Win7LocationProvider(api);
107 } // namespace content