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 #include "content/browser/geolocation/wifi_data_provider_common.h" |
| 6 |
| 7 #include "base/bind.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" |
| 12 |
| 13 namespace content { |
| 14 |
| 15 base::string16 MacAddressAsString16(const uint8_t 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, |
| 21 mac_as_int[0], |
| 22 mac_as_int[1], |
| 23 mac_as_int[2], |
| 24 mac_as_int[3], |
| 25 mac_as_int[4], |
| 26 mac_as_int[5])); |
| 27 } |
| 28 |
| 29 WifiDataProviderCommon::WifiDataProviderCommon() |
| 30 : is_first_scan_complete_(false), |
| 31 weak_factory_(this) { |
| 32 } |
| 33 |
| 34 WifiDataProviderCommon::~WifiDataProviderCommon() { |
| 35 } |
| 36 |
| 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; |
| 43 return; |
| 44 } |
| 45 |
| 46 DCHECK(polling_policy_ == NULL); |
| 47 polling_policy_.reset(NewPollingPolicy()); |
| 48 DCHECK(polling_policy_ != NULL); |
| 49 |
| 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. |
| 52 ScheduleNextScan(0); |
| 53 } |
| 54 |
| 55 void WifiDataProviderCommon::StopDataProvider() { |
| 56 wlan_api_.reset(); |
| 57 polling_policy_.reset(); |
| 58 } |
| 59 |
| 60 bool WifiDataProviderCommon::GetData(WifiData* data) { |
| 61 *data = wifi_data_; |
| 62 // If we've successfully completed a scan, indicate that we have all of the |
| 63 // data we can get. |
| 64 return is_first_scan_complete_; |
| 65 } |
| 66 |
| 67 void WifiDataProviderCommon::DoWifiScanTask() { |
| 68 bool update_available = false; |
| 69 WifiData new_data; |
| 70 if (!wlan_api_->GetAccessPointData(&new_data.access_point_data)) { |
| 71 ScheduleNextScan(polling_policy_->NoWifiInterval()); |
| 72 } else { |
| 73 update_available = wifi_data_.DiffersSignificantly(new_data); |
| 74 wifi_data_ = new_data; |
| 75 polling_policy_->UpdatePollingInterval(update_available); |
| 76 ScheduleNextScan(polling_policy_->PollingInterval()); |
| 77 } |
| 78 if (update_available || !is_first_scan_complete_) { |
| 79 is_first_scan_complete_ = true; |
| 80 RunCallbacks(); |
| 81 } |
| 82 } |
| 83 |
| 84 void WifiDataProviderCommon::ScheduleNextScan(int interval) { |
| 85 client_task_runner()->PostDelayedTask( |
| 86 FROM_HERE, base::Bind(&WifiDataProviderCommon::DoWifiScanTask, |
| 87 weak_factory_.GetWeakPtr()), |
| 88 base::TimeDelta::FromMilliseconds(interval)); |
| 89 } |
| 90 |
| 91 } // namespace content |
OLD | NEW |