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 <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
11 #include "base/format_macros.h" | 11 #include "base/format_macros.h" |
12 #include "base/guid.h" | 12 #include "base/guid.h" |
13 #include "base/json/json_string_value_serializer.h" | 13 #include "base/json/json_string_value_serializer.h" |
14 #include "base/json/json_writer.h" | 14 #include "base/json/json_writer.h" |
15 #include "base/location.h" | 15 #include "base/location.h" |
16 #include "base/logging.h" | |
17 #include "base/memory/ptr_util.h" | 16 #include "base/memory/ptr_util.h" |
18 #include "base/metrics/histogram_macros.h" | 17 #include "base/metrics/histogram_macros.h" |
19 #include "base/strings/string_number_conversions.h" | 18 #include "base/strings/string_number_conversions.h" |
20 #include "base/strings/string_util.h" | 19 #include "base/strings/string_util.h" |
21 #include "base/strings/stringprintf.h" | 20 #include "base/strings/stringprintf.h" |
22 #include "base/values.h" | 21 #include "base/values.h" |
23 #include "chromeos/chromeos_switches.h" | 22 #include "chromeos/chromeos_switches.h" |
24 #include "chromeos/network/device_state.h" | 23 #include "chromeos/network/device_state.h" |
25 #include "chromeos/network/network_event_log.h" | 24 #include "chromeos/network/network_event_log.h" |
26 #include "chromeos/network/network_state.h" | 25 #include "chromeos/network/network_state.h" |
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
216 } | 215 } |
217 | 216 |
218 const NetworkState* NetworkStateHandler::DefaultNetwork() const { | 217 const NetworkState* NetworkStateHandler::DefaultNetwork() const { |
219 if (default_network_path_.empty()) | 218 if (default_network_path_.empty()) |
220 return nullptr; | 219 return nullptr; |
221 return GetNetworkState(default_network_path_); | 220 return GetNetworkState(default_network_path_); |
222 } | 221 } |
223 | 222 |
224 const NetworkState* NetworkStateHandler::ConnectedNetworkByType( | 223 const NetworkState* NetworkStateHandler::ConnectedNetworkByType( |
225 const NetworkTypePattern& type) const { | 224 const NetworkTypePattern& type) const { |
225 // If a tether network is connected, return that network before checking other | |
226 // network types. | |
227 if (type.MatchesPattern(NetworkTypePattern::Tether())) { | |
228 for (auto iter = tether_network_list_.begin(); | |
229 iter != tether_network_list_.end(); ++iter) { | |
230 const NetworkState* network = (*iter)->AsNetworkState(); | |
231 DCHECK(network); | |
232 if (network->IsConnectedState()) | |
233 return network; | |
234 } | |
235 } | |
236 | |
226 // Active networks are always listed first by Shill so no need to sort. | 237 // Active networks are always listed first by Shill so no need to sort. |
227 for (auto iter = network_list_.begin(); iter != network_list_.end(); ++iter) { | 238 for (auto iter = network_list_.begin(); iter != network_list_.end(); ++iter) { |
228 const NetworkState* network = (*iter)->AsNetworkState(); | 239 const NetworkState* network = (*iter)->AsNetworkState(); |
229 DCHECK(network); | 240 DCHECK(network); |
230 if (!network->update_received()) | 241 if (!network->update_received()) |
231 continue; | 242 continue; |
232 if (!network->IsConnectedState()) | 243 if (!network->IsConnectedState()) |
233 break; // Connected networks are listed first. | 244 break; // Connected networks are listed first. |
234 if (network->Matches(type)) | 245 if (network->Matches(type)) |
235 return network; | 246 return network; |
236 } | 247 } |
237 return nullptr; | 248 return nullptr; |
238 } | 249 } |
239 | 250 |
240 const NetworkState* NetworkStateHandler::ConnectingNetworkByType( | 251 const NetworkState* NetworkStateHandler::ConnectingNetworkByType( |
241 const NetworkTypePattern& type) const { | 252 const NetworkTypePattern& type) const { |
253 // If a tether network is connecting, return that network before checking | |
254 // other network types. | |
255 if (type.MatchesPattern(NetworkTypePattern::Tether())) { | |
256 for (auto iter = tether_network_list_.begin(); | |
257 iter != tether_network_list_.end(); ++iter) { | |
258 const NetworkState* network = (*iter)->AsNetworkState(); | |
259 DCHECK(network); | |
260 if (network->IsConnectingState()) | |
261 return network; | |
262 } | |
263 } | |
264 | |
242 // Active networks are always listed first by Shill so no need to sort. | 265 // Active networks are always listed first by Shill so no need to sort. |
243 for (auto iter = network_list_.begin(); iter != network_list_.end(); ++iter) { | 266 for (auto iter = network_list_.begin(); iter != network_list_.end(); ++iter) { |
244 const NetworkState* network = (*iter)->AsNetworkState(); | 267 const NetworkState* network = (*iter)->AsNetworkState(); |
245 DCHECK(network); | 268 DCHECK(network); |
246 if (!network->update_received() || network->IsConnectedState()) | 269 if (!network->update_received() || network->IsConnectedState()) |
247 continue; | 270 continue; |
248 if (!network->IsConnectingState()) | 271 if (!network->IsConnectingState()) |
249 break; // Connected and connecting networks are listed first. | 272 break; // Connected and connecting networks are listed first. |
250 if (network->Matches(type)) | 273 if (network->Matches(type)) |
251 return network; | 274 return network; |
252 } | 275 } |
253 return nullptr; | 276 return nullptr; |
254 } | 277 } |
255 | 278 |
256 const NetworkState* NetworkStateHandler::FirstNetworkByType( | 279 const NetworkState* NetworkStateHandler::FirstNetworkByType( |
257 const NetworkTypePattern& type) { | 280 const NetworkTypePattern& type) { |
258 if (!network_list_sorted_) | 281 if (!network_list_sorted_) |
259 SortNetworkList(); // Sort to ensure visible networks are listed first. | 282 SortNetworkList(); // Sort to ensure visible networks are listed first. |
283 | |
284 // If |type| matches tether networks and at least one tether network is | |
285 // present, return the first network (since it has been sorted already). | |
286 if (type.MatchesPattern(NetworkTypePattern::Tether()) && | |
287 !tether_network_list_.empty()) { | |
288 return tether_network_list_[0]->AsNetworkState(); | |
289 } | |
290 | |
260 for (auto iter = network_list_.begin(); iter != network_list_.end(); ++iter) { | 291 for (auto iter = network_list_.begin(); iter != network_list_.end(); ++iter) { |
261 const NetworkState* network = (*iter)->AsNetworkState(); | 292 const NetworkState* network = (*iter)->AsNetworkState(); |
262 DCHECK(network); | 293 DCHECK(network); |
263 if (!network->update_received()) | 294 if (!network->update_received()) |
264 continue; | 295 continue; |
265 if (!network->visible()) | 296 if (!network->visible()) |
266 break; | 297 break; |
267 if (network->Matches(type)) | 298 if (network->Matches(type)) |
268 return network; | 299 return network; |
269 } | 300 } |
(...skipping 21 matching lines...) Expand all Loading... | |
291 GetVisibleNetworkListByType(NetworkTypePattern::Default(), list); | 322 GetVisibleNetworkListByType(NetworkTypePattern::Default(), list); |
292 } | 323 } |
293 | 324 |
294 void NetworkStateHandler::GetNetworkListByType(const NetworkTypePattern& type, | 325 void NetworkStateHandler::GetNetworkListByType(const NetworkTypePattern& type, |
295 bool configured_only, | 326 bool configured_only, |
296 bool visible_only, | 327 bool visible_only, |
297 int limit, | 328 int limit, |
298 NetworkStateList* list) { | 329 NetworkStateList* list) { |
299 DCHECK(list); | 330 DCHECK(list); |
300 list->clear(); | 331 list->clear(); |
301 int count = 0; | 332 |
302 // Sort the network list if necessary. | 333 // Sort the network list if necessary. |
303 if (!network_list_sorted_) | 334 if (!network_list_sorted_) |
304 SortNetworkList(); | 335 SortNetworkList(); |
336 | |
337 if (type.MatchesPattern(NetworkTypePattern::Tether())) { | |
338 GetTetherNetworkList(limit, list); | |
339 } | |
340 | |
341 int count = list->size(); | |
342 | |
343 if (type.Equals(NetworkTypePattern::Tether()) || | |
344 (limit != 0 && count >= limit)) { | |
345 // If only searching for tether networks, there is no need to continue | |
346 // searching through other network types; likewise, if the limit has already | |
347 // been reached, there is no need to continue searching. | |
348 return; | |
349 } | |
350 | |
305 for (auto iter = network_list_.begin(); iter != network_list_.end(); ++iter) { | 351 for (auto iter = network_list_.begin(); iter != network_list_.end(); ++iter) { |
306 const NetworkState* network = (*iter)->AsNetworkState(); | 352 const NetworkState* network = (*iter)->AsNetworkState(); |
307 DCHECK(network); | 353 DCHECK(network); |
308 if (!network->update_received() || !network->Matches(type)) | 354 if (!network->update_received() || !network->Matches(type)) |
309 continue; | 355 continue; |
310 if (configured_only && !network->IsInProfile()) | 356 if (configured_only && !network->IsInProfile()) |
311 continue; | 357 continue; |
312 if (visible_only && !network->visible()) | 358 if (visible_only && !network->visible()) |
313 continue; | 359 continue; |
314 list->push_back(network); | 360 list->push_back(network); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
347 return network; | 393 return network; |
348 } | 394 } |
349 | 395 |
350 const NetworkState* NetworkStateHandler::GetNetworkStateFromGuid( | 396 const NetworkState* NetworkStateHandler::GetNetworkStateFromGuid( |
351 const std::string& guid) const { | 397 const std::string& guid) const { |
352 DCHECK(!guid.empty()); | 398 DCHECK(!guid.empty()); |
353 return GetModifiableNetworkStateFromGuid(guid); | 399 return GetModifiableNetworkStateFromGuid(guid); |
354 } | 400 } |
355 | 401 |
356 void NetworkStateHandler::AddTetherNetworkState(const std::string& guid, | 402 void NetworkStateHandler::AddTetherNetworkState(const std::string& guid, |
357 const std::string& name) { | 403 const std::string& name, |
404 const std::string& carrier, | |
405 int battery_percentage, | |
406 int signal_strength) { | |
358 DCHECK(!guid.empty()); | 407 DCHECK(!guid.empty()); |
408 DCHECK(battery_percentage >= 0 && battery_percentage <= 100); | |
409 DCHECK(signal_strength >= 0 && signal_strength <= 100); | |
359 | 410 |
360 // If the network already exists, do nothing. | 411 // If the network already exists, do nothing. |
361 if (GetNetworkStateFromGuid(guid)) { | 412 if (GetNetworkStateFromGuid(guid)) { |
362 NET_LOG(ERROR) << "AddTetherNetworkState: " << name | 413 NET_LOG(ERROR) << "AddTetherNetworkState: " << name |
363 << " called with existing guid:" << guid; | 414 << " called with existing guid:" << guid; |
364 return; | 415 return; |
365 } | 416 } |
366 | 417 |
418 // Use the GUID as the network's service path. | |
367 std::unique_ptr<NetworkState> tether_network_state = | 419 std::unique_ptr<NetworkState> tether_network_state = |
368 base::MakeUnique<NetworkState>(guid); | 420 base::MakeUnique<NetworkState>(guid /* path */); |
369 | 421 |
422 // Tether networks are always connectable | |
423 tether_network_state->connectable_ = true; | |
370 tether_network_state->set_name(name); | 424 tether_network_state->set_name(name); |
371 tether_network_state->set_type(kTypeTether); | 425 tether_network_state->set_type(kTypeTether); |
372 tether_network_state->SetGuid(guid); | 426 tether_network_state->SetGuid(guid); |
373 tether_network_state->set_visible(true); | 427 tether_network_state->set_visible(true); |
374 tether_network_state->set_update_received(); | 428 tether_network_state->set_update_received(); |
429 tether_network_state->SetCarrier(carrier); | |
430 tether_network_state->SetBatteryPercentage(battery_percentage); | |
431 tether_network_state->SetTetherSignalStrength(signal_strength); | |
375 | 432 |
376 tether_network_list_.push_back(std::move(tether_network_state)); | 433 tether_network_list_.push_back(std::move(tether_network_state)); |
377 NotifyNetworkListChanged(); | 434 NotifyNetworkListChanged(); |
378 } | 435 } |
379 | 436 |
437 bool NetworkStateHandler::UpdateTetherNetworkProperties( | |
438 const std::string& guid, | |
439 const std::string& carrier, | |
440 int battery_percentage, | |
441 int signal_strength) { | |
442 NetworkState* tether_network_state = GetModifiableNetworkStateFromGuid(guid); | |
443 if (!tether_network_state) | |
444 return false; | |
445 | |
446 tether_network_state->SetCarrier(carrier); | |
447 tether_network_state->SetBatteryPercentage(battery_percentage); | |
448 tether_network_state->SetTetherSignalStrength(signal_strength); | |
449 | |
450 NotifyNetworkListChanged(); | |
451 return true; | |
452 } | |
453 | |
380 void NetworkStateHandler::RemoveTetherNetworkState(const std::string& guid) { | 454 void NetworkStateHandler::RemoveTetherNetworkState(const std::string& guid) { |
381 for (auto iter = tether_network_list_.begin(); | 455 for (auto iter = tether_network_list_.begin(); |
382 iter != tether_network_list_.end(); ++iter) { | 456 iter != tether_network_list_.end(); ++iter) { |
383 if (iter->get()->AsNetworkState()->guid() == guid) { | 457 if (iter->get()->AsNetworkState()->guid() == guid) { |
384 NetworkState* wifi_network = GetModifiableNetworkStateFromGuid( | 458 NetworkState* wifi_network = GetModifiableNetworkStateFromGuid( |
385 iter->get()->AsNetworkState()->tether_guid()); | 459 iter->get()->AsNetworkState()->tether_guid()); |
386 if (wifi_network) | 460 if (wifi_network) |
387 wifi_network->set_tether_guid(std::string()); | 461 wifi_network->set_tether_guid(std::string()); |
388 | 462 |
389 tether_network_list_.erase(iter); | 463 tether_network_list_.erase(iter); |
(...skipping 30 matching lines...) Expand all Loading... | |
420 } | 494 } |
421 | 495 |
422 tether_network->set_tether_guid(wifi_network_guid); | 496 tether_network->set_tether_guid(wifi_network_guid); |
423 wifi_network->set_tether_guid(tether_network_guid); | 497 wifi_network->set_tether_guid(tether_network_guid); |
424 NotifyNetworkListChanged(); | 498 NotifyNetworkListChanged(); |
425 return true; | 499 return true; |
426 } | 500 } |
427 | 501 |
428 void NetworkStateHandler::SetTetherNetworkStateDisconnected( | 502 void NetworkStateHandler::SetTetherNetworkStateDisconnected( |
429 const std::string& guid) { | 503 const std::string& guid) { |
504 // TODO(khorimoto): Change default network? Notify of connection change? | |
Kyle Horimoto
2017/04/18 01:16:06
stevenjb@: Can you comment on what the correct thi
| |
430 SetTetherNetworkStateConnectionState(guid, shill::kStateDisconnect); | 505 SetTetherNetworkStateConnectionState(guid, shill::kStateDisconnect); |
431 } | 506 } |
432 | 507 |
433 void NetworkStateHandler::SetTetherNetworkStateConnecting( | 508 void NetworkStateHandler::SetTetherNetworkStateConnecting( |
434 const std::string& guid) { | 509 const std::string& guid) { |
510 // TODO(khorimoto): Change default network? Notify of connection change? | |
435 SetTetherNetworkStateConnectionState(guid, shill::kStateConfiguration); | 511 SetTetherNetworkStateConnectionState(guid, shill::kStateConfiguration); |
436 } | 512 } |
437 | 513 |
438 void NetworkStateHandler::SetTetherNetworkStateConnected( | 514 void NetworkStateHandler::SetTetherNetworkStateConnected( |
439 const std::string& guid) { | 515 const std::string& guid) { |
516 // Being connected implies that AssociateTetherNetworkStateWithWifiNetwork() | |
517 // was already called, so ensure that the association is still intact. | |
518 DCHECK(GetNetworkStateFromGuid(GetNetworkStateFromGuid(guid)->tether_guid()) | |
519 ->tether_guid() == guid); | |
520 | |
521 // TODO(khorimoto): Change default network? Notify of connection change? | |
440 SetTetherNetworkStateConnectionState(guid, shill::kStateOnline); | 522 SetTetherNetworkStateConnectionState(guid, shill::kStateOnline); |
441 } | 523 } |
442 | 524 |
443 void NetworkStateHandler::SetTetherNetworkStateConnectionState( | 525 void NetworkStateHandler::SetTetherNetworkStateConnectionState( |
444 const std::string& guid, | 526 const std::string& guid, |
445 const std::string& connection_state) { | 527 const std::string& connection_state) { |
446 NetworkState* tether_network = GetModifiableNetworkStateFromGuid(guid); | 528 NetworkState* tether_network = GetModifiableNetworkStateFromGuid(guid); |
447 if (!tether_network) { | 529 if (!tether_network) { |
448 NET_LOG(ERROR) << "SetTetherNetworkStateConnectionState: Tether network " | 530 NET_LOG(ERROR) << "SetTetherNetworkStateConnectionState: Tether network " |
449 << "not found: " << guid; | 531 << "not found: " << guid; |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
568 service_path.c_str())); | 650 service_path.c_str())); |
569 return nullptr; | 651 return nullptr; |
570 } | 652 } |
571 return list.front(); | 653 return list.front(); |
572 } | 654 } |
573 | 655 |
574 void NetworkStateHandler::SetLastErrorForTest(const std::string& service_path, | 656 void NetworkStateHandler::SetLastErrorForTest(const std::string& service_path, |
575 const std::string& error) { | 657 const std::string& error) { |
576 NetworkState* network_state = GetModifiableNetworkState(service_path); | 658 NetworkState* network_state = GetModifiableNetworkState(service_path); |
577 if (!network_state) { | 659 if (!network_state) { |
578 LOG(ERROR) << "No matching NetworkState for: " << service_path; | 660 NET_LOG(ERROR) << "No matching NetworkState for: " << service_path; |
579 return; | 661 return; |
580 } | 662 } |
581 network_state->last_error_ = error; | 663 network_state->last_error_ = error; |
582 } | 664 } |
583 | 665 |
584 //------------------------------------------------------------------------------ | 666 //------------------------------------------------------------------------------ |
585 // ShillPropertyHandler::Delegate overrides | 667 // ShillPropertyHandler::Delegate overrides |
586 | 668 |
587 void NetworkStateHandler::UpdateManagedList(ManagedState::ManagedType type, | 669 void NetworkStateHandler::UpdateManagedList(ManagedState::ManagedType type, |
588 const base::ListValue& entries) { | 670 const base::ListValue& entries) { |
(...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
860 devices += ", "; | 942 devices += ", "; |
861 devices += (*iter)->name(); | 943 devices += (*iter)->name(); |
862 } | 944 } |
863 NET_LOG_EVENT("DeviceList", devices); | 945 NET_LOG_EVENT("DeviceList", devices); |
864 NotifyDeviceListChanged(); | 946 NotifyDeviceListChanged(); |
865 } else { | 947 } else { |
866 NOTREACHED(); | 948 NOTREACHED(); |
867 } | 949 } |
868 } | 950 } |
869 | 951 |
952 // TODO(khorimoto): Add sorting for the tether network list as well. | |
870 void NetworkStateHandler::SortNetworkList() { | 953 void NetworkStateHandler::SortNetworkList() { |
871 // Note: usually active networks will precede inactive networks, however | 954 // Note: usually active networks will precede inactive networks, however |
872 // this may briefly be untrue during state transitions (e.g. a network may | 955 // this may briefly be untrue during state transitions (e.g. a network may |
873 // transition to idle before the list is updated). | 956 // transition to idle before the list is updated). |
874 ManagedStateList active, non_wifi_visible, wifi_visible, hidden, new_networks; | 957 ManagedStateList active, non_wifi_visible, wifi_visible, hidden, new_networks; |
875 for (ManagedStateList::iterator iter = network_list_.begin(); | 958 for (ManagedStateList::iterator iter = network_list_.begin(); |
876 iter != network_list_.end(); ++iter) { | 959 iter != network_list_.end(); ++iter) { |
877 NetworkState* network = (*iter)->AsNetworkState(); | 960 NetworkState* network = (*iter)->AsNetworkState(); |
878 if (!network->update_received()) { | 961 if (!network->update_received()) { |
879 new_networks.push_back(std::move(*iter)); | 962 new_networks.push_back(std::move(*iter)); |
(...skipping 289 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1169 if (type.MatchesType(shill::kTypeBluetooth)) | 1252 if (type.MatchesType(shill::kTypeBluetooth)) |
1170 technologies.emplace_back(shill::kTypeBluetooth); | 1253 technologies.emplace_back(shill::kTypeBluetooth); |
1171 if (type.MatchesType(shill::kTypeVPN)) | 1254 if (type.MatchesType(shill::kTypeVPN)) |
1172 technologies.emplace_back(shill::kTypeVPN); | 1255 technologies.emplace_back(shill::kTypeVPN); |
1173 | 1256 |
1174 CHECK_GT(technologies.size(), 0ul); | 1257 CHECK_GT(technologies.size(), 0ul); |
1175 return technologies; | 1258 return technologies; |
1176 } | 1259 } |
1177 | 1260 |
1178 } // namespace chromeos | 1261 } // namespace chromeos |
OLD | NEW |