1 // Copyright 2014 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 CHROME_BROWSER_SIGNIN_EASY_UNLOCK_SERVICE_H_
6 #define CHROME_BROWSER_SIGNIN_EASY_UNLOCK_SERVICE_H_
11 #include "base/callback_forward.h"
12 #include "base/macros.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/observer_list.h"
16 #include "chrome/browser/signin/easy_unlock_auth_attempt.h"
17 #include "chrome/browser/signin/easy_unlock_metrics.h"
18 #include "chrome/browser/signin/easy_unlock_screenlock_state_handler.h"
19 #include "components/keyed_service/core/keyed_service.h"
21 #if defined(OS_CHROMEOS)
22 #include "chrome/browser/chromeos/login/easy_unlock/easy_unlock_types.h"
26 class DictionaryValue
;
30 namespace user_manager
{
34 namespace user_prefs
{
35 class PrefRegistrySyncable
;
38 class EasyUnlockAppManager
;
39 class EasyUnlockServiceObserver
;
41 class PrefRegistrySimple
;
43 class EasyUnlockService
: public KeyedService
{
45 enum TurnOffFlowStatus
{
56 // Easy Unlock settings that the user can configure.
61 // Whether to require the remote device to be in very close proximity
62 // before allowing unlock (~1 feet).
63 bool require_close_proximity
;
66 // Gets EasyUnlockService instance.
67 static EasyUnlockService
* Get(Profile
* profile
);
69 // Gets EasyUnlockService instance associated with a user if the user is
70 // logged in and his profile is initialized.
71 static EasyUnlockService
* GetForUser(const user_manager::User
& user
);
73 // Registers Easy Unlock profile preferences.
74 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable
* registry
);
76 // Registers Easy Unlock local state entries.
77 static void RegisterPrefs(PrefRegistrySimple
* registry
);
79 // Removes the hardlock state for the given user.
80 static void ResetLocalStateForUser(const std::string
& user_id
);
82 // Returns the user's preferences.
83 static UserSettings
GetUserSettings(const std::string
& user_id
);
85 // Returns the identifier for the device.
86 static std::string
GetDeviceId();
88 // Returns true if Easy sign-in is enabled.
89 static bool IsSignInEnabled();
91 // Returns the EasyUnlockService type.
92 virtual Type
GetType() const = 0;
94 // Returns the user currently associated with the service.
95 virtual std::string
GetUserEmail() const = 0;
97 // Launches Easy Unlock setup app.
98 virtual void LaunchSetup() = 0;
100 // Gets/Sets/Clears the permit access for the local device.
101 virtual const base::DictionaryValue
* GetPermitAccess() const = 0;
102 virtual void SetPermitAccess(const base::DictionaryValue
& permit
) = 0;
103 virtual void ClearPermitAccess() = 0;
105 // Gets/Sets the remote devices list.
106 virtual const base::ListValue
* GetRemoteDevices() const = 0;
107 virtual void SetRemoteDevices(const base::ListValue
& devices
) = 0;
109 // Runs the flow for turning Easy unlock off.
110 virtual void RunTurnOffFlow() = 0;
112 // Resets the turn off flow if one is in progress.
113 virtual void ResetTurnOffFlow() = 0;
115 // Returns the current turn off flow status.
116 virtual TurnOffFlowStatus
GetTurnOffFlowStatus() const = 0;
118 // Gets the challenge bytes for the user currently associated with the
120 virtual std::string
GetChallenge() const = 0;
122 // Retrieved wrapped secret that should be used to unlock cryptohome for the
123 // user currently associated with the service. If the service does not support
124 // signin (i.e. service for a regular profile) or there is no secret available
125 // for the user, returns an empty string.
126 virtual std::string
GetWrappedSecret() const = 0;
128 // Records metrics for Easy sign-in outcome for the given user.
129 virtual void RecordEasySignInOutcome(const std::string
& user_id
,
130 bool success
) const = 0;
132 // Records metrics for password based flow for the given user.
133 virtual void RecordPasswordLoginEvent(const std::string
& user_id
) const = 0;
135 // Starts auto pairing.
136 typedef base::Callback
<void(bool success
, const std::string
& error
)>
137 AutoPairingResultCallback
;
138 virtual void StartAutoPairing(const AutoPairingResultCallback
& callback
) = 0;
140 // Sets auto pairing result.
141 virtual void SetAutoPairingResult(bool success
, const std::string
& error
) = 0;
143 // Sets the service up and schedules service initialization.
144 void Initialize(scoped_ptr
<EasyUnlockAppManager
> app_manager
);
146 // Whether easy unlock is allowed to be used. If the controlling preference
147 // is set (from policy), this returns the preference value. Otherwise, it is
148 // permitted if the flag is enabled.
149 bool IsAllowed() const;
151 // Whether Easy Unlock is currently enabled for this user.
152 bool IsEnabled() const;
154 // Sets the hardlock state for the associated user.
155 void SetHardlockState(EasyUnlockScreenlockStateHandler::HardlockState state
);
157 // Returns the hardlock state for the associated user.
158 EasyUnlockScreenlockStateHandler::HardlockState
GetHardlockState() const;
160 // Gets the persisted hardlock state. Return true if there is persisted
161 // hardlock state and the value would be set to |state|. Otherwise,
162 // returns false and |state| is unchanged.
163 bool GetPersistedHardlockState(
164 EasyUnlockScreenlockStateHandler::HardlockState
* state
) const;
166 // Shows the hardlock or connecting state as initial UI before cryptohome
167 // keys checking and state update from the app.
168 void ShowInitialUserState();
170 // Updates the user pod on the signin/lock screen for the user associated with
171 // the service to reflect the provided screenlock state.
172 bool UpdateScreenlockState(EasyUnlockScreenlockStateHandler::State state
);
174 // Returns the screenlock state if it is available. Otherwise STATE_INACTIVE
176 EasyUnlockScreenlockStateHandler::State
GetScreenlockState();
178 // Starts an auth attempt for the user associated with the service. The
179 // attempt type (unlock vs. signin) will depend on the service type.
180 void AttemptAuth(const std::string
& user_id
);
182 // Similar to above but a callback is invoked after the auth attempt is
183 // finalized instead of default unlock/sign-in.
184 typedef EasyUnlockAuthAttempt::FinalizedCallback AttemptAuthCallback
;
185 void AttemptAuth(const std::string
& user_id
,
186 const AttemptAuthCallback
& callback
);
188 // Finalizes the previously started auth attempt for easy unlock. If called on
189 // signin profile service, it will cancel the current auth attempt if one
191 void FinalizeUnlock(bool success
);
193 // Finalizes previously started auth attempt for easy signin. If called on
194 // regular profile service, it will cancel the current auth attempt if one
196 void FinalizeSignin(const std::string
& secret
);
198 // Handles Easy Unlock auth failure for the user.
199 void HandleAuthFailure(const std::string
& user_id
);
201 // Checks the consistency between pairing data and cryptohome keys. Set
202 // hardlock state if the two do not match.
203 void CheckCryptohomeKeysAndMaybeHardlock();
205 // Marks the Easy Unlock screen lock state as the one associated with the
206 // trial run initiated by Easy Unlock app.
209 // Records that the user clicked on the lock icon during the trial run
210 // initiated by the Easy Unlock app.
211 void RecordClickOnLockIcon();
213 void AddObserver(EasyUnlockServiceObserver
* observer
);
214 void RemoveObserver(EasyUnlockServiceObserver
* observer
);
217 explicit EasyUnlockService(Profile
* profile
);
218 ~EasyUnlockService() override
;
220 // Does a service type specific initialization.
221 virtual void InitializeInternal() = 0;
223 // Does a service type specific shutdown. Called from |Shutdown|.
224 virtual void ShutdownInternal() = 0;
226 // Service type specific tests for whether the service is allowed. Returns
227 // false if service is not allowed. If true is returned, the service may still
228 // not be allowed if common tests fail (e.g. if Bluetooth is not available).
229 virtual bool IsAllowedInternal() const = 0;
231 // Called while processing a user gesture to unlock the screen using Easy
232 // Unlock, just before the screen is unlocked.
233 virtual void OnWillFinalizeUnlock(bool success
) = 0;
235 // Called when the local device resumes after a suspend.
236 virtual void OnSuspendDone() = 0;
238 // KeyedService override:
239 void Shutdown() override
;
241 // Exposes the profile to which the service is attached to subclasses.
242 const Profile
* profile() const { return profile_
; }
243 Profile
* profile() { return profile_
; }
245 // Opens an Easy Unlock Setup app window.
248 // Reloads the Easy unlock component app if it's loaded and resets the lock
250 void ReloadAppAndLockScreen();
252 // Checks whether Easy unlock should be running and updates app state.
253 void UpdateAppState();
255 // Disables easy unlock app without affecting lock screen state.
256 // Used primarily by signin service when user logged in state changes to
257 // logged in but before screen gets unlocked. At this point service shutdown
258 // is imminent and the app can be safely unloaded, but, for esthetic reasons,
259 // the lock screen UI should remain unchanged until the screen unlocks.
260 void DisableAppWithoutResettingScreenlockState();
262 // Notifies the easy unlock app that the user state has been updated.
263 void NotifyUserUpdated();
265 // Notifies observers that the turn off flow status changed.
266 void NotifyTurnOffOperationStatusChanged();
268 // Resets the screenlock state set by this service.
269 void ResetScreenlockState();
271 // Updates |screenlock_state_handler_|'s hardlocked state.
272 void SetScreenlockHardlockedState(
273 EasyUnlockScreenlockStateHandler::HardlockState state
);
275 const EasyUnlockScreenlockStateHandler
* screenlock_state_handler() const {
276 return screenlock_state_handler_
.get();
279 // Saves hardlock state for the given user. Update UI if the currently
280 // associated user is the same.
281 void SetHardlockStateForUser(
282 const std::string
& user_id
,
283 EasyUnlockScreenlockStateHandler::HardlockState state
);
285 // Returns the authentication event for a recent password sign-in or unlock,
286 // according to the current state of the service.
287 EasyUnlockAuthEvent
GetPasswordAuthEvent() const;
290 // A class to detect whether a bluetooth adapter is present.
291 class BluetoothDetector
;
293 // Initializes the service after EasyUnlockAppManager is ready.
294 void InitializeOnAppManagerReady();
296 // Gets |screenlock_state_handler_|. Returns NULL if Easy Unlock is not
297 // allowed. Otherwise, if |screenlock_state_handler_| is not set, an instance
298 // is created. Do not cache the returned value, as it may go away if Easy
299 // Unlock gets disabled.
300 EasyUnlockScreenlockStateHandler
* GetScreenlockStateHandler();
302 // Callback when Bluetooth adapter present state changes.
303 void OnBluetoothAdapterPresentChanged();
305 #if defined(OS_CHROMEOS)
306 // Callback for get key operation from CheckCryptohomeKeysAndMaybeHardlock.
307 void OnCryptohomeKeysFetchedForChecking(
308 const std::string
& user_id
,
309 const std::set
<std::string
> paired_devices
,
311 const chromeos::EasyUnlockDeviceKeyDataList
& key_data_list
);
314 // Updates the service to state for handling system suspend.
315 void PrepareForSuspend();
317 void EnsureTpmKeyPresentIfNeeded();
321 scoped_ptr
<EasyUnlockAppManager
> app_manager_
;
323 // Created lazily in |GetScreenlockStateHandler|.
324 scoped_ptr
<EasyUnlockScreenlockStateHandler
> screenlock_state_handler_
;
326 // The handler for the current auth attempt. Set iff an auth attempt is in
328 scoped_ptr
<EasyUnlockAuthAttempt
> auth_attempt_
;
330 scoped_ptr
<BluetoothDetector
> bluetooth_detector_
;
332 #if defined(OS_CHROMEOS)
333 // Monitors suspend and wake state of ChromeOS.
335 scoped_ptr
<PowerMonitor
> power_monitor_
;
338 // Whether the service has been shut down.
341 bool tpm_key_checked_
;
343 ObserverList
<EasyUnlockServiceObserver
> observers_
;
345 base::WeakPtrFactory
<EasyUnlockService
> weak_ptr_factory_
;
347 DISALLOW_COPY_AND_ASSIGN(EasyUnlockService
);
350 #endif // CHROME_BROWSER_SIGNIN_EASY_UNLOCK_SERVICE_H_