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

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: Rebased. 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 bool changed =
43 prefs::kMostRecentConnectTetheringResponderIds); 51 AddRecentResponse(remote_device.GetDeviceId(),
52 prefs::kMostRecentConnectTetheringResponderIds);
53
54 if (changed) {
stevenjb 2017/05/01 18:26:12 local not needed
Kyle Horimoto 2017/05/01 21:14:44 Done.
55 NotifyObserversPreviouslyConnectedHostIdsChanged();
56 }
44 } 57 }
45 58
46 std::vector<std::string> 59 std::vector<std::string>
47 TetherHostResponseRecorder::GetPreviouslyConnectedHostIds() const { 60 TetherHostResponseRecorder::GetPreviouslyConnectedHostIds() const {
48 return GetDeviceIdsForPref(prefs::kMostRecentConnectTetheringResponderIds); 61 return GetDeviceIdsForPref(prefs::kMostRecentConnectTetheringResponderIds);
49 } 62 }
50 63
51 void TetherHostResponseRecorder::AddRecentResponse( 64 void TetherHostResponseRecorder::
65 NotifyObserversPreviouslyConnectedHostIdsChanged() {
66 for (Observer& observer : observer_list_) {
67 observer.OnPreviouslyConnectedHostIdsChanged();
68 }
69 }
70
71 bool TetherHostResponseRecorder::AddRecentResponse(
52 const std::string& device_id, 72 const std::string& device_id,
53 const std::string& pref_name) { 73 const std::string& pref_name) {
54 const base::ListValue* ids = pref_service_->GetList(pref_name); 74 const base::ListValue* ids = pref_service_->GetList(pref_name);
55 75
76 std::string first_device_id_in_list;
77 ids->GetString(0u, &first_device_id_in_list);
78 if (device_id == first_device_id_in_list) {
79 // If the device ID that is being inserted is already at the front of the
80 // list, there is nothing to do.
81 return false;
82 }
83
56 // Create a mutable copy of the stored IDs, or create one if it has yet to be 84 // Create a mutable copy of the stored IDs, or create one if it has yet to be
57 // stored. 85 // stored.
58 std::unique_ptr<base::ListValue> updated_ids = 86 std::unique_ptr<base::ListValue> updated_ids =
59 ids ? ids->CreateDeepCopy() : base::MakeUnique<base::ListValue>(); 87 ids ? ids->CreateDeepCopy() : base::MakeUnique<base::ListValue>();
60 88
61 // Remove the device ID if it was already present in the list. 89 // Remove the device ID if it was already present in the list.
62 std::unique_ptr<base::Value> device_id_value = 90 std::unique_ptr<base::Value> device_id_value =
63 base::MakeUnique<base::Value>(device_id); 91 base::MakeUnique<base::Value>(device_id);
64 updated_ids->Remove(*device_id_value, nullptr); 92 updated_ids->Remove(*device_id_value, nullptr);
65 93
66 // Add the device ID to the front of the queue. 94 // Add the device ID to the front of the queue.
67 updated_ids->Insert(0, std::move(device_id_value)); 95 updated_ids->Insert(0, std::move(device_id_value));
68 96
69 // Store the updated list back in |pref_service_|. 97 // Store the updated list back in |pref_service_|.
70 pref_service_->Set(pref_name, *updated_ids); 98 pref_service_->Set(pref_name, *updated_ids);
99
100 return true;
71 } 101 }
72 102
73 std::vector<std::string> TetherHostResponseRecorder::GetDeviceIdsForPref( 103 std::vector<std::string> TetherHostResponseRecorder::GetDeviceIdsForPref(
74 const std::string& pref_name) const { 104 const std::string& pref_name) const {
75 std::vector<std::string> device_ids; 105 std::vector<std::string> device_ids;
76 106
77 const base::ListValue* ids = pref_service_->GetList(pref_name); 107 const base::ListValue* ids = pref_service_->GetList(pref_name);
78 if (!ids) 108 if (!ids)
79 return device_ids; 109 return device_ids;
80 110
81 for (auto it = ids->begin(); it != ids->end(); ++it) { 111 for (auto it = ids->begin(); it != ids->end(); ++it) {
82 std::string device_id; 112 std::string device_id;
83 bool success = it->GetAsString(&device_id); 113 bool success = it->GetAsString(&device_id);
84 if (success) 114 if (success)
85 device_ids.push_back(device_id); 115 device_ids.push_back(device_id);
86 } 116 }
87 117
88 return device_ids; 118 return device_ids;
89 } 119 }
90 120
91 } // namespace tether 121 } // namespace tether
92 122
93 } // namespace chromeos 123 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698