OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 COMPONENTS_PROXIMITY_AUTH_CRYPTAUTH_CRYPTAUTH_DEVICE_MANAGER_H | 5 #ifndef COMPONENTS_PROXIMITY_AUTH_CRYPTAUTH_CRYPTAUTH_DEVICE_MANAGER_H |
6 #define COMPONENTS_PROXIMITY_AUTH_CRYPTAUTH_CRYPTAUTH_DEVICE_MANAGER_H | 6 #define COMPONENTS_PROXIMITY_AUTH_CRYPTAUTH_CRYPTAUTH_DEVICE_MANAGER_H |
7 | 7 |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/memory/weak_ptr.h" | 9 #include "base/memory/weak_ptr.h" |
10 #include "base/observer_list.h" | 10 #include "base/observer_list.h" |
11 #include "base/time/clock.h" | 11 #include "base/time/clock.h" |
12 #include "base/time/time.h" | 12 #include "base/time/time.h" |
| 13 #include "components/proximity_auth/cryptauth/cryptauth_gcm_manager.h" |
13 #include "components/proximity_auth/cryptauth/proto/cryptauth_api.pb.h" | 14 #include "components/proximity_auth/cryptauth/proto/cryptauth_api.pb.h" |
14 #include "components/proximity_auth/cryptauth/sync_scheduler.h" | 15 #include "components/proximity_auth/cryptauth/sync_scheduler.h" |
15 | 16 |
16 class PrefService; | 17 class PrefService; |
17 class PrefRegistrySimple; | 18 class PrefRegistrySimple; |
18 | 19 |
19 namespace proximity_auth { | 20 namespace proximity_auth { |
20 | 21 |
21 class CryptAuthClient; | 22 class CryptAuthClient; |
22 class CryptAuthClientFactory; | 23 class CryptAuthClientFactory; |
23 | 24 |
24 // This class manages syncing and storing the user's phones that are registered | 25 // This class manages syncing and storing the user's phones that are registered |
25 // with CryptAuth and are capable of unlocking the user's other devices. These | 26 // with CryptAuth and are capable of unlocking the user's other devices. These |
26 // phones are called "unlock keys". | 27 // phones are called "unlock keys". |
27 // The manager periodically syncs the user's devices from CryptAuth to keep the | 28 // The manager periodically syncs the user's devices from CryptAuth to keep the |
28 // list of unlock keys fresh. If a sync attempts fails, the manager will | 29 // list of unlock keys fresh. If a sync attempts fails, the manager will |
29 // schedule the next sync more aggressively to recover. | 30 // schedule the next sync more aggressively to recover. |
30 class CryptAuthDeviceManager : public SyncScheduler::Delegate { | 31 class CryptAuthDeviceManager : public SyncScheduler::Delegate, |
| 32 public CryptAuthGCMManager::Observer { |
31 public: | 33 public: |
32 // Respresents the success result of a sync attempt. | 34 // Respresents the success result of a sync attempt. |
33 enum class SyncResult { SUCCESS, FAILURE }; | 35 enum class SyncResult { SUCCESS, FAILURE }; |
34 | 36 |
35 // Represents whether the list of unlock keys has changed after a sync | 37 // Represents whether the list of unlock keys has changed after a sync |
36 // attempt completes. | 38 // attempt completes. |
37 enum class DeviceChangeResult { | 39 enum class DeviceChangeResult { |
38 UNCHANGED, | 40 UNCHANGED, |
39 CHANGED, | 41 CHANGED, |
40 }; | 42 }; |
41 | 43 |
42 class Observer { | 44 class Observer { |
43 public: | 45 public: |
44 // Called when a sync attempt is started. | 46 // Called when a sync attempt is started. |
45 virtual void OnSyncStarted() = 0; | 47 virtual void OnSyncStarted() = 0; |
46 | 48 |
47 // Called when a sync attempt finishes with the |success| of the request. | 49 // Called when a sync attempt finishes with the |success| of the request. |
48 // |devices_changed| specifies if the sync caused the stored unlock keys to | 50 // |devices_changed| specifies if the sync caused the stored unlock keys to |
49 // change. | 51 // change. |
50 virtual void OnSyncFinished(SyncResult sync_result, | 52 virtual void OnSyncFinished(SyncResult sync_result, |
51 DeviceChangeResult device_change_result) = 0; | 53 DeviceChangeResult device_change_result) = 0; |
52 | 54 |
53 virtual ~Observer() {} | 55 virtual ~Observer() {} |
54 }; | 56 }; |
55 | 57 |
56 // Creates the manager: | 58 // Creates the manager: |
57 // |clock|: Used to determine the time between sync attempts. | 59 // |clock|: Used to determine the time between sync attempts. |
58 // |client_factory|: Creates CryptAuthClient instances to perform each sync. | 60 // |client_factory|: Creates CryptAuthClient instances to perform each sync. |
| 61 // |gcm_manager|: Notifies when GCM push messages trigger device syncs. |
| 62 // Not owned and must outlive this instance. |
59 // |pref_service|: Stores syncing metadata and unlock key information to | 63 // |pref_service|: Stores syncing metadata and unlock key information to |
60 // persist across browser restarts. Must already be registered | 64 // persist across browser restarts. Must already be registered |
61 // with RegisterPrefs(). | 65 // with RegisterPrefs(). |
62 CryptAuthDeviceManager(scoped_ptr<base::Clock> clock, | 66 CryptAuthDeviceManager(scoped_ptr<base::Clock> clock, |
63 scoped_ptr<CryptAuthClientFactory> client_factory, | 67 scoped_ptr<CryptAuthClientFactory> client_factory, |
| 68 CryptAuthGCMManager* gcm_manager, |
64 PrefService* pref_service); | 69 PrefService* pref_service); |
65 | 70 |
66 ~CryptAuthDeviceManager() override; | 71 ~CryptAuthDeviceManager() override; |
67 | 72 |
68 // Registers the prefs used by this class to the given |registry|. | 73 // Registers the prefs used by this class to the given |registry|. |
69 static void RegisterPrefs(PrefRegistrySimple* registry); | 74 static void RegisterPrefs(PrefRegistrySimple* registry); |
70 | 75 |
71 // Starts device manager to begin syncing devices. | 76 // Starts device manager to begin syncing devices. |
72 void Start(); | 77 void Start(); |
73 | 78 |
(...skipping 27 matching lines...) Expand all Loading... |
101 // Returns a list of remote devices that can unlock the user's other devices. | 106 // Returns a list of remote devices that can unlock the user's other devices. |
102 const std::vector<cryptauth::ExternalDeviceInfo>& unlock_keys() { | 107 const std::vector<cryptauth::ExternalDeviceInfo>& unlock_keys() { |
103 return unlock_keys_; | 108 return unlock_keys_; |
104 } | 109 } |
105 | 110 |
106 protected: | 111 protected: |
107 // Creates a new SyncScheduler instance. Exposed for testing. | 112 // Creates a new SyncScheduler instance. Exposed for testing. |
108 virtual scoped_ptr<SyncScheduler> CreateSyncScheduler(); | 113 virtual scoped_ptr<SyncScheduler> CreateSyncScheduler(); |
109 | 114 |
110 private: | 115 private: |
| 116 // CryptAuthGCMManager::Observer: |
| 117 void OnResyncMessage() override; |
| 118 |
111 // Updates |unlock_keys_| by fetching the list stored in |pref_service_|. | 119 // Updates |unlock_keys_| by fetching the list stored in |pref_service_|. |
112 void UpdateUnlockKeysFromPrefs(); | 120 void UpdateUnlockKeysFromPrefs(); |
113 | 121 |
114 // SyncScheduler::Delegate: | 122 // SyncScheduler::Delegate: |
115 void OnSyncRequested( | 123 void OnSyncRequested( |
116 scoped_ptr<SyncScheduler::SyncRequest> sync_request) override; | 124 scoped_ptr<SyncScheduler::SyncRequest> sync_request) override; |
117 | 125 |
118 // Callback when |cryptauth_client_| completes with the response. | 126 // Callback when |cryptauth_client_| completes with the response. |
119 void OnGetMyDevicesSuccess(const cryptauth::GetMyDevicesResponse& response); | 127 void OnGetMyDevicesSuccess(const cryptauth::GetMyDevicesResponse& response); |
120 void OnGetMyDevicesFailure(const std::string& error); | 128 void OnGetMyDevicesFailure(const std::string& error); |
121 | 129 |
122 // Used to determine the time. | 130 // Used to determine the time. |
123 scoped_ptr<base::Clock> clock_; | 131 scoped_ptr<base::Clock> clock_; |
124 | 132 |
125 // Creates CryptAuthClient instances for each sync attempt. | 133 // Creates CryptAuthClient instances for each sync attempt. |
126 scoped_ptr<CryptAuthClientFactory> client_factory_; | 134 scoped_ptr<CryptAuthClientFactory> client_factory_; |
127 | 135 |
128 // Contains perferences that outlive the lifetime of this object and across | 136 // Notifies when GCM push messages trigger device sync. Not owned and must |
| 137 // outlive this instance. |
| 138 CryptAuthGCMManager* gcm_manager_; |
| 139 |
| 140 // Contains preferences that outlive the lifetime of this object and across |
129 // process restarts. |pref_service_| must outlive the lifetime of this | 141 // process restarts. |pref_service_| must outlive the lifetime of this |
130 // instance. | 142 // instance. |
131 PrefService* const pref_service_; | 143 PrefService* const pref_service_; |
132 | 144 |
133 // The unlock keys currently synced from CryptAuth. | 145 // The unlock keys currently synced from CryptAuth. |
134 std::vector<cryptauth::ExternalDeviceInfo> unlock_keys_; | 146 std::vector<cryptauth::ExternalDeviceInfo> unlock_keys_; |
135 | 147 |
136 // Schedules the time between device sync attempts. | 148 // Schedules the time between device sync attempts. |
137 scoped_ptr<SyncScheduler> scheduler_; | 149 scoped_ptr<SyncScheduler> scheduler_; |
138 | 150 |
139 // Contains the SyncRequest that |scheduler_| requests when a device sync | 151 // Contains the SyncRequest that |scheduler_| requests when a device sync |
140 // attempt is made. | 152 // attempt is made. |
141 scoped_ptr<SyncScheduler::SyncRequest> sync_request_; | 153 scoped_ptr<SyncScheduler::SyncRequest> sync_request_; |
142 | 154 |
143 // The CryptAuthEnroller instance for the current sync attempt. A new | 155 // The CryptAuthEnroller instance for the current sync attempt. A new |
144 // instance will be created for each individual attempt. | 156 // instance will be created for each individual attempt. |
145 scoped_ptr<CryptAuthClient> cryptauth_client_; | 157 scoped_ptr<CryptAuthClient> cryptauth_client_; |
146 | 158 |
147 // List of observers. | 159 // List of observers. |
148 base::ObserverList<Observer> observers_; | 160 base::ObserverList<Observer> observers_; |
149 | 161 |
150 base::WeakPtrFactory<CryptAuthDeviceManager> weak_ptr_factory_; | 162 base::WeakPtrFactory<CryptAuthDeviceManager> weak_ptr_factory_; |
151 | 163 |
152 DISALLOW_COPY_AND_ASSIGN(CryptAuthDeviceManager); | 164 DISALLOW_COPY_AND_ASSIGN(CryptAuthDeviceManager); |
153 }; | 165 }; |
154 | 166 |
155 } // namespace proximity_auth | 167 } // namespace proximity_auth |
156 | 168 |
157 #endif // COMPONENTS_PROXIMITY_CRYPTAUTH_CRYPTAUTH_DEVICE_MANAGER_H | 169 #endif // COMPONENTS_PROXIMITY_CRYPTAUTH_CRYPTAUTH_DEVICE_MANAGER_H |
OLD | NEW |