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/pref_names.h" | 9 #include "chromeos/components/tether/pref_names.h" |
| 10 #include "chromeos/components/tether/tether_host_response_recorder.h" |
10 #include "components/prefs/pref_registry_simple.h" | 11 #include "components/prefs/pref_registry_simple.h" |
11 #include "components/prefs/pref_service.h" | 12 #include "components/prefs/pref_service.h" |
12 | 13 |
13 namespace chromeos { | 14 namespace chromeos { |
14 | 15 |
15 namespace tether { | 16 namespace tether { |
16 | 17 |
17 // static | 18 HostScanDevicePrioritizer::HostScanDevicePrioritizer( |
18 void HostScanDevicePrioritizer::RegisterPrefs(PrefRegistrySimple* registry) { | 19 TetherHostResponseRecorder* tether_host_response_recorder) |
19 registry->RegisterListPref(prefs::kMostRecentTetherAvailablilityResponderIds); | 20 : tether_host_response_recorder_(tether_host_response_recorder) {} |
20 registry->RegisterStringPref(prefs::kMostRecentConnectTetheringResponderId, | |
21 ""); | |
22 } | |
23 | |
24 HostScanDevicePrioritizer::HostScanDevicePrioritizer(PrefService* pref_service) | |
25 : pref_service_(pref_service) {} | |
26 | 21 |
27 HostScanDevicePrioritizer::~HostScanDevicePrioritizer() {} | 22 HostScanDevicePrioritizer::~HostScanDevicePrioritizer() {} |
28 | 23 |
29 void HostScanDevicePrioritizer::RecordSuccessfulTetherAvailabilityResponse( | |
30 const cryptauth::RemoteDevice& remote_device) { | |
31 std::string device_id = remote_device.GetDeviceId(); | |
32 | |
33 const base::ListValue* ids = | |
34 pref_service_->GetList(prefs::kMostRecentTetherAvailablilityResponderIds); | |
35 | |
36 // Create a mutable copy of the stored IDs, or create one if it has yet to be | |
37 // stored. | |
38 std::unique_ptr<base::ListValue> updated_ids = | |
39 ids ? ids->CreateDeepCopy() : base::MakeUnique<base::ListValue>(); | |
40 | |
41 // Remove the device ID if it was already present in the list. | |
42 std::unique_ptr<base::Value> device_id_value = | |
43 base::MakeUnique<base::Value>(device_id); | |
44 updated_ids->Remove(*device_id_value, nullptr); | |
45 | |
46 // Add the device ID to the front of the queue. | |
47 updated_ids->Insert(0, std::move(device_id_value)); | |
48 | |
49 // Store the updated list back in |pref_service_|. | |
50 pref_service_->Set(prefs::kMostRecentTetherAvailablilityResponderIds, | |
51 *updated_ids); | |
52 } | |
53 | |
54 void HostScanDevicePrioritizer::RecordSuccessfulConnectTetheringResponse( | |
55 const cryptauth::RemoteDevice& remote_device) { | |
56 pref_service_->Set(prefs::kMostRecentConnectTetheringResponderId, | |
57 base::Value(remote_device.GetDeviceId())); | |
58 } | |
59 | |
60 void HostScanDevicePrioritizer::SortByHostScanOrder( | 24 void HostScanDevicePrioritizer::SortByHostScanOrder( |
61 std::vector<cryptauth::RemoteDevice>* remote_devices) const { | 25 std::vector<cryptauth::RemoteDevice>* remote_devices) const { |
62 // Fetch the stored IDs associated with the devices which most recently sent | 26 // First, fetch the hosts which have previously responded. |
63 // TetherAvailabilityResponses. | 27 std::vector<std::string> prioritized_ids = |
64 const base::ListValue* tether_availability_ids = | 28 tether_host_response_recorder_->GetPreviouslyAvailableHostIds(); |
65 pref_service_->GetList(prefs::kMostRecentTetherAvailablilityResponderIds); | |
66 | 29 |
67 // Create a mutable copy of the stored IDs, or create one if it has yet to be | 30 std::vector<std::string> previously_connectable_host_ids = |
68 // stored. | 31 tether_host_response_recorder_->GetPreviouslyConnectableHostIds(); |
69 std::unique_ptr<base::ListValue> prioritized_ids = | 32 if (!previously_connectable_host_ids.empty()) { |
70 tether_availability_ids ? tether_availability_ids->CreateDeepCopy() | 33 // If there is a most-recently connectable host, insert it at the front of |
71 : base::MakeUnique<base::ListValue>(); | 34 // the list. |
72 | 35 prioritized_ids.insert(prioritized_ids.begin(), |
73 // Now, fetch the ID associated with the device which most recently sent a | 36 previously_connectable_host_ids[0]); |
74 // ConnectTetheringRequest. | |
75 std::string connect_tethering_id = | |
76 pref_service_->GetString(prefs::kMostRecentConnectTetheringResponderId); | |
77 | |
78 // If an ID exists, insert it at the front of |prioritized_ids|. | |
79 if (!connect_tethering_id.empty()) { | |
80 prioritized_ids->Insert( | |
81 0, base::MakeUnique<base::Value>(connect_tethering_id)); | |
82 } | 37 } |
83 | 38 |
84 // Iterate from the last stored ID to the first stored ID. This ensures that | 39 // Iterate from the last stored ID to the first stored ID. This ensures that |
85 // the items at the front of the list end up in the front of the prioritized | 40 // the items at the front of the list end up in the front of the prioritized |
86 // |remote_devices| vector. | 41 // |remote_devices| vector. |
87 for (size_t i = prioritized_ids->GetSize(); i-- > 0;) { | 42 for (auto prioritized_it = prioritized_ids.rbegin(); |
88 base::Value* stored_id_value; | 43 prioritized_it != prioritized_ids.rend(); ++prioritized_it) { |
89 if (!prioritized_ids->Get(i, &stored_id_value)) { | |
90 continue; | |
91 } | |
92 | |
93 std::string stored_id; | |
94 if (!stored_id_value->GetAsString(&stored_id)) { | |
95 continue; | |
96 } | |
97 | |
98 // Iterate through |remote_devices| to see if a device exists with a | 44 // Iterate through |remote_devices| to see if a device exists with a |
99 // device ID of |stored_id|. If one exists, remove it from its previous | 45 // device ID of |stored_id|. If one exists, remove it from its previous |
100 // position in the list and add it at the front instead. | 46 // position in the list and add it at the front instead. |
101 for (auto it = remote_devices->begin(); it != remote_devices->end(); ++it) { | 47 for (auto remote_devices_it = remote_devices->begin(); |
102 if (it->GetDeviceId() != stored_id) { | 48 remote_devices_it != remote_devices->end(); ++remote_devices_it) { |
| 49 if (remote_devices_it->GetDeviceId() != *prioritized_it) { |
103 continue; | 50 continue; |
104 } | 51 } |
105 | 52 |
106 cryptauth::RemoteDevice device_to_move = *it; | 53 cryptauth::RemoteDevice device_to_move = *remote_devices_it; |
107 remote_devices->erase(it); | 54 remote_devices->erase(remote_devices_it); |
108 remote_devices->insert(remote_devices->begin(), device_to_move); | 55 remote_devices->insert(remote_devices->begin(), device_to_move); |
109 break; | 56 break; |
110 } | 57 } |
111 } | 58 } |
112 } | 59 } |
113 | 60 |
114 } // namespace tether | 61 } // namespace tether |
115 | 62 |
116 } // namespace chromeos | 63 } // namespace chromeos |
OLD | NEW |