| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef COMPONENTS_PROXIMITY_AUTH_CRYPTAUTH_CRYPTAUTH_ENROLLMENT_MANAGER_H | |
| 6 #define COMPONENTS_PROXIMITY_AUTH_CRYPTAUTH_CRYPTAUTH_ENROLLMENT_MANAGER_H | |
| 7 | |
| 8 #include <memory> | |
| 9 | |
| 10 #include "base/macros.h" | |
| 11 #include "base/memory/weak_ptr.h" | |
| 12 #include "base/observer_list.h" | |
| 13 #include "base/time/time.h" | |
| 14 #include "components/proximity_auth/cryptauth/cryptauth_gcm_manager.h" | |
| 15 #include "components/proximity_auth/cryptauth/proto/cryptauth_api.pb.h" | |
| 16 #include "components/proximity_auth/cryptauth/sync_scheduler.h" | |
| 17 | |
| 18 class PrefRegistrySimple; | |
| 19 class PrefService; | |
| 20 | |
| 21 namespace base { | |
| 22 class Clock; | |
| 23 class Time; | |
| 24 } | |
| 25 | |
| 26 namespace proximity_auth { | |
| 27 | |
| 28 class CryptAuthEnroller; | |
| 29 class CryptAuthEnrollerFactory; | |
| 30 class SecureMessageDelegate; | |
| 31 | |
| 32 // This class manages the device's enrollment with CryptAuth, periodically | |
| 33 // re-enrolling to keep the state on the server fresh. If an enrollment fails, | |
| 34 // the manager will schedule the next enrollment more aggressively to recover | |
| 35 // from the failure. | |
| 36 class CryptAuthEnrollmentManager : public SyncScheduler::Delegate, | |
| 37 public CryptAuthGCMManager::Observer { | |
| 38 public: | |
| 39 class Observer { | |
| 40 public: | |
| 41 // Called when an enrollment attempt is started. | |
| 42 virtual void OnEnrollmentStarted() = 0; | |
| 43 | |
| 44 // Called when an enrollment attempt finishes with the |success| of the | |
| 45 // attempt. | |
| 46 virtual void OnEnrollmentFinished(bool success) = 0; | |
| 47 | |
| 48 virtual ~Observer() {} | |
| 49 }; | |
| 50 | |
| 51 // Creates the manager: | |
| 52 // |clock|: Used to determine the time between sync attempts. | |
| 53 // |enroller_factory|: Creates CryptAuthEnroller instances to perform each | |
| 54 // enrollment attempt. | |
| 55 // |secure_message_delegate|: Used to generate the user's keypair if it does | |
| 56 // not exist. | |
| 57 // |device_info|: Contains information about the local device that will be | |
| 58 // uploaded to CryptAuth with each enrollment request. | |
| 59 // |gcm_manager|: Used to perform GCM registrations and also notifies when GCM | |
| 60 // push messages trigger re-enrollments. | |
| 61 // Not owned and must outlive this instance. | |
| 62 // |pref_service|: Contains preferences across browser restarts, and should | |
| 63 // have been registered through RegisterPrefs(). | |
| 64 CryptAuthEnrollmentManager( | |
| 65 std::unique_ptr<base::Clock> clock, | |
| 66 std::unique_ptr<CryptAuthEnrollerFactory> enroller_factory, | |
| 67 std::unique_ptr<SecureMessageDelegate> secure_message_delegate, | |
| 68 const cryptauth::GcmDeviceInfo& device_info, | |
| 69 CryptAuthGCMManager* gcm_manager, | |
| 70 PrefService* pref_service); | |
| 71 | |
| 72 ~CryptAuthEnrollmentManager() override; | |
| 73 | |
| 74 // Registers the prefs used by this class to the given |pref_service|. | |
| 75 static void RegisterPrefs(PrefRegistrySimple* registry); | |
| 76 | |
| 77 // Begins scheduling periodic enrollment attempts. | |
| 78 void Start(); | |
| 79 | |
| 80 // Adds an observer. | |
| 81 void AddObserver(Observer* observer); | |
| 82 | |
| 83 // Removes an observer. | |
| 84 void RemoveObserver(Observer* observer); | |
| 85 | |
| 86 // Skips the waiting period and forces an enrollment immediately. If an | |
| 87 // enrollment is already in progress, this function does nothing. | |
| 88 // |invocation_reason| specifies the reason that the enrollment was triggered, | |
| 89 // which is upload to the server. | |
| 90 void ForceEnrollmentNow(cryptauth::InvocationReason invocation_reason); | |
| 91 | |
| 92 // Returns true if a successful enrollment has been recorded and this | |
| 93 // enrollment has not expired. | |
| 94 bool IsEnrollmentValid() const; | |
| 95 | |
| 96 // Returns the timestamp of the last successful enrollment. If no enrollment | |
| 97 // has ever been made, then a null base::Time object will be returned. | |
| 98 base::Time GetLastEnrollmentTime() const; | |
| 99 | |
| 100 // Returns the time to the next enrollment attempt. | |
| 101 base::TimeDelta GetTimeToNextAttempt() const; | |
| 102 | |
| 103 // Returns true if an enrollment attempt is currently in progress. | |
| 104 bool IsEnrollmentInProgress() const; | |
| 105 | |
| 106 // Returns true if the last enrollment failed and the manager is now | |
| 107 // scheduling enrollments more aggressively to recover. If no enrollment has | |
| 108 // ever been recorded, then this function will also return true. | |
| 109 bool IsRecoveringFromFailure() const; | |
| 110 | |
| 111 // Returns the keypair used to enroll with CryptAuth. If no enrollment has | |
| 112 // been completed, then an empty string will be returned. | |
| 113 // Note: These keys are really serialized protocol buffer messages, and should | |
| 114 // only be used by passing to SecureMessageDelegate. | |
| 115 std::string GetUserPublicKey(); | |
| 116 std::string GetUserPrivateKey(); | |
| 117 | |
| 118 protected: | |
| 119 // Creates a new SyncScheduler instance. Exposed for testing. | |
| 120 virtual std::unique_ptr<SyncScheduler> CreateSyncScheduler(); | |
| 121 | |
| 122 private: | |
| 123 // CryptAuthGCMManager::Observer: | |
| 124 void OnGCMRegistrationResult(bool success) override; | |
| 125 void OnReenrollMessage() override; | |
| 126 | |
| 127 // Callback when a new keypair is generated. | |
| 128 void OnKeyPairGenerated(const std::string& public_key, | |
| 129 const std::string& private_key); | |
| 130 | |
| 131 // SyncScheduler::Delegate: | |
| 132 void OnSyncRequested( | |
| 133 std::unique_ptr<SyncScheduler::SyncRequest> sync_request) override; | |
| 134 | |
| 135 // Starts a CryptAuth enrollment attempt, generating a new keypair if one is | |
| 136 // not already stored in the user prefs. | |
| 137 void DoCryptAuthEnrollment(); | |
| 138 | |
| 139 // Starts a CryptAuth enrollment attempt, after a key-pair is stored in the | |
| 140 // user prefs. | |
| 141 void DoCryptAuthEnrollmentWithKeys(); | |
| 142 | |
| 143 // Callback when |cryptauth_enroller_| completes. | |
| 144 void OnEnrollmentFinished(bool success); | |
| 145 | |
| 146 // Used to determine the time. | |
| 147 std::unique_ptr<base::Clock> clock_; | |
| 148 | |
| 149 // Creates CryptAuthEnroller instances for each enrollment attempt. | |
| 150 std::unique_ptr<CryptAuthEnrollerFactory> enroller_factory_; | |
| 151 | |
| 152 // The SecureMessageDelegate used to generate the user's keypair if it does | |
| 153 // not already exist. | |
| 154 std::unique_ptr<SecureMessageDelegate> secure_message_delegate_; | |
| 155 | |
| 156 // The local device information to upload to CryptAuth. | |
| 157 const cryptauth::GcmDeviceInfo device_info_; | |
| 158 | |
| 159 // Used to perform GCM registrations and also notifies when GCM push messages | |
| 160 // trigger re-enrollments. Not owned and must outlive this instance. | |
| 161 CryptAuthGCMManager* gcm_manager_; | |
| 162 | |
| 163 // Contains perferences that outlive the lifetime of this object and across | |
| 164 // process restarts. | |
| 165 // Not owned and must outlive this instance. | |
| 166 PrefService* pref_service_; | |
| 167 | |
| 168 // Schedules the time between enrollment attempts. | |
| 169 std::unique_ptr<SyncScheduler> scheduler_; | |
| 170 | |
| 171 // Contains the SyncRequest that |scheduler_| requests when an enrollment | |
| 172 // attempt is made. | |
| 173 std::unique_ptr<SyncScheduler::SyncRequest> sync_request_; | |
| 174 | |
| 175 // The CryptAuthEnroller instance for the current enrollment attempt. A new | |
| 176 // instance will be created for each individual attempt. | |
| 177 std::unique_ptr<CryptAuthEnroller> cryptauth_enroller_; | |
| 178 | |
| 179 // List of observers. | |
| 180 base::ObserverList<Observer> observers_; | |
| 181 | |
| 182 base::WeakPtrFactory<CryptAuthEnrollmentManager> weak_ptr_factory_; | |
| 183 | |
| 184 DISALLOW_COPY_AND_ASSIGN(CryptAuthEnrollmentManager); | |
| 185 }; | |
| 186 | |
| 187 } // namespace proximity_auth | |
| 188 | |
| 189 #endif // COMPONENTS_PROXIMITY_CRYPTAUTH_CRYPTAUTH_ENROLLMENT_MANAGER_H | |
| OLD | NEW |