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 // Provides wifi scan API binding for chromeos, using proprietary APIs. | 5 // Provides wifi scan API binding for chromeos, using proprietary APIs. |
| 6 | 6 |
| 7 #include "content/browser/geolocation/wifi_data_provider_chromeos.h" | 7 #include "content/browser/geolocation/wifi_data_provider_chromeos.h" |
| 8 | 8 |
| 9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
| 10 #include "chrome/browser/chromeos/cros/cros_library.h" | 10 #include "chrome/browser/chromeos/cros/cros_library.h" |
| 11 #include "chrome/browser/chromeos/cros/network_library.h" | 11 #include "chrome/browser/chromeos/cros/network_library.h" |
| 12 #include "content/browser/browser_thread.h" | |
| 12 | 13 |
| 13 namespace { | 14 namespace { |
| 14 // The time periods between successive polls of the wifi data. | 15 // The time periods between successive polls of the wifi data. |
| 15 const int kDefaultPollingIntervalMilliseconds = 10 * 1000; // 10s | 16 const int kDefaultPollingIntervalMilliseconds = 10 * 1000; // 10s |
| 16 const int kNoChangePollingIntervalMilliseconds = 2 * 60 * 1000; // 2 mins | 17 const int kNoChangePollingIntervalMilliseconds = 2 * 60 * 1000; // 2 mins |
| 17 const int kTwoNoChangePollingIntervalMilliseconds = 10 * 60 * 1000; // 10 mins | 18 const int kTwoNoChangePollingIntervalMilliseconds = 10 * 60 * 1000; // 10 mins |
| 18 const int kNoWifiPollingIntervalMilliseconds = 20 * 1000; // 20s | 19 const int kNoWifiPollingIntervalMilliseconds = 20 * 1000; // 20s |
| 19 } | 20 } |
| 20 | 21 |
| 21 namespace chromeos { | 22 namespace chromeos { |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 65 } | 66 } |
| 66 | 67 |
| 67 } // namespace | 68 } // namespace |
| 68 } // namespace chromeos | 69 } // namespace chromeos |
| 69 | 70 |
| 70 template<> | 71 template<> |
| 71 WifiDataProviderImplBase* WifiDataProvider::DefaultFactoryFunction() { | 72 WifiDataProviderImplBase* WifiDataProvider::DefaultFactoryFunction() { |
| 72 return new WifiDataProviderChromeOs(); | 73 return new WifiDataProviderChromeOs(); |
| 73 } | 74 } |
| 74 | 75 |
| 75 WifiDataProviderChromeOs::WifiDataProviderChromeOs() { | 76 WifiDataProviderChromeOs::WifiDataProviderChromeOs() : |
| 77 started_(false) { | |
| 76 } | 78 } |
| 77 | 79 |
| 78 WifiDataProviderChromeOs::~WifiDataProviderChromeOs() { | 80 WifiDataProviderChromeOs::~WifiDataProviderChromeOs() { |
| 79 } | 81 } |
| 80 | 82 |
| 83 bool WifiDataProviderChromeOs::StartDataProvider() { | |
| 84 DCHECK(CalledOnClientThread()); | |
| 85 DCHECK(!started_); | |
| 86 started_ = true; | |
| 87 | |
| 88 wlan_api_.reset(NewWlanApi()); | |
| 89 if (wlan_api_ == NULL) { | |
| 90 // Error! Can't do scans, so don't try and schedule one. | |
| 91 is_first_scan_complete_ = true; | |
| 92 return false; | |
| 93 } | |
| 94 | |
| 95 DCHECK(polling_policy_ == NULL); | |
| 96 polling_policy_.reset(NewPollingPolicy()); | |
| 97 DCHECK(polling_policy_ != NULL); | |
| 98 | |
| 99 // Perform first scan ASAP regardless of the polling policy. If this scan | |
| 100 // fails we'll retry at a rate in line with the polling policy. | |
| 101 ScheduleNextScan(0); | |
| 102 return true; | |
| 103 } | |
| 104 | |
| 105 void WifiDataProviderChromeOs::StopDataProvider() { | |
| 106 DCHECK(CalledOnClientThread()); | |
| 107 started_ = false; | |
| 108 wlan_api_.reset(); | |
| 109 polling_policy_.reset(); | |
| 110 } | |
| 111 | |
| 112 bool WifiDataProviderChromeOs::GetData(WifiData *data) { | |
|
bulach
2011/03/16 18:01:00
nit: WifiData* data
John Knottenbelt
2011/03/16 18:20:14
Done.
| |
| 113 DCHECK(CalledOnClientThread()); | |
| 114 DCHECK(data); | |
| 115 *data = wifi_data_; | |
| 116 return is_first_scan_complete_; | |
| 117 } | |
| 118 | |
| 81 WifiDataProviderCommon::WlanApiInterface* | 119 WifiDataProviderCommon::WlanApiInterface* |
| 82 WifiDataProviderChromeOs::NewWlanApi(chromeos::NetworkLibrary* lib) { | 120 WifiDataProviderChromeOs::NewWlanApi(chromeos::NetworkLibrary* lib) { |
| 83 return new chromeos::NetworkLibraryWlanApi(lib); | 121 return new chromeos::NetworkLibraryWlanApi(lib); |
| 84 } | 122 } |
| 85 | 123 |
| 86 WifiDataProviderCommon::WlanApiInterface* | 124 WifiDataProviderCommon::WlanApiInterface* |
| 87 WifiDataProviderChromeOs::NewWlanApi() { | 125 WifiDataProviderChromeOs::NewWlanApi() { |
| 88 chromeos::CrosLibrary* cros_lib = chromeos::CrosLibrary::Get(); | 126 chromeos::CrosLibrary* cros_lib = chromeos::CrosLibrary::Get(); |
| 89 DCHECK(cros_lib); | 127 DCHECK(cros_lib); |
| 90 if (!cros_lib->EnsureLoaded()) | 128 if (!cros_lib->EnsureLoaded()) |
| 91 return NULL; | 129 return NULL; |
| 92 return NewWlanApi(cros_lib->GetNetworkLibrary()); | 130 return NewWlanApi(cros_lib->GetNetworkLibrary()); |
| 93 } | 131 } |
| 94 | 132 |
| 95 PollingPolicyInterface* WifiDataProviderChromeOs::NewPollingPolicy() { | 133 PollingPolicyInterface* WifiDataProviderChromeOs::NewPollingPolicy() { |
| 96 return new GenericPollingPolicy<kDefaultPollingIntervalMilliseconds, | 134 return new GenericPollingPolicy<kDefaultPollingIntervalMilliseconds, |
| 97 kNoChangePollingIntervalMilliseconds, | 135 kNoChangePollingIntervalMilliseconds, |
| 98 kTwoNoChangePollingIntervalMilliseconds, | 136 kTwoNoChangePollingIntervalMilliseconds, |
| 99 kNoWifiPollingIntervalMilliseconds>; | 137 kNoWifiPollingIntervalMilliseconds>; |
| 100 } | 138 } |
| 139 | |
| 140 void WifiDataProviderChromeOs::DoWifiScanTask() { | |
| 141 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 142 scoped_ptr<WifiData> new_data(new WifiData); | |
| 143 if (!wlan_api_->GetAccessPointData(&new_data->access_point_data)) | |
| 144 new_data.reset(); | |
| 145 | |
| 146 client_loop_->PostTask(FROM_HERE, NewRunnableMethod( | |
| 147 this, &WifiDataProviderChromeOs::DidWifiScanTask, | |
| 148 new_data.release())); | |
|
bulach
2011/03/16 18:01:00
heh, the thing is that whilst in flight, new_data
John Knottenbelt
2011/03/16 18:20:14
Done.
| |
| 149 } | |
| 150 | |
| 151 void WifiDataProviderChromeOs::DidWifiScanTask(WifiData* new_data) { | |
|
bulach
2011/03/16 18:01:00
and this can be come a const WifiData& new_data
John Knottenbelt
2011/03/16 18:20:14
Done.
| |
| 152 DCHECK(CalledOnClientThread()); | |
| 153 scoped_ptr<WifiData> delete_new_data(new_data); | |
| 154 | |
| 155 bool update_available = false; | |
| 156 if (!new_data) | |
| 157 ScheduleNextScan(polling_policy_->NoWifiInterval()); | |
| 158 else { | |
| 159 update_available = wifi_data_.DiffersSignificantly(*new_data); | |
| 160 wifi_data_ = *new_data; | |
| 161 | |
| 162 polling_policy_->UpdatePollingInterval(update_available); | |
| 163 ScheduleNextScan(polling_policy_->PollingInterval()); | |
| 164 } | |
| 165 if (update_available || !is_first_scan_complete_) { | |
| 166 is_first_scan_complete_ = true; | |
| 167 NotifyListeners(); | |
| 168 } | |
| 169 } | |
| 170 | |
| 171 void WifiDataProviderChromeOs::ScheduleNextScan(int interval) { | |
| 172 DCHECK(CalledOnClientThread()); | |
| 173 if (started_) { | |
| 174 BrowserThread::PostDelayedTask( | |
| 175 BrowserThread::UI, | |
| 176 FROM_HERE, | |
| 177 NewRunnableMethod(this, | |
| 178 &WifiDataProviderChromeOs::DoWifiScanTask), | |
| 179 interval); | |
| 180 } | |
| 181 } | |
| OLD | NEW |