Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(35)

Side by Side Diff: chromeos/network/network_state_handler.cc

Issue 23712002: Cleanup network type matching. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Added unit tests and rebased. Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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 "chromeos/network/network_state_handler.h" 5 #include "chromeos/network/network_state_handler.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/format_macros.h" 8 #include "base/format_macros.h"
9 #include "base/location.h" 9 #include "base/location.h"
10 #include "base/metrics/histogram.h" 10 #include "base/metrics/histogram.h"
11 #include "base/stl_util.h" 11 #include "base/stl_util.h"
12 #include "base/strings/string_util.h" 12 #include "base/strings/string_util.h"
13 #include "base/strings/stringprintf.h" 13 #include "base/strings/stringprintf.h"
14 #include "base/values.h" 14 #include "base/values.h"
15 #include "chromeos/network/device_state.h" 15 #include "chromeos/network/device_state.h"
16 #include "chromeos/network/favorite_state.h" 16 #include "chromeos/network/favorite_state.h"
17 #include "chromeos/network/managed_state.h" 17 #include "chromeos/network/managed_state.h"
18 #include "chromeos/network/network_event_log.h" 18 #include "chromeos/network/network_event_log.h"
19 #include "chromeos/network/network_state.h" 19 #include "chromeos/network/network_state.h"
20 #include "chromeos/network/network_state_handler_observer.h" 20 #include "chromeos/network/network_state_handler_observer.h"
21 #include "chromeos/network/shill_property_handler.h" 21 #include "chromeos/network/shill_property_handler.h"
22 #include "chromeos/network/shill_property_util.h"
22 #include "third_party/cros_system_api/dbus/service_constants.h" 23 #include "third_party/cros_system_api/dbus/service_constants.h"
23 24
24 namespace chromeos { 25 namespace chromeos {
25 26
26 namespace { 27 namespace {
27 28
28 // Returns true if |network->type()| == |match_type|, or it matches one of the
29 // following special match types:
30 // * kMatchTypeDefault matches any network (i.e. the first instance)
31 // * kMatchTypeNonVirtual matches non virtual networks
32 // * kMatchTypeWireless matches wireless networks
33 // * kMatchTypeMobile matches cellular or wimax networks
34 bool ManagedStateMatchesType(const ManagedState* managed,
35 const std::string& match_type) {
36 const std::string& type = managed->type();
37 if (match_type == NetworkStateHandler::kMatchTypeDefault)
38 return true;
39 if (match_type == type)
40 return true;
41 if (match_type == NetworkStateHandler::kMatchTypeNonVirtual &&
42 type != flimflam::kTypeVPN) {
43 return true;
44 }
45 if (match_type == NetworkStateHandler::kMatchTypeWireless &&
46 type != flimflam::kTypeEthernet && type != flimflam::kTypeVPN) {
47 return true;
48 }
49 if (match_type == NetworkStateHandler::kMatchTypeMobile &&
50 (type == flimflam::kTypeCellular || type == flimflam::kTypeWimax)) {
51 return true;
52 }
53 return false;
54 }
55
56 bool ConnectionStateChanged(NetworkState* network, 29 bool ConnectionStateChanged(NetworkState* network,
57 const std::string& prev_connection_state) { 30 const std::string& prev_connection_state) {
58 return (network->connection_state() != prev_connection_state) && 31 return (network->connection_state() != prev_connection_state) &&
59 (network->connection_state() != flimflam::kStateIdle || 32 (network->connection_state() != flimflam::kStateIdle ||
60 !prev_connection_state.empty()); 33 !prev_connection_state.empty());
61 } 34 }
62 35
63 std::string GetManagedStateLogType(const ManagedState* state) { 36 std::string GetManagedStateLogType(const ManagedState* state) {
64 switch (state->managed_type()) { 37 switch (state->managed_type()) {
65 case ManagedState::MANAGED_TYPE_NETWORK: 38 case ManagedState::MANAGED_TYPE_NETWORK:
66 return "Network"; 39 return "Network";
67 case ManagedState::MANAGED_TYPE_FAVORITE: 40 case ManagedState::MANAGED_TYPE_FAVORITE:
68 return "Favorite"; 41 return "Favorite";
69 case ManagedState::MANAGED_TYPE_DEVICE: 42 case ManagedState::MANAGED_TYPE_DEVICE:
70 return "Device"; 43 return "Device";
71 } 44 }
72 NOTREACHED(); 45 NOTREACHED();
73 return ""; 46 return "";
74 } 47 }
75 48
76 std::string GetManagedStateLogName(const ManagedState* state) { 49 std::string GetManagedStateLogName(const ManagedState* state) {
77 if (!state) 50 if (!state)
78 return "None"; 51 return "None";
79 return base::StringPrintf("%s (%s)", state->name().c_str(), 52 return base::StringPrintf("%s (%s)", state->name().c_str(),
80 state->path().c_str()); 53 state->path().c_str());
81 } 54 }
82 55
83 } // namespace 56 } // namespace
84 57
85 const char NetworkStateHandler::kMatchTypeDefault[] = "default";
86 const char NetworkStateHandler::kMatchTypeWireless[] = "wireless";
87 const char NetworkStateHandler::kMatchTypeMobile[] = "mobile";
88 const char NetworkStateHandler::kMatchTypeNonVirtual[] = "non-virtual";
89 const char NetworkStateHandler::kDefaultCheckPortalList[] = 58 const char NetworkStateHandler::kDefaultCheckPortalList[] =
90 "ethernet,wifi,cellular"; 59 "ethernet,wifi,cellular";
91 60
92 NetworkStateHandler::NetworkStateHandler() { 61 NetworkStateHandler::NetworkStateHandler() {
93 } 62 }
94 63
95 NetworkStateHandler::~NetworkStateHandler() { 64 NetworkStateHandler::~NetworkStateHandler() {
96 STLDeleteContainerPointers(network_list_.begin(), network_list_.end()); 65 STLDeleteContainerPointers(network_list_.begin(), network_list_.end());
97 STLDeleteContainerPointers(favorite_list_.begin(), favorite_list_.end()); 66 STLDeleteContainerPointers(favorite_list_.begin(), favorite_list_.end());
98 STLDeleteContainerPointers(device_list_.begin(), device_list_.end()); 67 STLDeleteContainerPointers(device_list_.begin(), device_list_.end());
(...skipping 30 matching lines...) Expand all
129 network_event_log::LOG_LEVEL_DEBUG, 98 network_event_log::LOG_LEVEL_DEBUG,
130 "NetworkStateHandler::RemoveObserver", ""); 99 "NetworkStateHandler::RemoveObserver", "");
131 } 100 }
132 101
133 void NetworkStateHandler::UpdateManagerProperties() { 102 void NetworkStateHandler::UpdateManagerProperties() {
134 NET_LOG_USER("UpdateManagerProperties", ""); 103 NET_LOG_USER("UpdateManagerProperties", "");
135 shill_property_handler_->UpdateManagerProperties(); 104 shill_property_handler_->UpdateManagerProperties();
136 } 105 }
137 106
138 NetworkStateHandler::TechnologyState NetworkStateHandler::GetTechnologyState( 107 NetworkStateHandler::TechnologyState NetworkStateHandler::GetTechnologyState(
139 const std::string& type) const { 108 const NetworkTypePattern& type) const {
140 std::string technology = GetTechnologyForType(type); 109 std::string technology = GetTechnologyForType(type);
141 TechnologyState state; 110 TechnologyState state;
142 if (shill_property_handler_->IsTechnologyEnabled(technology)) 111 if (shill_property_handler_->IsTechnologyEnabled(technology))
143 state = TECHNOLOGY_ENABLED; 112 state = TECHNOLOGY_ENABLED;
144 else if (shill_property_handler_->IsTechnologyEnabling(technology)) 113 else if (shill_property_handler_->IsTechnologyEnabling(technology))
145 state = TECHNOLOGY_ENABLING; 114 state = TECHNOLOGY_ENABLING;
146 else if (shill_property_handler_->IsTechnologyUninitialized(technology)) 115 else if (shill_property_handler_->IsTechnologyUninitialized(technology))
147 state = TECHNOLOGY_UNINITIALIZED; 116 state = TECHNOLOGY_UNINITIALIZED;
148 else if (shill_property_handler_->IsTechnologyAvailable(technology)) 117 else if (shill_property_handler_->IsTechnologyAvailable(technology))
149 state = TECHNOLOGY_AVAILABLE; 118 state = TECHNOLOGY_AVAILABLE;
150 else 119 else
151 state = TECHNOLOGY_UNAVAILABLE; 120 state = TECHNOLOGY_UNAVAILABLE;
152 VLOG(2) << "GetTechnologyState: " << type << " = " << state; 121 VLOG(2) << "GetTechnologyState: " << type.ToDebugString() << " = " << state;
153 return state; 122 return state;
154 } 123 }
155 124
156 void NetworkStateHandler::SetTechnologyEnabled( 125 void NetworkStateHandler::SetTechnologyEnabled(
157 const std::string& type, 126 const NetworkTypePattern& type,
158 bool enabled, 127 bool enabled,
159 const network_handler::ErrorCallback& error_callback) { 128 const network_handler::ErrorCallback& error_callback) {
160 std::string technology = GetTechnologyForType(type); 129 std::string technology = GetTechnologyForType(type);
161 NET_LOG_USER("SetTechnologyEnabled", 130 NET_LOG_USER("SetTechnologyEnabled",
162 base::StringPrintf("%s:%d", technology.c_str(), enabled)); 131 base::StringPrintf("%s:%d", technology.c_str(), enabled));
163 shill_property_handler_->SetTechnologyEnabled( 132 shill_property_handler_->SetTechnologyEnabled(
164 technology, enabled, error_callback); 133 technology, enabled, error_callback);
165 // Signal Technology state changed -> ENABLING 134 // Signal Technology state changed -> ENABLING
166 NotifyManagerPropertyChanged(); 135 NotifyManagerPropertyChanged();
167 } 136 }
168 137
169 const DeviceState* NetworkStateHandler::GetDeviceState( 138 const DeviceState* NetworkStateHandler::GetDeviceState(
170 const std::string& device_path) const { 139 const std::string& device_path) const {
171 return GetModifiableDeviceState(device_path); 140 return GetModifiableDeviceState(device_path);
172 } 141 }
173 142
174 const DeviceState* NetworkStateHandler::GetDeviceStateByType( 143 const DeviceState* NetworkStateHandler::GetDeviceStateByType(
175 const std::string& type) const { 144 const NetworkTypePattern& type) const {
176 for (ManagedStateList::const_iterator iter = device_list_.begin(); 145 for (ManagedStateList::const_iterator iter = device_list_.begin();
177 iter != device_list_.end(); ++iter) { 146 iter != device_list_.end(); ++iter) {
178 ManagedState* device = *iter; 147 ManagedState* device = *iter;
179 if (ManagedStateMatchesType(device, type)) 148 if (device->Matches(type))
180 return device->AsDeviceState(); 149 return device->AsDeviceState();
181 } 150 }
182 return NULL; 151 return NULL;
183 } 152 }
184 153
185 bool NetworkStateHandler::GetScanningByType(const std::string& type) const { 154 bool NetworkStateHandler::GetScanningByType(
155 const NetworkTypePattern& type) const {
186 for (ManagedStateList::const_iterator iter = device_list_.begin(); 156 for (ManagedStateList::const_iterator iter = device_list_.begin();
187 iter != device_list_.end(); ++iter) { 157 iter != device_list_.end(); ++iter) {
188 const DeviceState* device = (*iter)->AsDeviceState(); 158 const DeviceState* device = (*iter)->AsDeviceState();
189 DCHECK(device); 159 DCHECK(device);
190 if (ManagedStateMatchesType(device, type) && device->scanning()) 160 if (device->Matches(type) && device->scanning())
191 return true; 161 return true;
192 } 162 }
193 return false; 163 return false;
194 } 164 }
195 165
196 const NetworkState* NetworkStateHandler::GetNetworkState( 166 const NetworkState* NetworkStateHandler::GetNetworkState(
197 const std::string& service_path) const { 167 const std::string& service_path) const {
198 return GetModifiableNetworkState(service_path); 168 return GetModifiableNetworkState(service_path);
199 } 169 }
200 170
201 const NetworkState* NetworkStateHandler::DefaultNetwork() const { 171 const NetworkState* NetworkStateHandler::DefaultNetwork() const {
202 if (network_list_.empty()) 172 if (network_list_.empty())
203 return NULL; 173 return NULL;
204 const NetworkState* network = network_list_.front()->AsNetworkState(); 174 const NetworkState* network = network_list_.front()->AsNetworkState();
205 DCHECK(network); 175 DCHECK(network);
206 if (!network->IsConnectedState()) 176 if (!network->IsConnectedState())
207 return NULL; 177 return NULL;
208 return network; 178 return network;
209 } 179 }
210 180
211 const NetworkState* NetworkStateHandler::ConnectedNetworkByType( 181 const NetworkState* NetworkStateHandler::ConnectedNetworkByType(
212 const std::string& type) const { 182 const NetworkTypePattern& type) const {
213 for (ManagedStateList::const_iterator iter = network_list_.begin(); 183 for (ManagedStateList::const_iterator iter = network_list_.begin();
214 iter != network_list_.end(); ++iter) { 184 iter != network_list_.end(); ++iter) {
215 const NetworkState* network = (*iter)->AsNetworkState(); 185 const NetworkState* network = (*iter)->AsNetworkState();
216 DCHECK(network); 186 DCHECK(network);
217 if (!network->IsConnectedState()) 187 if (!network->IsConnectedState())
218 break; // Connected networks are listed first. 188 break; // Connected networks are listed first.
219 if (ManagedStateMatchesType(network, type)) 189 if (network->Matches(type))
220 return network; 190 return network;
221 } 191 }
222 return NULL; 192 return NULL;
223 } 193 }
224 194
225 const NetworkState* NetworkStateHandler::ConnectingNetworkByType( 195 const NetworkState* NetworkStateHandler::ConnectingNetworkByType(
226 const std::string& type) const { 196 const NetworkTypePattern& type) const {
227 for (ManagedStateList::const_iterator iter = network_list_.begin(); 197 for (ManagedStateList::const_iterator iter = network_list_.begin();
228 iter != network_list_.end(); ++iter) { 198 iter != network_list_.end(); ++iter) {
229 const NetworkState* network = (*iter)->AsNetworkState(); 199 const NetworkState* network = (*iter)->AsNetworkState();
230 DCHECK(network); 200 DCHECK(network);
231 if (network->IsConnectedState()) 201 if (network->IsConnectedState())
232 continue; 202 continue;
233 if (!network->IsConnectingState()) 203 if (!network->IsConnectingState())
234 break; // Connected and connecting networks are listed first. 204 break; // Connected and connecting networks are listed first.
235 if (ManagedStateMatchesType(network, type)) 205 if (network->Matches(type))
236 return network; 206 return network;
237 } 207 }
238 return NULL; 208 return NULL;
239 } 209 }
240 210
241 const NetworkState* NetworkStateHandler::FirstNetworkByType( 211 const NetworkState* NetworkStateHandler::FirstNetworkByType(
242 const std::string& type) const { 212 const NetworkTypePattern& type) const {
243 for (ManagedStateList::const_iterator iter = network_list_.begin(); 213 for (ManagedStateList::const_iterator iter = network_list_.begin();
244 iter != network_list_.end(); ++iter) { 214 iter != network_list_.end(); ++iter) {
245 const NetworkState* network = (*iter)->AsNetworkState(); 215 const NetworkState* network = (*iter)->AsNetworkState();
246 DCHECK(network); 216 DCHECK(network);
247 if (ManagedStateMatchesType(network, type)) 217 if (network->Matches(type))
248 return network; 218 return network;
249 } 219 }
250 return NULL; 220 return NULL;
251 } 221 }
252 222
253 std::string NetworkStateHandler::HardwareAddressForType( 223 std::string NetworkStateHandler::HardwareAddressForType(
254 const std::string& type) const { 224 const NetworkTypePattern& type) const {
255 std::string result; 225 std::string result;
256 const NetworkState* network = ConnectedNetworkByType(type); 226 const NetworkState* network = ConnectedNetworkByType(type);
257 if (network) { 227 if (network) {
258 const DeviceState* device = GetDeviceState(network->device_path()); 228 const DeviceState* device = GetDeviceState(network->device_path());
259 if (device) 229 if (device)
260 result = device->mac_address(); 230 result = device->mac_address();
261 } 231 }
262 StringToUpperASCII(&result); 232 StringToUpperASCII(&result);
263 return result; 233 return result;
264 } 234 }
265 235
266 std::string NetworkStateHandler::FormattedHardwareAddressForType( 236 std::string NetworkStateHandler::FormattedHardwareAddressForType(
267 const std::string& type) const { 237 const NetworkTypePattern& type) const {
268 std::string address = HardwareAddressForType(type); 238 std::string address = HardwareAddressForType(type);
269 if (address.size() % 2 != 0) 239 if (address.size() % 2 != 0)
270 return address; 240 return address;
271 std::string result; 241 std::string result;
272 for (size_t i = 0; i < address.size(); ++i) { 242 for (size_t i = 0; i < address.size(); ++i) {
273 if ((i != 0) && (i % 2 == 0)) 243 if ((i != 0) && (i % 2 == 0))
274 result.push_back(':'); 244 result.push_back(':');
275 result.push_back(address[i]); 245 result.push_back(address[i]);
276 } 246 }
277 return result; 247 return result;
278 } 248 }
279 249
280 void NetworkStateHandler::GetNetworkList(NetworkStateList* list) const { 250 void NetworkStateHandler::GetNetworkList(NetworkStateList* list) const {
281 GetNetworkListByType(kMatchTypeDefault, list); 251 GetNetworkListByType(NetworkTypePattern::Default(), list);
282 } 252 }
283 253
284 void NetworkStateHandler::GetNetworkListByType(const std::string& type, 254 void NetworkStateHandler::GetNetworkListByType(const NetworkTypePattern& type,
285 NetworkStateList* list) const { 255 NetworkStateList* list) const {
286 DCHECK(list); 256 DCHECK(list);
287 list->clear(); 257 list->clear();
288 for (ManagedStateList::const_iterator iter = network_list_.begin(); 258 for (ManagedStateList::const_iterator iter = network_list_.begin();
289 iter != network_list_.end(); ++iter) { 259 iter != network_list_.end(); ++iter) {
290 if (!(*iter)->update_received()) 260 if (!(*iter)->update_received())
291 continue; 261 continue;
292 const NetworkState* network = (*iter)->AsNetworkState(); 262 const NetworkState* network = (*iter)->AsNetworkState();
293 DCHECK(network); 263 DCHECK(network);
294 if (ManagedStateMatchesType(network, type)) 264 if (network->Matches(type))
295 list->push_back(network); 265 list->push_back(network);
296 } 266 }
297 } 267 }
298 268
299 void NetworkStateHandler::GetDeviceList(DeviceStateList* list) const { 269 void NetworkStateHandler::GetDeviceList(DeviceStateList* list) const {
300 DCHECK(list); 270 DCHECK(list);
301 list->clear(); 271 list->clear();
302 for (ManagedStateList::const_iterator iter = device_list_.begin(); 272 for (ManagedStateList::const_iterator iter = device_list_.begin();
303 iter != device_list_.end(); ++iter) { 273 iter != device_list_.end(); ++iter) {
304 if (!(*iter)->update_received()) 274 if (!(*iter)->update_received())
(...skipping 29 matching lines...) Expand all
334 } 304 }
335 305
336 void NetworkStateHandler::RequestScan() const { 306 void NetworkStateHandler::RequestScan() const {
337 NET_LOG_USER("RequestScan", ""); 307 NET_LOG_USER("RequestScan", "");
338 shill_property_handler_->RequestScan(); 308 shill_property_handler_->RequestScan();
339 } 309 }
340 310
341 void NetworkStateHandler::WaitForScan(const std::string& type, 311 void NetworkStateHandler::WaitForScan(const std::string& type,
342 const base::Closure& callback) { 312 const base::Closure& callback) {
343 scan_complete_callbacks_[type].push_back(callback); 313 scan_complete_callbacks_[type].push_back(callback);
344 if (!GetScanningByType(type)) 314 if (!GetScanningByType(NetworkTypePattern::Primitive(type)))
345 RequestScan(); 315 RequestScan();
346 } 316 }
347 317
348 void NetworkStateHandler::ConnectToBestWifiNetwork() { 318 void NetworkStateHandler::ConnectToBestWifiNetwork() {
349 NET_LOG_USER("ConnectToBestWifiNetwork", ""); 319 NET_LOG_USER("ConnectToBestWifiNetwork", "");
350 WaitForScan(flimflam::kTypeWifi, 320 WaitForScan(flimflam::kTypeWifi,
351 base::Bind(&internal::ShillPropertyHandler::ConnectToBestServices, 321 base::Bind(&internal::ShillPropertyHandler::ConnectToBestServices,
352 shill_property_handler_->AsWeakPtr())); 322 shill_property_handler_->AsWeakPtr()));
353 } 323 }
354 324
(...skipping 370 matching lines...) Expand 10 before | Expand all | Expand 10 after
725 return; 695 return;
726 ScanCallbackList& callback_list = scan_complete_callbacks_[type]; 696 ScanCallbackList& callback_list = scan_complete_callbacks_[type];
727 for (ScanCallbackList::iterator iter = callback_list.begin(); 697 for (ScanCallbackList::iterator iter = callback_list.begin();
728 iter != callback_list.end(); ++iter) { 698 iter != callback_list.end(); ++iter) {
729 (*iter).Run(); 699 (*iter).Run();
730 } 700 }
731 scan_complete_callbacks_.erase(type); 701 scan_complete_callbacks_.erase(type);
732 } 702 }
733 703
734 std::string NetworkStateHandler::GetTechnologyForType( 704 std::string NetworkStateHandler::GetTechnologyForType(
735 const std::string& type) const { 705 const NetworkTypePattern& type) const {
736 if (type == kMatchTypeMobile) { 706 if (type.MatchesType(flimflam::kTypeEthernet))
737 if (shill_property_handler_->IsTechnologyAvailable(flimflam::kTypeWimax)) 707 return flimflam::kTypeEthernet;
708
709 if (type.MatchesType(flimflam::kTypeWifi))
710 return flimflam::kTypeWifi;
711
712 if (type.Equals(NetworkTypePattern::Wimax()))
738 return flimflam::kTypeWimax; 713 return flimflam::kTypeWimax;
739 else 714
715 // Prefer Wimax over Cellular only if it's available.
716 if (type.MatchesType(flimflam::kTypeWimax) &&
717 shill_property_handler_->IsTechnologyAvailable(flimflam::kTypeWimax)) {
718 return flimflam::kTypeWimax;
719 }
720
721 if (type.MatchesType(flimflam::kTypeCellular))
740 return flimflam::kTypeCellular; 722 return flimflam::kTypeCellular;
741 } 723
742 if (type == kMatchTypeDefault || type == kMatchTypeNonVirtual || 724 NOTREACHED();
743 type == kMatchTypeWireless) { 725 return std::string();
744 NOTREACHED();
745 return flimflam::kTypeWifi;
746 }
747 return type;
748 } 726 }
749 727
750 } // namespace chromeos 728 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698