Vectorize website settings icons in omnibox
[chromium-blink-merge.git] / components / proximity_auth / cryptauth / cryptauth_device_manager.h
blob0c615712a1a6a99975440fdc676eeb7f3243b606
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/cryptauth_gcm_manager.h"
14 #include "components/proximity_auth/cryptauth/proto/cryptauth_api.pb.h"
15 #include "components/proximity_auth/cryptauth/sync_scheduler.h"
17 class PrefService;
18 class PrefRegistrySimple;
20 namespace proximity_auth {
22 class CryptAuthClient;
23 class CryptAuthClientFactory;
25 // This class manages syncing and storing the user's phones that are registered
26 // with CryptAuth and are capable of unlocking the user's other devices. These
27 // phones are called "unlock keys".
28 // The manager periodically syncs the user's devices from CryptAuth to keep the
29 // list of unlock keys fresh. If a sync attempts fails, the manager will
30 // schedule the next sync more aggressively to recover.
31 class CryptAuthDeviceManager : public SyncScheduler::Delegate,
32 public CryptAuthGCMManager::Observer {
33 public:
34 // Respresents the success result of a sync attempt.
35 enum class SyncResult { SUCCESS, FAILURE };
37 // Represents whether the list of unlock keys has changed after a sync
38 // attempt completes.
39 enum class DeviceChangeResult {
40 UNCHANGED,
41 CHANGED,
44 class Observer {
45 public:
46 // Called when a sync attempt is started.
47 virtual void OnSyncStarted() = 0;
49 // Called when a sync attempt finishes with the |success| of the request.
50 // |devices_changed| specifies if the sync caused the stored unlock keys to
51 // change.
52 virtual void OnSyncFinished(SyncResult sync_result,
53 DeviceChangeResult device_change_result) = 0;
55 virtual ~Observer() {}
58 // Creates the manager:
59 // |clock|: Used to determine the time between sync attempts.
60 // |client_factory|: Creates CryptAuthClient instances to perform each sync.
61 // |gcm_manager|: Notifies when GCM push messages trigger device syncs.
62 // Not owned and must outlive this instance.
63 // |pref_service|: Stores syncing metadata and unlock key information to
64 // persist across browser restarts. Must already be registered
65 // with RegisterPrefs().
66 CryptAuthDeviceManager(scoped_ptr<base::Clock> clock,
67 scoped_ptr<CryptAuthClientFactory> client_factory,
68 CryptAuthGCMManager* gcm_manager,
69 PrefService* pref_service);
71 ~CryptAuthDeviceManager() override;
73 // Registers the prefs used by this class to the given |registry|.
74 static void RegisterPrefs(PrefRegistrySimple* registry);
76 // Starts device manager to begin syncing devices.
77 void Start();
79 // Adds an observer.
80 void AddObserver(Observer* observer);
82 // Removes an observer.
83 void RemoveObserver(Observer* observer);
85 // Skips the waiting period and forces a sync immediately. If a
86 // sync attempt is already in progress, this function does nothing.
87 // |invocation_reason| specifies the reason that the sync was triggered,
88 // which is upload to the server.
89 void ForceSyncNow(cryptauth::InvocationReason invocation_reason);
91 // Returns the timestamp of the last successful sync. If no sync
92 // has ever been made, then returns a null base::Time object.
93 base::Time GetLastSyncTime() const;
95 // Returns the time to the next sync attempt.
96 base::TimeDelta GetTimeToNextAttempt() const;
98 // Returns true if a device sync attempt is currently in progress.
99 bool IsSyncInProgress() const;
101 // Returns true if the last device sync failed and the manager is now
102 // scheduling sync attempts more aggressively to recover. If no enrollment
103 // has ever been recorded, then this function will also return true.
104 bool IsRecoveringFromFailure() const;
106 // Returns a list of remote devices that can unlock the user's other devices.
107 const std::vector<cryptauth::ExternalDeviceInfo>& unlock_keys() const {
108 return unlock_keys_;
111 protected:
112 // Creates a new SyncScheduler instance. Exposed for testing.
113 virtual scoped_ptr<SyncScheduler> CreateSyncScheduler();
115 private:
116 // CryptAuthGCMManager::Observer:
117 void OnResyncMessage() override;
119 // Updates |unlock_keys_| by fetching the list stored in |pref_service_|.
120 void UpdateUnlockKeysFromPrefs();
122 // SyncScheduler::Delegate:
123 void OnSyncRequested(
124 scoped_ptr<SyncScheduler::SyncRequest> sync_request) override;
126 // Callback when |cryptauth_client_| completes with the response.
127 void OnGetMyDevicesSuccess(const cryptauth::GetMyDevicesResponse& response);
128 void OnGetMyDevicesFailure(const std::string& error);
130 // Used to determine the time.
131 scoped_ptr<base::Clock> clock_;
133 // Creates CryptAuthClient instances for each sync attempt.
134 scoped_ptr<CryptAuthClientFactory> client_factory_;
136 // Notifies when GCM push messages trigger device sync. Not owned and must
137 // outlive this instance.
138 CryptAuthGCMManager* gcm_manager_;
140 // Contains preferences that outlive the lifetime of this object and across
141 // process restarts. |pref_service_| must outlive the lifetime of this
142 // instance.
143 PrefService* const pref_service_;
145 // The unlock keys currently synced from CryptAuth.
146 std::vector<cryptauth::ExternalDeviceInfo> unlock_keys_;
148 // Schedules the time between device sync attempts.
149 scoped_ptr<SyncScheduler> scheduler_;
151 // Contains the SyncRequest that |scheduler_| requests when a device sync
152 // attempt is made.
153 scoped_ptr<SyncScheduler::SyncRequest> sync_request_;
155 // The CryptAuthEnroller instance for the current sync attempt. A new
156 // instance will be created for each individual attempt.
157 scoped_ptr<CryptAuthClient> cryptauth_client_;
159 // List of observers.
160 base::ObserverList<Observer> observers_;
162 base::WeakPtrFactory<CryptAuthDeviceManager> weak_ptr_factory_;
164 DISALLOW_COPY_AND_ASSIGN(CryptAuthDeviceManager);
167 } // namespace proximity_auth
169 #endif // COMPONENTS_PROXIMITY_CRYPTAUTH_CRYPTAUTH_DEVICE_MANAGER_H