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