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

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

Issue 2945643002: [CrOS Tether] Sort Tether network lists. (Closed)
Patch Set: stevenjb@ comments. 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 namespace {
22
23 cryptauth::RemoteDevice Move(const cryptauth::RemoteDevice& remote_device) {
24 return remote_device;
25 }
26
27 std::unique_ptr<ManagedState> Move(
28 std::unique_ptr<ManagedState>& tether_network_state) {
29 return std::move(tether_network_state);
30 }
31
32 } // namespace
33
34 HostScanDevicePrioritizerImpl::HostScanDevicePrioritizerImpl(
35 TetherHostResponseRecorder* tether_host_response_recorder,
36 DeviceIdTetherNetworkGuidMap* device_id_tether_network_guid_map)
37 : tether_host_response_recorder_(tether_host_response_recorder),
38 device_id_tether_network_guid_map_(device_id_tether_network_guid_map) {}
39
40 HostScanDevicePrioritizerImpl::~HostScanDevicePrioritizerImpl() {}
41
42 void HostScanDevicePrioritizerImpl::SortByHostScanOrder(
43 std::vector<cryptauth::RemoteDevice>* remote_devices) const {
44 SortNetworks(remote_devices);
45 }
46
47 void HostScanDevicePrioritizerImpl::SortTetherNetworkList(
48 NetworkStateHandler::ManagedStateList* tether_networks) const {
49 SortNetworks(tether_networks);
50 }
51
52 template <typename T>
53 void HostScanDevicePrioritizerImpl::SortNetworks(
54 std::vector<T>* list_to_sort) const {
55 // First, fetch the hosts which have previously responded.
56 std::vector<std::string> prioritized_ids =
57 tether_host_response_recorder_->GetPreviouslyAvailableHostIds();
58
59 std::vector<std::string> previously_connectable_host_ids =
60 tether_host_response_recorder_->GetPreviouslyConnectedHostIds();
61 if (!previously_connectable_host_ids.empty()) {
62 // If there is a most-recently connectable host, insert it at the front of
63 // the list.
64 prioritized_ids.insert(prioritized_ids.begin(),
65 previously_connectable_host_ids[0]);
66 }
67
68 // Iterate from the last stored ID to the first stored ID. This ensures that
69 // the items at the front of the list end up in the front of the prioritized
70 // |list_to_sort| vector.
71 for (auto prioritized_it = prioritized_ids.rbegin();
72 prioritized_it != prioritized_ids.rend(); ++prioritized_it) {
73 // Iterate through |list_to_sort| to see if a device ID exists which is
74 // equal to |stored_id|. If one exists, remove it from its previous
75 // position in the list and add it at the front instead.
76 for (auto list_to_sort_it = list_to_sort->begin();
77 list_to_sort_it != list_to_sort->end(); ++list_to_sort_it) {
78 if (GetDeviceId(*list_to_sort_it) != *prioritized_it) {
79 continue;
80 }
81
82 T entry_to_move = Move(*list_to_sort_it);
83 list_to_sort->erase(list_to_sort_it);
84 list_to_sort->emplace(list_to_sort->begin(), Move(entry_to_move));
85 break;
86 }
87 }
88 }
89
90 std::string HostScanDevicePrioritizerImpl::GetDeviceId(
91 const cryptauth::RemoteDevice& remote_device) const {
92 return remote_device.GetDeviceId();
93 }
94
95 std::string HostScanDevicePrioritizerImpl::GetDeviceId(
96 const std::unique_ptr<ManagedState>& tether_network_state) const {
97 NetworkState* network_state =
98 static_cast<NetworkState*>(tether_network_state.get());
99 return device_id_tether_network_guid_map_->GetDeviceIdForTetherNetworkGuid(
100 network_state->guid());
101 }
102
103 } // namespace tether
104
105 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698