| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2010 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/utf_string_conversions.h" | |
| 8 | |
| 9 string16 MacAddressAsString16(const uint8 mac_as_int[6]) { | |
| 10 // mac_as_int is big-endian. Write in byte chunks. | |
| 11 // Format is XX-XX-XX-XX-XX-XX. | |
| 12 static const wchar_t* const kMacFormatString = | |
| 13 L"%02x-%02x-%02x-%02x-%02x-%02x"; | |
| 14 return WideToUTF16(StringPrintf(kMacFormatString, | |
| 15 mac_as_int[0], mac_as_int[1], mac_as_int[2], | |
| 16 mac_as_int[3], mac_as_int[4], mac_as_int[5])); | |
| 17 } | |
| 18 | |
| 19 WifiDataProviderCommon::WifiDataProviderCommon() | |
| 20 : Thread("Geolocation_wifi_provider"), | |
| 21 is_first_scan_complete_(false), | |
| 22 ALLOW_THIS_IN_INITIALIZER_LIST(task_factory_(this)) { | |
| 23 } | |
| 24 | |
| 25 WifiDataProviderCommon::~WifiDataProviderCommon() { | |
| 26 // Thread must be stopped before entering destructor chain to avoid race | |
| 27 // conditions; see comment in DeviceDataProvider::Unregister. | |
| 28 DCHECK(!IsRunning()); // Must call StopDataProvider before destroying me. | |
| 29 } | |
| 30 | |
| 31 bool WifiDataProviderCommon::StartDataProvider() { | |
| 32 DCHECK(CalledOnClientThread()); | |
| 33 DCHECK(!IsRunning()); // StartDataProvider must only be called once. | |
| 34 return Start(); | |
| 35 } | |
| 36 | |
| 37 void WifiDataProviderCommon::StopDataProvider() { | |
| 38 DCHECK(CalledOnClientThread()); | |
| 39 Stop(); | |
| 40 } | |
| 41 | |
| 42 bool WifiDataProviderCommon::GetData(WifiData *data) { | |
| 43 DCHECK(CalledOnClientThread()); | |
| 44 DCHECK(data); | |
| 45 base::AutoLock lock(data_mutex_); | |
| 46 *data = wifi_data_; | |
| 47 // If we've successfully completed a scan, indicate that we have all of the | |
| 48 // data we can get. | |
| 49 return is_first_scan_complete_; | |
| 50 } | |
| 51 | |
| 52 // Thread implementation | |
| 53 void WifiDataProviderCommon::Init() { | |
| 54 DCHECK(wlan_api_ == NULL); | |
| 55 wlan_api_.reset(NewWlanApi()); | |
| 56 if (wlan_api_ == NULL) { | |
| 57 // Error! Can't do scans, so don't try and schedule one. | |
| 58 is_first_scan_complete_ = true; | |
| 59 return; | |
| 60 } | |
| 61 | |
| 62 DCHECK(polling_policy_ == NULL); | |
| 63 polling_policy_.reset(NewPollingPolicy()); | |
| 64 DCHECK(polling_policy_ != NULL); | |
| 65 | |
| 66 // Perform first scan ASAP regardless of the polling policy. If this scan | |
| 67 // fails we'll retry at a rate in line with the polling policy. | |
| 68 ScheduleNextScan(0); | |
| 69 } | |
| 70 | |
| 71 void WifiDataProviderCommon::CleanUp() { | |
| 72 // Destroy these instances in the thread on which they were created. | |
| 73 wlan_api_.reset(); | |
| 74 polling_policy_.reset(); | |
| 75 } | |
| 76 | |
| 77 void WifiDataProviderCommon::DoWifiScanTask() { | |
| 78 bool update_available = false; | |
| 79 WifiData new_data; | |
| 80 if (!wlan_api_->GetAccessPointData(&new_data.access_point_data)) { | |
| 81 ScheduleNextScan(polling_policy_->NoWifiInterval()); | |
| 82 } else { | |
| 83 { | |
| 84 base::AutoLock lock(data_mutex_); | |
| 85 update_available = wifi_data_.DiffersSignificantly(new_data); | |
| 86 wifi_data_ = new_data; | |
| 87 } | |
| 88 polling_policy_->UpdatePollingInterval(update_available); | |
| 89 ScheduleNextScan(polling_policy_->PollingInterval()); | |
| 90 } | |
| 91 if (update_available || !is_first_scan_complete_) { | |
| 92 is_first_scan_complete_ = true; | |
| 93 NotifyListeners(); | |
| 94 } | |
| 95 } | |
| 96 | |
| 97 void WifiDataProviderCommon::ScheduleNextScan(int interval) { | |
| 98 message_loop()->PostDelayedTask( | |
| 99 FROM_HERE, | |
| 100 task_factory_.NewRunnableMethod(&WifiDataProviderCommon::DoWifiScanTask), | |
| 101 interval); | |
| 102 } | |
| OLD | NEW |