OLD | NEW |
---|---|
(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.h" | |
6 | |
7 #include "base/memory/ptr_util.h" | |
8 #include "base/values.h" | |
9 #include "chromeos/components/tether/pref_names.h" | |
10 #include "components/prefs/pref_registry_simple.h" | |
11 #include "components/prefs/pref_service.h" | |
12 | |
13 namespace chromeos { | |
14 | |
15 namespace tether { | |
16 | |
17 // static | |
18 void HostScanDevicePrioritizer::RegisterPrefs(PrefRegistrySimple* registry) { | |
19 registry->RegisterListPref(prefs::kMostRecentTetherAvailablilityResponderIds); | |
20 registry->RegisterStringPref(prefs::kMostRecentConnectTetheringResponderId, | |
21 ""); | |
22 } | |
23 | |
24 HostScanDevicePrioritizer::HostScanDevicePrioritizer(PrefService* pref_service) | |
25 : pref_service_(pref_service) {} | |
26 | |
27 HostScanDevicePrioritizer::~HostScanDevicePrioritizer() {} | |
28 | |
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::PrioritizeHostScanDeviceOrder( | |
61 std::vector<cryptauth::RemoteDevice>& remote_devices) const { | |
62 // Fetch the stored IDs associated with the devices which most recently sent | |
63 // TetherAvailabilityResponses. | |
64 const base::ListValue* tether_availability_ids = | |
65 pref_service_->GetList(prefs::kMostRecentTetherAvailablilityResponderIds); | |
66 | |
67 // Create a mutable copy of the stored IDs, or create one if it has yet to be | |
68 // stored. | |
69 std::unique_ptr<base::ListValue> prioritized_ids = | |
70 tether_availability_ids ? tether_availability_ids->CreateDeepCopy() | |
71 : base::MakeUnique<base::ListValue>(); | |
72 | |
73 // Now, fetch the ID associated with the device which most recently sent a | |
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 } | |
83 | |
84 // 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 | |
86 // |remote_devices| vector. | |
87 for (size_t i = prioritized_ids->GetSize(); i-- > 0;) { | |
Tim Song
2017/02/24 18:57:40
For readability. Move the decrement outside of the
Kyle Horimoto
2017/02/24 19:21:30
Unfortunately, that doesn't work since the loop wo
| |
88 base::Value* stored_id_value; | |
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 | |
99 // 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. | |
101 for (auto it = remote_devices.begin(); it != remote_devices.end(); ++it) { | |
102 if (it->GetDeviceId() != stored_id) { | |
103 continue; | |
104 } | |
105 | |
106 cryptauth::RemoteDevice device_to_move = *it; | |
107 remote_devices.erase(it); | |
108 remote_devices.insert(remote_devices.begin(), device_to_move); | |
109 break; | |
110 } | |
111 } | |
112 } | |
113 | |
114 } // namespace tether | |
115 | |
116 } // namespace chromeos | |
OLD | NEW |