OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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/components/tether/host_scan_device_prioritizer.h" | 5 #include "chromeos/components/tether/host_scan_device_prioritizer.h" |
6 | 6 |
7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
8 #include "base/values.h" | 8 #include "base/values.h" |
| 9 #include "chromeos/components/tether/device_id_tether_network_guid_map.h" |
9 #include "chromeos/components/tether/pref_names.h" | 10 #include "chromeos/components/tether/pref_names.h" |
10 #include "chromeos/components/tether/tether_host_response_recorder.h" | 11 #include "chromeos/components/tether/tether_host_response_recorder.h" |
| 12 #include "chromeos/network/network_state.h" |
| 13 #include "chromeos/network/network_state_handler.h" |
11 #include "components/prefs/pref_registry_simple.h" | 14 #include "components/prefs/pref_registry_simple.h" |
12 #include "components/prefs/pref_service.h" | 15 #include "components/prefs/pref_service.h" |
13 | 16 |
14 namespace chromeos { | 17 namespace chromeos { |
15 | 18 |
16 namespace tether { | 19 namespace tether { |
17 | 20 |
18 HostScanDevicePrioritizer::HostScanDevicePrioritizer( | 21 HostScanDevicePrioritizer::HostScanDevicePrioritizer( |
19 TetherHostResponseRecorder* tether_host_response_recorder) | 22 NetworkStateHandler* network_state_handler, |
20 : tether_host_response_recorder_(tether_host_response_recorder) {} | 23 TetherHostResponseRecorder* tether_host_response_recorder, |
| 24 DeviceIdTetherNetworkGuidMap* device_id_tether_network_guid_map) |
| 25 : network_state_handler_(network_state_handler), |
| 26 tether_host_response_recorder_(tether_host_response_recorder), |
| 27 device_id_tether_network_guid_map_(device_id_tether_network_guid_map) { |
| 28 network_state_handler_->SetTetherNetworkListSorter(this); |
| 29 } |
21 | 30 |
22 HostScanDevicePrioritizer::~HostScanDevicePrioritizer() {} | 31 HostScanDevicePrioritizer::~HostScanDevicePrioritizer() { |
| 32 network_state_handler_->SetTetherNetworkListSorter(nullptr); |
| 33 } |
23 | 34 |
24 void HostScanDevicePrioritizer::SortByHostScanOrder( | 35 void HostScanDevicePrioritizer::SortByHostScanOrder( |
25 std::vector<cryptauth::RemoteDevice>* remote_devices) const { | 36 std::vector<cryptauth::RemoteDevice>* remote_devices) const { |
| 37 SortNetworks(remote_devices); |
| 38 } |
| 39 |
| 40 void HostScanDevicePrioritizer::SortTetherNetworkList( |
| 41 NetworkStateHandler::ManagedStateList* tether_networks) const { |
| 42 SortNetworks(tether_networks); |
| 43 } |
| 44 |
| 45 template <typename T> |
| 46 void HostScanDevicePrioritizer::SortNetworks( |
| 47 std::vector<T>* list_to_sort) const { |
26 // First, fetch the hosts which have previously responded. | 48 // First, fetch the hosts which have previously responded. |
27 std::vector<std::string> prioritized_ids = | 49 std::vector<std::string> prioritized_ids = |
28 tether_host_response_recorder_->GetPreviouslyAvailableHostIds(); | 50 tether_host_response_recorder_->GetPreviouslyAvailableHostIds(); |
29 | 51 |
30 std::vector<std::string> previously_connectable_host_ids = | 52 std::vector<std::string> previously_connectable_host_ids = |
31 tether_host_response_recorder_->GetPreviouslyConnectedHostIds(); | 53 tether_host_response_recorder_->GetPreviouslyConnectedHostIds(); |
32 if (!previously_connectable_host_ids.empty()) { | 54 if (!previously_connectable_host_ids.empty()) { |
33 // If there is a most-recently connectable host, insert it at the front of | 55 // If there is a most-recently connectable host, insert it at the front of |
34 // the list. | 56 // the list. |
35 prioritized_ids.insert(prioritized_ids.begin(), | 57 prioritized_ids.insert(prioritized_ids.begin(), |
36 previously_connectable_host_ids[0]); | 58 previously_connectable_host_ids[0]); |
37 } | 59 } |
38 | 60 |
39 // Iterate from the last stored ID to the first stored ID. This ensures that | 61 // Iterate from the last stored ID to the first stored ID. This ensures that |
40 // the items at the front of the list end up in the front of the prioritized | 62 // the items at the front of the list end up in the front of the prioritized |
41 // |remote_devices| vector. | 63 // |list_to_sort| vector. |
42 for (auto prioritized_it = prioritized_ids.rbegin(); | 64 for (auto prioritized_it = prioritized_ids.rbegin(); |
43 prioritized_it != prioritized_ids.rend(); ++prioritized_it) { | 65 prioritized_it != prioritized_ids.rend(); ++prioritized_it) { |
44 // Iterate through |remote_devices| to see if a device exists with a | 66 // Iterate through |list_to_sort| to see if a device ID exists which is |
45 // device ID of |stored_id|. If one exists, remove it from its previous | 67 // equal to |stored_id|. If one exists, remove it from its previous |
46 // position in the list and add it at the front instead. | 68 // position in the list and add it at the front instead. |
47 for (auto remote_devices_it = remote_devices->begin(); | 69 for (auto list_to_sort_it = list_to_sort->begin(); |
48 remote_devices_it != remote_devices->end(); ++remote_devices_it) { | 70 list_to_sort_it != list_to_sort->end(); ++list_to_sort_it) { |
49 if (remote_devices_it->GetDeviceId() != *prioritized_it) { | 71 if (GetDeviceId(*list_to_sort_it) != *prioritized_it) { |
50 continue; | 72 continue; |
51 } | 73 } |
52 | 74 |
53 cryptauth::RemoteDevice device_to_move = *remote_devices_it; | 75 T entry_to_move = Move(*list_to_sort_it); |
54 remote_devices->erase(remote_devices_it); | 76 list_to_sort->erase(list_to_sort_it); |
55 remote_devices->insert(remote_devices->begin(), device_to_move); | 77 list_to_sort->emplace(list_to_sort->begin(), Move(entry_to_move)); |
56 break; | 78 break; |
57 } | 79 } |
58 } | 80 } |
59 } | 81 } |
60 | 82 |
| 83 std::string HostScanDevicePrioritizer::GetDeviceId( |
| 84 const cryptauth::RemoteDevice& remote_device) const { |
| 85 return remote_device.GetDeviceId(); |
| 86 } |
| 87 |
| 88 std::string HostScanDevicePrioritizer::GetDeviceId( |
| 89 const std::unique_ptr<ManagedState>& tether_network_state) const { |
| 90 NetworkState* network_state = |
| 91 static_cast<NetworkState*>(tether_network_state.get()); |
| 92 return device_id_tether_network_guid_map_->GetDeviceIdForTetherNetworkGuid( |
| 93 network_state->guid()); |
| 94 } |
| 95 |
| 96 cryptauth::RemoteDevice HostScanDevicePrioritizer::Move( |
| 97 const cryptauth::RemoteDevice& remote_device) const { |
| 98 return remote_device; |
| 99 } |
| 100 |
| 101 std::unique_ptr<ManagedState> HostScanDevicePrioritizer::Move( |
| 102 std::unique_ptr<ManagedState>& tether_network_state) const { |
| 103 return std::move(tether_network_state); |
| 104 } |
| 105 |
61 } // namespace tether | 106 } // namespace tether |
62 | 107 |
63 } // namespace chromeos | 108 } // namespace chromeos |
OLD | NEW |