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

Side by Side Diff: chromeos/components/tether/tether_host_response_recorder.cc

Issue 2852693004: [CrOS Tether] Create HostScanCache, which caches scan results and inserts them into the network sta… (Closed)
Patch Set: stevenjb@ comments. Created 3 years, 7 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 unified diff | Download patch
OLDNEW
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
29 void TetherHostResponseRecorder::RecordSuccessfulTetherAvailabilityResponse( 37 void TetherHostResponseRecorder::RecordSuccessfulTetherAvailabilityResponse(
30 const cryptauth::RemoteDevice& remote_device) { 38 const cryptauth::RemoteDevice& remote_device) {
31 AddRecentResponse(remote_device.GetDeviceId(), 39 AddRecentResponse(remote_device.GetDeviceId(),
32 prefs::kMostRecentTetherAvailablilityResponderIds); 40 prefs::kMostRecentTetherAvailablilityResponderIds);
33 } 41 }
34 42
35 std::vector<std::string> 43 std::vector<std::string>
36 TetherHostResponseRecorder::GetPreviouslyAvailableHostIds() const { 44 TetherHostResponseRecorder::GetPreviouslyAvailableHostIds() const {
37 return GetDeviceIdsForPref(prefs::kMostRecentTetherAvailablilityResponderIds); 45 return GetDeviceIdsForPref(prefs::kMostRecentTetherAvailablilityResponderIds);
38 } 46 }
39 47
40 void TetherHostResponseRecorder::RecordSuccessfulConnectTetheringResponse( 48 void TetherHostResponseRecorder::RecordSuccessfulConnectTetheringResponse(
41 const cryptauth::RemoteDevice& remote_device) { 49 const cryptauth::RemoteDevice& remote_device) {
42 AddRecentResponse(remote_device.GetDeviceId(), 50 if (AddRecentResponse(remote_device.GetDeviceId(),
43 prefs::kMostRecentConnectTetheringResponderIds); 51 prefs::kMostRecentConnectTetheringResponderIds)) {
52 NotifyObserversPreviouslyConnectedHostIdsChanged();
53 }
44 } 54 }
45 55
46 std::vector<std::string> 56 std::vector<std::string>
47 TetherHostResponseRecorder::GetPreviouslyConnectedHostIds() const { 57 TetherHostResponseRecorder::GetPreviouslyConnectedHostIds() const {
48 return GetDeviceIdsForPref(prefs::kMostRecentConnectTetheringResponderIds); 58 return GetDeviceIdsForPref(prefs::kMostRecentConnectTetheringResponderIds);
49 } 59 }
50 60
51 void TetherHostResponseRecorder::AddRecentResponse( 61 void TetherHostResponseRecorder::
62 NotifyObserversPreviouslyConnectedHostIdsChanged() {
63 for (Observer& observer : observer_list_) {
64 observer.OnPreviouslyConnectedHostIdsChanged();
65 }
66 }
67
68 bool TetherHostResponseRecorder::AddRecentResponse(
52 const std::string& device_id, 69 const std::string& device_id,
53 const std::string& pref_name) { 70 const std::string& pref_name) {
54 const base::ListValue* ids = pref_service_->GetList(pref_name); 71 const base::ListValue* ids = pref_service_->GetList(pref_name);
55 72
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
56 // Create a mutable copy of the stored IDs, or create one if it has yet to be 81 // Create a mutable copy of the stored IDs, or create one if it has yet to be
57 // stored. 82 // stored.
58 std::unique_ptr<base::ListValue> updated_ids = 83 std::unique_ptr<base::ListValue> updated_ids =
59 ids ? ids->CreateDeepCopy() : base::MakeUnique<base::ListValue>(); 84 ids ? ids->CreateDeepCopy() : base::MakeUnique<base::ListValue>();
60 85
61 // Remove the device ID if it was already present in the list. 86 // Remove the device ID if it was already present in the list.
62 std::unique_ptr<base::Value> device_id_value = 87 std::unique_ptr<base::Value> device_id_value =
63 base::MakeUnique<base::Value>(device_id); 88 base::MakeUnique<base::Value>(device_id);
64 updated_ids->Remove(*device_id_value, nullptr); 89 updated_ids->Remove(*device_id_value, nullptr);
65 90
66 // Add the device ID to the front of the queue. 91 // Add the device ID to the front of the queue.
67 updated_ids->Insert(0, std::move(device_id_value)); 92 updated_ids->Insert(0, std::move(device_id_value));
68 93
69 // Store the updated list back in |pref_service_|. 94 // Store the updated list back in |pref_service_|.
70 pref_service_->Set(pref_name, *updated_ids); 95 pref_service_->Set(pref_name, *updated_ids);
96
97 return true;
71 } 98 }
72 99
73 std::vector<std::string> TetherHostResponseRecorder::GetDeviceIdsForPref( 100 std::vector<std::string> TetherHostResponseRecorder::GetDeviceIdsForPref(
74 const std::string& pref_name) const { 101 const std::string& pref_name) const {
75 std::vector<std::string> device_ids; 102 std::vector<std::string> device_ids;
76 103
77 const base::ListValue* ids = pref_service_->GetList(pref_name); 104 const base::ListValue* ids = pref_service_->GetList(pref_name);
78 if (!ids) 105 if (!ids)
79 return device_ids; 106 return device_ids;
80 107
81 for (auto it = ids->begin(); it != ids->end(); ++it) { 108 for (auto it = ids->begin(); it != ids->end(); ++it) {
82 std::string device_id; 109 std::string device_id;
83 bool success = it->GetAsString(&device_id); 110 bool success = it->GetAsString(&device_id);
84 if (success) 111 if (success)
85 device_ids.push_back(device_id); 112 device_ids.push_back(device_id);
86 } 113 }
87 114
88 return device_ids; 115 return device_ids;
89 } 116 }
90 117
91 } // namespace tether 118 } // namespace tether
92 119
93 } // namespace chromeos 120 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698