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_ENROLLMENT_MANAGER_H | 5 #ifndef COMPONENTS_PROXIMITY_AUTH_CRYPTAUTH_CRYPTAUTH_ENROLLMENT_MANAGER_H |
6 #define COMPONENTS_PROXIMITY_AUTH_CRYPTAUTH_CRYPTAUTH_ENROLLMENT_MANAGER_H | 6 #define COMPONENTS_PROXIMITY_AUTH_CRYPTAUTH_CRYPTAUTH_ENROLLMENT_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/time.h" | 11 #include "base/time/time.h" |
| 12 #include "components/proximity_auth/cryptauth/cryptauth_gcm_manager.h" |
12 #include "components/proximity_auth/cryptauth/proto/cryptauth_api.pb.h" | 13 #include "components/proximity_auth/cryptauth/proto/cryptauth_api.pb.h" |
13 #include "components/proximity_auth/cryptauth/sync_scheduler.h" | 14 #include "components/proximity_auth/cryptauth/sync_scheduler.h" |
14 | 15 |
15 class PrefRegistrySimple; | 16 class PrefRegistrySimple; |
16 class PrefService; | 17 class PrefService; |
17 | 18 |
18 namespace base { | 19 namespace base { |
19 class Clock; | 20 class Clock; |
20 class Time; | 21 class Time; |
21 } | 22 } |
22 | 23 |
23 namespace proximity_auth { | 24 namespace proximity_auth { |
24 | 25 |
25 class CryptAuthEnroller; | 26 class CryptAuthEnroller; |
26 class CryptAuthEnrollerFactory; | 27 class CryptAuthEnrollerFactory; |
27 | 28 |
28 // This class manages the device's enrollment with CryptAuth, periodically | 29 // This class manages the device's enrollment with CryptAuth, periodically |
29 // re-enrolling to keep the state on the server fresh. If an enrollment fails, | 30 // re-enrolling to keep the state on the server fresh. If an enrollment fails, |
30 // the manager will schedule the next enrollment more aggressively to recover | 31 // the manager will schedule the next enrollment more aggressively to recover |
31 // from the failure. | 32 // from the failure. |
32 class CryptAuthEnrollmentManager : public SyncScheduler::Delegate { | 33 class CryptAuthEnrollmentManager : public SyncScheduler::Delegate, |
| 34 public CryptAuthGCMManager::Observer { |
33 public: | 35 public: |
34 class Observer { | 36 class Observer { |
35 public: | 37 public: |
36 // Called when an enrollment attempt is started. | 38 // Called when an enrollment attempt is started. |
37 virtual void OnEnrollmentStarted() = 0; | 39 virtual void OnEnrollmentStarted() = 0; |
38 | 40 |
39 // Called when an enrollment attempt finishes with the |success| of the | 41 // Called when an enrollment attempt finishes with the |success| of the |
40 // attempt. | 42 // attempt. |
41 virtual void OnEnrollmentFinished(bool success) = 0; | 43 virtual void OnEnrollmentFinished(bool success) = 0; |
42 | 44 |
43 virtual ~Observer() {} | 45 virtual ~Observer() {} |
44 }; | 46 }; |
45 | 47 |
46 // Creates the manager: | 48 // Creates the manager: |
47 // |clock|: Used to determine the time between sync attempts. | 49 // |clock|: Used to determine the time between sync attempts. |
48 // |enroller_factory|: Creates CryptAuthEnroller instances to perform each | 50 // |enroller_factory|: Creates CryptAuthEnroller instances to perform each |
49 // enrollment attempt. | 51 // enrollment attempt. |
50 // |user_public_key|: The user's persistent public key identifying the device. | 52 // |user_public_key|: The user's persistent public key identifying the device. |
51 // |user_private_key|: The corresponding private key to |user_public_key|. | 53 // |user_private_key|: The corresponding private key to |user_public_key|. |
52 // |device_info|: Contains information about the local device that will be | 54 // |device_info|: Contains information about the local device that will be |
53 // uploaded to CryptAuth with each enrollment request. | 55 // uploaded to CryptAuth with each enrollment request. |
| 56 // |gcm_manager|: Used to perform GCM registrations and also notifies when GCM |
| 57 // push messages trigger re-enrollments. |
| 58 // Not owned and must outlive this instance. |
54 // |pref_service|: Contains preferences across browser restarts, and should | 59 // |pref_service|: Contains preferences across browser restarts, and should |
55 // have been registered through RegisterPrefs(). | 60 // have been registered through RegisterPrefs(). |
56 CryptAuthEnrollmentManager( | 61 CryptAuthEnrollmentManager( |
57 scoped_ptr<base::Clock> clock, | 62 scoped_ptr<base::Clock> clock, |
58 scoped_ptr<CryptAuthEnrollerFactory> enroller_factory, | 63 scoped_ptr<CryptAuthEnrollerFactory> enroller_factory, |
59 const std::string& user_public_key, | 64 const std::string& user_public_key, |
60 const std::string& user_private_key, | 65 const std::string& user_private_key, |
61 const cryptauth::GcmDeviceInfo& device_info, | 66 const cryptauth::GcmDeviceInfo& device_info, |
| 67 CryptAuthGCMManager* gcm_manager, |
62 PrefService* pref_service); | 68 PrefService* pref_service); |
63 | 69 |
64 ~CryptAuthEnrollmentManager() override; | 70 ~CryptAuthEnrollmentManager() override; |
65 | 71 |
66 // Registers the prefs used by this class to the given |pref_service|. | 72 // Registers the prefs used by this class to the given |pref_service|. |
67 static void RegisterPrefs(PrefRegistrySimple* registry); | 73 static void RegisterPrefs(PrefRegistrySimple* registry); |
68 | 74 |
69 // Begins scheduling periodic enrollment attempts. | 75 // Begins scheduling periodic enrollment attempts. |
70 void Start(); | 76 void Start(); |
71 | 77 |
(...skipping 26 matching lines...) Expand all Loading... |
98 // Returns true if the last enrollment failed and the manager is now | 104 // Returns true if the last enrollment failed and the manager is now |
99 // scheduling enrollments more aggressively to recover. If no enrollment has | 105 // scheduling enrollments more aggressively to recover. If no enrollment has |
100 // ever been recorded, then this function will also return true. | 106 // ever been recorded, then this function will also return true. |
101 bool IsRecoveringFromFailure() const; | 107 bool IsRecoveringFromFailure() const; |
102 | 108 |
103 protected: | 109 protected: |
104 // Creates a new SyncScheduler instance. Exposed for testing. | 110 // Creates a new SyncScheduler instance. Exposed for testing. |
105 virtual scoped_ptr<SyncScheduler> CreateSyncScheduler(); | 111 virtual scoped_ptr<SyncScheduler> CreateSyncScheduler(); |
106 | 112 |
107 private: | 113 private: |
| 114 // CryptAuthGCMManager::Observer: |
| 115 void OnGCMRegistrationResult(bool success) override; |
| 116 void OnReenrollMessage() override; |
| 117 |
108 // SyncScheduler::Delegate: | 118 // SyncScheduler::Delegate: |
109 void OnSyncRequested( | 119 void OnSyncRequested( |
110 scoped_ptr<SyncScheduler::SyncRequest> sync_request) override; | 120 scoped_ptr<SyncScheduler::SyncRequest> sync_request) override; |
111 | 121 |
| 122 // Starts a CryptAuth enrollment attempt. |
| 123 void DoCryptAuthEnrollment(); |
| 124 |
112 // Callback when |cryptauth_enroller_| completes. | 125 // Callback when |cryptauth_enroller_| completes. |
113 void OnEnrollmentFinished(bool success); | 126 void OnEnrollmentFinished(bool success); |
114 | 127 |
115 // Used to determine the time. | 128 // Used to determine the time. |
116 scoped_ptr<base::Clock> clock_; | 129 scoped_ptr<base::Clock> clock_; |
117 | 130 |
118 // Creates CryptAuthEnroller instances for each enrollment attempt. | 131 // Creates CryptAuthEnroller instances for each enrollment attempt. |
119 scoped_ptr<CryptAuthEnrollerFactory> enroller_factory_; | 132 scoped_ptr<CryptAuthEnrollerFactory> enroller_factory_; |
120 | 133 |
121 // The user's persistent key-pair identifying the local device. | 134 // The user's persistent key-pair identifying the local device. |
122 std::string user_public_key_; | 135 std::string user_public_key_; |
123 std::string user_private_key_; | 136 std::string user_private_key_; |
124 | 137 |
125 // The local device information to upload to CryptAuth. | 138 // The local device information to upload to CryptAuth. |
126 const cryptauth::GcmDeviceInfo device_info_; | 139 const cryptauth::GcmDeviceInfo device_info_; |
127 | 140 |
| 141 // Used to perform GCM registrations and also notifies when GCM push messages |
| 142 // trigger re-enrollments. Not owned and must outlive this instance. |
| 143 CryptAuthGCMManager* gcm_manager_; |
| 144 |
128 // Contains perferences that outlive the lifetime of this object and across | 145 // Contains perferences that outlive the lifetime of this object and across |
129 // process restarts. | 146 // process restarts. |
130 // Not owned and must outlive this instance. | 147 // Not owned and must outlive this instance. |
131 PrefService* pref_service_; | 148 PrefService* pref_service_; |
132 | 149 |
133 // Schedules the time between enrollment attempts. | 150 // Schedules the time between enrollment attempts. |
134 scoped_ptr<SyncScheduler> scheduler_; | 151 scoped_ptr<SyncScheduler> scheduler_; |
135 | 152 |
136 // Contains the SyncRequest that |scheduler_| requests when an enrollment | 153 // Contains the SyncRequest that |scheduler_| requests when an enrollment |
137 // attempt is made. | 154 // attempt is made. |
138 scoped_ptr<SyncScheduler::SyncRequest> sync_request_; | 155 scoped_ptr<SyncScheduler::SyncRequest> sync_request_; |
139 | 156 |
140 // The CryptAuthEnroller instance for the current enrollment attempt. A new | 157 // The CryptAuthEnroller instance for the current enrollment attempt. A new |
141 // instance will be created for each individual attempt. | 158 // instance will be created for each individual attempt. |
142 scoped_ptr<CryptAuthEnroller> cryptauth_enroller_; | 159 scoped_ptr<CryptAuthEnroller> cryptauth_enroller_; |
143 | 160 |
144 // List of observers. | 161 // List of observers. |
145 base::ObserverList<Observer> observers_; | 162 base::ObserverList<Observer> observers_; |
146 | 163 |
147 base::WeakPtrFactory<CryptAuthEnrollmentManager> weak_ptr_factory_; | 164 base::WeakPtrFactory<CryptAuthEnrollmentManager> weak_ptr_factory_; |
148 | 165 |
149 DISALLOW_COPY_AND_ASSIGN(CryptAuthEnrollmentManager); | 166 DISALLOW_COPY_AND_ASSIGN(CryptAuthEnrollmentManager); |
150 }; | 167 }; |
151 | 168 |
152 } // namespace proximity_auth | 169 } // namespace proximity_auth |
153 | 170 |
154 #endif // COMPONENTS_PROXIMITY_CRYPTAUTH_CRYPTAUTH_ENROLLMENT_MANAGER_H | 171 #endif // COMPONENTS_PROXIMITY_CRYPTAUTH_CRYPTAUTH_ENROLLMENT_MANAGER_H |
OLD | NEW |