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

Unified Diff: chromeos/components/tether/host_scan_device_prioritizer.cc

Issue 2844973002: [CrOS Tether] Create TetherHostResponseRecorder, which records ConnectTetheringResponses and Tether… (Closed)
Patch Set: Changed "Connectable" wording to "Connected". Created 3 years, 8 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 side-by-side diff with in-line comments
Download patch
Index: chromeos/components/tether/host_scan_device_prioritizer.cc
diff --git a/chromeos/components/tether/host_scan_device_prioritizer.cc b/chromeos/components/tether/host_scan_device_prioritizer.cc
index 13f2b506897ab6fe3e9fda9dcc590e3e4183ca00..e2bfb9ac421140fd86410557de6676b5d08e355f 100644
--- a/chromeos/components/tether/host_scan_device_prioritizer.cc
+++ b/chromeos/components/tether/host_scan_device_prioritizer.cc
@@ -7,6 +7,7 @@
#include "base/memory/ptr_util.h"
#include "base/values.h"
#include "chromeos/components/tether/pref_names.h"
+#include "chromeos/components/tether/tether_host_response_recorder.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
@@ -14,97 +15,43 @@ namespace chromeos {
namespace tether {
-// static
-void HostScanDevicePrioritizer::RegisterPrefs(PrefRegistrySimple* registry) {
- registry->RegisterListPref(prefs::kMostRecentTetherAvailablilityResponderIds);
- registry->RegisterStringPref(prefs::kMostRecentConnectTetheringResponderId,
- "");
-}
-
-HostScanDevicePrioritizer::HostScanDevicePrioritizer(PrefService* pref_service)
- : pref_service_(pref_service) {}
+HostScanDevicePrioritizer::HostScanDevicePrioritizer(
+ TetherHostResponseRecorder* tether_host_response_recorder)
+ : tether_host_response_recorder_(tether_host_response_recorder) {}
HostScanDevicePrioritizer::~HostScanDevicePrioritizer() {}
-void HostScanDevicePrioritizer::RecordSuccessfulTetherAvailabilityResponse(
- const cryptauth::RemoteDevice& remote_device) {
- std::string device_id = remote_device.GetDeviceId();
-
- const base::ListValue* ids =
- pref_service_->GetList(prefs::kMostRecentTetherAvailablilityResponderIds);
-
- // Create a mutable copy of the stored IDs, or create one if it has yet to be
- // stored.
- std::unique_ptr<base::ListValue> updated_ids =
- ids ? ids->CreateDeepCopy() : base::MakeUnique<base::ListValue>();
-
- // Remove the device ID if it was already present in the list.
- std::unique_ptr<base::Value> device_id_value =
- base::MakeUnique<base::Value>(device_id);
- updated_ids->Remove(*device_id_value, nullptr);
-
- // Add the device ID to the front of the queue.
- updated_ids->Insert(0, std::move(device_id_value));
-
- // Store the updated list back in |pref_service_|.
- pref_service_->Set(prefs::kMostRecentTetherAvailablilityResponderIds,
- *updated_ids);
-}
-
-void HostScanDevicePrioritizer::RecordSuccessfulConnectTetheringResponse(
- const cryptauth::RemoteDevice& remote_device) {
- pref_service_->Set(prefs::kMostRecentConnectTetheringResponderId,
- base::Value(remote_device.GetDeviceId()));
-}
-
void HostScanDevicePrioritizer::SortByHostScanOrder(
std::vector<cryptauth::RemoteDevice>* remote_devices) const {
- // Fetch the stored IDs associated with the devices which most recently sent
- // TetherAvailabilityResponses.
- const base::ListValue* tether_availability_ids =
- pref_service_->GetList(prefs::kMostRecentTetherAvailablilityResponderIds);
-
- // Create a mutable copy of the stored IDs, or create one if it has yet to be
- // stored.
- std::unique_ptr<base::ListValue> prioritized_ids =
- tether_availability_ids ? tether_availability_ids->CreateDeepCopy()
- : base::MakeUnique<base::ListValue>();
-
- // Now, fetch the ID associated with the device which most recently sent a
- // ConnectTetheringRequest.
- std::string connect_tethering_id =
- pref_service_->GetString(prefs::kMostRecentConnectTetheringResponderId);
-
- // If an ID exists, insert it at the front of |prioritized_ids|.
- if (!connect_tethering_id.empty()) {
- prioritized_ids->Insert(
- 0, base::MakeUnique<base::Value>(connect_tethering_id));
+ // First, fetch the hosts which have previously responded.
+ std::vector<std::string> prioritized_ids =
+ tether_host_response_recorder_->GetPreviouslyAvailableHostIds();
+
+ std::vector<std::string> previously_connectable_host_ids =
+ tether_host_response_recorder_->GetPreviouslyConnectedHostIds();
+ if (!previously_connectable_host_ids.empty()) {
+ // If there is a most-recently connectable host, insert it at the front of
+ // the list.
+ prioritized_ids.insert(prioritized_ids.begin(),
+ previously_connectable_host_ids[0]);
}
// Iterate from the last stored ID to the first stored ID. This ensures that
// the items at the front of the list end up in the front of the prioritized
// |remote_devices| vector.
- for (size_t i = prioritized_ids->GetSize(); i-- > 0;) {
- base::Value* stored_id_value;
- if (!prioritized_ids->Get(i, &stored_id_value)) {
- continue;
- }
-
- std::string stored_id;
- if (!stored_id_value->GetAsString(&stored_id)) {
- continue;
- }
-
+ for (auto prioritized_it = prioritized_ids.rbegin();
+ prioritized_it != prioritized_ids.rend(); ++prioritized_it) {
// Iterate through |remote_devices| to see if a device exists with a
// device ID of |stored_id|. If one exists, remove it from its previous
// position in the list and add it at the front instead.
- for (auto it = remote_devices->begin(); it != remote_devices->end(); ++it) {
- if (it->GetDeviceId() != stored_id) {
+ for (auto remote_devices_it = remote_devices->begin();
+ remote_devices_it != remote_devices->end(); ++remote_devices_it) {
+ if (remote_devices_it->GetDeviceId() != *prioritized_it) {
continue;
}
- cryptauth::RemoteDevice device_to_move = *it;
- remote_devices->erase(it);
+ cryptauth::RemoteDevice device_to_move = *remote_devices_it;
+ remote_devices->erase(remote_devices_it);
remote_devices->insert(remote_devices->begin(), device_to_move);
break;
}

Powered by Google App Engine
This is Rietveld 408576698