| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2010 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 "chrome/browser/geolocation/win7_location_provider_win.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 #include <cmath> | |
| 9 | |
| 10 #include "base/compiler_specific.h" | |
| 11 #include "base/logging.h" | |
| 12 #include "base/message_loop.h" | |
| 13 | |
| 14 namespace{ | |
| 15 const int kPollPeriodMovingMillis = 500; | |
| 16 // Poll less frequently whilst stationary. | |
| 17 const int kPollPeriodStationaryMillis = kPollPeriodMovingMillis * 3; | |
| 18 // Reading must differ by more than this amount to be considered movement. | |
| 19 const int kMovementThresholdMeters = 20; | |
| 20 | |
| 21 // This algorithm is reused from the corresponding code in the gears project | |
| 22 // and is also used in gps_location_provider_linux.cc | |
| 23 // The arbitrary delta is decreased (gears used 100 meters); if we need to | |
| 24 // decrease it any further we'll likely want to do some smarter filtering to | |
| 25 // remove GPS location jitter noise. | |
| 26 bool PositionsDifferSiginificantly(const Geoposition& position_1, | |
| 27 const Geoposition& position_2) { | |
| 28 const bool pos_1_valid = position_1.IsValidFix(); | |
| 29 if (pos_1_valid != position_2.IsValidFix()) | |
| 30 return true; | |
| 31 if (!pos_1_valid) { | |
| 32 DCHECK(!position_2.IsValidFix()); | |
| 33 return false; | |
| 34 } | |
| 35 double delta = std::sqrt( | |
| 36 std::pow(std::fabs(position_1.latitude - position_2.latitude), 2) + | |
| 37 std::pow(std::fabs(position_1.longitude - position_2.longitude), 2)); | |
| 38 // Convert to meters. 1 minute of arc of latitude (or longitude at the | |
| 39 // equator) is 1 nautical mile or 1852m. | |
| 40 delta *= 60 * 1852; | |
| 41 return delta > kMovementThresholdMeters; | |
| 42 } | |
| 43 } | |
| 44 | |
| 45 Win7LocationProvider::Win7LocationProvider(Win7LocationApi* api) | |
| 46 : ALLOW_THIS_IN_INITIALIZER_LIST(task_factory_(this)) { | |
| 47 DCHECK(api != NULL); | |
| 48 api_.reset(api); | |
| 49 } | |
| 50 | |
| 51 Win7LocationProvider::~Win7LocationProvider() { | |
| 52 api_.reset(); | |
| 53 } | |
| 54 | |
| 55 bool Win7LocationProvider::StartProvider(bool high_accuracy){ | |
| 56 if (api_ == NULL) | |
| 57 return false; | |
| 58 api_->SetHighAccuracy(high_accuracy); | |
| 59 if (task_factory_.empty()) | |
| 60 ScheduleNextPoll(0); | |
| 61 return true; | |
| 62 } | |
| 63 | |
| 64 void Win7LocationProvider::StopProvider() { | |
| 65 task_factory_.RevokeAll(); | |
| 66 } | |
| 67 | |
| 68 void Win7LocationProvider::GetPosition(Geoposition* position) { | |
| 69 DCHECK(position); | |
| 70 *position = position_; | |
| 71 } | |
| 72 | |
| 73 void Win7LocationProvider::UpdatePosition() { | |
| 74 ScheduleNextPoll(0); | |
| 75 } | |
| 76 | |
| 77 void Win7LocationProvider::OnPermissionGranted( | |
| 78 const GURL& requesting_frame) { | |
| 79 } | |
| 80 | |
| 81 void Win7LocationProvider::DoPollTask() { | |
| 82 Geoposition new_position; | |
| 83 api_->GetPosition(&new_position); | |
| 84 const bool differ = PositionsDifferSiginificantly(position_, new_position); | |
| 85 ScheduleNextPoll(differ ? kPollPeriodMovingMillis : | |
| 86 kPollPeriodStationaryMillis); | |
| 87 if (differ || new_position.error_code != Geoposition::ERROR_CODE_NONE) { | |
| 88 // Update if the new location is interesting or we have an error to report | |
| 89 position_ = new_position; | |
| 90 UpdateListeners(); | |
| 91 } | |
| 92 } | |
| 93 | |
| 94 void Win7LocationProvider::ScheduleNextPoll(int interval) { | |
| 95 MessageLoop::current()->PostDelayedTask( | |
| 96 FROM_HERE, | |
| 97 task_factory_.NewRunnableMethod(&Win7LocationProvider::DoPollTask), | |
| 98 interval); | |
| 99 } | |
| 100 | |
| 101 LocationProviderBase* NewSystemLocationProvider() { | |
| 102 Win7LocationApi* api = Win7LocationApi::Create(); | |
| 103 if (api == NULL) | |
| 104 return NULL; // API not supported on this machine. | |
| 105 return new Win7LocationProvider(api); | |
| 106 } | |
| OLD | NEW |