| 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 // Provides wifi scan API binding for chromeos, using proprietary APIs. | |
| 6 | |
| 7 #include "content/browser/geolocation/wifi_data_provider_chromeos.h" | |
| 8 | |
| 9 #include <stdint.h> | |
| 10 | |
| 11 #include "base/bind.h" | |
| 12 #include "base/strings/utf_string_conversions.h" | |
| 13 #include "chromeos/network/geolocation_handler.h" | |
| 14 #include "content/browser/geolocation/wifi_data_provider_manager.h" | |
| 15 #include "content/public/browser/browser_thread.h" | |
| 16 | |
| 17 namespace content { | |
| 18 | |
| 19 namespace { | |
| 20 | |
| 21 // The time periods between successive polls of the wifi data. | |
| 22 const int kDefaultPollingIntervalMilliseconds = 10 * 1000; // 10s | |
| 23 const int kNoChangePollingIntervalMilliseconds = 2 * 60 * 1000; // 2 mins | |
| 24 const int kTwoNoChangePollingIntervalMilliseconds = 10 * 60 * 1000; // 10 mins | |
| 25 const int kNoWifiPollingIntervalMilliseconds = 20 * 1000; // 20s | |
| 26 | |
| 27 } // namespace | |
| 28 | |
| 29 WifiDataProviderChromeOs::WifiDataProviderChromeOs() | |
| 30 : started_(false), is_first_scan_complete_(false) { | |
| 31 } | |
| 32 | |
| 33 WifiDataProviderChromeOs::~WifiDataProviderChromeOs() { | |
| 34 } | |
| 35 | |
| 36 void WifiDataProviderChromeOs::StartDataProvider() { | |
| 37 DCHECK(CalledOnClientThread()); | |
| 38 | |
| 39 DCHECK(polling_policy_ == NULL); | |
| 40 polling_policy_.reset( | |
| 41 new GenericWifiPollingPolicy<kDefaultPollingIntervalMilliseconds, | |
| 42 kNoChangePollingIntervalMilliseconds, | |
| 43 kTwoNoChangePollingIntervalMilliseconds, | |
| 44 kNoWifiPollingIntervalMilliseconds>); | |
| 45 | |
| 46 ScheduleStart(); | |
| 47 } | |
| 48 | |
| 49 void WifiDataProviderChromeOs::StopDataProvider() { | |
| 50 DCHECK(CalledOnClientThread()); | |
| 51 | |
| 52 polling_policy_.reset(); | |
| 53 ScheduleStop(); | |
| 54 } | |
| 55 | |
| 56 bool WifiDataProviderChromeOs::GetData(WifiData* data) { | |
| 57 DCHECK(CalledOnClientThread()); | |
| 58 DCHECK(data); | |
| 59 *data = wifi_data_; | |
| 60 return is_first_scan_complete_; | |
| 61 } | |
| 62 | |
| 63 void WifiDataProviderChromeOs::DoStartTaskOnUIThread() { | |
| 64 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 65 DoWifiScanTaskOnUIThread(); | |
| 66 } | |
| 67 | |
| 68 void WifiDataProviderChromeOs::DoWifiScanTaskOnUIThread() { | |
| 69 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 70 | |
| 71 // This method could be scheduled after a ScheduleStop. | |
| 72 if (!started_) | |
| 73 return; | |
| 74 | |
| 75 WifiData new_data; | |
| 76 | |
| 77 if (GetAccessPointData(&new_data.access_point_data)) { | |
| 78 client_task_runner()->PostTask( | |
| 79 FROM_HERE, | |
| 80 base::Bind(&WifiDataProviderChromeOs::DidWifiScanTask, this, new_data)); | |
| 81 } else { | |
| 82 client_task_runner()->PostTask( | |
| 83 FROM_HERE, | |
| 84 base::Bind(&WifiDataProviderChromeOs::DidWifiScanTaskNoResults, this)); | |
| 85 } | |
| 86 } | |
| 87 | |
| 88 void WifiDataProviderChromeOs::DidWifiScanTaskNoResults() { | |
| 89 DCHECK(CalledOnClientThread()); | |
| 90 // Schedule next scan if started (StopDataProvider could have been called | |
| 91 // in between DoWifiScanTaskOnUIThread and this method). | |
| 92 if (started_) | |
| 93 ScheduleNextScan(polling_policy_->NoWifiInterval()); | |
| 94 } | |
| 95 | |
| 96 void WifiDataProviderChromeOs::DidWifiScanTask(const WifiData& new_data) { | |
| 97 DCHECK(CalledOnClientThread()); | |
| 98 bool update_available = wifi_data_.DiffersSignificantly(new_data); | |
| 99 wifi_data_ = new_data; | |
| 100 // Schedule next scan if started (StopDataProvider could have been called | |
| 101 // in between DoWifiScanTaskOnUIThread and this method). | |
| 102 if (started_) { | |
| 103 polling_policy_->UpdatePollingInterval(update_available); | |
| 104 ScheduleNextScan(polling_policy_->PollingInterval()); | |
| 105 } | |
| 106 | |
| 107 if (update_available || !is_first_scan_complete_) { | |
| 108 is_first_scan_complete_ = true; | |
| 109 RunCallbacks(); | |
| 110 } | |
| 111 } | |
| 112 | |
| 113 void WifiDataProviderChromeOs::ScheduleNextScan(int interval) { | |
| 114 DCHECK(CalledOnClientThread()); | |
| 115 DCHECK(started_); | |
| 116 BrowserThread::PostDelayedTask( | |
| 117 BrowserThread::UI, | |
| 118 FROM_HERE, | |
| 119 base::Bind(&WifiDataProviderChromeOs::DoWifiScanTaskOnUIThread, this), | |
| 120 base::TimeDelta::FromMilliseconds(interval)); | |
| 121 } | |
| 122 | |
| 123 void WifiDataProviderChromeOs::ScheduleStop() { | |
| 124 DCHECK(CalledOnClientThread()); | |
| 125 DCHECK(started_); | |
| 126 started_ = false; | |
| 127 } | |
| 128 | |
| 129 void WifiDataProviderChromeOs::ScheduleStart() { | |
| 130 DCHECK(CalledOnClientThread()); | |
| 131 DCHECK(!started_); | |
| 132 started_ = true; | |
| 133 // 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. | |
| 135 BrowserThread::PostTask( | |
| 136 BrowserThread::UI, | |
| 137 FROM_HERE, | |
| 138 base::Bind(&WifiDataProviderChromeOs::DoStartTaskOnUIThread, this)); | |
| 139 } | |
| 140 | |
| 141 bool WifiDataProviderChromeOs::GetAccessPointData( | |
| 142 WifiData::AccessPointDataSet* result) { | |
| 143 // If wifi isn't enabled, we've effectively completed the task. | |
| 144 // Return true to indicate an empty access point list. | |
| 145 if (!chromeos::NetworkHandler::Get()->geolocation_handler()->wifi_enabled()) | |
| 146 return true; | |
| 147 | |
| 148 chromeos::WifiAccessPointVector access_points; | |
| 149 int64_t age_ms = 0; | |
| 150 if (!chromeos::NetworkHandler::Get()->geolocation_handler()-> | |
| 151 GetWifiAccessPoints(&access_points, &age_ms)) { | |
| 152 return false; | |
| 153 } | |
| 154 for (const auto& access_point : access_points) { | |
| 155 AccessPointData ap_data; | |
| 156 ap_data.mac_address = base::ASCIIToUTF16(access_point.mac_address); | |
| 157 ap_data.radio_signal_strength = access_point.signal_strength; | |
| 158 ap_data.channel = access_point.channel; | |
| 159 ap_data.signal_to_noise = access_point.signal_to_noise; | |
| 160 ap_data.ssid = base::UTF8ToUTF16(access_point.ssid); | |
| 161 result->insert(ap_data); | |
| 162 } | |
| 163 // If the age is significantly longer than our long polling time, assume the | |
| 164 // data is stale and return false which will trigger a faster update. | |
| 165 if (age_ms > kTwoNoChangePollingIntervalMilliseconds * 2) | |
| 166 return false; | |
| 167 return true; | |
| 168 } | |
| 169 | |
| 170 // static | |
| 171 WifiDataProvider* WifiDataProviderManager::DefaultFactoryFunction() { | |
| 172 return new WifiDataProviderChromeOs(); | |
| 173 } | |
| 174 | |
| 175 } // namespace content | |
| OLD | NEW |