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 // Provides wifi scan API binding for chromeos, using proprietary APIs.
7 #include "content/browser/geolocation/wifi_data_provider_chromeos.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "chromeos/network/geolocation_handler.h"
12 #include "content/public/browser/browser_thread.h"
18 // The time periods between successive polls of the wifi data.
19 const int kDefaultPollingIntervalMilliseconds = 10 * 1000; // 10s
20 const int kNoChangePollingIntervalMilliseconds = 2 * 60 * 1000; // 2 mins
21 const int kTwoNoChangePollingIntervalMilliseconds = 10 * 60 * 1000; // 10 mins
22 const int kNoWifiPollingIntervalMilliseconds = 20 * 1000; // 20s
26 WifiDataProviderChromeOs::WifiDataProviderChromeOs() : started_(false) {
29 WifiDataProviderChromeOs::~WifiDataProviderChromeOs() {
32 void WifiDataProviderChromeOs::StartDataProvider() {
33 DCHECK(CalledOnClientThread());
35 DCHECK(polling_policy_ == NULL);
36 polling_policy_.reset(
37 new GenericWifiPollingPolicy<kDefaultPollingIntervalMilliseconds,
38 kNoChangePollingIntervalMilliseconds,
39 kTwoNoChangePollingIntervalMilliseconds,
40 kNoWifiPollingIntervalMilliseconds>);
45 void WifiDataProviderChromeOs::StopDataProvider() {
46 DCHECK(CalledOnClientThread());
48 polling_policy_.reset();
52 bool WifiDataProviderChromeOs::GetData(WifiData* data) {
53 DCHECK(CalledOnClientThread());
56 return is_first_scan_complete_;
59 void WifiDataProviderChromeOs::DoStartTaskOnUIThread() {
60 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
61 DoWifiScanTaskOnUIThread();
64 void WifiDataProviderChromeOs::DoWifiScanTaskOnUIThread() {
65 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
67 // This method could be scheduled after a ScheduleStop.
73 if (GetAccessPointData(&new_data.access_point_data)) {
74 client_loop()->PostTask(
76 base::Bind(&WifiDataProviderChromeOs::DidWifiScanTask, this, new_data));
78 client_loop()->PostTask(
80 base::Bind(&WifiDataProviderChromeOs::DidWifiScanTaskNoResults, this));
84 void WifiDataProviderChromeOs::DidWifiScanTaskNoResults() {
85 DCHECK(CalledOnClientThread());
86 // Schedule next scan if started (StopDataProvider could have been called
87 // in between DoWifiScanTaskOnUIThread and this method).
89 ScheduleNextScan(polling_policy_->NoWifiInterval());
92 void WifiDataProviderChromeOs::DidWifiScanTask(const WifiData& new_data) {
93 DCHECK(CalledOnClientThread());
94 bool update_available = wifi_data_.DiffersSignificantly(new_data);
95 wifi_data_ = new_data;
96 // Schedule next scan if started (StopDataProvider could have been called
97 // in between DoWifiScanTaskOnUIThread and this method).
99 polling_policy_->UpdatePollingInterval(update_available);
100 ScheduleNextScan(polling_policy_->PollingInterval());
103 if (update_available || !is_first_scan_complete_) {
104 is_first_scan_complete_ = true;
109 void WifiDataProviderChromeOs::ScheduleNextScan(int interval) {
110 DCHECK(CalledOnClientThread());
112 BrowserThread::PostDelayedTask(
115 base::Bind(&WifiDataProviderChromeOs::DoWifiScanTaskOnUIThread, this),
116 base::TimeDelta::FromMilliseconds(interval));
119 void WifiDataProviderChromeOs::ScheduleStop() {
120 DCHECK(CalledOnClientThread());
125 void WifiDataProviderChromeOs::ScheduleStart() {
126 DCHECK(CalledOnClientThread());
129 // Perform first scan ASAP regardless of the polling policy. If this scan
130 // fails we'll retry at a rate in line with the polling policy.
131 BrowserThread::PostTask(
134 base::Bind(&WifiDataProviderChromeOs::DoStartTaskOnUIThread, this));
137 bool WifiDataProviderChromeOs::GetAccessPointData(
138 WifiData::AccessPointDataSet* result) {
139 // If wifi isn't enabled, we've effectively completed the task.
140 // Return true to indicate an empty access point list.
141 if (!chromeos::NetworkHandler::Get()->geolocation_handler()->wifi_enabled())
144 chromeos::WifiAccessPointVector access_points;
146 if (!chromeos::NetworkHandler::Get()->geolocation_handler()->
147 GetWifiAccessPoints(&access_points, &age_ms)) {
150 for (chromeos::WifiAccessPointVector::const_iterator i
151 = access_points.begin();
152 i != access_points.end(); ++i) {
153 AccessPointData ap_data;
154 ap_data.mac_address = ASCIIToUTF16(i->mac_address);
155 ap_data.radio_signal_strength = i->signal_strength;
156 ap_data.channel = i->channel;
157 ap_data.signal_to_noise = i->signal_to_noise;
158 ap_data.ssid = UTF8ToUTF16(i->ssid);
159 result->insert(ap_data);
161 // If the age is significantly longer than our long polling time, assume the
162 // data is stale and return false which will trigger a faster update.
163 if (age_ms > kTwoNoChangePollingIntervalMilliseconds * 2)
169 WifiDataProviderImplBase* WifiDataProvider::DefaultFactoryFunction() {
170 return new WifiDataProviderChromeOs();
173 } // namespace content