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

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: Fix comments, add style change, change a LOG to a DCHECK. 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 }
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 14 matching lines...) Expand all
284 const NetworkTypePattern& type, 315 const NetworkTypePattern& type,
285 NetworkStateList* list) { 316 NetworkStateList* list) {
286 GetNetworkListByType(type, false /* configured_only */, 317 GetNetworkListByType(type, false /* configured_only */,
287 true /* visible_only */, 0 /* no limit */, list); 318 true /* visible_only */, 0 /* no limit */, list);
288 } 319 }
289 320
290 void NetworkStateHandler::GetVisibleNetworkList(NetworkStateList* list) { 321 void NetworkStateHandler::GetVisibleNetworkList(NetworkStateList* list) {
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,
Ryan Hansberry 2017/04/18 16:08:55 This now makes GetTetherNetworkList pointless. Is
Kyle Horimoto 2017/04/18 17:15:23 The implementation of the chrome.networkingPrivate
Ryan Hansberry 2017/04/18 23:56:17 I would prefer if GetTetherNetworkList() were priv
Kyle Horimoto 2017/04/19 00:28:17 Done.
Kyle Horimoto 2017/04/19 02:05:46 Actually, this function is still called by ash. Ch
Ryan Hansberry 2017/04/19 17:49:07 Do you mean that GetTetherNetworkList() is called
Kyle Horimoto 2017/04/19 19:23:28 Done.
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
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);
Ryan Hansberry 2017/04/18 16:08:55 Signal strength can be between 0 and 4 inclusive.
Kyle Horimoto 2017/04/18 17:15:23 Not on Chrome OS.
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;
Ryan Hansberry 2017/04/18 16:08:55 Should probably create a set_connectable().
Kyle Horimoto 2017/04/18 17:15:23 Done.
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->set_update_requested(false);
430 tether_network_state->SetCarrier(carrier);
431 tether_network_state->SetBatteryPercentage(battery_percentage);
432 tether_network_state->SetTetherSignalStrength(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->SetCarrier(carrier);
448 tether_network_state->SetBatteryPercentage(battery_percentage);
449 tether_network_state->SetTetherSignalStrength(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): Change default network? Notify of connection change?
Ryan Hansberry 2017/04/18 16:08:55 Will the Tether component be the only caller of th
Kyle Horimoto 2017/04/18 17:15:23 That's not true. There's no public method for chan
Ryan Hansberry 2017/04/18 23:56:17 Do we need to change default_network_path_? In any
Kyle Horimoto 2017/04/19 00:28:17 Yes, we want to change the default network path so
430 SetTetherNetworkStateConnectionState(guid, shill::kStateDisconnect); 506 SetTetherNetworkStateConnectionState(guid, shill::kStateDisconnect);
431 } 507 }
432 508
433 void NetworkStateHandler::SetTetherNetworkStateConnecting( 509 void NetworkStateHandler::SetTetherNetworkStateConnecting(
434 const std::string& guid) { 510 const std::string& guid) {
511 // TODO(khorimoto): Change default network? Notify of connection change?
435 SetTetherNetworkStateConnectionState(guid, shill::kStateConfiguration); 512 SetTetherNetworkStateConnectionState(guid, shill::kStateConfiguration);
436 } 513 }
437 514
438 void NetworkStateHandler::SetTetherNetworkStateConnected( 515 void NetworkStateHandler::SetTetherNetworkStateConnected(
439 const std::string& guid) { 516 const std::string& guid) {
517 // Being connected implies that AssociateTetherNetworkStateWithWifiNetwork()
518 // was already called, so ensure that the association is still intact.
519 DCHECK(GetNetworkStateFromGuid(GetNetworkStateFromGuid(guid)->tether_guid())
520 ->tether_guid() == guid);
521
522 // TODO(khorimoto): Change default network? Notify of connection change?
440 SetTetherNetworkStateConnectionState(guid, shill::kStateOnline); 523 SetTetherNetworkStateConnectionState(guid, shill::kStateOnline);
441 } 524 }
442 525
443 void NetworkStateHandler::SetTetherNetworkStateConnectionState( 526 void NetworkStateHandler::SetTetherNetworkStateConnectionState(
444 const std::string& guid, 527 const std::string& guid,
445 const std::string& connection_state) { 528 const std::string& connection_state) {
446 NetworkState* tether_network = GetModifiableNetworkStateFromGuid(guid); 529 NetworkState* tether_network = GetModifiableNetworkStateFromGuid(guid);
447 if (!tether_network) { 530 if (!tether_network) {
448 NET_LOG(ERROR) << "SetTetherNetworkStateConnectionState: Tether network " 531 NET_LOG(ERROR) << "SetTetherNetworkStateConnectionState: Tether network "
449 << "not found: " << guid; 532 << "not found: " << guid;
450 return; 533 return;
451 } 534 }
452 535
453 if (!NetworkTypePattern::Tether().MatchesType(tether_network->type())) { 536 DCHECK(NetworkTypePattern::Tether().MatchesType(tether_network->type()));
454 NET_LOG(ERROR) << "SetTetherNetworkStateConnectionState: network "
455 << "is not a Tether network: " << guid;
456 return;
457 }
458 537
459 tether_network->set_connection_state(connection_state); 538 tether_network->set_connection_state(connection_state);
460 NotifyNetworkListChanged(); 539 NotifyNetworkListChanged();
461 } 540 }
462 541
463 void NetworkStateHandler::GetDeviceList(DeviceStateList* list) const { 542 void NetworkStateHandler::GetDeviceList(DeviceStateList* list) const {
464 GetDeviceListByType(NetworkTypePattern::Default(), list); 543 GetDeviceListByType(NetworkTypePattern::Default(), list);
465 } 544 }
466 545
467 void NetworkStateHandler::GetDeviceListByType(const NetworkTypePattern& type, 546 void NetworkStateHandler::GetDeviceListByType(const NetworkTypePattern& type,
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
568 service_path.c_str())); 647 service_path.c_str()));
569 return nullptr; 648 return nullptr;
570 } 649 }
571 return list.front(); 650 return list.front();
572 } 651 }
573 652
574 void NetworkStateHandler::SetLastErrorForTest(const std::string& service_path, 653 void NetworkStateHandler::SetLastErrorForTest(const std::string& service_path,
575 const std::string& error) { 654 const std::string& error) {
576 NetworkState* network_state = GetModifiableNetworkState(service_path); 655 NetworkState* network_state = GetModifiableNetworkState(service_path);
577 if (!network_state) { 656 if (!network_state) {
578 LOG(ERROR) << "No matching NetworkState for: " << service_path; 657 NET_LOG(ERROR) << "No matching NetworkState for: " << service_path;
579 return; 658 return;
580 } 659 }
581 network_state->last_error_ = error; 660 network_state->last_error_ = error;
582 } 661 }
583 662
584 //------------------------------------------------------------------------------ 663 //------------------------------------------------------------------------------
585 // ShillPropertyHandler::Delegate overrides 664 // ShillPropertyHandler::Delegate overrides
586 665
587 void NetworkStateHandler::UpdateManagedList(ManagedState::ManagedType type, 666 void NetworkStateHandler::UpdateManagedList(ManagedState::ManagedType type,
588 const base::ListValue& entries) { 667 const base::ListValue& entries) {
(...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after
860 devices += ", "; 939 devices += ", ";
861 devices += (*iter)->name(); 940 devices += (*iter)->name();
862 } 941 }
863 NET_LOG_EVENT("DeviceList", devices); 942 NET_LOG_EVENT("DeviceList", devices);
864 NotifyDeviceListChanged(); 943 NotifyDeviceListChanged();
865 } else { 944 } else {
866 NOTREACHED(); 945 NOTREACHED();
867 } 946 }
868 } 947 }
869 948
949 // TODO(khorimoto): Add sorting for the tether network list as well.
870 void NetworkStateHandler::SortNetworkList() { 950 void NetworkStateHandler::SortNetworkList() {
871 // Note: usually active networks will precede inactive networks, however 951 // Note: usually active networks will precede inactive networks, however
872 // this may briefly be untrue during state transitions (e.g. a network may 952 // this may briefly be untrue during state transitions (e.g. a network may
873 // transition to idle before the list is updated). 953 // transition to idle before the list is updated).
874 ManagedStateList active, non_wifi_visible, wifi_visible, hidden, new_networks; 954 ManagedStateList active, non_wifi_visible, wifi_visible, hidden, new_networks;
875 for (ManagedStateList::iterator iter = network_list_.begin(); 955 for (ManagedStateList::iterator iter = network_list_.begin();
876 iter != network_list_.end(); ++iter) { 956 iter != network_list_.end(); ++iter) {
877 NetworkState* network = (*iter)->AsNetworkState(); 957 NetworkState* network = (*iter)->AsNetworkState();
878 if (!network->update_received()) { 958 if (!network->update_received()) {
879 new_networks.push_back(std::move(*iter)); 959 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)) 1249 if (type.MatchesType(shill::kTypeBluetooth))
1170 technologies.emplace_back(shill::kTypeBluetooth); 1250 technologies.emplace_back(shill::kTypeBluetooth);
1171 if (type.MatchesType(shill::kTypeVPN)) 1251 if (type.MatchesType(shill::kTypeVPN))
1172 technologies.emplace_back(shill::kTypeVPN); 1252 technologies.emplace_back(shill::kTypeVPN);
1173 1253
1174 CHECK_GT(technologies.size(), 0ul); 1254 CHECK_GT(technologies.size(), 0ul);
1175 return technologies; 1255 return technologies;
1176 } 1256 }
1177 1257
1178 } // namespace chromeos 1258 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698