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/tether_host_response_recorder.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 TetherHostResponseRecorder::RegisterPrefs(PrefRegistrySimple* registry) { |
| 19 registry->RegisterListPref(prefs::kMostRecentTetherAvailablilityResponderIds); |
| 20 registry->RegisterListPref(prefs::kMostRecentConnectTetheringResponderIds); |
| 21 } |
| 22 |
| 23 TetherHostResponseRecorder::TetherHostResponseRecorder( |
| 24 PrefService* pref_service) |
| 25 : pref_service_(pref_service) {} |
| 26 |
| 27 TetherHostResponseRecorder::~TetherHostResponseRecorder() {} |
| 28 |
| 29 void TetherHostResponseRecorder::RecordSuccessfulTetherAvailabilityResponse( |
| 30 const cryptauth::RemoteDevice& remote_device) { |
| 31 AddRecentResponse(remote_device.GetDeviceId(), |
| 32 prefs::kMostRecentTetherAvailablilityResponderIds); |
| 33 } |
| 34 |
| 35 std::vector<std::string> |
| 36 TetherHostResponseRecorder::GetPreviouslyAvailableHostIds() const { |
| 37 return GetDeviceIdsForPref(prefs::kMostRecentTetherAvailablilityResponderIds); |
| 38 } |
| 39 |
| 40 void TetherHostResponseRecorder::RecordSuccessfulConnectTetheringResponse( |
| 41 const cryptauth::RemoteDevice& remote_device) { |
| 42 AddRecentResponse(remote_device.GetDeviceId(), |
| 43 prefs::kMostRecentConnectTetheringResponderIds); |
| 44 } |
| 45 |
| 46 std::vector<std::string> |
| 47 TetherHostResponseRecorder::GetPreviouslyConnectedHostIds() const { |
| 48 return GetDeviceIdsForPref(prefs::kMostRecentConnectTetheringResponderIds); |
| 49 } |
| 50 |
| 51 void TetherHostResponseRecorder::AddRecentResponse( |
| 52 const std::string& device_id, |
| 53 const std::string& pref_name) { |
| 54 const base::ListValue* ids = pref_service_->GetList(pref_name); |
| 55 |
| 56 // Create a mutable copy of the stored IDs, or create one if it has yet to be |
| 57 // stored. |
| 58 std::unique_ptr<base::ListValue> updated_ids = |
| 59 ids ? ids->CreateDeepCopy() : base::MakeUnique<base::ListValue>(); |
| 60 |
| 61 // Remove the device ID if it was already present in the list. |
| 62 std::unique_ptr<base::Value> device_id_value = |
| 63 base::MakeUnique<base::Value>(device_id); |
| 64 updated_ids->Remove(*device_id_value, nullptr); |
| 65 |
| 66 // Add the device ID to the front of the queue. |
| 67 updated_ids->Insert(0, std::move(device_id_value)); |
| 68 |
| 69 // Store the updated list back in |pref_service_|. |
| 70 pref_service_->Set(pref_name, *updated_ids); |
| 71 } |
| 72 |
| 73 std::vector<std::string> TetherHostResponseRecorder::GetDeviceIdsForPref( |
| 74 const std::string& pref_name) const { |
| 75 std::vector<std::string> device_ids; |
| 76 |
| 77 const base::ListValue* ids = pref_service_->GetList(pref_name); |
| 78 if (!ids) |
| 79 return device_ids; |
| 80 |
| 81 for (auto it = ids->begin(); it != ids->end(); ++it) { |
| 82 std::string device_id; |
| 83 bool success = it->GetAsString(&device_id); |
| 84 if (success) |
| 85 device_ids.push_back(device_id); |
| 86 } |
| 87 |
| 88 return device_ids; |
| 89 } |
| 90 |
| 91 } // namespace tether |
| 92 |
| 93 } // namespace chromeos |
OLD | NEW |