Extension syncing: Introduce a NeedsSync pref
[chromium-blink-merge.git] / components / proximity_auth / cryptauth / cryptauth_device_manager.h
blobf1dfebeb981cea931136b1e6b17276026ae40171
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_DEVICE_MANAGER_H
6 #define COMPONENTS_PROXIMITY_AUTH_CRYPTAUTH_CRYPTAUTH_DEVICE_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/clock.h"
12 #include "base/time/time.h"
13 #include "components/proximity_auth/cryptauth/proto/cryptauth_api.pb.h"
14 #include "components/proximity_auth/cryptauth/sync_scheduler.h"
16 class PrefService;
17 class PrefRegistrySimple;
19 namespace proximity_auth {
21 class CryptAuthClient;
22 class CryptAuthClientFactory;
24 // This class manages syncing and storing the user's phones that are registered
25 // with CryptAuth and are capable of unlocking the user's other devices. These
26 // phones are called "unlock keys".
27 // The manager periodically syncs the user's devices from CryptAuth to keep the
28 // list of unlock keys fresh. If a sync attempts fails, the manager will
29 // schedule the next sync more aggressively to recover.
30 class CryptAuthDeviceManager : public SyncScheduler::Delegate {
31 public:
32 // Respresents the success result of a sync attempt.
33 enum class SyncResult { SUCCESS, FAILURE };
35 // Represents whether the list of unlock keys has changed after a sync
36 // attempt completes.
37 enum class DeviceChangeResult {
38 UNCHANGED,
39 CHANGED,
42 class Observer {
43 public:
44 // Called when a sync attempt is started.
45 virtual void OnSyncStarted() = 0;
47 // Called when a sync attempt finishes with the |success| of the request.
48 // |devices_changed| specifies if the sync caused the stored unlock keys to
49 // change.
50 virtual void OnSyncFinished(SyncResult sync_result,
51 DeviceChangeResult device_change_result) = 0;
53 virtual ~Observer() {}
56 // Creates the manager:
57 // |clock|: Used to determine the time between sync attempts.
58 // |client_factory|: Creates CryptAuthClient instances to perform each sync.
59 // |pref_service|: Stores syncing metadata and unlock key information to
60 // persist across browser restarts. Must already be registered
61 // with RegisterPrefs().
62 CryptAuthDeviceManager(scoped_ptr<base::Clock> clock,
63 scoped_ptr<CryptAuthClientFactory> client_factory,
64 PrefService* pref_service);
66 ~CryptAuthDeviceManager() override;
68 // Registers the prefs used by this class to the given |registry|.
69 static void RegisterPrefs(PrefRegistrySimple* registry);
71 // Starts device manager to begin syncing devices.
72 void Start();
74 // Adds an observer.
75 void AddObserver(Observer* observer);
77 // Removes an observer.
78 void RemoveObserver(Observer* observer);
80 // Skips the waiting period and forces a sync immediately. If a
81 // sync attempt is already in progress, this function does nothing.
82 // |invocation_reason| specifies the reason that the sync was triggered,
83 // which is upload to the server.
84 void ForceSyncNow(cryptauth::InvocationReason invocation_reason);
86 // Returns the timestamp of the last successful sync. If no sync
87 // has ever been made, then returns a null base::Time object.
88 base::Time GetLastSyncTime() const;
90 // Returns the time to the next sync attempt.
91 base::TimeDelta GetTimeToNextAttempt() const;
93 // Returns true if a device sync attempt is currently in progress.
94 bool IsSyncInProgress() const;
96 // Returns true if the last device sync failed and the manager is now
97 // scheduling sync attempts more aggressively to recover. If no enrollment
98 // has ever been recorded, then this function will also return true.
99 bool IsRecoveringFromFailure() const;
101 // Returns a list of remote devices that can unlock the user's other devices.
102 const std::vector<cryptauth::ExternalDeviceInfo>& unlock_keys() {
103 return unlock_keys_;
106 protected:
107 // Creates a new SyncScheduler instance. Exposed for testing.
108 virtual scoped_ptr<SyncScheduler> CreateSyncScheduler();
110 private:
111 // Updates |unlock_keys_| by fetching the list stored in |pref_service_|.
112 void UpdateUnlockKeysFromPrefs();
114 // SyncScheduler::Delegate:
115 void OnSyncRequested(
116 scoped_ptr<SyncScheduler::SyncRequest> sync_request) override;
118 // Callback when |cryptauth_client_| completes with the response.
119 void OnGetMyDevicesSuccess(const cryptauth::GetMyDevicesResponse& response);
120 void OnGetMyDevicesFailure(const std::string& error);
122 // Used to determine the time.
123 scoped_ptr<base::Clock> clock_;
125 // Creates CryptAuthClient instances for each sync attempt.
126 scoped_ptr<CryptAuthClientFactory> client_factory_;
128 // Contains perferences that outlive the lifetime of this object and across
129 // process restarts. |pref_service_| must outlive the lifetime of this
130 // instance.
131 PrefService* const pref_service_;
133 // The unlock keys currently synced from CryptAuth.
134 std::vector<cryptauth::ExternalDeviceInfo> unlock_keys_;
136 // Schedules the time between device sync attempts.
137 scoped_ptr<SyncScheduler> scheduler_;
139 // Contains the SyncRequest that |scheduler_| requests when a device sync
140 // attempt is made.
141 scoped_ptr<SyncScheduler::SyncRequest> sync_request_;
143 // The CryptAuthEnroller instance for the current sync attempt. A new
144 // instance will be created for each individual attempt.
145 scoped_ptr<CryptAuthClient> cryptauth_client_;
147 // List of observers.
148 base::ObserverList<Observer> observers_;
150 base::WeakPtrFactory<CryptAuthDeviceManager> weak_ptr_factory_;
152 DISALLOW_COPY_AND_ASSIGN(CryptAuthDeviceManager);
155 } // namespace proximity_auth
157 #endif // COMPONENTS_PROXIMITY_CRYPTAUTH_CRYPTAUTH_DEVICE_MANAGER_H