| 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 #ifndef CHROMEOS_COMPONENTS_TETHER_TETHER_HOST_RESPONSE_RECORDER_H_ | 5 #ifndef CHROMEOS_COMPONENTS_TETHER_TETHER_HOST_RESPONSE_RECORDER_H_ |
| 6 #define CHROMEOS_COMPONENTS_TETHER_TETHER_HOST_RESPONSE_RECORDER_H_ | 6 #define CHROMEOS_COMPONENTS_TETHER_TETHER_HOST_RESPONSE_RECORDER_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "base/observer_list.h" | |
| 13 #include "components/cryptauth/remote_device.h" | 12 #include "components/cryptauth/remote_device.h" |
| 14 | 13 |
| 15 class PrefService; | 14 class PrefService; |
| 16 class PrefRegistrySimple; | 15 class PrefRegistrySimple; |
| 17 | 16 |
| 18 namespace chromeos { | 17 namespace chromeos { |
| 19 | 18 |
| 20 namespace tether { | 19 namespace tether { |
| 21 | 20 |
| 22 // Records responses from tether hosts in user prefs, which persists these | 21 // Records responses from tether hosts in user prefs, which persists these |
| 23 // responses between reboots of the device. When a TetherAvailabilityResponse or | 22 // responses between reboots of the device. When a TetherAvailabilityResponse or |
| 24 // ConnectTetheringResponse is received, it should be recorded using this class. | 23 // ConnectTetheringResponse is received, it should be recorded using this class. |
| 25 // Responses can be retrieved at a later time via getter methods. | 24 // Responses can be retrieved at a later time via getter methods. |
| 26 class TetherHostResponseRecorder { | 25 class TetherHostResponseRecorder { |
| 27 public: | 26 public: |
| 28 class Observer { | 27 // Note: The PrefService* passed here must be created using the same registry |
| 29 public: | 28 // passed to RegisterPrefs(). |
| 30 virtual void OnPreviouslyConnectedHostIdsChanged() = 0; | 29 TetherHostResponseRecorder(PrefService* pref_service); |
| 31 }; | 30 virtual ~TetherHostResponseRecorder(); |
| 32 | 31 |
| 33 // Registers the prefs used by this class to |registry|. Must be called before | 32 // Registers the prefs used by this class to |registry|. Must be called before |
| 34 // this class is utilized. | 33 // this class is utilized. |
| 35 static void RegisterPrefs(PrefRegistrySimple* registry); | 34 static void RegisterPrefs(PrefRegistrySimple* registry); |
| 36 | 35 |
| 37 // Note: The PrefService* passed here must be created using the same registry | |
| 38 // passed to RegisterPrefs(). | |
| 39 explicit TetherHostResponseRecorder(PrefService* pref_service); | |
| 40 virtual ~TetherHostResponseRecorder(); | |
| 41 | |
| 42 void AddObserver(Observer* observer); | |
| 43 void RemoveObserver(Observer* observer); | |
| 44 | |
| 45 // Records a TetherAvailabilityResponse. This function should be called each | 36 // Records a TetherAvailabilityResponse. This function should be called each |
| 46 // time that a response is received from a potential host, even if a | 37 // time that a response is received from a potential host, even if a |
| 47 // connection is not started. | 38 // connection is not started. |
| 48 virtual void RecordSuccessfulTetherAvailabilityResponse( | 39 virtual void RecordSuccessfulTetherAvailabilityResponse( |
| 49 const cryptauth::RemoteDevice& remote_device); | 40 const cryptauth::RemoteDevice& remote_device); |
| 50 | 41 |
| 51 // Gets device IDs corresponding to hosts which have sent | 42 // Gets device IDs corresponding to hosts which have sent |
| 52 // TetherAvailabilityResponses with a response code indicating that tethering | 43 // TetherAvailabilityResponses with a response code indicating that tethering |
| 53 // is available. The list is sorted; the IDs of the devices which have | 44 // is available. The list is sorted; the IDs of the devices which have |
| 54 // responded most recently are at the front of the list and the IDs of the | 45 // responded most recently are at the front of the list and the IDs of the |
| 55 // devices which have responded least recently are at the end of the list. | 46 // devices which have responded least recently are at the end of the list. |
| 56 virtual std::vector<std::string> GetPreviouslyAvailableHostIds() const; | 47 virtual std::vector<std::string> GetPreviouslyAvailableHostIds() const; |
| 57 | 48 |
| 58 // Records a ConnectTetheringResponse. This function should be called each | 49 // Records a ConnectTetheringResponse. This function should be called each |
| 59 // time that a response is received from a host. | 50 // time that a response is received from a host. |
| 60 virtual void RecordSuccessfulConnectTetheringResponse( | 51 virtual void RecordSuccessfulConnectTetheringResponse( |
| 61 const cryptauth::RemoteDevice& remote_device); | 52 const cryptauth::RemoteDevice& remote_device); |
| 62 | 53 |
| 63 // Gets device IDs corresponding to hosts which have sent | 54 // Gets device IDs corresponding to hosts which have sent |
| 64 // ConnectTetheringResponses with a response code indicating that they have | 55 // ConnectTetheringResponses with a response code indicating that they have |
| 65 // successfully turned on their Wi-Fi hotspots. The list is sorted; the IDs of | 56 // successfully turned on their Wi-Fi hotspots. The list is sorted; the IDs of |
| 66 // the devices which have responded most recently are at the front of the list | 57 // the devices which have responded most recently are at the front of the list |
| 67 // and the IDs of the devices which have responded least recently are at the | 58 // and the IDs of the devices which have responded least recently are at the |
| 68 // end of the list. | 59 // end of the list. |
| 69 virtual std::vector<std::string> GetPreviouslyConnectedHostIds() const; | 60 virtual std::vector<std::string> GetPreviouslyConnectedHostIds() const; |
| 70 | 61 |
| 71 private: | 62 private: |
| 72 friend class HostScanCacheTest; | 63 void AddRecentResponse(const std::string& device_id, |
| 73 | |
| 74 void NotifyObserversPreviouslyConnectedHostIdsChanged(); | |
| 75 | |
| 76 // Returns whether the list was changed due to adding the response. | |
| 77 bool AddRecentResponse(const std::string& device_id, | |
| 78 const std::string& pref_name); | 64 const std::string& pref_name); |
| 79 std::vector<std::string> GetDeviceIdsForPref( | 65 std::vector<std::string> GetDeviceIdsForPref( |
| 80 const std::string& pref_name) const; | 66 const std::string& pref_name) const; |
| 81 | 67 |
| 82 PrefService* pref_service_; | 68 PrefService* pref_service_; |
| 83 base::ObserverList<Observer> observer_list_; | |
| 84 | 69 |
| 85 DISALLOW_COPY_AND_ASSIGN(TetherHostResponseRecorder); | 70 DISALLOW_COPY_AND_ASSIGN(TetherHostResponseRecorder); |
| 86 }; | 71 }; |
| 87 | 72 |
| 88 } // namespace tether | 73 } // namespace tether |
| 89 | 74 |
| 90 } // namespace chromeos | 75 } // namespace chromeos |
| 91 | 76 |
| 92 #endif // CHROMEOS_COMPONENTS_TETHER_TETHER_HOST_RESPONSE_RECORDER_H_ | 77 #endif // CHROMEOS_COMPONENTS_TETHER_TETHER_HOST_RESPONSE_RECORDER_H_ |
| OLD | NEW |