| 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 // Provides wifi scan API binding for chromeos, using proprietary APIs. | 5 // Provides wifi scan API binding for chromeos, using proprietary APIs. |
| 6 | 6 |
| 7 #include "device/geolocation/wifi_data_provider_chromeos.h" | 7 #include "device/geolocation/wifi_data_provider_chromeos.h" |
| 8 | 8 |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 | 10 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/single_thread_task_runner.h" | |
| 13 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
| 14 #include "base/threading/thread_task_runner_handle.h" | |
| 15 #include "chromeos/network/geolocation_handler.h" | 13 #include "chromeos/network/geolocation_handler.h" |
| 14 #include "chromeos/network/network_handler.h" |
| 16 #include "device/geolocation/wifi_data_provider_manager.h" | 15 #include "device/geolocation/wifi_data_provider_manager.h" |
| 17 | 16 |
| 17 using chromeos::NetworkHandler; |
| 18 |
| 18 namespace device { | 19 namespace device { |
| 19 | 20 |
| 20 namespace { | 21 namespace { |
| 21 | 22 |
| 22 // The time periods between successive polls of the wifi data. | 23 // The time periods between successive polls of the wifi data. |
| 23 const int kDefaultPollingIntervalMilliseconds = 10 * 1000; // 10s | 24 const int kDefaultPollingIntervalMilliseconds = 10 * 1000; // 10s |
| 24 const int kNoChangePollingIntervalMilliseconds = 2 * 60 * 1000; // 2 mins | 25 const int kNoChangePollingIntervalMilliseconds = 2 * 60 * 1000; // 2 mins |
| 25 const int kTwoNoChangePollingIntervalMilliseconds = 10 * 60 * 1000; // 10 mins | 26 const int kTwoNoChangePollingIntervalMilliseconds = 10 * 60 * 1000; // 10 mins |
| 26 const int kNoWifiPollingIntervalMilliseconds = 20 * 1000; // 20s | 27 const int kNoWifiPollingIntervalMilliseconds = 20 * 1000; // 20s |
| 27 | 28 |
| 28 } // namespace | 29 } // namespace |
| 29 | 30 |
| 30 WifiDataProviderChromeOs::WifiDataProviderChromeOs() | 31 WifiDataProviderChromeOs::WifiDataProviderChromeOs() |
| 31 : started_(false), | 32 : started_(false), is_first_scan_complete_(false) {} |
| 32 is_first_scan_complete_(false), | |
| 33 main_task_runner_(base::ThreadTaskRunnerHandle::Get()) {} | |
| 34 | 33 |
| 35 WifiDataProviderChromeOs::~WifiDataProviderChromeOs() {} | 34 WifiDataProviderChromeOs::~WifiDataProviderChromeOs() {} |
| 36 | 35 |
| 37 void WifiDataProviderChromeOs::StartDataProvider() { | 36 void WifiDataProviderChromeOs::StartDataProvider() { |
| 38 DCHECK(CalledOnClientThread()); | 37 DCHECK(CalledOnClientThread()); |
| 39 | 38 |
| 40 DCHECK(polling_policy_ == NULL); | 39 DCHECK(polling_policy_ == nullptr); |
| 41 polling_policy_.reset( | 40 polling_policy_.reset( |
| 42 new GenericWifiPollingPolicy<kDefaultPollingIntervalMilliseconds, | 41 new GenericWifiPollingPolicy<kDefaultPollingIntervalMilliseconds, |
| 43 kNoChangePollingIntervalMilliseconds, | 42 kNoChangePollingIntervalMilliseconds, |
| 44 kTwoNoChangePollingIntervalMilliseconds, | 43 kTwoNoChangePollingIntervalMilliseconds, |
| 45 kNoWifiPollingIntervalMilliseconds>); | 44 kNoWifiPollingIntervalMilliseconds>); |
| 46 | 45 |
| 47 ScheduleStart(); | 46 ScheduleStart(); |
| 48 } | 47 } |
| 49 | 48 |
| 50 void WifiDataProviderChromeOs::StopDataProvider() { | 49 void WifiDataProviderChromeOs::StopDataProvider() { |
| 51 DCHECK(CalledOnClientThread()); | 50 DCHECK(CalledOnClientThread()); |
| 52 | 51 |
| 53 polling_policy_.reset(); | 52 polling_policy_.reset(); |
| 54 ScheduleStop(); | 53 ScheduleStop(); |
| 55 } | 54 } |
| 56 | 55 |
| 57 bool WifiDataProviderChromeOs::GetData(WifiData* data) { | 56 bool WifiDataProviderChromeOs::GetData(WifiData* data) { |
| 58 DCHECK(CalledOnClientThread()); | 57 DCHECK(CalledOnClientThread()); |
| 59 DCHECK(data); | 58 DCHECK(data); |
| 60 *data = wifi_data_; | 59 *data = wifi_data_; |
| 61 return is_first_scan_complete_; | 60 return is_first_scan_complete_; |
| 62 } | 61 } |
| 63 | 62 |
| 64 void WifiDataProviderChromeOs::DoStartTaskOnUIThread() { | 63 void WifiDataProviderChromeOs::DoWifiScanTaskOnNetworkHandlerThread() { |
| 65 CHECK(main_task_runner_->BelongsToCurrentThread()); | |
| 66 DoWifiScanTaskOnUIThread(); | |
| 67 } | |
| 68 | |
| 69 void WifiDataProviderChromeOs::DoWifiScanTaskOnUIThread() { | |
| 70 CHECK(main_task_runner_->BelongsToCurrentThread()); | |
| 71 | |
| 72 // This method could be scheduled after a ScheduleStop. | 64 // This method could be scheduled after a ScheduleStop. |
| 73 if (!started_) | 65 if (!started_) |
| 74 return; | 66 return; |
| 75 | 67 |
| 76 WifiData new_data; | 68 WifiData new_data; |
| 77 | 69 |
| 78 if (GetAccessPointData(&new_data.access_point_data)) { | 70 if (GetAccessPointData(&new_data.access_point_data)) { |
| 79 client_task_runner()->PostTask( | 71 client_task_runner()->PostTask( |
| 80 FROM_HERE, | 72 FROM_HERE, |
| 81 base::Bind(&WifiDataProviderChromeOs::DidWifiScanTask, this, new_data)); | 73 base::Bind(&WifiDataProviderChromeOs::DidWifiScanTask, this, new_data)); |
| 82 } else { | 74 } else { |
| 83 client_task_runner()->PostTask( | 75 client_task_runner()->PostTask( |
| 84 FROM_HERE, | 76 FROM_HERE, |
| 85 base::Bind(&WifiDataProviderChromeOs::DidWifiScanTaskNoResults, this)); | 77 base::Bind(&WifiDataProviderChromeOs::DidWifiScanTaskNoResults, this)); |
| 86 } | 78 } |
| 87 } | 79 } |
| 88 | 80 |
| 89 void WifiDataProviderChromeOs::DidWifiScanTaskNoResults() { | 81 void WifiDataProviderChromeOs::DidWifiScanTaskNoResults() { |
| 90 DCHECK(CalledOnClientThread()); | 82 DCHECK(CalledOnClientThread()); |
| 91 // Schedule next scan if started (StopDataProvider could have been called | 83 // Schedule next scan if started (StopDataProvider could have been called |
| 92 // in between DoWifiScanTaskOnUIThread and this method). | 84 // in between DoWifiScanTaskOnNetworkHandlerThread and this method). |
| 93 if (started_) | 85 if (started_) |
| 94 ScheduleNextScan(polling_policy_->NoWifiInterval()); | 86 ScheduleNextScan(polling_policy_->NoWifiInterval()); |
| 95 } | 87 } |
| 96 | 88 |
| 97 void WifiDataProviderChromeOs::DidWifiScanTask(const WifiData& new_data) { | 89 void WifiDataProviderChromeOs::DidWifiScanTask(const WifiData& new_data) { |
| 98 DCHECK(CalledOnClientThread()); | 90 DCHECK(CalledOnClientThread()); |
| 99 bool update_available = wifi_data_.DiffersSignificantly(new_data); | 91 bool update_available = wifi_data_.DiffersSignificantly(new_data); |
| 100 wifi_data_ = new_data; | 92 wifi_data_ = new_data; |
| 101 // Schedule next scan if started (StopDataProvider could have been called | 93 // Schedule next scan if started (StopDataProvider could have been called |
| 102 // in between DoWifiScanTaskOnUIThread and this method). | 94 // in between DoWifiScanTaskOnNetworkHandlerThread and this method). |
| 103 if (started_) { | 95 if (started_) { |
| 104 polling_policy_->UpdatePollingInterval(update_available); | 96 polling_policy_->UpdatePollingInterval(update_available); |
| 105 ScheduleNextScan(polling_policy_->PollingInterval()); | 97 ScheduleNextScan(polling_policy_->PollingInterval()); |
| 106 } | 98 } |
| 107 | 99 |
| 108 if (update_available || !is_first_scan_complete_) { | 100 if (update_available || !is_first_scan_complete_) { |
| 109 is_first_scan_complete_ = true; | 101 is_first_scan_complete_ = true; |
| 110 RunCallbacks(); | 102 RunCallbacks(); |
| 111 } | 103 } |
| 112 } | 104 } |
| 113 | 105 |
| 114 void WifiDataProviderChromeOs::ScheduleNextScan(int interval) { | 106 void WifiDataProviderChromeOs::ScheduleNextScan(int interval) { |
| 115 DCHECK(CalledOnClientThread()); | 107 DCHECK(CalledOnClientThread()); |
| 116 DCHECK(started_); | 108 DCHECK(started_); |
| 117 main_task_runner_->PostDelayedTask( | 109 if (!NetworkHandler::IsInitialized()) { |
| 110 LOG(ERROR) << "ScheduleNextScan called with uninitialized NetworkHandler"; |
| 111 return; |
| 112 } |
| 113 NetworkHandler::Get()->task_runner()->PostDelayedTask( |
| 118 FROM_HERE, | 114 FROM_HERE, |
| 119 base::Bind(&WifiDataProviderChromeOs::DoWifiScanTaskOnUIThread, this), | 115 base::Bind( |
| 116 &WifiDataProviderChromeOs::DoWifiScanTaskOnNetworkHandlerThread, |
| 117 this), |
| 120 base::TimeDelta::FromMilliseconds(interval)); | 118 base::TimeDelta::FromMilliseconds(interval)); |
| 121 } | 119 } |
| 122 | 120 |
| 123 void WifiDataProviderChromeOs::ScheduleStop() { | 121 void WifiDataProviderChromeOs::ScheduleStop() { |
| 124 DCHECK(CalledOnClientThread()); | 122 DCHECK(CalledOnClientThread()); |
| 125 DCHECK(started_); | 123 DCHECK(started_); |
| 126 started_ = false; | 124 started_ = false; |
| 127 } | 125 } |
| 128 | 126 |
| 129 void WifiDataProviderChromeOs::ScheduleStart() { | 127 void WifiDataProviderChromeOs::ScheduleStart() { |
| 130 DCHECK(CalledOnClientThread()); | 128 DCHECK(CalledOnClientThread()); |
| 131 DCHECK(!started_); | 129 DCHECK(!started_); |
| 130 if (!NetworkHandler::IsInitialized()) { |
| 131 LOG(ERROR) << "ScheduleStart called with uninitialized NetworkHandler"; |
| 132 return; |
| 133 } |
| 132 started_ = true; | 134 started_ = true; |
| 133 // Perform first scan ASAP regardless of the polling policy. If this scan | 135 // Perform first scan ASAP regardless of the polling policy. If this scan |
| 134 // fails we'll retry at a rate in line with the polling policy. | 136 // fails we'll retry at a rate in line with the polling policy. |
| 135 main_task_runner_->PostTask( | 137 NetworkHandler::Get()->task_runner()->PostTask( |
| 136 FROM_HERE, | 138 FROM_HERE, |
| 137 base::Bind(&WifiDataProviderChromeOs::DoStartTaskOnUIThread, this)); | 139 base::Bind( |
| 140 &WifiDataProviderChromeOs::DoWifiScanTaskOnNetworkHandlerThread, |
| 141 this)); |
| 138 } | 142 } |
| 139 | 143 |
| 140 bool WifiDataProviderChromeOs::GetAccessPointData( | 144 bool WifiDataProviderChromeOs::GetAccessPointData( |
| 141 WifiData::AccessPointDataSet* result) { | 145 WifiData::AccessPointDataSet* result) { |
| 142 // If in startup or shutdown, chromeos::NetworkHandler is uninitialized. | 146 // If in startup or shutdown, NetworkHandler is uninitialized. |
| 143 if (!chromeos::NetworkHandler::IsInitialized()) | 147 if (!NetworkHandler::IsInitialized()) |
| 144 return false; // Data not ready. | 148 return false; // Data not ready. |
| 145 | 149 |
| 150 DCHECK(NetworkHandler::Get()->task_runner()->BelongsToCurrentThread()); |
| 151 |
| 146 // If wifi isn't enabled, we've effectively completed the task. | 152 // If wifi isn't enabled, we've effectively completed the task. |
| 147 chromeos::GeolocationHandler* const geolocation_handler = | 153 chromeos::GeolocationHandler* const geolocation_handler = |
| 148 chromeos::NetworkHandler::Get()->geolocation_handler(); | 154 NetworkHandler::Get()->geolocation_handler(); |
| 149 if (!geolocation_handler || !geolocation_handler->wifi_enabled()) | 155 if (!geolocation_handler || !geolocation_handler->wifi_enabled()) |
| 150 return true; // Access point list is empty, no more data. | 156 return true; // Access point list is empty, no more data. |
| 151 | 157 |
| 152 chromeos::WifiAccessPointVector access_points; | 158 chromeos::WifiAccessPointVector access_points; |
| 153 int64_t age_ms = 0; | 159 int64_t age_ms = 0; |
| 154 if (!geolocation_handler->GetWifiAccessPoints(&access_points, &age_ms)) | 160 if (!geolocation_handler->GetWifiAccessPoints(&access_points, &age_ms)) |
| 155 return false; | 161 return false; |
| 156 | 162 |
| 157 for (const auto& access_point : access_points) { | 163 for (const auto& access_point : access_points) { |
| 158 AccessPointData ap_data; | 164 AccessPointData ap_data; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 169 return false; | 175 return false; |
| 170 return true; | 176 return true; |
| 171 } | 177 } |
| 172 | 178 |
| 173 // static | 179 // static |
| 174 WifiDataProvider* WifiDataProviderManager::DefaultFactoryFunction() { | 180 WifiDataProvider* WifiDataProviderManager::DefaultFactoryFunction() { |
| 175 return new WifiDataProviderChromeOs(); | 181 return new WifiDataProviderChromeOs(); |
| 176 } | 182 } |
| 177 | 183 |
| 178 } // namespace device | 184 } // namespace device |
| OLD | NEW |