Chromium Code Reviews| 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 size_t unused_index; | |
| 45 updated_ids->Remove(*device_id_value, &unused_index); | |
|
Ryan Hansberry
2017/02/24 16:00:17
I believe you can instead pass null as the second
Kyle Horimoto
2017/02/24 18:02:58
Done.
| |
| 46 | |
| 47 // Add the device ID to the front of the queue. | |
| 48 updated_ids->Insert(0, std::move(device_id_value)); | |
| 49 | |
| 50 // Store the updated list back in |pref_service_|. | |
| 51 pref_service_->Set(prefs::kMostRecentTetherAvailablilityResponderIds, | |
| 52 *updated_ids); | |
| 53 } | |
| 54 | |
| 55 void HostScanDevicePrioritizer::RecordSuccessfulConnectTetheringResponse( | |
| 56 const cryptauth::RemoteDevice& remote_device) { | |
| 57 pref_service_->Set(prefs::kMostRecentConnectTetheringResponderId, | |
| 58 base::Value(remote_device.GetDeviceId())); | |
|
Ryan Hansberry
2017/02/24 16:00:17
Sanity check: this is UTF-8 right? i.e., safe to p
Kyle Horimoto
2017/02/24 18:02:59
Yep - it's a base-64 string.
| |
| 59 } | |
| 60 | |
| 61 void HostScanDevicePrioritizer::PrioritizeHostScanDeviceOrder( | |
| 62 std::vector<cryptauth::RemoteDevice>& remote_devices) const { | |
| 63 // Fetch the stored IDs associated with the devices which most recently sent | |
| 64 // TetherAvailabilityResponses. | |
| 65 const base::ListValue* tether_availability_ids = | |
| 66 pref_service_->GetList(prefs::kMostRecentTetherAvailablilityResponderIds); | |
| 67 | |
| 68 // Create a mutable copy of the stored IDs, or create one if it has yet to be | |
| 69 // stored. | |
| 70 std::unique_ptr<base::ListValue> prioritized_ids = | |
| 71 tether_availability_ids ? tether_availability_ids->CreateDeepCopy() | |
| 72 : base::MakeUnique<base::ListValue>(); | |
| 73 | |
| 74 // Now, fetch the ID associated with the device which most recently sent a | |
| 75 // ConnectTetheringRequest. | |
| 76 std::string connect_tethering_id = | |
| 77 pref_service_->GetString(prefs::kMostRecentConnectTetheringResponderId); | |
| 78 | |
| 79 // If an ID exists, insert it at the front of |prioritized_ids|. | |
| 80 if (connect_tethering_id != "") { | |
|
Ryan Hansberry
2017/02/24 16:00:17
Use empty() instead.
Kyle Horimoto
2017/02/24 18:02:58
Done.
| |
| 81 prioritized_ids->Insert( | |
| 82 0, base::MakeUnique<base::Value>(connect_tethering_id)); | |
| 83 } | |
| 84 | |
| 85 // Iterate from the last stored ID to the first stored ID. This ensures that | |
| 86 // the items at the front of the list end up in the front of the prioritized | |
| 87 // |remote_devices| vector. | |
| 88 for (size_t i = prioritized_ids->GetSize(); i-- > 0;) { | |
|
Ryan Hansberry
2017/02/24 16:00:17
Rearrange to '...; i >= 0; i--)'
Kyle Horimoto
2017/02/24 18:02:58
That doesn't work since size_t is an unsigned valu
| |
| 89 base::Value* stored_id_value; | |
| 90 if (prioritized_ids->Get(i, &stored_id_value)) { | |
|
Ryan Hansberry
2017/02/24 16:00:17
Instead of all these nested if statements, use con
Kyle Horimoto
2017/02/24 18:02:58
Done.
| |
| 91 std::string stored_id; | |
| 92 if (stored_id_value->GetAsString(&stored_id)) { | |
| 93 // Iterate through |remote_devices| to see if a device exists with a | |
| 94 // device ID of |stored_id|. If one exists, remove it from its previous | |
| 95 // position in the list and add it at the front instead. | |
| 96 for (auto it = remote_devices.begin(); it != remote_devices.end(); | |
| 97 ++it) { | |
| 98 if (it->GetDeviceId() == stored_id) { | |
| 99 cryptauth::RemoteDevice device_to_move = *it; | |
| 100 remote_devices.erase(it); | |
| 101 remote_devices.insert(remote_devices.begin(), device_to_move); | |
| 102 break; | |
| 103 } | |
| 104 } | |
| 105 } | |
| 106 } | |
| 107 } | |
| 108 } | |
| 109 | |
| 110 } // namespace tether | |
| 111 | |
| 112 } // namespace chromeos | |
| OLD | NEW |