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 "device/geolocation/wifi_data_provider_chromeos.h" | |
8 | |
9 #include <stdint.h> | |
10 | |
11 #include "base/bind.h" | |
12 #include "base/single_thread_task_runner.h" | |
13 #include "base/strings/utf_string_conversions.h" | |
14 #include "base/threading/thread_task_runner_handle.h" | |
15 #include "chromeos/network/geolocation_handler.h" | |
16 #include "device/geolocation/wifi_data_provider_manager.h" | |
17 | |
18 namespace device { | |
19 | |
20 namespace { | |
21 | |
22 // The time periods between successive polls of the wifi data. | |
23 const int kDefaultPollingIntervalMilliseconds = 10 * 1000; // 10s | |
24 const int kNoChangePollingIntervalMilliseconds = 2 * 60 * 1000; // 2 mins | |
25 const int kTwoNoChangePollingIntervalMilliseconds = 10 * 60 * 1000; // 10 mins | |
26 const int kNoWifiPollingIntervalMilliseconds = 20 * 1000; // 20s | |
27 | |
28 } // namespace | |
29 | |
30 WifiDataProviderChromeOs::WifiDataProviderChromeOs() | |
31 : started_(false), | |
32 is_first_scan_complete_(false), | |
33 main_task_runner_(base::ThreadTaskRunnerHandle::Get()) {} | |
34 | |
35 WifiDataProviderChromeOs::~WifiDataProviderChromeOs() { | |
36 } | |
37 | |
38 void WifiDataProviderChromeOs::StartDataProvider() { | |
39 DCHECK(CalledOnClientThread()); | |
40 | |
41 DCHECK(polling_policy_ == NULL); | |
42 polling_policy_.reset( | |
43 new GenericWifiPollingPolicy<kDefaultPollingIntervalMilliseconds, | |
44 kNoChangePollingIntervalMilliseconds, | |
45 kTwoNoChangePollingIntervalMilliseconds, | |
46 kNoWifiPollingIntervalMilliseconds>); | |
47 | |
48 ScheduleStart(); | |
49 } | |
50 | |
51 void WifiDataProviderChromeOs::StopDataProvider() { | |
52 DCHECK(CalledOnClientThread()); | |
53 | |
54 polling_policy_.reset(); | |
55 ScheduleStop(); | |
56 } | |
57 | |
58 bool WifiDataProviderChromeOs::GetData(WifiData* data) { | |
59 DCHECK(CalledOnClientThread()); | |
60 DCHECK(data); | |
61 *data = wifi_data_; | |
62 return is_first_scan_complete_; | |
63 } | |
64 | |
65 void WifiDataProviderChromeOs::DoStartTaskOnUIThread() { | |
66 CHECK(main_task_runner_->BelongsToCurrentThread()); | |
67 DoWifiScanTaskOnUIThread(); | |
68 } | |
69 | |
70 void WifiDataProviderChromeOs::DoWifiScanTaskOnUIThread() { | |
71 CHECK(main_task_runner_->BelongsToCurrentThread()); | |
72 | |
73 // This method could be scheduled after a ScheduleStop. | |
74 if (!started_) | |
75 return; | |
76 | |
77 WifiData new_data; | |
78 | |
79 if (GetAccessPointData(&new_data.access_point_data)) { | |
80 client_task_runner()->PostTask( | |
81 FROM_HERE, | |
82 base::Bind(&WifiDataProviderChromeOs::DidWifiScanTask, this, new_data)); | |
83 } else { | |
84 client_task_runner()->PostTask( | |
85 FROM_HERE, | |
86 base::Bind(&WifiDataProviderChromeOs::DidWifiScanTaskNoResults, this)); | |
87 } | |
88 } | |
89 | |
90 void WifiDataProviderChromeOs::DidWifiScanTaskNoResults() { | |
91 DCHECK(CalledOnClientThread()); | |
92 // Schedule next scan if started (StopDataProvider could have been called | |
93 // in between DoWifiScanTaskOnUIThread and this method). | |
94 if (started_) | |
95 ScheduleNextScan(polling_policy_->NoWifiInterval()); | |
96 } | |
97 | |
98 void WifiDataProviderChromeOs::DidWifiScanTask(const WifiData& new_data) { | |
99 DCHECK(CalledOnClientThread()); | |
100 bool update_available = wifi_data_.DiffersSignificantly(new_data); | |
101 wifi_data_ = new_data; | |
102 // Schedule next scan if started (StopDataProvider could have been called | |
103 // in between DoWifiScanTaskOnUIThread and this method). | |
104 if (started_) { | |
105 polling_policy_->UpdatePollingInterval(update_available); | |
106 ScheduleNextScan(polling_policy_->PollingInterval()); | |
107 } | |
108 | |
109 if (update_available || !is_first_scan_complete_) { | |
110 is_first_scan_complete_ = true; | |
111 RunCallbacks(); | |
112 } | |
113 } | |
114 | |
115 void WifiDataProviderChromeOs::ScheduleNextScan(int interval) { | |
116 DCHECK(CalledOnClientThread()); | |
117 DCHECK(started_); | |
118 main_task_runner_->PostDelayedTask( | |
119 FROM_HERE, | |
120 base::Bind(&WifiDataProviderChromeOs::DoWifiScanTaskOnUIThread, this), | |
121 base::TimeDelta::FromMilliseconds(interval)); | |
122 } | |
123 | |
124 void WifiDataProviderChromeOs::ScheduleStop() { | |
125 DCHECK(CalledOnClientThread()); | |
126 DCHECK(started_); | |
127 started_ = false; | |
128 } | |
129 | |
130 void WifiDataProviderChromeOs::ScheduleStart() { | |
131 DCHECK(CalledOnClientThread()); | |
132 DCHECK(!started_); | |
133 started_ = true; | |
134 // Perform first scan ASAP regardless of the polling policy. If this scan | |
135 // fails we'll retry at a rate in line with the polling policy. | |
136 main_task_runner_->PostTask( | |
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 device | |
OLD | NEW |