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

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

Issue 2819383002: [CrOS Tether] Update NetworkState to include tether properties and integrate into NetworkStateHandl… (Closed)
Patch Set: stevenjb@ and hansberry@ comments. Created 3 years, 8 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
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 <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
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 }
stevenjb 2017/04/19 21:16:02 Hmm. I realize it's kind of an edge case, but I do
Kyle Horimoto 2017/04/19 22:31:05 Done.
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
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);
stevenjb 2017/04/19 21:16:02 Here we can just push_front if Type == Ethernet. (
Kyle Horimoto 2017/04/19 22:31:04 Done.
315 if (limit > 0 && ++count >= limit) 361 if (limit > 0 && ++count >= limit)
316 break; 362 break;
317 } 363 }
318 } 364 }
319 365
320 void NetworkStateHandler::GetTetherNetworkList(int limit, 366 void NetworkStateHandler::GetTetherNetworkList(int limit,
321 NetworkStateList* list) { 367 NetworkStateList* list) {
322 DCHECK(list); 368 DCHECK(list);
323 list->clear(); 369 list->clear();
324 int count = 0; 370 int count = 0;
(...skipping 22 matching lines...) Expand all
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
370 tether_network_state->set_name(name); 423 tether_network_state->set_name(name);
371 tether_network_state->set_type(kTypeTether); 424 tether_network_state->set_type(kTypeTether);
372 tether_network_state->SetGuid(guid); 425 tether_network_state->SetGuid(guid);
373 tether_network_state->set_visible(true); 426 tether_network_state->set_visible(true);
374 tether_network_state->set_update_received(); 427 tether_network_state->set_update_received();
428 tether_network_state->set_update_requested(false);
429 tether_network_state->set_connectable(true);
430 tether_network_state->set_carrier(carrier);
431 tether_network_state->set_battery_percentage(battery_percentage);
432 tether_network_state->set_signal_strength(signal_strength);
375 433
376 tether_network_list_.push_back(std::move(tether_network_state)); 434 tether_network_list_.push_back(std::move(tether_network_state));
377 NotifyNetworkListChanged(); 435 NotifyNetworkListChanged();
378 } 436 }
379 437
438 bool NetworkStateHandler::UpdateTetherNetworkProperties(
439 const std::string& guid,
440 const std::string& carrier,
441 int battery_percentage,
442 int signal_strength) {
443 NetworkState* tether_network_state = GetModifiableNetworkStateFromGuid(guid);
444 if (!tether_network_state)
445 return false;
446
447 tether_network_state->set_carrier(carrier);
448 tether_network_state->set_battery_percentage(battery_percentage);
449 tether_network_state->set_signal_strength(signal_strength);
450
451 NotifyNetworkListChanged();
452 return true;
453 }
454
380 void NetworkStateHandler::RemoveTetherNetworkState(const std::string& guid) { 455 void NetworkStateHandler::RemoveTetherNetworkState(const std::string& guid) {
381 for (auto iter = tether_network_list_.begin(); 456 for (auto iter = tether_network_list_.begin();
382 iter != tether_network_list_.end(); ++iter) { 457 iter != tether_network_list_.end(); ++iter) {
383 if (iter->get()->AsNetworkState()->guid() == guid) { 458 if (iter->get()->AsNetworkState()->guid() == guid) {
384 NetworkState* wifi_network = GetModifiableNetworkStateFromGuid( 459 NetworkState* wifi_network = GetModifiableNetworkStateFromGuid(
385 iter->get()->AsNetworkState()->tether_guid()); 460 iter->get()->AsNetworkState()->tether_guid());
386 if (wifi_network) 461 if (wifi_network)
387 wifi_network->set_tether_guid(std::string()); 462 wifi_network->set_tether_guid(std::string());
388 463
389 tether_network_list_.erase(iter); 464 tether_network_list_.erase(iter);
(...skipping 30 matching lines...) Expand all
420 } 495 }
421 496
422 tether_network->set_tether_guid(wifi_network_guid); 497 tether_network->set_tether_guid(wifi_network_guid);
423 wifi_network->set_tether_guid(tether_network_guid); 498 wifi_network->set_tether_guid(tether_network_guid);
424 NotifyNetworkListChanged(); 499 NotifyNetworkListChanged();
425 return true; 500 return true;
426 } 501 }
427 502
428 void NetworkStateHandler::SetTetherNetworkStateDisconnected( 503 void NetworkStateHandler::SetTetherNetworkStateDisconnected(
429 const std::string& guid) { 504 const std::string& guid) {
505 // TODO(khorimoto): Remove the tether network as the default network, and
506 // send a connection status change.
430 SetTetherNetworkStateConnectionState(guid, shill::kStateDisconnect); 507 SetTetherNetworkStateConnectionState(guid, shill::kStateDisconnect);
431 } 508 }
432 509
433 void NetworkStateHandler::SetTetherNetworkStateConnecting( 510 void NetworkStateHandler::SetTetherNetworkStateConnecting(
434 const std::string& guid) { 511 const std::string& guid) {
512 // TODO(khorimoto): Set the tether network as the default network, and send
513 // a connection status change.
435 SetTetherNetworkStateConnectionState(guid, shill::kStateConfiguration); 514 SetTetherNetworkStateConnectionState(guid, shill::kStateConfiguration);
436 } 515 }
437 516
438 void NetworkStateHandler::SetTetherNetworkStateConnected( 517 void NetworkStateHandler::SetTetherNetworkStateConnected(
439 const std::string& guid) { 518 const std::string& guid) {
519 // Being connected implies that AssociateTetherNetworkStateWithWifiNetwork()
520 // was already called, so ensure that the association is still intact.
521 DCHECK(GetNetworkStateFromGuid(GetNetworkStateFromGuid(guid)->tether_guid())
522 ->tether_guid() == guid);
523
524 // TODO(khorimoto): Send a connection status change.
440 SetTetherNetworkStateConnectionState(guid, shill::kStateOnline); 525 SetTetherNetworkStateConnectionState(guid, shill::kStateOnline);
441 } 526 }
442 527
443 void NetworkStateHandler::SetTetherNetworkStateConnectionState( 528 void NetworkStateHandler::SetTetherNetworkStateConnectionState(
444 const std::string& guid, 529 const std::string& guid,
445 const std::string& connection_state) { 530 const std::string& connection_state) {
446 NetworkState* tether_network = GetModifiableNetworkStateFromGuid(guid); 531 NetworkState* tether_network = GetModifiableNetworkStateFromGuid(guid);
447 if (!tether_network) { 532 if (!tether_network) {
448 NET_LOG(ERROR) << "SetTetherNetworkStateConnectionState: Tether network " 533 NET_LOG(ERROR) << "SetTetherNetworkStateConnectionState: Tether network "
449 << "not found: " << guid; 534 << "not found: " << guid;
450 return; 535 return;
451 } 536 }
452 537
453 if (!NetworkTypePattern::Tether().MatchesType(tether_network->type())) { 538 DCHECK(NetworkTypePattern::Tether().MatchesType(tether_network->type()));
454 NET_LOG(ERROR) << "SetTetherNetworkStateConnectionState: network "
455 << "is not a Tether network: " << guid;
456 return;
457 }
458 539
459 tether_network->set_connection_state(connection_state); 540 tether_network->set_connection_state(connection_state);
460 NotifyNetworkListChanged(); 541 NotifyNetworkListChanged();
461 } 542 }
462 543
463 void NetworkStateHandler::GetDeviceList(DeviceStateList* list) const { 544 void NetworkStateHandler::GetDeviceList(DeviceStateList* list) const {
464 GetDeviceListByType(NetworkTypePattern::Default(), list); 545 GetDeviceListByType(NetworkTypePattern::Default(), list);
465 } 546 }
466 547
467 void NetworkStateHandler::GetDeviceListByType(const NetworkTypePattern& type, 548 void NetworkStateHandler::GetDeviceListByType(const NetworkTypePattern& type,
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
568 service_path.c_str())); 649 service_path.c_str()));
569 return nullptr; 650 return nullptr;
570 } 651 }
571 return list.front(); 652 return list.front();
572 } 653 }
573 654
574 void NetworkStateHandler::SetLastErrorForTest(const std::string& service_path, 655 void NetworkStateHandler::SetLastErrorForTest(const std::string& service_path,
575 const std::string& error) { 656 const std::string& error) {
576 NetworkState* network_state = GetModifiableNetworkState(service_path); 657 NetworkState* network_state = GetModifiableNetworkState(service_path);
577 if (!network_state) { 658 if (!network_state) {
578 LOG(ERROR) << "No matching NetworkState for: " << service_path; 659 NET_LOG(ERROR) << "No matching NetworkState for: " << service_path;
579 return; 660 return;
580 } 661 }
581 network_state->last_error_ = error; 662 network_state->last_error_ = error;
582 } 663 }
583 664
584 //------------------------------------------------------------------------------ 665 //------------------------------------------------------------------------------
585 // ShillPropertyHandler::Delegate overrides 666 // ShillPropertyHandler::Delegate overrides
586 667
587 void NetworkStateHandler::UpdateManagedList(ManagedState::ManagedType type, 668 void NetworkStateHandler::UpdateManagedList(ManagedState::ManagedType type,
588 const base::ListValue& entries) { 669 const base::ListValue& entries) {
(...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after
860 devices += ", "; 941 devices += ", ";
861 devices += (*iter)->name(); 942 devices += (*iter)->name();
862 } 943 }
863 NET_LOG_EVENT("DeviceList", devices); 944 NET_LOG_EVENT("DeviceList", devices);
864 NotifyDeviceListChanged(); 945 NotifyDeviceListChanged();
865 } else { 946 } else {
866 NOTREACHED(); 947 NOTREACHED();
867 } 948 }
868 } 949 }
869 950
951 // TODO(khorimoto): Add sorting for the tether network list as well.
870 void NetworkStateHandler::SortNetworkList() { 952 void NetworkStateHandler::SortNetworkList() {
871 // Note: usually active networks will precede inactive networks, however 953 // Note: usually active networks will precede inactive networks, however
872 // this may briefly be untrue during state transitions (e.g. a network may 954 // this may briefly be untrue during state transitions (e.g. a network may
873 // transition to idle before the list is updated). 955 // transition to idle before the list is updated).
874 ManagedStateList active, non_wifi_visible, wifi_visible, hidden, new_networks; 956 ManagedStateList active, non_wifi_visible, wifi_visible, hidden, new_networks;
875 for (ManagedStateList::iterator iter = network_list_.begin(); 957 for (ManagedStateList::iterator iter = network_list_.begin();
876 iter != network_list_.end(); ++iter) { 958 iter != network_list_.end(); ++iter) {
877 NetworkState* network = (*iter)->AsNetworkState(); 959 NetworkState* network = (*iter)->AsNetworkState();
878 if (!network->update_received()) { 960 if (!network->update_received()) {
879 new_networks.push_back(std::move(*iter)); 961 new_networks.push_back(std::move(*iter));
(...skipping 289 matching lines...) Expand 10 before | Expand all | Expand 10 after
1169 if (type.MatchesType(shill::kTypeBluetooth)) 1251 if (type.MatchesType(shill::kTypeBluetooth))
1170 technologies.emplace_back(shill::kTypeBluetooth); 1252 technologies.emplace_back(shill::kTypeBluetooth);
1171 if (type.MatchesType(shill::kTypeVPN)) 1253 if (type.MatchesType(shill::kTypeVPN))
1172 technologies.emplace_back(shill::kTypeVPN); 1254 technologies.emplace_back(shill::kTypeVPN);
1173 1255
1174 CHECK_GT(technologies.size(), 0ul); 1256 CHECK_GT(technologies.size(), 0ul);
1175 return technologies; 1257 return technologies;
1176 } 1258 }
1177 1259
1178 } // namespace chromeos 1260 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698