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