| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chromeos/components/tether/tether_host_response_recorder.h" | 5 #include "chromeos/components/tether/tether_host_response_recorder.h" |
| 6 | 6 |
| 7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
| 8 #include "base/values.h" | 8 #include "base/values.h" |
| 9 #include "chromeos/components/tether/pref_names.h" | 9 #include "chromeos/components/tether/pref_names.h" |
| 10 #include "components/prefs/pref_registry_simple.h" | 10 #include "components/prefs/pref_registry_simple.h" |
| 11 #include "components/prefs/pref_service.h" | 11 #include "components/prefs/pref_service.h" |
| 12 | 12 |
| 13 namespace chromeos { | 13 namespace chromeos { |
| 14 | 14 |
| 15 namespace tether { | 15 namespace tether { |
| 16 | 16 |
| 17 // static | 17 // static |
| 18 void TetherHostResponseRecorder::RegisterPrefs(PrefRegistrySimple* registry) { | 18 void TetherHostResponseRecorder::RegisterPrefs(PrefRegistrySimple* registry) { |
| 19 registry->RegisterListPref(prefs::kMostRecentTetherAvailablilityResponderIds); | 19 registry->RegisterListPref(prefs::kMostRecentTetherAvailablilityResponderIds); |
| 20 registry->RegisterListPref(prefs::kMostRecentConnectTetheringResponderIds); | 20 registry->RegisterListPref(prefs::kMostRecentConnectTetheringResponderIds); |
| 21 } | 21 } |
| 22 | 22 |
| 23 TetherHostResponseRecorder::TetherHostResponseRecorder( | 23 TetherHostResponseRecorder::TetherHostResponseRecorder( |
| 24 PrefService* pref_service) | 24 PrefService* pref_service) |
| 25 : pref_service_(pref_service) {} | 25 : pref_service_(pref_service) {} |
| 26 | 26 |
| 27 TetherHostResponseRecorder::~TetherHostResponseRecorder() {} | 27 TetherHostResponseRecorder::~TetherHostResponseRecorder() {} |
| 28 | 28 |
| 29 void TetherHostResponseRecorder::AddObserver(Observer* observer) { | |
| 30 observer_list_.AddObserver(observer); | |
| 31 } | |
| 32 | |
| 33 void TetherHostResponseRecorder::RemoveObserver(Observer* observer) { | |
| 34 observer_list_.RemoveObserver(observer); | |
| 35 } | |
| 36 | |
| 37 void TetherHostResponseRecorder::RecordSuccessfulTetherAvailabilityResponse( | 29 void TetherHostResponseRecorder::RecordSuccessfulTetherAvailabilityResponse( |
| 38 const cryptauth::RemoteDevice& remote_device) { | 30 const cryptauth::RemoteDevice& remote_device) { |
| 39 AddRecentResponse(remote_device.GetDeviceId(), | 31 AddRecentResponse(remote_device.GetDeviceId(), |
| 40 prefs::kMostRecentTetherAvailablilityResponderIds); | 32 prefs::kMostRecentTetherAvailablilityResponderIds); |
| 41 } | 33 } |
| 42 | 34 |
| 43 std::vector<std::string> | 35 std::vector<std::string> |
| 44 TetherHostResponseRecorder::GetPreviouslyAvailableHostIds() const { | 36 TetherHostResponseRecorder::GetPreviouslyAvailableHostIds() const { |
| 45 return GetDeviceIdsForPref(prefs::kMostRecentTetherAvailablilityResponderIds); | 37 return GetDeviceIdsForPref(prefs::kMostRecentTetherAvailablilityResponderIds); |
| 46 } | 38 } |
| 47 | 39 |
| 48 void TetherHostResponseRecorder::RecordSuccessfulConnectTetheringResponse( | 40 void TetherHostResponseRecorder::RecordSuccessfulConnectTetheringResponse( |
| 49 const cryptauth::RemoteDevice& remote_device) { | 41 const cryptauth::RemoteDevice& remote_device) { |
| 50 if (AddRecentResponse(remote_device.GetDeviceId(), | 42 AddRecentResponse(remote_device.GetDeviceId(), |
| 51 prefs::kMostRecentConnectTetheringResponderIds)) { | 43 prefs::kMostRecentConnectTetheringResponderIds); |
| 52 NotifyObserversPreviouslyConnectedHostIdsChanged(); | |
| 53 } | |
| 54 } | 44 } |
| 55 | 45 |
| 56 std::vector<std::string> | 46 std::vector<std::string> |
| 57 TetherHostResponseRecorder::GetPreviouslyConnectedHostIds() const { | 47 TetherHostResponseRecorder::GetPreviouslyConnectedHostIds() const { |
| 58 return GetDeviceIdsForPref(prefs::kMostRecentConnectTetheringResponderIds); | 48 return GetDeviceIdsForPref(prefs::kMostRecentConnectTetheringResponderIds); |
| 59 } | 49 } |
| 60 | 50 |
| 61 void TetherHostResponseRecorder:: | 51 void TetherHostResponseRecorder::AddRecentResponse( |
| 62 NotifyObserversPreviouslyConnectedHostIdsChanged() { | |
| 63 for (Observer& observer : observer_list_) { | |
| 64 observer.OnPreviouslyConnectedHostIdsChanged(); | |
| 65 } | |
| 66 } | |
| 67 | |
| 68 bool TetherHostResponseRecorder::AddRecentResponse( | |
| 69 const std::string& device_id, | 52 const std::string& device_id, |
| 70 const std::string& pref_name) { | 53 const std::string& pref_name) { |
| 71 const base::ListValue* ids = pref_service_->GetList(pref_name); | 54 const base::ListValue* ids = pref_service_->GetList(pref_name); |
| 72 | 55 |
| 73 std::string first_device_id_in_list; | |
| 74 ids->GetString(0u, &first_device_id_in_list); | |
| 75 if (device_id == first_device_id_in_list) { | |
| 76 // If the device ID that is being inserted is already at the front of the | |
| 77 // list, there is nothing to do. | |
| 78 return false; | |
| 79 } | |
| 80 | |
| 81 // Create a mutable copy of the stored IDs, or create one if it has yet to be | 56 // Create a mutable copy of the stored IDs, or create one if it has yet to be |
| 82 // stored. | 57 // stored. |
| 83 std::unique_ptr<base::ListValue> updated_ids = | 58 std::unique_ptr<base::ListValue> updated_ids = |
| 84 ids ? ids->CreateDeepCopy() : base::MakeUnique<base::ListValue>(); | 59 ids ? ids->CreateDeepCopy() : base::MakeUnique<base::ListValue>(); |
| 85 | 60 |
| 86 // Remove the device ID if it was already present in the list. | 61 // Remove the device ID if it was already present in the list. |
| 87 std::unique_ptr<base::Value> device_id_value = | 62 std::unique_ptr<base::Value> device_id_value = |
| 88 base::MakeUnique<base::Value>(device_id); | 63 base::MakeUnique<base::Value>(device_id); |
| 89 updated_ids->Remove(*device_id_value, nullptr); | 64 updated_ids->Remove(*device_id_value, nullptr); |
| 90 | 65 |
| 91 // Add the device ID to the front of the queue. | 66 // Add the device ID to the front of the queue. |
| 92 updated_ids->Insert(0, std::move(device_id_value)); | 67 updated_ids->Insert(0, std::move(device_id_value)); |
| 93 | 68 |
| 94 // Store the updated list back in |pref_service_|. | 69 // Store the updated list back in |pref_service_|. |
| 95 pref_service_->Set(pref_name, *updated_ids); | 70 pref_service_->Set(pref_name, *updated_ids); |
| 96 | |
| 97 return true; | |
| 98 } | 71 } |
| 99 | 72 |
| 100 std::vector<std::string> TetherHostResponseRecorder::GetDeviceIdsForPref( | 73 std::vector<std::string> TetherHostResponseRecorder::GetDeviceIdsForPref( |
| 101 const std::string& pref_name) const { | 74 const std::string& pref_name) const { |
| 102 std::vector<std::string> device_ids; | 75 std::vector<std::string> device_ids; |
| 103 | 76 |
| 104 const base::ListValue* ids = pref_service_->GetList(pref_name); | 77 const base::ListValue* ids = pref_service_->GetList(pref_name); |
| 105 if (!ids) | 78 if (!ids) |
| 106 return device_ids; | 79 return device_ids; |
| 107 | 80 |
| 108 for (auto it = ids->begin(); it != ids->end(); ++it) { | 81 for (auto it = ids->begin(); it != ids->end(); ++it) { |
| 109 std::string device_id; | 82 std::string device_id; |
| 110 bool success = it->GetAsString(&device_id); | 83 bool success = it->GetAsString(&device_id); |
| 111 if (success) | 84 if (success) |
| 112 device_ids.push_back(device_id); | 85 device_ids.push_back(device_id); |
| 113 } | 86 } |
| 114 | 87 |
| 115 return device_ids; | 88 return device_ids; |
| 116 } | 89 } |
| 117 | 90 |
| 118 } // namespace tether | 91 } // namespace tether |
| 119 | 92 |
| 120 } // namespace chromeos | 93 } // namespace chromeos |
| OLD | NEW |