Rename Animate as Begin(Main)Frame
[chromium-blink-merge.git] / content / browser / geolocation / wifi_data_provider_chromeos.cc
blob49edb23e85c29f20758d36441f14731d9955a081
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"
9 #include "base/bind.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "chromeos/network/geolocation_handler.h"
12 #include "content/public/browser/browser_thread.h"
14 namespace content {
16 namespace {
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
24 } // namespace
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>);
42 ScheduleStart();
45 void WifiDataProviderChromeOs::StopDataProvider() {
46 DCHECK(CalledOnClientThread());
48 polling_policy_.reset();
49 ScheduleStop();
52 bool WifiDataProviderChromeOs::GetData(WifiData* data) {
53 DCHECK(CalledOnClientThread());
54 DCHECK(data);
55 *data = wifi_data_;
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.
68 if (!started_)
69 return;
71 WifiData new_data;
73 if (GetAccessPointData(&new_data.access_point_data)) {
74 client_loop()->PostTask(
75 FROM_HERE,
76 base::Bind(&WifiDataProviderChromeOs::DidWifiScanTask, this, new_data));
77 } else {
78 client_loop()->PostTask(
79 FROM_HERE,
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).
88 if (started_)
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).
98 if (started_) {
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;
105 RunCallbacks();
109 void WifiDataProviderChromeOs::ScheduleNextScan(int interval) {
110 DCHECK(CalledOnClientThread());
111 DCHECK(started_);
112 BrowserThread::PostDelayedTask(
113 BrowserThread::UI,
114 FROM_HERE,
115 base::Bind(&WifiDataProviderChromeOs::DoWifiScanTaskOnUIThread, this),
116 base::TimeDelta::FromMilliseconds(interval));
119 void WifiDataProviderChromeOs::ScheduleStop() {
120 DCHECK(CalledOnClientThread());
121 DCHECK(started_);
122 started_ = false;
125 void WifiDataProviderChromeOs::ScheduleStart() {
126 DCHECK(CalledOnClientThread());
127 DCHECK(!started_);
128 started_ = true;
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(
132 BrowserThread::UI,
133 FROM_HERE,
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())
142 return true;
144 chromeos::WifiAccessPointVector access_points;
145 int64 age_ms = 0;
146 if (!chromeos::NetworkHandler::Get()->geolocation_handler()->
147 GetWifiAccessPoints(&access_points, &age_ms)) {
148 return false;
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 = base::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 = base::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)
164 return false;
165 return true;
168 // static
169 WifiDataProviderImplBase* WifiDataProvider::DefaultFactoryFunction() {
170 return new WifiDataProviderChromeOs();
173 } // namespace content