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
;
24 namespace proximity_auth
{
26 class CryptAuthEnroller
;
27 class CryptAuthEnrollerFactory
;
29 // This class manages the device's enrollment with CryptAuth, periodically
30 // re-enrolling to keep the state on the server fresh. If an enrollment fails,
31 // the manager will schedule the next enrollment more aggressively to recover
33 class CryptAuthEnrollmentManager
: public SyncScheduler::Delegate
,
34 public CryptAuthGCMManager::Observer
{
38 // Called when an enrollment attempt is started.
39 virtual void OnEnrollmentStarted() = 0;
41 // Called when an enrollment attempt finishes with the |success| of the
43 virtual void OnEnrollmentFinished(bool success
) = 0;
45 virtual ~Observer() {}
48 // Creates the manager:
49 // |clock|: Used to determine the time between sync attempts.
50 // |enroller_factory|: Creates CryptAuthEnroller instances to perform each
51 // enrollment attempt.
52 // |user_public_key|: The user's persistent public key identifying the device.
53 // |user_private_key|: The corresponding private key to |user_public_key|.
54 // |device_info|: Contains information about the local device that will be
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.
59 // |pref_service|: Contains preferences across browser restarts, and should
60 // have been registered through RegisterPrefs().
61 CryptAuthEnrollmentManager(
62 scoped_ptr
<base::Clock
> clock
,
63 scoped_ptr
<CryptAuthEnrollerFactory
> enroller_factory
,
64 const std::string
& user_public_key
,
65 const std::string
& user_private_key
,
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.
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;
110 // Creates a new SyncScheduler instance. Exposed for testing.
111 virtual scoped_ptr
<SyncScheduler
> CreateSyncScheduler();
114 // CryptAuthGCMManager::Observer:
115 void OnGCMRegistrationResult(bool success
) override
;
116 void OnReenrollMessage() override
;
118 // SyncScheduler::Delegate:
119 void OnSyncRequested(
120 scoped_ptr
<SyncScheduler::SyncRequest
> sync_request
) override
;
122 // Starts a CryptAuth enrollment attempt.
123 void DoCryptAuthEnrollment();
125 // Callback when |cryptauth_enroller_| completes.
126 void OnEnrollmentFinished(bool success
);
128 // Used to determine the time.
129 scoped_ptr
<base::Clock
> clock_
;
131 // Creates CryptAuthEnroller instances for each enrollment attempt.
132 scoped_ptr
<CryptAuthEnrollerFactory
> enroller_factory_
;
134 // The user's persistent key-pair identifying the local device.
135 std::string user_public_key_
;
136 std::string user_private_key_
;
138 // The local device information to upload to CryptAuth.
139 const cryptauth::GcmDeviceInfo device_info_
;
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_
;
145 // Contains perferences that outlive the lifetime of this object and across
147 // Not owned and must outlive this instance.
148 PrefService
* pref_service_
;
150 // Schedules the time between enrollment attempts.
151 scoped_ptr
<SyncScheduler
> scheduler_
;
153 // Contains the SyncRequest that |scheduler_| requests when an enrollment
155 scoped_ptr
<SyncScheduler::SyncRequest
> sync_request_
;
157 // The CryptAuthEnroller instance for the current enrollment attempt. A new
158 // instance will be created for each individual attempt.
159 scoped_ptr
<CryptAuthEnroller
> cryptauth_enroller_
;
161 // List of observers.
162 base::ObserverList
<Observer
> observers_
;
164 base::WeakPtrFactory
<CryptAuthEnrollmentManager
> weak_ptr_factory_
;
166 DISALLOW_COPY_AND_ASSIGN(CryptAuthEnrollmentManager
);
169 } // namespace proximity_auth
171 #endif // COMPONENTS_PROXIMITY_CRYPTAUTH_CRYPTAUTH_ENROLLMENT_MANAGER_H