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/proto/cryptauth_api.pb.h"
13 #include "components/proximity_auth/cryptauth/sync_scheduler.h"
15 class PrefRegistrySimple
;
23 namespace proximity_auth
{
25 class CryptAuthEnroller
;
26 class CryptAuthEnrollerFactory
;
28 // 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 // the manager will schedule the next enrollment more aggressively to recover
32 class CryptAuthEnrollmentManager
: public SyncScheduler::Delegate
{
36 // Called when an enrollment attempt is started.
37 virtual void OnEnrollmentStarted() = 0;
39 // Called when an enrollment attempt finishes with the |success| of the
41 virtual void OnEnrollmentFinished(bool success
) = 0;
43 virtual ~Observer() {}
46 // Creates the manager:
47 // |clock|: Used to determine the time between sync attempts.
48 // |enroller_factory|: Creates CryptAuthEnroller instances to perform each
49 // enrollment attempt.
50 // |user_public_key|: The user's persistent public key identifying the device.
51 // |user_private_key|: The corresponding private key to |user_public_key|.
52 // |device_info|: Contains information about the local device that will be
53 // uploaded to CryptAuth with each enrollment request.
54 // |pref_service|: Contains preferences across browser restarts, and should
55 // have been registered through RegisterPrefs().
56 CryptAuthEnrollmentManager(
57 scoped_ptr
<base::Clock
> clock
,
58 scoped_ptr
<CryptAuthEnrollerFactory
> enroller_factory
,
59 const std::string
& user_public_key
,
60 const std::string
& user_private_key
,
61 const cryptauth::GcmDeviceInfo
& device_info
,
62 PrefService
* pref_service
);
64 ~CryptAuthEnrollmentManager() override
;
66 // Registers the prefs used by this class to the given |pref_service|.
67 static void RegisterPrefs(PrefRegistrySimple
* registry
);
69 // Begins scheduling periodic enrollment attempts.
73 void AddObserver(Observer
* observer
);
75 // Removes an observer.
76 void RemoveObserver(Observer
* observer
);
78 // Skips the waiting period and forces an enrollment immediately. If an
79 // enrollment is already in progress, this function does nothing.
80 // |invocation_reason| specifies the reason that the enrollment was triggered,
81 // which is upload to the server.
82 void ForceEnrollmentNow(cryptauth::InvocationReason invocation_reason
);
84 // Returns true if a successful enrollment has been recorded and this
85 // enrollment has not expired.
86 bool IsEnrollmentValid() const;
88 // Returns the timestamp of the last successful enrollment. If no enrollment
89 // has ever been made, then a null base::Time object will be returned.
90 base::Time
GetLastEnrollmentTime() const;
92 // Returns the time to the next enrollment attempt.
93 base::TimeDelta
GetTimeToNextAttempt() const;
95 // Returns true if an enrollment attempt is currently in progress.
96 bool IsEnrollmentInProgress() const;
98 // Returns true if the last enrollment failed and the manager is now
99 // scheduling enrollments more aggressively to recover. If no enrollment has
100 // ever been recorded, then this function will also return true.
101 bool IsRecoveringFromFailure() const;
104 // Creates a new SyncScheduler instance. Exposed for testing.
105 virtual scoped_ptr
<SyncScheduler
> CreateSyncScheduler();
108 // SyncScheduler::Delegate:
109 void OnSyncRequested(
110 scoped_ptr
<SyncScheduler::SyncRequest
> sync_request
) override
;
112 // Callback when |cryptauth_enroller_| completes.
113 void OnEnrollmentFinished(bool success
);
115 // Used to determine the time.
116 scoped_ptr
<base::Clock
> clock_
;
118 // Creates CryptAuthEnroller instances for each enrollment attempt.
119 scoped_ptr
<CryptAuthEnrollerFactory
> enroller_factory_
;
121 // The user's persistent key-pair identifying the local device.
122 std::string user_public_key_
;
123 std::string user_private_key_
;
125 // The local device information to upload to CryptAuth.
126 const cryptauth::GcmDeviceInfo device_info_
;
128 // Contains perferences that outlive the lifetime of this object and across
130 // Not owned and must outlive this instance.
131 PrefService
* pref_service_
;
133 // Schedules the time between enrollment attempts.
134 scoped_ptr
<SyncScheduler
> scheduler_
;
136 // Contains the SyncRequest that |scheduler_| requests when an enrollment
138 scoped_ptr
<SyncScheduler::SyncRequest
> sync_request_
;
140 // The CryptAuthEnroller instance for the current enrollment attempt. A new
141 // instance will be created for each individual attempt.
142 scoped_ptr
<CryptAuthEnroller
> cryptauth_enroller_
;
144 // List of observers.
145 base::ObserverList
<Observer
> observers_
;
147 base::WeakPtrFactory
<CryptAuthEnrollmentManager
> weak_ptr_factory_
;
149 DISALLOW_COPY_AND_ASSIGN(CryptAuthEnrollmentManager
);
152 } // namespace proximity_auth
154 #endif // COMPONENTS_PROXIMITY_CRYPTAUTH_CRYPTAUTH_ENROLLMENT_MANAGER_H