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

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

Issue 2819993002: [CrOS Tether] Add the notion of a tether DeviceState. (Closed)
Patch Set: stevenjb@ 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/memory/ptr_util.h" 16 #include "base/memory/ptr_util.h"
17 #include "base/metrics/histogram_macros.h" 17 #include "base/metrics/histogram_macros.h"
18 #include "base/strings/string_number_conversions.h" 18 #include "base/strings/string_number_conversions.h"
19 #include "base/strings/string_util.h" 19 #include "base/strings/string_util.h"
20 #include "base/strings/stringprintf.h" 20 #include "base/strings/stringprintf.h"
21 #include "base/values.h" 21 #include "base/values.h"
22 #include "chromeos/chromeos_switches.h" 22 #include "chromeos/chromeos_switches.h"
23 #include "chromeos/network/device_state.h" 23 #include "chromeos/network/device_state.h"
24 #include "chromeos/network/network_connection_handler.h"
24 #include "chromeos/network/network_event_log.h" 25 #include "chromeos/network/network_event_log.h"
26 #include "chromeos/network/network_handler_callbacks.h"
25 #include "chromeos/network/network_state.h" 27 #include "chromeos/network/network_state.h"
26 #include "chromeos/network/network_state_handler_observer.h" 28 #include "chromeos/network/network_state_handler_observer.h"
27 #include "chromeos/network/tether_constants.h" 29 #include "chromeos/network/tether_constants.h"
28 #include "third_party/cros_system_api/dbus/service_constants.h" 30 #include "third_party/cros_system_api/dbus/service_constants.h"
29 31
30 namespace chromeos { 32 namespace chromeos {
31 33
32 namespace { 34 namespace {
33 35
34 bool ConnectionStateChanged(NetworkState* network, 36 bool ConnectionStateChanged(NetworkState* network,
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
119 device_event_log::LOG_TYPE_NETWORK, device_event_log::LOG_LEVEL_DEBUG, 121 device_event_log::LOG_TYPE_NETWORK, device_event_log::LOG_LEVEL_DEBUG,
120 base::StringPrintf("NetworkStateHandler::RemoveObserver: 0x%p", 122 base::StringPrintf("NetworkStateHandler::RemoveObserver: 0x%p",
121 observer)); 123 observer));
122 } 124 }
123 125
124 NetworkStateHandler::TechnologyState NetworkStateHandler::GetTechnologyState( 126 NetworkStateHandler::TechnologyState NetworkStateHandler::GetTechnologyState(
125 const NetworkTypePattern& type) const { 127 const NetworkTypePattern& type) const {
126 std::string technology = GetTechnologyForType(type); 128 std::string technology = GetTechnologyForType(type);
127 129
128 if (technology == kTypeTether) { 130 if (technology == kTypeTether) {
129 bool is_tether_enabled = base::CommandLine::ForCurrentProcess()->HasSwitch( 131 return tether_technology_state_;
130 chromeos::switches::kEnableTether);
131 return is_tether_enabled ? TECHNOLOGY_ENABLED : TECHNOLOGY_UNAVAILABLE;
132 } 132 }
133 133
134 TechnologyState state; 134 TechnologyState state;
135 if (shill_property_handler_->IsTechnologyEnabled(technology)) 135 if (shill_property_handler_->IsTechnologyEnabled(technology))
136 state = TECHNOLOGY_ENABLED; 136 state = TECHNOLOGY_ENABLED;
137 else if (shill_property_handler_->IsTechnologyEnabling(technology)) 137 else if (shill_property_handler_->IsTechnologyEnabling(technology))
138 state = TECHNOLOGY_ENABLING; 138 state = TECHNOLOGY_ENABLING;
139 else if (shill_property_handler_->IsTechnologyProhibited(technology)) 139 else if (shill_property_handler_->IsTechnologyProhibited(technology))
140 state = TECHNOLOGY_PROHIBITED; 140 state = TECHNOLOGY_PROHIBITED;
141 else if (shill_property_handler_->IsTechnologyUninitialized(technology)) 141 else if (shill_property_handler_->IsTechnologyUninitialized(technology))
142 state = TECHNOLOGY_UNINITIALIZED; 142 state = TECHNOLOGY_UNINITIALIZED;
143 else if (shill_property_handler_->IsTechnologyAvailable(technology)) 143 else if (shill_property_handler_->IsTechnologyAvailable(technology))
144 state = TECHNOLOGY_AVAILABLE; 144 state = TECHNOLOGY_AVAILABLE;
145 else 145 else
146 state = TECHNOLOGY_UNAVAILABLE; 146 state = TECHNOLOGY_UNAVAILABLE;
147 VLOG(2) << "GetTechnologyState: " << type.ToDebugString() << " = " << state; 147 VLOG(2) << "GetTechnologyState: " << type.ToDebugString() << " = " << state;
148 return state; 148 return state;
149 } 149 }
150 150
151 void NetworkStateHandler::SetTechnologyEnabled( 151 void NetworkStateHandler::SetTechnologyEnabled(
152 const NetworkTypePattern& type, 152 const NetworkTypePattern& type,
153 bool enabled, 153 bool enabled,
154 const network_handler::ErrorCallback& error_callback) { 154 const network_handler::ErrorCallback& error_callback) {
155 std::vector<std::string> technologies = GetTechnologiesForType(type); 155 std::vector<std::string> technologies = GetTechnologiesForType(type);
156 for (const std::string& technology : technologies) { 156 for (const std::string& technology : technologies) {
157 if (technology == kTypeTether) {
158 if (tether_technology_state_ != TECHNOLOGY_ENABLED &&
159 tether_technology_state_ != TECHNOLOGY_AVAILABLE) {
160 // SetTechnologyEnabled() should only be called if the device is already
161 // in either the TECHNOLOGY_ENABLED or TECHNOLOGY_AVAILABLE states. If
162 // this is not the case, invoke the error callback.
stevenjb 2017/04/20 20:01:11 nit: I think this is clear from the code (i..e the
Kyle Horimoto 2017/04/20 20:49:20 Done.
163 NET_LOG(ERROR) << "SetTechnologyEnabled() called for the Tether "
164 << "DeviceState, but the current state was not "
165 << "TECHNOLOGY_ENABLED or TECHNOLOGY_AVAILABLE.";
166 network_handler::RunErrorCallback(
167 error_callback, kTetherDevicePath,
168 NetworkConnectionHandler::kEnabledOrDisabledWhenNotAvailable, "");
169 continue;
170 }
171
172 // Tether does not exist in Shill, so set |tether_technology_state_| and
173 // skip the below interactions with |shill_property_handler_|.
174 tether_technology_state_ =
175 enabled ? TECHNOLOGY_ENABLED : TECHNOLOGY_AVAILABLE;
176 continue;
177 }
178
157 if (!shill_property_handler_->IsTechnologyAvailable(technology)) 179 if (!shill_property_handler_->IsTechnologyAvailable(technology))
158 continue; 180 continue;
159 NET_LOG_USER("SetTechnologyEnabled", 181 NET_LOG_USER("SetTechnologyEnabled",
160 base::StringPrintf("%s:%d", technology.c_str(), enabled)); 182 base::StringPrintf("%s:%d", technology.c_str(), enabled));
161 shill_property_handler_->SetTechnologyEnabled(technology, enabled, 183 shill_property_handler_->SetTechnologyEnabled(technology, enabled,
162 error_callback); 184 error_callback);
163 } 185 }
164 // Signal Device/Technology state changed. 186 // Signal Device/Technology state changed.
165 NotifyDeviceListChanged(); 187 NotifyDeviceListChanged();
166 } 188 }
167 189
190 void NetworkStateHandler::SetTetherTechnologyState(
191 TechnologyState technology_state) {
192 if (tether_technology_state_ == technology_state)
193 return;
194
195 tether_technology_state_ = technology_state;
196
197 // Signal Device/Technology state changed.
stevenjb 2017/04/20 20:01:11 I would actually call EnsureTetherDeviceState() ex
Kyle Horimoto 2017/04/20 20:49:20 Done.
198 NotifyDeviceListChanged();
199 }
200
201 void NetworkStateHandler::SetTetherScanState(bool is_scanning) {
202 DeviceState* tether_device_state =
203 GetModifiableDeviceState(kTetherDevicePath);
204 DCHECK(tether_device_state);
205
206 bool was_scanning = tether_device_state->scanning();
207 tether_device_state->set_scanning(is_scanning);
208
209 if (was_scanning && !is_scanning) {
210 // If a scan was in progress but has completed, notify observers.
211 NotifyScanCompleted(tether_device_state);
212 }
213 }
214
168 void NetworkStateHandler::SetProhibitedTechnologies( 215 void NetworkStateHandler::SetProhibitedTechnologies(
169 const std::vector<std::string>& prohibited_technologies, 216 const std::vector<std::string>& prohibited_technologies,
170 const network_handler::ErrorCallback& error_callback) { 217 const network_handler::ErrorCallback& error_callback) {
171 shill_property_handler_->SetProhibitedTechnologies(prohibited_technologies, 218 // Make a copy of |prohibited_technologies| since the list may be edited
172 error_callback); 219 // within this function.
220 std::vector<std::string> prohibited_technologies_copy =
221 prohibited_technologies;
222
223 for (auto it = prohibited_technologies_copy.begin();
224 it < prohibited_technologies_copy.end(); ++it) {
225 if (*it == kTypeTether) {
226 // If Tether networks are prohibited, set |tether_technology_state_| and
227 // remove |kTypeTether| from the list before passing it to
228 // |shill_property_handler_| below. Shill does not have a concept of
229 // Tether networks, so it cannot prohibit that technology type.
230 tether_technology_state_ = TECHNOLOGY_PROHIBITED;
231 it = prohibited_technologies_copy.erase(it);
232 }
233 }
234
235 shill_property_handler_->SetProhibitedTechnologies(
236 prohibited_technologies_copy, error_callback);
173 // Signal Device/Technology state changed. 237 // Signal Device/Technology state changed.
174 NotifyDeviceListChanged(); 238 NotifyDeviceListChanged();
175 } 239 }
176 240
177 const DeviceState* NetworkStateHandler::GetDeviceState( 241 const DeviceState* NetworkStateHandler::GetDeviceState(
178 const std::string& device_path) const { 242 const std::string& device_path) const {
179 const DeviceState* device = GetModifiableDeviceState(device_path); 243 const DeviceState* device = GetModifiableDeviceState(device_path);
180 if (device && !device->update_received()) 244 if (device && !device->update_received())
181 return nullptr; 245 return nullptr;
182 return device; 246 return device;
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
293 } 357 }
294 358
295 return connecting_network; 359 return connecting_network;
296 } 360 }
297 361
298 const NetworkState* NetworkStateHandler::FirstNetworkByType( 362 const NetworkState* NetworkStateHandler::FirstNetworkByType(
299 const NetworkTypePattern& type) { 363 const NetworkTypePattern& type) {
300 if (!network_list_sorted_) 364 if (!network_list_sorted_)
301 SortNetworkList(); // Sort to ensure visible networks are listed first. 365 SortNetworkList(); // Sort to ensure visible networks are listed first.
302 366
303 // If |type| matches tether networks and at least one tether network is 367 // If |type| matches Tether networks and at least one Tether network is
304 // present, return the first network (since it has been sorted already). 368 // present, return the first network (since it has been sorted already).
305 if (type.MatchesPattern(NetworkTypePattern::Tether()) && 369 if (type.MatchesPattern(NetworkTypePattern::Tether()) &&
306 !tether_network_list_.empty()) { 370 !tether_network_list_.empty()) {
307 return tether_network_list_[0]->AsNetworkState(); 371 return tether_network_list_[0]->AsNetworkState();
308 } 372 }
309 373
310 for (auto iter = network_list_.begin(); iter != network_list_.end(); ++iter) { 374 for (auto iter = network_list_.begin(); iter != network_list_.end(); ++iter) {
311 const NetworkState* network = (*iter)->AsNetworkState(); 375 const NetworkState* network = (*iter)->AsNetworkState();
312 DCHECK(network); 376 DCHECK(network);
313 if (!network->update_received()) 377 if (!network->update_received())
314 continue; 378 continue;
315 if (!network->visible()) 379 if (!network->visible())
316 break; 380 break;
317 if (network->Matches(type)) 381 if (network->Matches(type))
318 return network; 382 return network;
319 } 383 }
320 return nullptr; 384 return nullptr;
321 } 385 }
322 386
323 std::string NetworkStateHandler::FormattedHardwareAddressForType( 387 std::string NetworkStateHandler::FormattedHardwareAddressForType(
324 const NetworkTypePattern& type) const { 388 const NetworkTypePattern& type) const {
325 const NetworkState* network = ConnectedNetworkByType(type); 389 const NetworkState* network = ConnectedNetworkByType(type);
390 if (network && network->type() == kTypeTether) {
391 // If this is a Tether network, get the MAC address corresponding to that
392 // network instead.
393 network = GetNetworkStateFromGuid(network->tether_guid());
394 }
326 const DeviceState* device = network ? GetDeviceState(network->device_path()) 395 const DeviceState* device = network ? GetDeviceState(network->device_path())
327 : GetDeviceStateByType(type); 396 : GetDeviceStateByType(type);
328 if (!device) 397 if (!device || device->mac_address().empty())
329 return std::string(); 398 return std::string();
330 return network_util::FormattedMacAddress(device->mac_address()); 399 return network_util::FormattedMacAddress(device->mac_address());
331 } 400 }
332 401
333 void NetworkStateHandler::GetVisibleNetworkListByType( 402 void NetworkStateHandler::GetVisibleNetworkListByType(
334 const NetworkTypePattern& type, 403 const NetworkTypePattern& type,
335 NetworkStateList* list) { 404 NetworkStateList* list) {
336 GetNetworkListByType(type, false /* configured_only */, 405 GetNetworkListByType(type, false /* configured_only */,
337 true /* visible_only */, 0 /* no limit */, list); 406 true /* visible_only */, 0 /* no limit */, list);
338 } 407 }
(...skipping 15 matching lines...) Expand all
354 SortNetworkList(); 423 SortNetworkList();
355 424
356 if (type.MatchesPattern(NetworkTypePattern::Tether())) { 425 if (type.MatchesPattern(NetworkTypePattern::Tether())) {
357 GetTetherNetworkList(limit, list); 426 GetTetherNetworkList(limit, list);
358 } 427 }
359 428
360 int count = list->size(); 429 int count = list->size();
361 430
362 if (type.Equals(NetworkTypePattern::Tether()) || 431 if (type.Equals(NetworkTypePattern::Tether()) ||
363 (limit != 0 && count >= limit)) { 432 (limit != 0 && count >= limit)) {
364 // If only searching for tether networks, there is no need to continue 433 // If only searching for Tether networks, there is no need to continue
365 // searching through other network types; likewise, if the limit has already 434 // searching through other network types; likewise, if the limit has already
366 // been reached, there is no need to continue searching. 435 // been reached, there is no need to continue searching.
367 return; 436 return;
368 } 437 }
369 438
370 for (auto iter = network_list_.begin(); iter != network_list_.end(); ++iter) { 439 for (auto iter = network_list_.begin(); iter != network_list_.end(); ++iter) {
371 const NetworkState* network = (*iter)->AsNetworkState(); 440 const NetworkState* network = (*iter)->AsNetworkState();
372 DCHECK(network); 441 DCHECK(network);
373 if (!network->update_received() || !network->Matches(type)) 442 if (!network->update_received() || !network->Matches(type))
374 continue; 443 continue;
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
425 494
426 void NetworkStateHandler::AddTetherNetworkState(const std::string& guid, 495 void NetworkStateHandler::AddTetherNetworkState(const std::string& guid,
427 const std::string& name, 496 const std::string& name,
428 const std::string& carrier, 497 const std::string& carrier,
429 int battery_percentage, 498 int battery_percentage,
430 int signal_strength) { 499 int signal_strength) {
431 DCHECK(!guid.empty()); 500 DCHECK(!guid.empty());
432 DCHECK(battery_percentage >= 0 && battery_percentage <= 100); 501 DCHECK(battery_percentage >= 0 && battery_percentage <= 100);
433 DCHECK(signal_strength >= 0 && signal_strength <= 100); 502 DCHECK(signal_strength >= 0 && signal_strength <= 100);
434 503
504 if (tether_technology_state_ != TECHNOLOGY_ENABLED) {
505 NET_LOG(ERROR) << "AddTetherNetworkState() called when Tether networks "
506 << "are not enabled. Cannot add NetworkState.";
507 return;
508 }
509
435 // If the network already exists, do nothing. 510 // If the network already exists, do nothing.
436 if (GetNetworkStateFromGuid(guid)) { 511 if (GetNetworkStateFromGuid(guid)) {
437 NET_LOG(ERROR) << "AddTetherNetworkState: " << name 512 NET_LOG(ERROR) << "AddTetherNetworkState: " << name
438 << " called with existing guid:" << guid; 513 << " called with existing guid:" << guid;
439 return; 514 return;
440 } 515 }
441 516
442 // Use the GUID as the network's service path. 517 // Use the GUID as the network's service path.
443 std::unique_ptr<NetworkState> tether_network_state = 518 std::unique_ptr<NetworkState> tether_network_state =
444 base::MakeUnique<NetworkState>(guid /* path */); 519 base::MakeUnique<NetworkState>(guid /* path */);
(...skipping 11 matching lines...) Expand all
456 531
457 tether_network_list_.push_back(std::move(tether_network_state)); 532 tether_network_list_.push_back(std::move(tether_network_state));
458 NotifyNetworkListChanged(); 533 NotifyNetworkListChanged();
459 } 534 }
460 535
461 bool NetworkStateHandler::UpdateTetherNetworkProperties( 536 bool NetworkStateHandler::UpdateTetherNetworkProperties(
462 const std::string& guid, 537 const std::string& guid,
463 const std::string& carrier, 538 const std::string& carrier,
464 int battery_percentage, 539 int battery_percentage,
465 int signal_strength) { 540 int signal_strength) {
541 if (tether_technology_state_ != TECHNOLOGY_ENABLED) {
542 NET_LOG(ERROR) << "UpdateTetherNetworkProperties() called when Tether "
543 << "networks are not enabled. Cannot update.";
544 return false;
545 }
546
466 NetworkState* tether_network_state = GetModifiableNetworkStateFromGuid(guid); 547 NetworkState* tether_network_state = GetModifiableNetworkStateFromGuid(guid);
467 if (!tether_network_state) 548 if (!tether_network_state)
468 return false; 549 return false;
469 550
470 tether_network_state->set_carrier(carrier); 551 tether_network_state->set_carrier(carrier);
471 tether_network_state->set_battery_percentage(battery_percentage); 552 tether_network_state->set_battery_percentage(battery_percentage);
472 tether_network_state->set_signal_strength(signal_strength); 553 tether_network_state->set_signal_strength(signal_strength);
473 554
474 NotifyNetworkListChanged(); 555 NotifyNetworkListChanged();
475 return true; 556 return true;
(...skipping 11 matching lines...) Expand all
487 tether_network_list_.erase(iter); 568 tether_network_list_.erase(iter);
488 NotifyNetworkListChanged(); 569 NotifyNetworkListChanged();
489 return; 570 return;
490 } 571 }
491 } 572 }
492 } 573 }
493 574
494 bool NetworkStateHandler::AssociateTetherNetworkStateWithWifiNetwork( 575 bool NetworkStateHandler::AssociateTetherNetworkStateWithWifiNetwork(
495 const std::string& tether_network_guid, 576 const std::string& tether_network_guid,
496 const std::string& wifi_network_guid) { 577 const std::string& wifi_network_guid) {
578 if (tether_technology_state_ != TECHNOLOGY_ENABLED) {
579 NET_LOG(ERROR) << "AssociateTetherNetworkStateWithWifiNetwork() called "
580 << "when Tether networks are not enabled. Cannot "
581 << "associate.";
582 return false;
583 }
584
497 NetworkState* tether_network = 585 NetworkState* tether_network =
498 GetModifiableNetworkStateFromGuid(tether_network_guid); 586 GetModifiableNetworkStateFromGuid(tether_network_guid);
499 if (!tether_network) { 587 if (!tether_network) {
500 NET_LOG(ERROR) << "Tether network does not exist: " << tether_network_guid; 588 NET_LOG(ERROR) << "Tether network does not exist: " << tether_network_guid;
501 return false; 589 return false;
502 } 590 }
503 if (!NetworkTypePattern::Tether().MatchesType(tether_network->type())) { 591 if (!NetworkTypePattern::Tether().MatchesType(tether_network->type())) {
504 NET_LOG(ERROR) << "Network is not a Tether network: " 592 NET_LOG(ERROR) << "Network is not a Tether network: "
505 << tether_network_guid; 593 << tether_network_guid;
506 return false; 594 return false;
(...skipping 11 matching lines...) Expand all
518 } 606 }
519 607
520 tether_network->set_tether_guid(wifi_network_guid); 608 tether_network->set_tether_guid(wifi_network_guid);
521 wifi_network->set_tether_guid(tether_network_guid); 609 wifi_network->set_tether_guid(tether_network_guid);
522 NotifyNetworkListChanged(); 610 NotifyNetworkListChanged();
523 return true; 611 return true;
524 } 612 }
525 613
526 void NetworkStateHandler::SetTetherNetworkStateDisconnected( 614 void NetworkStateHandler::SetTetherNetworkStateDisconnected(
527 const std::string& guid) { 615 const std::string& guid) {
528 // TODO(khorimoto): Remove the tether network as the default network, and 616 // TODO(khorimoto): Remove the Tether network as the default network, and
529 // send a connection status change. 617 // send a connection status change.
530 SetTetherNetworkStateConnectionState(guid, shill::kStateDisconnect); 618 SetTetherNetworkStateConnectionState(guid, shill::kStateDisconnect);
531 } 619 }
532 620
533 void NetworkStateHandler::SetTetherNetworkStateConnecting( 621 void NetworkStateHandler::SetTetherNetworkStateConnecting(
534 const std::string& guid) { 622 const std::string& guid) {
535 // TODO(khorimoto): Set the tether network as the default network, and send 623 // TODO(khorimoto): Set the Tether network as the default network, and send
536 // a connection status change. 624 // a connection status change.
537 SetTetherNetworkStateConnectionState(guid, shill::kStateConfiguration); 625 SetTetherNetworkStateConnectionState(guid, shill::kStateConfiguration);
538 } 626 }
539 627
540 void NetworkStateHandler::SetTetherNetworkStateConnected( 628 void NetworkStateHandler::SetTetherNetworkStateConnected(
541 const std::string& guid) { 629 const std::string& guid) {
542 // Being connected implies that AssociateTetherNetworkStateWithWifiNetwork() 630 // Being connected implies that AssociateTetherNetworkStateWithWifiNetwork()
543 // was already called, so ensure that the association is still intact. 631 // was already called, so ensure that the association is still intact.
544 DCHECK(GetNetworkStateFromGuid(GetNetworkStateFromGuid(guid)->tether_guid()) 632 DCHECK(GetNetworkStateFromGuid(GetNetworkStateFromGuid(guid)->tether_guid())
545 ->tether_guid() == guid); 633 ->tether_guid() == guid);
(...skipping 11 matching lines...) Expand all
557 << "not found: " << guid; 645 << "not found: " << guid;
558 return; 646 return;
559 } 647 }
560 648
561 DCHECK(NetworkTypePattern::Tether().MatchesType(tether_network->type())); 649 DCHECK(NetworkTypePattern::Tether().MatchesType(tether_network->type()));
562 650
563 tether_network->set_connection_state(connection_state); 651 tether_network->set_connection_state(connection_state);
564 NotifyNetworkListChanged(); 652 NotifyNetworkListChanged();
565 } 653 }
566 654
655 void NetworkStateHandler::EnsureTetherDeviceState() {
656 bool should_be_present =
657 tether_technology_state_ != TechnologyState::TECHNOLOGY_UNAVAILABLE;
658
659 for (auto it = device_list_.begin(); it < device_list_.end(); ++it) {
660 std::string path = (*it)->path();
661 if (path == kTetherDevicePath) {
662 // If the Tether DeviceState is in the list and it should not be, remove
663 // it and return. If it is in the list and it should be, the list is
664 // already valid, so return without removing it.
665 if (!should_be_present) {
666 device_list_.erase(it);
667 }
stevenjb 2017/04/20 20:01:11 nit: no {}
Kyle Horimoto 2017/04/20 20:49:20 Done.
668 return;
669 }
670 }
671
672 if (!should_be_present) {
673 // If the Tether DeviceState was not in the list and it should not be, the
674 // list is already valid, so return.
675 return;
676 }
677
678 // The Tether DeviceState is not present in the list, but it should be. Since
679 // Tether networks are not recognized by Shill, they will never receive an
680 // update, so set properties on the state here.
681 std::unique_ptr<ManagedState> tether_device_state = ManagedState::Create(
682 ManagedState::ManagedType::MANAGED_TYPE_DEVICE, kTetherDevicePath);
683 tether_device_state->set_update_received();
684 tether_device_state->set_update_requested(false);
685 tether_device_state->set_name(kTetherDeviceName);
686 tether_device_state->set_type(kTypeTether);
687
688 device_list_.push_back(std::move(tether_device_state));
689 }
690
567 void NetworkStateHandler::GetDeviceList(DeviceStateList* list) const { 691 void NetworkStateHandler::GetDeviceList(DeviceStateList* list) const {
568 GetDeviceListByType(NetworkTypePattern::Default(), list); 692 GetDeviceListByType(NetworkTypePattern::Default(), list);
569 } 693 }
570 694
571 void NetworkStateHandler::GetDeviceListByType(const NetworkTypePattern& type, 695 void NetworkStateHandler::GetDeviceListByType(const NetworkTypePattern& type,
572 DeviceStateList* list) const { 696 DeviceStateList* list) const {
573 DCHECK(list); 697 DCHECK(list);
574 list->clear(); 698 list->clear();
699
575 for (auto iter = device_list_.begin(); iter != device_list_.end(); ++iter) { 700 for (auto iter = device_list_.begin(); iter != device_list_.end(); ++iter) {
576 const DeviceState* device = (*iter)->AsDeviceState(); 701 const DeviceState* device = (*iter)->AsDeviceState();
577 DCHECK(device); 702 DCHECK(device);
578 if (device->update_received() && device->Matches(type)) 703 if (device->update_received() && device->Matches(type))
579 list->push_back(device); 704 list->push_back(device);
580 } 705 }
581 } 706 }
582 707
583 void NetworkStateHandler::RequestScan() const { 708 void NetworkStateHandler::RequestScan() const {
584 NET_LOG_USER("RequestScan", ""); 709 NET_LOG_USER("RequestScan", "");
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
719 continue; 844 continue;
720 } 845 }
721 managed_list->push_back(ManagedState::Create(type, path)); 846 managed_list->push_back(ManagedState::Create(type, path));
722 } else { 847 } else {
723 managed_list->push_back(std::move(found->second)); 848 managed_list->push_back(std::move(found->second));
724 managed_map.erase(found); 849 managed_map.erase(found);
725 } 850 }
726 list_entries.insert(path); 851 list_entries.insert(path);
727 } 852 }
728 853
854 if (type == ManagedState::ManagedType::MANAGED_TYPE_DEVICE) {
855 EnsureTetherDeviceState();
stevenjb 2017/04/20 20:01:11 Rather than re-creating the tether state here, you
Kyle Horimoto 2017/04/20 20:49:19 Done.
856 }
857
729 if (type != ManagedState::ManagedType::MANAGED_TYPE_NETWORK) 858 if (type != ManagedState::ManagedType::MANAGED_TYPE_NETWORK)
730 return; 859 return;
731 860
732 // Remove associations Tether NetworkStates had with now removed Wi-Fi 861 // Remove associations Tether NetworkStates had with now removed Wi-Fi
733 // NetworkStates. 862 // NetworkStates.
734 for (auto& iter : managed_map) { 863 for (auto& iter : managed_map) {
735 if (!iter.second->Matches(NetworkTypePattern::WiFi())) 864 if (!iter.second->Matches(NetworkTypePattern::WiFi()))
736 continue; 865 continue;
737 866
738 NetworkState* tether_network = GetModifiableNetworkStateFromGuid( 867 NetworkState* tether_network = GetModifiableNetworkStateFromGuid(
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after
964 devices += ", "; 1093 devices += ", ";
965 devices += (*iter)->name(); 1094 devices += (*iter)->name();
966 } 1095 }
967 NET_LOG_EVENT("DeviceList", devices); 1096 NET_LOG_EVENT("DeviceList", devices);
968 NotifyDeviceListChanged(); 1097 NotifyDeviceListChanged();
969 } else { 1098 } else {
970 NOTREACHED(); 1099 NOTREACHED();
971 } 1100 }
972 } 1101 }
973 1102
974 // TODO(khorimoto): Add sorting for the tether network list as well. 1103 // TODO(khorimoto): Add sorting for the Tether network list as well.
975 void NetworkStateHandler::SortNetworkList() { 1104 void NetworkStateHandler::SortNetworkList() {
976 // Note: usually active networks will precede inactive networks, however 1105 // Note: usually active networks will precede inactive networks, however
977 // this may briefly be untrue during state transitions (e.g. a network may 1106 // this may briefly be untrue during state transitions (e.g. a network may
978 // transition to idle before the list is updated). 1107 // transition to idle before the list is updated).
979 ManagedStateList active, non_wifi_visible, wifi_visible, hidden, new_networks; 1108 ManagedStateList active, non_wifi_visible, wifi_visible, hidden, new_networks;
980 for (ManagedStateList::iterator iter = network_list_.begin(); 1109 for (ManagedStateList::iterator iter = network_list_.begin();
981 iter != network_list_.end(); ++iter) { 1110 iter != network_list_.end(); ++iter) {
982 NetworkState* network = (*iter)->AsNetworkState(); 1111 NetworkState* network = (*iter)->AsNetworkState();
983 if (!network->update_received()) { 1112 if (!network->update_received()) {
984 new_networks.push_back(std::move(*iter)); 1113 new_networks.push_back(std::move(*iter));
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
1099 NET_LOG_EVENT("NOTIFY:NetworkListChanged", 1228 NET_LOG_EVENT("NOTIFY:NetworkListChanged",
1100 base::StringPrintf("Size:%" PRIuS, network_list_.size())); 1229 base::StringPrintf("Size:%" PRIuS, network_list_.size()));
1101 for (auto& observer : observers_) 1230 for (auto& observer : observers_)
1102 observer.NetworkListChanged(); 1231 observer.NetworkListChanged();
1103 } 1232 }
1104 1233
1105 void NetworkStateHandler::NotifyDeviceListChanged() { 1234 void NetworkStateHandler::NotifyDeviceListChanged() {
1106 SCOPED_NET_LOG_IF_SLOW(); 1235 SCOPED_NET_LOG_IF_SLOW();
1107 NET_LOG_DEBUG("NOTIFY:DeviceListChanged", 1236 NET_LOG_DEBUG("NOTIFY:DeviceListChanged",
1108 base::StringPrintf("Size:%" PRIuS, device_list_.size())); 1237 base::StringPrintf("Size:%" PRIuS, device_list_.size()));
1238 EnsureTetherDeviceState();
1109 for (auto& observer : observers_) 1239 for (auto& observer : observers_)
1110 observer.DeviceListChanged(); 1240 observer.DeviceListChanged();
1111 } 1241 }
1112 1242
1113 DeviceState* NetworkStateHandler::GetModifiableDeviceState( 1243 DeviceState* NetworkStateHandler::GetModifiableDeviceState(
1114 const std::string& device_path) const { 1244 const std::string& device_path) const {
1115 ManagedState* managed = GetModifiableManagedState(&device_list_, device_path); 1245 ManagedState* managed = GetModifiableManagedState(&device_list_, device_path);
1116 if (!managed) 1246 if (!managed)
1117 return nullptr; 1247 return nullptr;
1118 return managed->AsDeviceState(); 1248 return managed->AsDeviceState();
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after
1268 if (type.MatchesType(shill::kTypeWifi)) 1398 if (type.MatchesType(shill::kTypeWifi))
1269 technologies.emplace_back(shill::kTypeWifi); 1399 technologies.emplace_back(shill::kTypeWifi);
1270 if (type.MatchesType(shill::kTypeWimax)) 1400 if (type.MatchesType(shill::kTypeWimax))
1271 technologies.emplace_back(shill::kTypeWimax); 1401 technologies.emplace_back(shill::kTypeWimax);
1272 if (type.MatchesType(shill::kTypeCellular)) 1402 if (type.MatchesType(shill::kTypeCellular))
1273 technologies.emplace_back(shill::kTypeCellular); 1403 technologies.emplace_back(shill::kTypeCellular);
1274 if (type.MatchesType(shill::kTypeBluetooth)) 1404 if (type.MatchesType(shill::kTypeBluetooth))
1275 technologies.emplace_back(shill::kTypeBluetooth); 1405 technologies.emplace_back(shill::kTypeBluetooth);
1276 if (type.MatchesType(shill::kTypeVPN)) 1406 if (type.MatchesType(shill::kTypeVPN))
1277 technologies.emplace_back(shill::kTypeVPN); 1407 technologies.emplace_back(shill::kTypeVPN);
1408 if (type.MatchesType(kTypeTether))
1409 technologies.emplace_back(kTypeTether);
1278 1410
1279 CHECK_GT(technologies.size(), 0ul); 1411 CHECK_GT(technologies.size(), 0ul);
1280 return technologies; 1412 return technologies;
1281 } 1413 }
1282 1414
1283 } // namespace chromeos 1415 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698