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_ACTIVE_HOST_H_ | 5 #ifndef CHROMEOS_COMPONENTS_TETHER_ACTIVE_HOST_H_ |
6 #define CHROMEOS_COMPONENTS_TETHER_ACTIVE_HOST_H_ | 6 #define CHROMEOS_COMPONENTS_TETHER_ACTIVE_HOST_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
| 13 #include "base/observer_list.h" |
13 | 14 |
14 class PrefRegistrySimple; | 15 class PrefRegistrySimple; |
15 class PrefService; | 16 class PrefService; |
16 | 17 |
17 namespace cryptauth { | 18 namespace cryptauth { |
18 struct RemoteDevice; | 19 struct RemoteDevice; |
19 } // namespace cryptauth | 20 } // namespace cryptauth |
20 | 21 |
21 namespace chromeos { | 22 namespace chromeos { |
22 | 23 |
23 namespace tether { | 24 namespace tether { |
24 | 25 |
25 class TetherHostFetcher; | 26 class TetherHostFetcher; |
26 | 27 |
27 // ActiveHost tracks metadata about the current connection to a tether host. | 28 // ActiveHost tracks metadata about the current connection to a tether host. |
28 // This data is persisted to user preferences. | 29 // This data is persisted to user preferences. |
29 class ActiveHost { | 30 class ActiveHost { |
30 public: | 31 public: |
31 // Enumeration used to describe the state of the active connection to a tether | 32 // Enumeration used to describe the state of the active connection to a tether |
32 // host. | 33 // host. |
33 enum class ActiveHostStatus { | 34 enum class ActiveHostStatus { |
34 DISCONNECTED = 0, | 35 DISCONNECTED = 0, |
35 CONNECTING = 1, | 36 CONNECTING = 1, |
36 CONNECTED = 2 | 37 CONNECTED = 2 |
37 }; | 38 }; |
38 | 39 |
| 40 class Observer { |
| 41 public: |
| 42 virtual void OnActiveHostChanged( |
| 43 ActiveHostStatus active_host_status, |
| 44 std::unique_ptr<cryptauth::RemoteDevice> active_host_device, |
| 45 const std::string& wifi_network_id) = 0; |
| 46 }; |
| 47 |
39 ActiveHost(TetherHostFetcher* tether_host_fetcher, PrefService* pref_service); | 48 ActiveHost(TetherHostFetcher* tether_host_fetcher, PrefService* pref_service); |
40 virtual ~ActiveHost(); | 49 virtual ~ActiveHost(); |
41 | 50 |
42 // Registers the prefs used by this class to the given |registry|. | 51 // Registers the prefs used by this class to the given |registry|. |
43 static void RegisterPrefs(PrefRegistrySimple* registry); | 52 static void RegisterPrefs(PrefRegistrySimple* registry); |
44 | 53 |
45 // Sets the active host to be no host at all (i.e., the local device is not | 54 // Sets the active host to be no host at all (i.e., the local device is not |
46 // connecting or connected to a tether host). | 55 // connecting or connected to a tether host). |
47 void SetActiveHostDisconnected(); | 56 virtual void SetActiveHostDisconnected(); |
48 | 57 |
49 // Sets the active host to be the device with ID |active_host_device_id| and | 58 // Sets the active host to be the device with ID |active_host_device_id| and |
50 // records that the there is an active attempt to connect to that host (i.e., | 59 // records that the there is an active attempt to connect to that host (i.e., |
51 // the host is not yet connected but it is in the process of connecting). | 60 // the host is not yet connected but it is in the process of connecting). |
52 void SetActiveHostConnecting(const std::string& active_host_device_id); | 61 virtual void SetActiveHostConnecting( |
| 62 const std::string& active_host_device_id); |
53 | 63 |
54 // Sets the active host to be the device with ID |active_host_device_id| and | 64 // Sets the active host to be the device with ID |active_host_device_id| and |
55 // that the local device is connected to that device on the mobile hotspot | 65 // that the local device is connected to that device on the mobile hotspot |
56 // with Wi-Fi network ID |wifi_network_id|. | 66 // with Wi-Fi network ID |wifi_network_id|. |
57 void SetActiveHostConnected(const std::string& active_host_device_id, | 67 virtual void SetActiveHostConnected(const std::string& active_host_device_id, |
58 const std::string& wifi_network_id); | 68 const std::string& wifi_network_id); |
59 | 69 |
60 // Gets the active host and associated metadata asynchronously. If | 70 // Gets the active host and associated metadata asynchronously. If |
61 // the active host status is... | 71 // the active host status is... |
62 // DISCONNECTED: The callback's |active_host| parameter will be nullptr | 72 // DISCONNECTED: The callback's |active_host| parameter will be nullptr |
63 // and |wifi_network_id| parameter will be "". | 73 // and |wifi_network_id| parameter will be "". |
64 // CONNECTING: The callback's |wifi_network_id| will be "". | 74 // CONNECTING: The callback's |wifi_network_id| will be "". |
65 // CONNECTED: All three parameter will be present. | 75 // CONNECTED: All three parameter will be present. |
66 using ActiveHostCallback = | 76 using ActiveHostCallback = |
67 base::Callback<void(ActiveHostStatus active_host_status, | 77 base::Callback<void(ActiveHostStatus active_host_status, |
68 std::unique_ptr<cryptauth::RemoteDevice> active_host, | 78 std::unique_ptr<cryptauth::RemoteDevice> active_host, |
69 const std::string& wifi_network_id)>; | 79 const std::string& wifi_network_id)>; |
70 void GetActiveHost(const ActiveHostCallback& active_host_callback); | 80 virtual void GetActiveHost(const ActiveHostCallback& active_host_callback); |
71 | 81 |
72 // Synchronous getter methods which do not return a full RemoteDevice object. | 82 // Synchronous getter methods which do not return a full RemoteDevice object. |
73 ActiveHostStatus GetActiveHostStatus() const; | 83 virtual ActiveHostStatus GetActiveHostStatus() const; |
74 const std::string GetActiveHostDeviceId() const; | 84 virtual std::string GetActiveHostDeviceId() const; |
75 const std::string GetWifiNetworkId() const; | 85 virtual std::string GetWifiNetworkId() const; |
| 86 |
| 87 void AddObserver(Observer* observer); |
| 88 void RemoveObserver(Observer* observer); |
| 89 |
| 90 protected: |
| 91 void SendActiveHostChangedUpdate( |
| 92 ActiveHostStatus active_host_status, |
| 93 std::unique_ptr<cryptauth::RemoteDevice> active_host, |
| 94 const std::string& wifi_network_id); |
76 | 95 |
77 private: | 96 private: |
78 void SetActiveHost(ActiveHostStatus active_host_status, | 97 void SetActiveHost(ActiveHostStatus active_host_status, |
79 const std::string& active_host_device_id, | 98 const std::string& active_host_device_id, |
80 const std::string& wifi_network_id); | 99 const std::string& wifi_network_id); |
81 | 100 |
82 void OnTetherHostFetched( | 101 void OnTetherHostFetched( |
83 const ActiveHostCallback& active_host_callback, | 102 const ActiveHostCallback& active_host_callback, |
84 std::unique_ptr<cryptauth::RemoteDevice> remote_device); | 103 std::unique_ptr<cryptauth::RemoteDevice> remote_device); |
85 | 104 |
86 TetherHostFetcher* tether_host_fetcher_; | 105 TetherHostFetcher* tether_host_fetcher_; |
87 PrefService* pref_service_; | 106 PrefService* pref_service_; |
88 | 107 |
| 108 base::ObserverList<Observer> observer_list_; |
| 109 |
89 base::WeakPtrFactory<ActiveHost> weak_ptr_factory_; | 110 base::WeakPtrFactory<ActiveHost> weak_ptr_factory_; |
90 | 111 |
91 DISALLOW_COPY_AND_ASSIGN(ActiveHost); | 112 DISALLOW_COPY_AND_ASSIGN(ActiveHost); |
92 }; | 113 }; |
93 | 114 |
94 } // namespace tether | 115 } // namespace tether |
95 | 116 |
96 } // namespace chromeos | 117 } // namespace chromeos |
97 | 118 |
98 #endif // CHROMEOS_COMPONENTS_TETHER_ACTIVE_HOST_H_ | 119 #endif // CHROMEOS_COMPONENTS_TETHER_ACTIVE_HOST_H_ |
OLD | NEW |