OLD | NEW |
---|---|
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/format_macros.h" | 7 #include "base/format_macros.h" |
8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
10 #include "base/stringprintf.h" | 10 #include "base/stringprintf.h" |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
101 return device->AsDeviceState(); | 101 return device->AsDeviceState(); |
102 } | 102 } |
103 return NULL; | 103 return NULL; |
104 } | 104 } |
105 | 105 |
106 const NetworkState* NetworkStateHandler::GetNetworkState( | 106 const NetworkState* NetworkStateHandler::GetNetworkState( |
107 const std::string& service_path) const { | 107 const std::string& service_path) const { |
108 return GetModifiableNetworkState(service_path); | 108 return GetModifiableNetworkState(service_path); |
109 } | 109 } |
110 | 110 |
111 const NetworkState* NetworkStateHandler::ActiveNetwork() const { | 111 const NetworkState* NetworkStateHandler::DefaultNetwork() const { |
112 if (network_list_.empty()) | 112 if (network_list_.empty()) |
113 return NULL; | 113 return NULL; |
114 const NetworkState* network = network_list_.front()->AsNetworkState(); | 114 const NetworkState* network = network_list_.front()->AsNetworkState(); |
115 DCHECK(network); | 115 DCHECK(network); |
116 if (!network->IsConnectedState()) | 116 if (!network->IsConnectedState()) |
117 return NULL; | 117 return NULL; |
118 return network; | 118 return network; |
119 } | 119 } |
120 | 120 |
121 const NetworkState* NetworkStateHandler::ConnectedNetworkByType( | 121 const NetworkState* NetworkStateHandler::ConnectedNetworkByType( |
122 const std::string& type) const { | 122 const std::string& type) const { |
123 for (ManagedStateList::const_iterator iter = network_list_.begin(); | 123 for (ManagedStateList::const_iterator iter = network_list_.begin(); |
124 iter != network_list_.end(); ++iter) { | 124 iter != network_list_.end(); ++iter) { |
125 const NetworkState* network = (*iter)->AsNetworkState(); | 125 const NetworkState* network = (*iter)->AsNetworkState(); |
126 DCHECK(network); | 126 DCHECK(network); |
127 if (!network->IsConnectedState()) | 127 if (!network->IsConnectedState()) |
128 break; // Connected networks are listed first. | 128 break; // Connected networks are listed first. |
129 if (network->type() == type) | 129 if (network->MatchesType(type)) |
130 return network; | 130 return network; |
131 } | 131 } |
132 return NULL; | 132 return NULL; |
133 } | 133 } |
134 | 134 |
135 const NetworkState* NetworkStateHandler::ConnectingNetworkByType( | 135 const NetworkState* NetworkStateHandler::ConnectingNetworkByType( |
136 const std::string& type) const { | 136 const std::string& type) const { |
137 for (ManagedStateList::const_iterator iter = network_list_.begin(); | 137 for (ManagedStateList::const_iterator iter = network_list_.begin(); |
138 iter != network_list_.end(); ++iter) { | 138 iter != network_list_.end(); ++iter) { |
139 const NetworkState* network = (*iter)->AsNetworkState(); | 139 const NetworkState* network = (*iter)->AsNetworkState(); |
140 DCHECK(network); | 140 DCHECK(network); |
141 if (network->IsConnectedState()) | 141 if (network->IsConnectedState()) |
142 continue; | 142 continue; |
143 if (!network->IsConnectingState()) | 143 if (!network->IsConnectingState()) |
144 break; // Connected and connecting networks are listed first. | 144 break; // Connected and connecting networks are listed first. |
145 if (network->type() == type || | 145 if (network->MatchesType(type)) |
146 (type.empty() && type != flimflam::kTypeEthernet)) { | |
147 return network; | 146 return network; |
148 } | |
149 } | 147 } |
150 return NULL; | 148 return NULL; |
151 } | 149 } |
152 | 150 |
153 std::string NetworkStateHandler::HardwareAddressForType( | 151 std::string NetworkStateHandler::HardwareAddressForType( |
154 const std::string& type) const { | 152 const std::string& type) const { |
155 std::string result; | 153 std::string result; |
156 const NetworkState* network = ConnectedNetworkByType(type); | 154 const NetworkState* network = ConnectedNetworkByType(type); |
157 if (network) { | 155 if (network) { |
158 const DeviceState* device = GetDeviceState(network->device_path()); | 156 const DeviceState* device = GetDeviceState(network->device_path()); |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
295 } | 293 } |
296 } else { | 294 } else { |
297 managed->PropertyChanged(iter.key(), iter.value()); | 295 managed->PropertyChanged(iter.key(), iter.value()); |
298 } | 296 } |
299 } | 297 } |
300 // Notify observers. | 298 // Notify observers. |
301 if (network_property_changed) { | 299 if (network_property_changed) { |
302 NetworkState* network = managed->AsNetworkState(); | 300 NetworkState* network = managed->AsNetworkState(); |
303 DCHECK(network); | 301 DCHECK(network); |
304 FOR_EACH_OBSERVER(NetworkStateHandlerObserver, observers_, | 302 FOR_EACH_OBSERVER(NetworkStateHandlerObserver, observers_, |
305 NetworkServiceChanged(network)); | 303 NetworkPropertyChanged(network)); |
306 } | 304 } |
307 network_event_log::AddEntry( | 305 network_event_log::AddEntry( |
308 kLogModule, "PropertiesReceived", | 306 kLogModule, "PropertiesReceived", |
309 StringPrintf("%s (%s)", path.c_str(), managed->name().c_str())); | 307 StringPrintf("%s (%s)", path.c_str(), managed->name().c_str())); |
310 } | 308 } |
311 | 309 |
312 void NetworkStateHandler::UpdateNetworkServiceProperty( | 310 void NetworkStateHandler::UpdateNetworkServiceProperty( |
313 const std::string& service_path, | 311 const std::string& service_path, |
314 const std::string& key, | 312 const std::string& key, |
315 const base::Value& value) { | 313 const base::Value& value) { |
316 NetworkState* network = GetModifiableNetworkState(service_path); | 314 NetworkState* network = GetModifiableNetworkState(service_path); |
317 if (!network) | 315 if (!network) |
318 return; | 316 return; |
319 if (ParseNetworkServiceProperty(network, key, value)) { | 317 std::string prev_state = network->state(); |
320 std::string detail = network->name() + "." + key; | 318 if (!ParseNetworkServiceProperty(network, key, value)) |
321 std::string vstr; | 319 return; |
322 if (value.GetAsString(&vstr)) | 320 if (key == flimflam::kStateProperty && network->state() == prev_state) |
pneubeck (no reviews)
2012/12/19 15:22:38
Why is this required? The return value of ParseNet
stevenjb
2012/12/19 18:01:07
This logic is now in ParseNetworkServiceProperty,
pneubeck (no reviews)
2012/12/19 19:40:06
Yeah, I was confused by the line
if (!network->P
| |
323 detail += " = " + vstr; | 321 return; // Ignore noop state updates |
324 network_event_log::AddEntry(kLogModule, "NetworkPropertyChanged", detail); | 322 std::string detail = network->name() + "." + key; |
325 FOR_EACH_OBSERVER(NetworkStateHandlerObserver, observers_, | 323 std::string vstr; |
326 NetworkServiceChanged(network)); | 324 if (value.GetAsString(&vstr)) |
327 } | 325 detail += " = " + vstr; |
326 network_event_log::AddEntry(kLogModule, "NetworkPropertyChanged", detail); | |
327 FOR_EACH_OBSERVER(NetworkStateHandlerObserver, observers_, | |
328 NetworkPropertyChanged(network)); | |
328 } | 329 } |
329 | 330 |
330 void NetworkStateHandler::UpdateNetworkServiceIPAddress( | 331 void NetworkStateHandler::UpdateNetworkServiceIPAddress( |
331 const std::string& service_path, | 332 const std::string& service_path, |
332 const std::string& ip_address) { | 333 const std::string& ip_address) { |
333 NetworkState* network = GetModifiableNetworkState(service_path); | 334 NetworkState* network = GetModifiableNetworkState(service_path); |
334 if (!network) | 335 if (!network) |
335 return; | 336 return; |
336 std::string detail = network->name() + ".IPAddress = " + ip_address; | 337 std::string detail = network->name() + ".IPAddress = " + ip_address; |
337 network_event_log::AddEntry(kLogModule, "NetworkIPChanged", detail); | 338 network_event_log::AddEntry(kLogModule, "NetworkIPChanged", detail); |
338 network->set_ip_address(ip_address); | 339 network->set_ip_address(ip_address); |
339 FOR_EACH_OBSERVER( | 340 FOR_EACH_OBSERVER( |
340 NetworkStateHandlerObserver, observers_, | 341 NetworkStateHandlerObserver, observers_, |
341 NetworkServiceChanged(network)); | 342 NetworkPropertyChanged(network)); |
342 } | 343 } |
343 | 344 |
344 void NetworkStateHandler::ManagerPropertyChanged() { | 345 void NetworkStateHandler::ManagerPropertyChanged() { |
345 FOR_EACH_OBSERVER(NetworkStateHandlerObserver, observers_, | 346 FOR_EACH_OBSERVER(NetworkStateHandlerObserver, observers_, |
346 NetworkManagerChanged()); | 347 NetworkManagerChanged()); |
347 } | 348 } |
348 | 349 |
349 void NetworkStateHandler::ManagedStateListChanged( | 350 void NetworkStateHandler::ManagedStateListChanged( |
350 ManagedState::ManagedType type) { | 351 ManagedState::ManagedType type) { |
351 if (type == ManagedState::MANAGED_TYPE_NETWORK) { | 352 if (type == ManagedState::MANAGED_TYPE_NETWORK) { |
352 // Notify observers that the list of networks has changed. | 353 // Notify observers that the list of networks has changed. |
353 NetworkStateList network_list; | 354 NetworkStateList network_list; |
354 GetNetworkList(&network_list); | 355 GetNetworkList(&network_list); |
355 network_event_log::AddEntry( | 356 network_event_log::AddEntry( |
356 kLogModule, "NetworkListChanged", | 357 kLogModule, "NetworkListChanged", |
357 StringPrintf("Size: %"PRIuS, network_list_.size())); | 358 StringPrintf("Size: %"PRIuS, network_list_.size())); |
358 FOR_EACH_OBSERVER(NetworkStateHandlerObserver, observers_, | 359 FOR_EACH_OBSERVER(NetworkStateHandlerObserver, observers_, |
359 NetworkListChanged(network_list)); | 360 NetworkListChanged(network_list)); |
360 // Update the active network and notify observers if it has changed. | 361 CheckDefaultNetworkChanged(); |
361 NetworkState* new_active_network = | |
362 network_list_.empty() ? NULL : network_list_.front()->AsNetworkState(); | |
363 std::string new_active_network_path; | |
364 if (new_active_network) | |
365 new_active_network_path = new_active_network->path(); | |
366 if (new_active_network_path != active_network_path_) { | |
367 network_event_log::AddEntry( | |
368 kLogModule, "ActiveNetworkChanged", new_active_network_path); | |
369 active_network_path_ = new_active_network_path; | |
370 FOR_EACH_OBSERVER(NetworkStateHandlerObserver, observers_, | |
371 ActiveNetworkChanged(new_active_network)); | |
372 } | |
373 } else if (type == ManagedState::MANAGED_TYPE_DEVICE) { | 362 } else if (type == ManagedState::MANAGED_TYPE_DEVICE) { |
374 network_event_log::AddEntry( | 363 network_event_log::AddEntry( |
375 kLogModule, "DeviceListChanged", | 364 kLogModule, "DeviceListChanged", |
376 StringPrintf("Size: %"PRIuS, device_list_.size())); | 365 StringPrintf("Size: %"PRIuS, device_list_.size())); |
377 FOR_EACH_OBSERVER(NetworkStateHandlerObserver, observers_, | 366 FOR_EACH_OBSERVER(NetworkStateHandlerObserver, observers_, |
378 DeviceListChanged()); | 367 DeviceListChanged()); |
379 } else { | 368 } else { |
380 NOTREACHED(); | 369 NOTREACHED(); |
381 } | 370 } |
382 } | 371 } |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
425 return NULL; | 414 return NULL; |
426 } | 415 } |
427 | 416 |
428 bool NetworkStateHandler::ParseNetworkServiceProperty( | 417 bool NetworkStateHandler::ParseNetworkServiceProperty( |
429 NetworkState* network, | 418 NetworkState* network, |
430 const std::string& key, | 419 const std::string& key, |
431 const base::Value& value) { | 420 const base::Value& value) { |
432 DCHECK(network); | 421 DCHECK(network); |
433 if (!network->PropertyChanged(key, value)) | 422 if (!network->PropertyChanged(key, value)) |
434 return false; | 423 return false; |
435 if (network->path() == active_network_path_ && | 424 // Signal observers when the State changes. |
436 key == flimflam::kStateProperty) { | 425 if (key == flimflam::kStateProperty) { |
426 std::string desc = StringPrintf( | |
427 "%s: %s", network->path().c_str(), network->state().c_str()); | |
428 network_event_log::AddEntry( | |
429 kLogModule, "NetworkStateChanged", desc); | |
437 FOR_EACH_OBSERVER(NetworkStateHandlerObserver, observers_, | 430 FOR_EACH_OBSERVER(NetworkStateHandlerObserver, observers_, |
438 ActiveNetworkStateChanged(network)); | 431 NetworkStateChanged(network)); |
432 bool signaled_observers = CheckDefaultNetworkChanged(); | |
433 if (!signaled_observers && network->path() == default_network_path_) { | |
434 // This was already the default network; signal observers. | |
435 FOR_EACH_OBSERVER(NetworkStateHandlerObserver, observers_, | |
436 DefaultNetworkChanged(network)); | |
437 } | |
439 } | 438 } |
440 return true; | 439 return true; |
441 } | 440 } |
442 | 441 |
442 bool NetworkStateHandler::CheckDefaultNetworkChanged() { | |
443 std::string new_default_network_path; | |
444 const NetworkState* new_default_network = DefaultNetwork(); | |
445 if (new_default_network) | |
446 new_default_network_path = new_default_network->path(); | |
447 if (new_default_network_path == default_network_path_) | |
448 return false; | |
449 default_network_path_ = new_default_network_path; | |
450 network_event_log::AddEntry( | |
451 kLogModule, "DefaultNetworkChanged", new_default_network_path); | |
452 FOR_EACH_OBSERVER(NetworkStateHandlerObserver, observers_, | |
453 DefaultNetworkChanged(new_default_network)); | |
454 return true; | |
455 } | |
456 | |
443 } // namespace chromeos | 457 } // namespace chromeos |
OLD | NEW |