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/macros.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/observer_list.h"
15 #include "chrome/browser/signin/easy_unlock_screenlock_state_handler.h"
16 #include "components/keyed_service/core/keyed_service.h"
18 #if defined(OS_CHROMEOS)
19 #include "chrome/browser/chromeos/login/easy_unlock/easy_unlock_types.h"
23 class DictionaryValue
;
27 namespace user_manager
{
31 namespace user_prefs
{
32 class PrefRegistrySyncable
;
35 class EasyUnlockAuthAttempt
;
36 class EasyUnlockServiceObserver
;
38 class PrefRegistrySimple
;
40 class EasyUnlockService
: public KeyedService
{
42 enum TurnOffFlowStatus
{
53 // Gets EasyUnlockService instance.
54 static EasyUnlockService
* Get(Profile
* profile
);
56 // Gets EasyUnlockService instance associated with a user if the user is
57 // logged in and his profile is initialized.
58 static EasyUnlockService
* GetForUser(const user_manager::User
& user
);
60 // Registers Easy Unlock profile preferences.
61 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable
* registry
);
63 // Registers Easy Unlock local state entries.
64 static void RegisterPrefs(PrefRegistrySimple
* registry
);
66 // Removes the hardlock state for the given user.
67 static void ResetLocalStateForUser(const std::string
& user_id
);
69 // Returns true if Easy sign-in is enabled.
70 static bool IsSignInEnabled();
72 // Returns the EasyUnlockService type.
73 virtual Type
GetType() const = 0;
75 // Returns the user currently associated with the service.
76 virtual std::string
GetUserEmail() const = 0;
78 // Launches Easy Unlock Setup app.
79 virtual void LaunchSetup() = 0;
81 // Gets/Sets/Clears the permit access for the local device.
82 virtual const base::DictionaryValue
* GetPermitAccess() const = 0;
83 virtual void SetPermitAccess(const base::DictionaryValue
& permit
) = 0;
84 virtual void ClearPermitAccess() = 0;
86 // Gets/Sets/Clears the remote devices list.
87 virtual const base::ListValue
* GetRemoteDevices() const = 0;
88 virtual void SetRemoteDevices(const base::ListValue
& devices
) = 0;
89 virtual void ClearRemoteDevices() = 0;
91 // Runs the flow for turning Easy unlock off.
92 virtual void RunTurnOffFlow() = 0;
94 // Resets the turn off flow if one is in progress.
95 virtual void ResetTurnOffFlow() = 0;
97 // Returns the current turn off flow status.
98 virtual TurnOffFlowStatus
GetTurnOffFlowStatus() const = 0;
100 // Gets the challenge bytes for the user currently associated with the
102 virtual std::string
GetChallenge() const = 0;
104 // Retrieved wrapped secret that should be used to unlock cryptohome for the
105 // user currently associated with the service. If the service does not support
106 // signin (i.e. service for a regular profile) or there is no secret available
107 // for the user, returns an empty string.
108 virtual std::string
GetWrappedSecret() const = 0;
110 // Whether easy unlock is allowed to be used. If the controlling preference
111 // is set (from policy), this returns the preference value. Otherwise, it is
112 // permitted either the flag is enabled or its field trial is enabled.
115 // Sets the hardlock state for the associated user.
116 void SetHardlockState(EasyUnlockScreenlockStateHandler::HardlockState state
);
118 // Returns the hardlock state for the associated user.
119 EasyUnlockScreenlockStateHandler::HardlockState
GetHardlockState() const;
121 // Ensures the hardlock state is visible even when there is no cryptohome
122 // keys and no state update from the app.
123 void MaybeShowHardlockUI();
125 // Updates the user pod on the signin/lock screen for the user associated with
126 // the service to reflect the provided screenlock state.
127 bool UpdateScreenlockState(EasyUnlockScreenlockStateHandler::State state
);
129 // Starts an auth attempt for the user associated with the service. The
130 // attempt type (unlock vs. signin) will depend on the service type.
131 void AttemptAuth(const std::string
& user_id
);
133 // Finalizes the previously started auth attempt for easy unlock. If called on
134 // signin profile service, it will cancel the current auth attempt if one
136 void FinalizeUnlock(bool success
);
138 // Finalizes previously started auth attempt for easy signin. If called on
139 // regular profile service, it will cancel the current auth attempt if one
141 void FinalizeSignin(const std::string
& secret
);
143 // Checks the consistency between pairing data and cryptohome keys. Set
144 // hardlock state if the two do not match.
145 void CheckCryptohomeKeysAndMaybeHardlock();
147 // Marks the Easy Unlock screen lock state as the one associated with the
148 // trial run initiated by Easy Unlock app.
151 void AddObserver(EasyUnlockServiceObserver
* observer
);
152 void RemoveObserver(EasyUnlockServiceObserver
* observer
);
155 explicit EasyUnlockService(Profile
* profile
);
156 ~EasyUnlockService() override
;
158 // Does a service type specific initialization.
159 virtual void InitializeInternal() = 0;
161 // Does a service type specific shutdown. Called from |Shutdown|.
162 virtual void ShutdownInternal() = 0;
164 // Service type specific tests for whether the service is allowed. Returns
165 // false if service is not allowed. If true is returned, the service may still
166 // not be allowed if common tests fail (e.g. if Bluetooth is not available).
167 virtual bool IsAllowedInternal() = 0;
169 // KeyedService override:
170 void Shutdown() override
;
172 // Exposes the profile to which the service is attached to subclasses.
173 Profile
* profile() const { return profile_
; }
175 // Installs the Easy unlock component app if it isn't installed and enables
176 // the app if it is disabled.
179 // Disables the Easy unlock component app if it's loaded.
180 void DisableAppIfLoaded();
182 // Unloads the Easy unlock component app if it's loaded.
185 // Reloads the Easy unlock component app if it's loaded.
188 // Checks whether Easy unlock should be running and updates app state.
189 void UpdateAppState();
191 // Notifies the easy unlock app that the user state has been updated.
192 void NotifyUserUpdated();
194 // Notifies observers that the turn off flow status changed.
195 void NotifyTurnOffOperationStatusChanged();
197 // Resets the screenlock state set by this service.
198 void ResetScreenlockState();
200 // Updates |screenlock_state_handler_|'s hardlocked state.
201 void SetScreenlockHardlockedState(
202 EasyUnlockScreenlockStateHandler::HardlockState state
);
205 // A class to detect whether a bluetooth adapter is present.
206 class BluetoothDetector
;
208 // Initializes the service after ExtensionService is ready.
211 // Gets |screenlock_state_handler_|. Returns NULL if Easy Unlock is not
212 // allowed. Otherwise, if |screenlock_state_handler_| is not set, an instance
213 // is created. Do not cache the returned value, as it may go away if Easy
214 // Unlock gets disabled.
215 EasyUnlockScreenlockStateHandler
* GetScreenlockStateHandler();
217 // Callback when Bluetooth adapter present state changes.
218 void OnBluetoothAdapterPresentChanged();
220 // Saves hardlock state for the given user. Update UI if the currently
221 // associated user is the same.
222 void SetHardlockStateForUser(
223 const std::string
& user_id
,
224 EasyUnlockScreenlockStateHandler::HardlockState state
);
226 #if defined(OS_CHROMEOS)
227 // Callback for get key operation from CheckCryptohomeKeysAndMaybeHardlock.
228 void OnCryptohomeKeysFetchedForChecking(
229 const std::string
& user_id
,
230 const std::set
<std::string
> paired_devices
,
232 const chromeos::EasyUnlockDeviceKeyDataList
& key_data_list
);
235 // Updates the service to state for handling system suspend.
236 void PrepareForSuspend();
240 // Created lazily in |GetScreenlockStateHandler|.
241 scoped_ptr
<EasyUnlockScreenlockStateHandler
> screenlock_state_handler_
;
243 // The handler for the current auth attempt. Set iff an auth attempt is in
245 scoped_ptr
<EasyUnlockAuthAttempt
> auth_attempt_
;
247 scoped_ptr
<BluetoothDetector
> bluetooth_detector_
;
249 #if defined(OS_CHROMEOS)
250 // Monitors suspend and wake state of ChromeOS.
252 scoped_ptr
<PowerMonitor
> power_monitor_
;
255 // Whether the service has been shut down.
258 ObserverList
<EasyUnlockServiceObserver
> observers_
;
260 base::WeakPtrFactory
<EasyUnlockService
> weak_ptr_factory_
;
262 DISALLOW_COPY_AND_ASSIGN(EasyUnlockService
);
265 #endif // CHROME_BROWSER_SIGNIN_EASY_UNLOCK_SERVICE_H_