| 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 "chromeos/components/tether/tether_host_response_recorder.h" | |
| 11 #include "components/prefs/pref_registry_simple.h" | |
| 12 #include "components/prefs/pref_service.h" | |
| 13 | |
| 14 namespace chromeos { | |
| 15 | |
| 16 namespace tether { | |
| 17 | |
| 18 HostScanDevicePrioritizer::HostScanDevicePrioritizer( | |
| 19 TetherHostResponseRecorder* tether_host_response_recorder) | |
| 20 : tether_host_response_recorder_(tether_host_response_recorder) {} | |
| 21 | |
| 22 HostScanDevicePrioritizer::~HostScanDevicePrioritizer() {} | |
| 23 | |
| 24 void HostScanDevicePrioritizer::SortByHostScanOrder( | |
| 25 std::vector<cryptauth::RemoteDevice>* remote_devices) const { | |
| 26 // First, fetch the hosts which have previously responded. | |
| 27 std::vector<std::string> prioritized_ids = | |
| 28 tether_host_response_recorder_->GetPreviouslyAvailableHostIds(); | |
| 29 | |
| 30 std::vector<std::string> previously_connectable_host_ids = | |
| 31 tether_host_response_recorder_->GetPreviouslyConnectedHostIds(); | |
| 32 if (!previously_connectable_host_ids.empty()) { | |
| 33 // If there is a most-recently connectable host, insert it at the front of | |
| 34 // the list. | |
| 35 prioritized_ids.insert(prioritized_ids.begin(), | |
| 36 previously_connectable_host_ids[0]); | |
| 37 } | |
| 38 | |
| 39 // Iterate from the last stored ID to the first stored ID. This ensures that | |
| 40 // the items at the front of the list end up in the front of the prioritized | |
| 41 // |remote_devices| vector. | |
| 42 for (auto prioritized_it = prioritized_ids.rbegin(); | |
| 43 prioritized_it != prioritized_ids.rend(); ++prioritized_it) { | |
| 44 // Iterate through |remote_devices| to see if a device exists with a | |
| 45 // device ID of |stored_id|. If one exists, remove it from its previous | |
| 46 // position in the list and add it at the front instead. | |
| 47 for (auto remote_devices_it = remote_devices->begin(); | |
| 48 remote_devices_it != remote_devices->end(); ++remote_devices_it) { | |
| 49 if (remote_devices_it->GetDeviceId() != *prioritized_it) { | |
| 50 continue; | |
| 51 } | |
| 52 | |
| 53 cryptauth::RemoteDevice device_to_move = *remote_devices_it; | |
| 54 remote_devices->erase(remote_devices_it); | |
| 55 remote_devices->insert(remote_devices->begin(), device_to_move); | |
| 56 break; | |
| 57 } | |
| 58 } | |
| 59 } | |
| 60 | |
| 61 } // namespace tether | |
| 62 | |
| 63 } // namespace chromeos | |
| OLD | NEW |