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

Side by Side Diff: chromeos/components/tether/host_scan_device_prioritizer_impl.cc

Issue 2945643002: [CrOS Tether] Sort Tether network lists. (Closed)
Patch Set: Order active networks before non-active. Created 3 years, 6 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chromeos/components/tether/host_scan_device_prioritizer_impl.h"
6
7 #include "base/memory/ptr_util.h"
8 #include "base/values.h"
9 #include "chromeos/components/tether/device_id_tether_network_guid_map.h"
10 #include "chromeos/components/tether/pref_names.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"
14 #include "components/prefs/pref_registry_simple.h"
15 #include "components/prefs/pref_service.h"
16
17 namespace chromeos {
18
19 namespace tether {
20
21 HostScanDevicePrioritizerImpl::HostScanDevicePrioritizerImpl(
22 NetworkStateHandler* network_state_handler,
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);
stevenjb 2017/06/21 20:59:03 We shouldn't do this here. Have whatever code crea
Kyle Horimoto 2017/06/21 22:17:07 Done.
29 }
30
31 HostScanDevicePrioritizerImpl::~HostScanDevicePrioritizerImpl() {
32 network_state_handler_->SetTetherNetworkListSorter(nullptr);
33 }
34
35 void HostScanDevicePrioritizerImpl::SortByHostScanOrder(
36 std::vector<cryptauth::RemoteDevice>* remote_devices) const {
37 SortNetworks(remote_devices);
38 }
39
40 void HostScanDevicePrioritizerImpl::SortTetherNetworkList(
41 NetworkStateHandler::ManagedStateList* tether_networks) const {
42 SortNetworks(tether_networks);
43 }
44
45 template <typename T>
46 void HostScanDevicePrioritizerImpl::SortNetworks(
47 std::vector<T>* list_to_sort) const {
48 // First, fetch the hosts which have previously responded.
49 std::vector<std::string> prioritized_ids =
50 tether_host_response_recorder_->GetPreviouslyAvailableHostIds();
51
52 std::vector<std::string> previously_connectable_host_ids =
53 tether_host_response_recorder_->GetPreviouslyConnectedHostIds();
54 if (!previously_connectable_host_ids.empty()) {
55 // If there is a most-recently connectable host, insert it at the front of
56 // the list.
57 prioritized_ids.insert(prioritized_ids.begin(),
58 previously_connectable_host_ids[0]);
59 }
60
61 // Iterate from the last stored ID to the first stored ID. This ensures that
62 // the items at the front of the list end up in the front of the prioritized
63 // |list_to_sort| vector.
64 for (auto prioritized_it = prioritized_ids.rbegin();
65 prioritized_it != prioritized_ids.rend(); ++prioritized_it) {
66 // Iterate through |list_to_sort| to see if a device ID exists which is
67 // equal to |stored_id|. If one exists, remove it from its previous
68 // position in the list and add it at the front instead.
69 for (auto list_to_sort_it = list_to_sort->begin();
70 list_to_sort_it != list_to_sort->end(); ++list_to_sort_it) {
71 if (GetDeviceId(*list_to_sort_it) != *prioritized_it) {
72 continue;
73 }
74
75 T entry_to_move = Move(*list_to_sort_it);
76 list_to_sort->erase(list_to_sort_it);
77 list_to_sort->emplace(list_to_sort->begin(), Move(entry_to_move));
78 break;
79 }
80 }
81 }
82
83 std::string HostScanDevicePrioritizerImpl::GetDeviceId(
84 const cryptauth::RemoteDevice& remote_device) const {
85 return remote_device.GetDeviceId();
86 }
87
88 std::string HostScanDevicePrioritizerImpl::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 HostScanDevicePrioritizerImpl::Move(
97 const cryptauth::RemoteDevice& remote_device) const {
98 return remote_device;
99 }
100
101 std::unique_ptr<ManagedState> HostScanDevicePrioritizerImpl::Move(
102 std::unique_ptr<ManagedState>& tether_network_state) const {
103 return std::move(tether_network_state);
104 }
105
106 } // namespace tether
107
108 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698