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" |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
119 device_event_log::LOG_TYPE_NETWORK, device_event_log::LOG_LEVEL_DEBUG, | 119 device_event_log::LOG_TYPE_NETWORK, device_event_log::LOG_LEVEL_DEBUG, |
120 base::StringPrintf("NetworkStateHandler::RemoveObserver: 0x%p", | 120 base::StringPrintf("NetworkStateHandler::RemoveObserver: 0x%p", |
121 observer)); | 121 observer)); |
122 } | 122 } |
123 | 123 |
124 NetworkStateHandler::TechnologyState NetworkStateHandler::GetTechnologyState( | 124 NetworkStateHandler::TechnologyState NetworkStateHandler::GetTechnologyState( |
125 const NetworkTypePattern& type) const { | 125 const NetworkTypePattern& type) const { |
126 std::string technology = GetTechnologyForType(type); | 126 std::string technology = GetTechnologyForType(type); |
127 | 127 |
128 if (technology == kTypeTether) { | 128 if (technology == kTypeTether) { |
129 bool is_tether_enabled = base::CommandLine::ForCurrentProcess()->HasSwitch( | 129 return tether_state_; |
130 chromeos::switches::kEnableTether); | |
131 return is_tether_enabled ? TECHNOLOGY_ENABLED : TECHNOLOGY_UNAVAILABLE; | |
132 } | 130 } |
133 | 131 |
134 TechnologyState state; | 132 TechnologyState state; |
135 if (shill_property_handler_->IsTechnologyEnabled(technology)) | 133 if (shill_property_handler_->IsTechnologyEnabled(technology)) |
136 state = TECHNOLOGY_ENABLED; | 134 state = TECHNOLOGY_ENABLED; |
137 else if (shill_property_handler_->IsTechnologyEnabling(technology)) | 135 else if (shill_property_handler_->IsTechnologyEnabling(technology)) |
138 state = TECHNOLOGY_ENABLING; | 136 state = TECHNOLOGY_ENABLING; |
139 else if (shill_property_handler_->IsTechnologyProhibited(technology)) | 137 else if (shill_property_handler_->IsTechnologyProhibited(technology)) |
140 state = TECHNOLOGY_PROHIBITED; | 138 state = TECHNOLOGY_PROHIBITED; |
141 else if (shill_property_handler_->IsTechnologyUninitialized(technology)) | 139 else if (shill_property_handler_->IsTechnologyUninitialized(technology)) |
142 state = TECHNOLOGY_UNINITIALIZED; | 140 state = TECHNOLOGY_UNINITIALIZED; |
143 else if (shill_property_handler_->IsTechnologyAvailable(technology)) | 141 else if (shill_property_handler_->IsTechnologyAvailable(technology)) |
144 state = TECHNOLOGY_AVAILABLE; | 142 state = TECHNOLOGY_AVAILABLE; |
145 else | 143 else |
146 state = TECHNOLOGY_UNAVAILABLE; | 144 state = TECHNOLOGY_UNAVAILABLE; |
147 VLOG(2) << "GetTechnologyState: " << type.ToDebugString() << " = " << state; | 145 VLOG(2) << "GetTechnologyState: " << type.ToDebugString() << " = " << state; |
148 return state; | 146 return state; |
149 } | 147 } |
150 | 148 |
151 void NetworkStateHandler::SetTechnologyEnabled( | 149 void NetworkStateHandler::SetTechnologyEnabled( |
152 const NetworkTypePattern& type, | 150 const NetworkTypePattern& type, |
153 bool enabled, | 151 bool enabled, |
154 const network_handler::ErrorCallback& error_callback) { | 152 const network_handler::ErrorCallback& error_callback) { |
155 std::vector<std::string> technologies = GetTechnologiesForType(type); | 153 std::vector<std::string> technologies = GetTechnologiesForType(type); |
156 for (const std::string& technology : technologies) { | 154 for (const std::string& technology : technologies) { |
155 if (technology == kTypeTether) { | |
156 // Tether does not exist in Shill, so call SetTetherTechnologyState() and | |
Ryan Hansberry
2017/04/20 01:38:35
The comment suggests that SetTetherTechnologyState
Kyle Horimoto
2017/04/20 02:16:08
Done.
| |
157 // skip the below interactions with |shill_property_handler_|. | |
158 tether_state_ = enabled ? TECHNOLOGY_ENABLED : TECHNOLOGY_AVAILABLE; | |
159 continue; | |
160 } | |
161 | |
157 if (!shill_property_handler_->IsTechnologyAvailable(technology)) | 162 if (!shill_property_handler_->IsTechnologyAvailable(technology)) |
158 continue; | 163 continue; |
159 NET_LOG_USER("SetTechnologyEnabled", | 164 NET_LOG_USER("SetTechnologyEnabled", |
160 base::StringPrintf("%s:%d", technology.c_str(), enabled)); | 165 base::StringPrintf("%s:%d", technology.c_str(), enabled)); |
161 shill_property_handler_->SetTechnologyEnabled(technology, enabled, | 166 shill_property_handler_->SetTechnologyEnabled(technology, enabled, |
162 error_callback); | 167 error_callback); |
163 } | 168 } |
164 // Signal Device/Technology state changed. | 169 // Signal Device/Technology state changed. |
165 NotifyDeviceListChanged(); | 170 NotifyDeviceListChanged(); |
166 } | 171 } |
167 | 172 |
173 void NetworkStateHandler::SetTetherTechnologyState( | |
174 TechnologyState technology_state) { | |
175 if (tether_state_ == technology_state) | |
176 return; | |
177 | |
178 tether_state_ = technology_state; | |
179 | |
180 // Signal Device/Technology state changed. | |
181 NotifyDeviceListChanged(); | |
182 } | |
183 | |
184 void NetworkStateHandler::SetTetherScanState(bool is_scanning) { | |
185 DeviceState* tether_device_state = | |
186 GetModifiableDeviceState(kTetherDevicePath); | |
187 DCHECK(tether_device_state); | |
188 | |
189 bool was_scanning = tether_device_state->scanning(); | |
190 tether_device_state->set_scanning(is_scanning); | |
191 | |
192 if (was_scanning && !is_scanning) { | |
193 // If a scan was in progress but has completed, notify observers. | |
194 NotifyScanCompleted(tether_device_state); | |
195 } | |
196 } | |
197 | |
168 void NetworkStateHandler::SetProhibitedTechnologies( | 198 void NetworkStateHandler::SetProhibitedTechnologies( |
169 const std::vector<std::string>& prohibited_technologies, | 199 const std::vector<std::string>& prohibited_technologies, |
170 const network_handler::ErrorCallback& error_callback) { | 200 const network_handler::ErrorCallback& error_callback) { |
171 shill_property_handler_->SetProhibitedTechnologies(prohibited_technologies, | 201 // Make a copy of |prohibited_technologies| since the list may be edited |
172 error_callback); | 202 // within this function. |
203 std::vector<std::string> prohibited_technologies_copy = | |
204 prohibited_technologies; | |
205 | |
206 for (auto it = prohibited_technologies_copy.begin(); | |
207 it < prohibited_technologies_copy.end(); ++it) { | |
208 if (*it == kTypeTether) { | |
209 // If tethering is prohibited, set |tether_state_| and remove tethering | |
Ryan Hansberry
2017/04/20 01:38:35
Tether*
Kyle Horimoto
2017/04/20 02:16:08
Done.
| |
210 // from the list before passing it to |shill_property_handler_| below. | |
211 // Shill does not have a concept of tether networks, so it cannot | |
212 // prohibit that technology type. | |
213 tether_state_ = TECHNOLOGY_PROHIBITED; | |
214 it = prohibited_technologies_copy.erase(it); | |
215 } | |
216 } | |
217 | |
218 shill_property_handler_->SetProhibitedTechnologies( | |
219 prohibited_technologies_copy, error_callback); | |
173 // Signal Device/Technology state changed. | 220 // Signal Device/Technology state changed. |
174 NotifyDeviceListChanged(); | 221 NotifyDeviceListChanged(); |
175 } | 222 } |
176 | 223 |
177 const DeviceState* NetworkStateHandler::GetDeviceState( | 224 const DeviceState* NetworkStateHandler::GetDeviceState( |
178 const std::string& device_path) const { | 225 const std::string& device_path) const { |
179 const DeviceState* device = GetModifiableDeviceState(device_path); | 226 const DeviceState* device = GetModifiableDeviceState(device_path); |
180 if (device && !device->update_received()) | 227 if (device && !device->update_received()) |
181 return nullptr; | 228 return nullptr; |
182 return device; | 229 return device; |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
316 break; | 363 break; |
317 if (network->Matches(type)) | 364 if (network->Matches(type)) |
318 return network; | 365 return network; |
319 } | 366 } |
320 return nullptr; | 367 return nullptr; |
321 } | 368 } |
322 | 369 |
323 std::string NetworkStateHandler::FormattedHardwareAddressForType( | 370 std::string NetworkStateHandler::FormattedHardwareAddressForType( |
324 const NetworkTypePattern& type) const { | 371 const NetworkTypePattern& type) const { |
325 const NetworkState* network = ConnectedNetworkByType(type); | 372 const NetworkState* network = ConnectedNetworkByType(type); |
373 if (network && network->type() == kTypeTether) { | |
374 // If this is a tether network, get the MAC address corresponding to that | |
375 // network instead. | |
376 network = GetNetworkStateFromGuid(network->tether_guid()); | |
Ryan Hansberry
2017/04/20 01:38:35
Just return empty string here instead.
Kyle Horimoto
2017/04/20 02:16:08
I don't think we should - we should return the MAC
Ryan Hansberry
2017/04/20 16:53:54
But we know at this point that the network does no
Kyle Horimoto
2017/04/20 19:38:46
stevenjb@, can you comment on this?
stevenjb
2017/04/20 20:01:10
Why wouldn't a tether network show the MAC address
Ryan Hansberry
2017/04/20 21:27:02
If that's the case, then we should return the mac_
Kyle Horimoto
2017/04/20 21:28:45
That's what I'm already doing ;)
| |
377 } | |
326 const DeviceState* device = network ? GetDeviceState(network->device_path()) | 378 const DeviceState* device = network ? GetDeviceState(network->device_path()) |
327 : GetDeviceStateByType(type); | 379 : GetDeviceStateByType(type); |
328 if (!device) | 380 if (!device || device->mac_address().empty()) |
329 return std::string(); | 381 return std::string(); |
330 return network_util::FormattedMacAddress(device->mac_address()); | 382 return network_util::FormattedMacAddress(device->mac_address()); |
331 } | 383 } |
332 | 384 |
333 void NetworkStateHandler::GetVisibleNetworkListByType( | 385 void NetworkStateHandler::GetVisibleNetworkListByType( |
334 const NetworkTypePattern& type, | 386 const NetworkTypePattern& type, |
335 NetworkStateList* list) { | 387 NetworkStateList* list) { |
336 GetNetworkListByType(type, false /* configured_only */, | 388 GetNetworkListByType(type, false /* configured_only */, |
337 true /* visible_only */, 0 /* no limit */, list); | 389 true /* visible_only */, 0 /* no limit */, list); |
338 } | 390 } |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
424 } | 476 } |
425 | 477 |
426 void NetworkStateHandler::AddTetherNetworkState(const std::string& guid, | 478 void NetworkStateHandler::AddTetherNetworkState(const std::string& guid, |
427 const std::string& name, | 479 const std::string& name, |
428 const std::string& carrier, | 480 const std::string& carrier, |
429 int battery_percentage, | 481 int battery_percentage, |
430 int signal_strength) { | 482 int signal_strength) { |
431 DCHECK(!guid.empty()); | 483 DCHECK(!guid.empty()); |
432 DCHECK(battery_percentage >= 0 && battery_percentage <= 100); | 484 DCHECK(battery_percentage >= 0 && battery_percentage <= 100); |
433 DCHECK(signal_strength >= 0 && signal_strength <= 100); | 485 DCHECK(signal_strength >= 0 && signal_strength <= 100); |
486 DCHECK(tether_state_ == TECHNOLOGY_ENABLED); | |
434 | 487 |
435 // If the network already exists, do nothing. | 488 // If the network already exists, do nothing. |
436 if (GetNetworkStateFromGuid(guid)) { | 489 if (GetNetworkStateFromGuid(guid)) { |
437 NET_LOG(ERROR) << "AddTetherNetworkState: " << name | 490 NET_LOG(ERROR) << "AddTetherNetworkState: " << name |
438 << " called with existing guid:" << guid; | 491 << " called with existing guid:" << guid; |
439 return; | 492 return; |
440 } | 493 } |
441 | 494 |
442 // Use the GUID as the network's service path. | 495 // Use the GUID as the network's service path. |
443 std::unique_ptr<NetworkState> tether_network_state = | 496 std::unique_ptr<NetworkState> tether_network_state = |
(...skipping 12 matching lines...) Expand all Loading... | |
456 | 509 |
457 tether_network_list_.push_back(std::move(tether_network_state)); | 510 tether_network_list_.push_back(std::move(tether_network_state)); |
458 NotifyNetworkListChanged(); | 511 NotifyNetworkListChanged(); |
459 } | 512 } |
460 | 513 |
461 bool NetworkStateHandler::UpdateTetherNetworkProperties( | 514 bool NetworkStateHandler::UpdateTetherNetworkProperties( |
462 const std::string& guid, | 515 const std::string& guid, |
463 const std::string& carrier, | 516 const std::string& carrier, |
464 int battery_percentage, | 517 int battery_percentage, |
465 int signal_strength) { | 518 int signal_strength) { |
519 DCHECK(tether_state_ == TECHNOLOGY_ENABLED); | |
520 | |
466 NetworkState* tether_network_state = GetModifiableNetworkStateFromGuid(guid); | 521 NetworkState* tether_network_state = GetModifiableNetworkStateFromGuid(guid); |
467 if (!tether_network_state) | 522 if (!tether_network_state) |
468 return false; | 523 return false; |
469 | 524 |
470 tether_network_state->set_carrier(carrier); | 525 tether_network_state->set_carrier(carrier); |
471 tether_network_state->set_battery_percentage(battery_percentage); | 526 tether_network_state->set_battery_percentage(battery_percentage); |
472 tether_network_state->set_signal_strength(signal_strength); | 527 tether_network_state->set_signal_strength(signal_strength); |
473 | 528 |
474 NotifyNetworkListChanged(); | 529 NotifyNetworkListChanged(); |
475 return true; | 530 return true; |
(...skipping 11 matching lines...) Expand all Loading... | |
487 tether_network_list_.erase(iter); | 542 tether_network_list_.erase(iter); |
488 NotifyNetworkListChanged(); | 543 NotifyNetworkListChanged(); |
489 return; | 544 return; |
490 } | 545 } |
491 } | 546 } |
492 } | 547 } |
493 | 548 |
494 bool NetworkStateHandler::AssociateTetherNetworkStateWithWifiNetwork( | 549 bool NetworkStateHandler::AssociateTetherNetworkStateWithWifiNetwork( |
495 const std::string& tether_network_guid, | 550 const std::string& tether_network_guid, |
496 const std::string& wifi_network_guid) { | 551 const std::string& wifi_network_guid) { |
552 DCHECK(tether_state_ == TECHNOLOGY_ENABLED); | |
553 | |
497 NetworkState* tether_network = | 554 NetworkState* tether_network = |
498 GetModifiableNetworkStateFromGuid(tether_network_guid); | 555 GetModifiableNetworkStateFromGuid(tether_network_guid); |
499 if (!tether_network) { | 556 if (!tether_network) { |
500 NET_LOG(ERROR) << "Tether network does not exist: " << tether_network_guid; | 557 NET_LOG(ERROR) << "Tether network does not exist: " << tether_network_guid; |
501 return false; | 558 return false; |
502 } | 559 } |
503 if (!NetworkTypePattern::Tether().MatchesType(tether_network->type())) { | 560 if (!NetworkTypePattern::Tether().MatchesType(tether_network->type())) { |
504 NET_LOG(ERROR) << "Network is not a Tether network: " | 561 NET_LOG(ERROR) << "Network is not a Tether network: " |
505 << tether_network_guid; | 562 << tether_network_guid; |
506 return false; | 563 return false; |
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
686 } | 743 } |
687 | 744 |
688 //------------------------------------------------------------------------------ | 745 //------------------------------------------------------------------------------ |
689 // ShillPropertyHandler::Delegate overrides | 746 // ShillPropertyHandler::Delegate overrides |
690 | 747 |
691 void NetworkStateHandler::UpdateManagedList(ManagedState::ManagedType type, | 748 void NetworkStateHandler::UpdateManagedList(ManagedState::ManagedType type, |
692 const base::ListValue& entries) { | 749 const base::ListValue& entries) { |
693 ManagedStateList* managed_list = GetManagedList(type); | 750 ManagedStateList* managed_list = GetManagedList(type); |
694 NET_LOG_DEBUG("UpdateManagedList: " + ManagedState::TypeToString(type), | 751 NET_LOG_DEBUG("UpdateManagedList: " + ManagedState::TypeToString(type), |
695 base::StringPrintf("%" PRIuS, entries.GetSize())); | 752 base::StringPrintf("%" PRIuS, entries.GetSize())); |
753 | |
754 // Create a copy of |entries| since it may be edited below. | |
755 base::ListValue entries_copy(entries); | |
756 if (type == ManagedState::ManagedType::MANAGED_TYPE_DEVICE) { | |
757 // If a device list update is received from Shill, the list supplied will | |
758 // never contain an entry for tether networks since Shill does not recognize | |
759 // tether networks as a device type. Before processing the list, add an | |
760 // entry for the tether device type with the constant tether device path. | |
761 entries_copy.Append(base::MakeUnique<base::Value>(kTetherDevicePath)); | |
762 } | |
763 | |
696 // Create a map of existing entries. Assumes all entries in |managed_list| | 764 // Create a map of existing entries. Assumes all entries in |managed_list| |
697 // are unique. | 765 // are unique. |
698 std::map<std::string, std::unique_ptr<ManagedState>> managed_map; | 766 std::map<std::string, std::unique_ptr<ManagedState>> managed_map; |
699 for (auto& item : *managed_list) { | 767 for (auto& item : *managed_list) { |
700 std::string path = item->path(); | 768 std::string path = item->path(); |
701 DCHECK(!base::ContainsKey(managed_map, path)); | 769 DCHECK(!base::ContainsKey(managed_map, path)); |
702 managed_map[path] = std::move(item); | 770 managed_map[path] = std::move(item); |
703 } | 771 } |
704 // Clear the list (objects are temporarily owned by managed_map). | 772 // Clear the list (objects are temporarily owned by managed_map). |
705 managed_list->clear(); | 773 managed_list->clear(); |
706 // Updates managed_list and request updates for new entries. | 774 // Updates managed_list and request updates for new entries. |
707 std::set<std::string> list_entries; | 775 std::set<std::string> list_entries; |
708 for (auto& iter : entries) { | 776 for (auto& iter : entries_copy) { |
709 std::string path; | 777 std::string path; |
710 iter.GetAsString(&path); | 778 iter.GetAsString(&path); |
711 if (path.empty() || path == shill::kFlimflamServicePath) { | 779 if (path.empty() || path == shill::kFlimflamServicePath) { |
712 NET_LOG_ERROR(base::StringPrintf("Bad path in list:%d", type), path); | 780 NET_LOG_ERROR(base::StringPrintf("Bad path in list:%d", type), path); |
713 continue; | 781 continue; |
714 } | 782 } |
715 auto found = managed_map.find(path); | 783 auto found = managed_map.find(path); |
716 if (found == managed_map.end()) { | 784 if (found == managed_map.end()) { |
717 if (list_entries.count(path) != 0) { | 785 if (list_entries.count(path) != 0) { |
718 NET_LOG_ERROR("Duplicate entry in list", path); | 786 NET_LOG_ERROR("Duplicate entry in list", path); |
719 continue; | 787 continue; |
720 } | 788 } |
721 managed_list->push_back(ManagedState::Create(type, path)); | 789 std::unique_ptr<ManagedState> state = ManagedState::Create(type, path); |
790 if (type == ManagedState::ManagedType::MANAGED_TYPE_DEVICE && | |
791 path == kTetherDevicePath) { | |
792 // If adding the tether DeviceState, set appropriate properties since | |
793 // it will never receive a properties update from Shill. | |
794 state->set_update_received(); | |
795 state->set_update_requested(false); | |
796 state->set_name(kTetherDeviceName); | |
797 state->set_type(kTypeTether); | |
798 } | |
799 managed_list->push_back(std::move(state)); | |
722 } else { | 800 } else { |
723 managed_list->push_back(std::move(found->second)); | 801 managed_list->push_back(std::move(found->second)); |
724 managed_map.erase(found); | 802 managed_map.erase(found); |
725 } | 803 } |
726 list_entries.insert(path); | 804 list_entries.insert(path); |
727 } | 805 } |
728 | 806 |
729 if (type != ManagedState::ManagedType::MANAGED_TYPE_NETWORK) | 807 if (type != ManagedState::ManagedType::MANAGED_TYPE_NETWORK) |
730 return; | 808 return; |
731 | 809 |
(...skipping 536 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1268 if (type.MatchesType(shill::kTypeWifi)) | 1346 if (type.MatchesType(shill::kTypeWifi)) |
1269 technologies.emplace_back(shill::kTypeWifi); | 1347 technologies.emplace_back(shill::kTypeWifi); |
1270 if (type.MatchesType(shill::kTypeWimax)) | 1348 if (type.MatchesType(shill::kTypeWimax)) |
1271 technologies.emplace_back(shill::kTypeWimax); | 1349 technologies.emplace_back(shill::kTypeWimax); |
1272 if (type.MatchesType(shill::kTypeCellular)) | 1350 if (type.MatchesType(shill::kTypeCellular)) |
1273 technologies.emplace_back(shill::kTypeCellular); | 1351 technologies.emplace_back(shill::kTypeCellular); |
1274 if (type.MatchesType(shill::kTypeBluetooth)) | 1352 if (type.MatchesType(shill::kTypeBluetooth)) |
1275 technologies.emplace_back(shill::kTypeBluetooth); | 1353 technologies.emplace_back(shill::kTypeBluetooth); |
1276 if (type.MatchesType(shill::kTypeVPN)) | 1354 if (type.MatchesType(shill::kTypeVPN)) |
1277 technologies.emplace_back(shill::kTypeVPN); | 1355 technologies.emplace_back(shill::kTypeVPN); |
1356 if (type.MatchesType(kTypeTether)) | |
1357 technologies.emplace_back(kTypeTether); | |
1278 | 1358 |
1279 CHECK_GT(technologies.size(), 0ul); | 1359 CHECK_GT(technologies.size(), 0ul); |
1280 return technologies; | 1360 return technologies; |
1281 } | 1361 } |
1282 | 1362 |
1283 } // namespace chromeos | 1363 } // namespace chromeos |
OLD | NEW |