Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/geolocation/wifi_data_provider_common.h" | 5 #include "chrome/browser/geolocation/wifi_data_provider_common.h" |
| 6 | 6 |
| 7 #include "base/string_util.h" | 7 #include "base/string_util.h" |
| 8 | 8 |
| 9 string16 MacAddressAsString16(const uint8 mac_as_int[6]) { | 9 string16 MacAddressAsString16(const uint8 mac_as_int[6]) { |
| 10 // mac_as_int is big-endian. Write in byte chunks. | 10 // mac_as_int is big-endian. Write in byte chunks. |
| 11 // Format is XX-XX-XX-XX-XX-XX. | 11 // Format is XX-XX-XX-XX-XX-XX. |
| 12 static const wchar_t* const kMacFormatString = | 12 static const wchar_t* const kMacFormatString = |
| 13 L"%02x-%02x-%02x-%02x-%02x-%02x"; | 13 L"%02x-%02x-%02x-%02x-%02x-%02x"; |
| 14 return WideToUTF16(StringPrintf(kMacFormatString, | 14 return WideToUTF16(StringPrintf(kMacFormatString, |
| 15 mac_as_int[0], mac_as_int[1], mac_as_int[2], | 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])); | 16 mac_as_int[3], mac_as_int[4], mac_as_int[5])); |
| 17 } | 17 } |
| 18 | |
| 19 WifiDataProviderCommon::WifiDataProviderCommon() | |
| 20 : Thread(__FILE__), | |
|
jorlow
2010/02/14 22:09:27
Hm...this seems kind of non-standard.
joth
2010/02/15 11:28:54
Good point, I did this as a placeholder and never
| |
| 21 is_first_scan_complete_(false), | |
| 22 ALLOW_THIS_IN_INITIALIZER_LIST(task_factory_(this)) { | |
|
jorlow
2010/02/14 22:09:27
I think usually this goes inside the task_factor_(
jorlow
2010/02/14 22:48:22
never mind
joth
2010/02/15 11:28:54
Now you point it out I do prefer the foo_(ALLOW_TH
| |
| 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 return Start(); | |
| 34 } | |
| 35 | |
| 36 void WifiDataProviderCommon::StopDataProvider() { | |
| 37 DCHECK(CalledOnClientThread()); | |
| 38 Stop(); | |
| 39 } | |
| 40 | |
| 41 bool WifiDataProviderCommon::GetData(WifiData *data) { | |
| 42 DCHECK(CalledOnClientThread()); | |
| 43 DCHECK(data); | |
| 44 AutoLock lock(data_mutex_); | |
| 45 *data = wifi_data_; | |
| 46 // If we've successfully completed a scan, indicate that we have all of the | |
| 47 // data we can get. | |
| 48 return is_first_scan_complete_; | |
| 49 } | |
| 50 | |
| 51 // Thread implementation | |
| 52 void WifiDataProviderCommon::Init() { | |
| 53 DCHECK(wlan_api_ == NULL); | |
| 54 wlan_api_.reset(NewWlanApi()); | |
| 55 if (wlan_api_ == NULL) { | |
| 56 // Error! Can't do scans, so don't try and schedule one. | |
| 57 is_first_scan_complete_ = true; | |
| 58 return; | |
| 59 } | |
| 60 | |
| 61 DCHECK(polling_policy_ == NULL); | |
| 62 polling_policy_.reset(NewPolicyPolicy()); | |
| 63 DCHECK(polling_policy_ != NULL); | |
| 64 | |
| 65 ScheduleNextScan(); | |
| 66 } | |
| 67 | |
| 68 void WifiDataProviderCommon::CleanUp() { | |
| 69 // Destroy these instances in the thread on which they were created. | |
| 70 wlan_api_.reset(); | |
| 71 polling_policy_.reset(); | |
| 72 } | |
| 73 | |
| 74 void WifiDataProviderCommon::DoWifiScanTask() { | |
| 75 WifiData new_data; | |
| 76 if (wlan_api_->GetAccessPointData(&new_data.access_point_data)) { | |
| 77 data_mutex_.Acquire(); | |
| 78 bool update_available = wifi_data_.DiffersSignificantly(new_data); | |
| 79 wifi_data_ = new_data; | |
| 80 data_mutex_.Release(); | |
| 81 polling_policy_->UpdatePollingInterval(update_available); | |
| 82 if (update_available || !is_first_scan_complete_) { | |
| 83 is_first_scan_complete_ = true; | |
| 84 NotifyListeners(); | |
| 85 } | |
| 86 } | |
| 87 ScheduleNextScan(); | |
| 88 } | |
| 89 | |
| 90 void WifiDataProviderCommon::ScheduleNextScan() { | |
| 91 message_loop()->PostDelayedTask(FROM_HERE, | |
| 92 task_factory_.NewRunnableMethod(&WifiDataProviderCommon::DoWifiScanTask), | |
| 93 polling_policy_->PollingInterval()); | |
| 94 } | |
| OLD | NEW |