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 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
282 const base::DictionaryValue& properties) { | 280 const base::DictionaryValue& properties) { |
283 ManagedState* managed = GetModifiableManagedState(GetManagedList(type), path); | 281 ManagedState* managed = GetModifiableManagedState(GetManagedList(type), path); |
284 if (!managed) { | 282 if (!managed) { |
285 LOG(ERROR) << "GetPropertiesCallback: " << path << " Not found!"; | 283 LOG(ERROR) << "GetPropertiesCallback: " << path << " Not found!"; |
286 return; | 284 return; |
287 } | 285 } |
288 bool network_property_changed = false; | 286 bool network_property_changed = false; |
289 for (base::DictionaryValue::Iterator iter(properties); | 287 for (base::DictionaryValue::Iterator iter(properties); |
290 iter.HasNext(); iter.Advance()) { | 288 iter.HasNext(); iter.Advance()) { |
291 if (type == ManagedState::MANAGED_TYPE_NETWORK) { | 289 if (type == ManagedState::MANAGED_TYPE_NETWORK) { |
292 if (ParseNetworkServiceProperty( | 290 if (ParseNetworkServiceProperty( |
pneubeck (no reviews)
2012/12/19 19:40:06
Why are you interested here in if something was su
stevenjb
2012/12/19 21:36:50
That is misleading. I renamed network_property_cha
| |
293 managed->AsNetworkState(), iter.key(), iter.value())) { | 291 managed->AsNetworkState(), iter.key(), iter.value())) { |
294 network_property_changed = true; | 292 network_property_changed = true; |
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 if (!ParseNetworkServiceProperty(network, key, value)) |
320 std::string detail = network->name() + "." + key; | 318 return; |
321 std::string vstr; | 319 std::string detail = network->name() + "." + key; |
322 if (value.GetAsString(&vstr)) | 320 std::string vstr; |
323 detail += " = " + vstr; | 321 if (value.GetAsString(&vstr)) |
324 network_event_log::AddEntry(kLogModule, "NetworkPropertyChanged", detail); | 322 detail += " = " + vstr; |
325 FOR_EACH_OBSERVER(NetworkStateHandlerObserver, observers_, | 323 network_event_log::AddEntry(kLogModule, "NetworkPropertyChanged", detail); |
326 NetworkServiceChanged(network)); | 324 FOR_EACH_OBSERVER(NetworkStateHandlerObserver, observers_, |
327 } | 325 NetworkPropertyChanged(network)); |
328 } | 326 } |
329 | 327 |
330 void NetworkStateHandler::UpdateNetworkServiceIPAddress( | 328 void NetworkStateHandler::UpdateNetworkServiceIPAddress( |
331 const std::string& service_path, | 329 const std::string& service_path, |
332 const std::string& ip_address) { | 330 const std::string& ip_address) { |
333 NetworkState* network = GetModifiableNetworkState(service_path); | 331 NetworkState* network = GetModifiableNetworkState(service_path); |
334 if (!network) | 332 if (!network) |
335 return; | 333 return; |
336 std::string detail = network->name() + ".IPAddress = " + ip_address; | 334 std::string detail = network->name() + ".IPAddress = " + ip_address; |
337 network_event_log::AddEntry(kLogModule, "NetworkIPChanged", detail); | 335 network_event_log::AddEntry(kLogModule, "NetworkIPChanged", detail); |
338 network->set_ip_address(ip_address); | 336 network->set_ip_address(ip_address); |
339 FOR_EACH_OBSERVER( | 337 FOR_EACH_OBSERVER( |
340 NetworkStateHandlerObserver, observers_, | 338 NetworkStateHandlerObserver, observers_, |
341 NetworkServiceChanged(network)); | 339 NetworkPropertyChanged(network)); |
342 } | 340 } |
343 | 341 |
344 void NetworkStateHandler::ManagerPropertyChanged() { | 342 void NetworkStateHandler::ManagerPropertyChanged() { |
345 FOR_EACH_OBSERVER(NetworkStateHandlerObserver, observers_, | 343 FOR_EACH_OBSERVER(NetworkStateHandlerObserver, observers_, |
346 NetworkManagerChanged()); | 344 NetworkManagerChanged()); |
347 } | 345 } |
348 | 346 |
349 void NetworkStateHandler::ManagedStateListChanged( | 347 void NetworkStateHandler::ManagedStateListChanged( |
350 ManagedState::ManagedType type) { | 348 ManagedState::ManagedType type) { |
351 if (type == ManagedState::MANAGED_TYPE_NETWORK) { | 349 if (type == ManagedState::MANAGED_TYPE_NETWORK) { |
352 // Notify observers that the list of networks has changed. | 350 // Notify observers that the list of networks has changed. |
353 NetworkStateList network_list; | 351 NetworkStateList network_list; |
354 GetNetworkList(&network_list); | 352 GetNetworkList(&network_list); |
355 network_event_log::AddEntry( | 353 network_event_log::AddEntry( |
356 kLogModule, "NetworkListChanged", | 354 kLogModule, "NetworkListChanged", |
357 StringPrintf("Size: %"PRIuS, network_list_.size())); | 355 StringPrintf("Size: %"PRIuS, network_list_.size())); |
358 FOR_EACH_OBSERVER(NetworkStateHandlerObserver, observers_, | 356 FOR_EACH_OBSERVER(NetworkStateHandlerObserver, observers_, |
359 NetworkListChanged(network_list)); | 357 NetworkListChanged(network_list)); |
360 // Update the active network and notify observers if it has changed. | 358 // The list order may have changed, so check if the default network changed. |
361 NetworkState* new_active_network = | 359 CheckDefaultNetworkChanged(); |
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) { | 360 } else if (type == ManagedState::MANAGED_TYPE_DEVICE) { |
374 network_event_log::AddEntry( | 361 network_event_log::AddEntry( |
375 kLogModule, "DeviceListChanged", | 362 kLogModule, "DeviceListChanged", |
376 StringPrintf("Size: %"PRIuS, device_list_.size())); | 363 StringPrintf("Size: %"PRIuS, device_list_.size())); |
377 FOR_EACH_OBSERVER(NetworkStateHandlerObserver, observers_, | 364 FOR_EACH_OBSERVER(NetworkStateHandlerObserver, observers_, |
378 DeviceListChanged()); | 365 DeviceListChanged()); |
379 } else { | 366 } else { |
380 NOTREACHED(); | 367 NOTREACHED(); |
381 } | 368 } |
382 } | 369 } |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
423 } | 410 } |
424 NOTREACHED(); | 411 NOTREACHED(); |
425 return NULL; | 412 return NULL; |
426 } | 413 } |
427 | 414 |
428 bool NetworkStateHandler::ParseNetworkServiceProperty( | 415 bool NetworkStateHandler::ParseNetworkServiceProperty( |
429 NetworkState* network, | 416 NetworkState* network, |
430 const std::string& key, | 417 const std::string& key, |
431 const base::Value& value) { | 418 const base::Value& value) { |
432 DCHECK(network); | 419 DCHECK(network); |
420 std::string prev_connection_state = network->connection_state(); | |
433 if (!network->PropertyChanged(key, value)) | 421 if (!network->PropertyChanged(key, value)) |
434 return false; | 422 return false; |
435 if (network->path() == active_network_path_ && | 423 if (key == flimflam::kStateProperty && |
436 key == flimflam::kStateProperty) { | 424 network->connection_state() != prev_connection_state) |
425 OnNetworkConnectionStateChanged(network); | |
pneubeck (no reviews)
2012/12/19 19:40:06
looks better. Now it's clearer which conditions ar
| |
426 return true; | |
427 } | |
428 | |
429 bool NetworkStateHandler::OnNetworkConnectionStateChanged( | |
430 NetworkState* network) { | |
431 std::string desc = StringPrintf( | |
432 "%s: %s", network->path().c_str(), network->connection_state().c_str()); | |
433 network_event_log::AddEntry( | |
434 kLogModule, "NetworkStateChanged", desc); | |
435 FOR_EACH_OBSERVER(NetworkStateHandlerObserver, observers_, | |
436 NetworkStateChanged(network)); | |
437 bool signaled_observers = CheckDefaultNetworkChanged(); | |
438 if (!signaled_observers && network->path() == default_network_path_) { | |
439 // This was already the default network; signal observers. | |
437 FOR_EACH_OBSERVER(NetworkStateHandlerObserver, observers_, | 440 FOR_EACH_OBSERVER(NetworkStateHandlerObserver, observers_, |
438 ActiveNetworkStateChanged(network)); | 441 DefaultNetworkChanged(network)); |
439 } | 442 } |
440 return true; | 443 return true; |
441 } | 444 } |
442 | 445 |
446 bool NetworkStateHandler::CheckDefaultNetworkChanged() { | |
447 std::string new_default_network_path; | |
448 const NetworkState* new_default_network = DefaultNetwork(); | |
449 if (new_default_network) | |
450 new_default_network_path = new_default_network->path(); | |
451 if (new_default_network_path == default_network_path_) | |
452 return false; | |
453 default_network_path_ = new_default_network_path; | |
454 network_event_log::AddEntry( | |
455 kLogModule, "DefaultNetworkChanged", new_default_network_path); | |
456 FOR_EACH_OBSERVER(NetworkStateHandlerObserver, observers_, | |
457 DefaultNetworkChanged(new_default_network)); | |
458 return true; | |
459 } | |
460 | |
443 } // namespace chromeos | 461 } // namespace chromeos |
OLD | NEW |