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 #include "content/browser/geolocation/wifi_data_provider_common.h"
8 #include "base/location.h"
9 #include "base/single_thread_task_runner.h"
10 #include "base/strings/stringprintf.h"
11 #include "base/strings/utf_string_conversions.h"
15 base::string16
MacAddressAsString16(const uint8 mac_as_int
[6]) {
16 // mac_as_int is big-endian. Write in byte chunks.
17 // Format is XX-XX-XX-XX-XX-XX.
18 static const char* const kMacFormatString
=
19 "%02x-%02x-%02x-%02x-%02x-%02x";
20 return base::ASCIIToUTF16(base::StringPrintf(kMacFormatString
,
29 WifiDataProviderCommon::WifiDataProviderCommon()
30 : is_first_scan_complete_(false),
34 WifiDataProviderCommon::~WifiDataProviderCommon() {
37 void WifiDataProviderCommon::StartDataProvider() {
38 DCHECK(wlan_api_
== NULL
);
39 wlan_api_
.reset(NewWlanApi());
40 if (wlan_api_
== NULL
) {
41 // Error! Can't do scans, so don't try and schedule one.
42 is_first_scan_complete_
= true;
46 DCHECK(polling_policy_
== NULL
);
47 polling_policy_
.reset(NewPollingPolicy());
48 DCHECK(polling_policy_
!= NULL
);
50 // Perform first scan ASAP regardless of the polling policy. If this scan
51 // fails we'll retry at a rate in line with the polling policy.
55 void WifiDataProviderCommon::StopDataProvider() {
57 polling_policy_
.reset();
60 bool WifiDataProviderCommon::GetData(WifiData
* data
) {
62 // If we've successfully completed a scan, indicate that we have all of the
64 return is_first_scan_complete_
;
67 void WifiDataProviderCommon::DoWifiScanTask() {
68 bool update_available
= false;
70 if (!wlan_api_
->GetAccessPointData(&new_data
.access_point_data
)) {
71 ScheduleNextScan(polling_policy_
->NoWifiInterval());
73 update_available
= wifi_data_
.DiffersSignificantly(new_data
);
74 wifi_data_
= new_data
;
75 polling_policy_
->UpdatePollingInterval(update_available
);
76 ScheduleNextScan(polling_policy_
->PollingInterval());
78 if (update_available
|| !is_first_scan_complete_
) {
79 is_first_scan_complete_
= true;
84 void WifiDataProviderCommon::ScheduleNextScan(int interval
) {
85 client_loop()->task_runner()->PostDelayedTask(
86 FROM_HERE
, base::Bind(&WifiDataProviderCommon::DoWifiScanTask
,
87 weak_factory_
.GetWeakPtr()),
88 base::TimeDelta::FromMilliseconds(interval
));
91 } // namespace content