Only grant permissions to new extensions from sync if they have the expected version
[chromium-blink-merge.git] / chrome / browser / signin / easy_unlock_service.h
blobd0ae1ddeb37982a7bffc61f1266bb97e23f4474d
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_
8 #include <set>
9 #include <string>
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/chrome_proximity_auth_client.h"
17 #include "chrome/browser/signin/easy_unlock_auth_attempt.h"
18 #include "chrome/browser/signin/easy_unlock_metrics.h"
19 #include "chrome/browser/signin/easy_unlock_screenlock_state_handler.h"
20 #include "components/keyed_service/core/keyed_service.h"
21 #include "components/proximity_auth/screenlock_state.h"
23 #if defined(OS_CHROMEOS)
24 #include "chrome/browser/chromeos/login/easy_unlock/easy_unlock_types.h"
25 #endif
27 namespace base {
28 class DictionaryValue;
29 class ListValue;
32 namespace user_manager {
33 class User;
36 namespace user_prefs {
37 class PrefRegistrySyncable;
40 namespace proximity_auth {
41 class ProximityAuthBleSystem;
44 class EasyUnlockAppManager;
45 class EasyUnlockServiceObserver;
46 class Profile;
47 class PrefRegistrySimple;
49 class EasyUnlockService : public KeyedService {
50 public:
51 enum TurnOffFlowStatus {
52 IDLE,
53 PENDING,
54 FAIL,
57 enum Type {
58 TYPE_REGULAR,
59 TYPE_SIGNIN
62 // Easy Unlock settings that the user can configure.
63 struct UserSettings {
64 UserSettings();
65 ~UserSettings();
67 // Whether to require the remote device to be in very close proximity
68 // before allowing unlock (~1 feet).
69 bool require_close_proximity;
72 // Gets EasyUnlockService instance.
73 static EasyUnlockService* Get(Profile* profile);
75 // Gets EasyUnlockService instance associated with a user if the user is
76 // logged in and his profile is initialized.
77 static EasyUnlockService* GetForUser(const user_manager::User& user);
79 // Registers Easy Unlock profile preferences.
80 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
82 // Registers Easy Unlock local state entries.
83 static void RegisterPrefs(PrefRegistrySimple* registry);
85 // Removes the hardlock state for the given user.
86 static void ResetLocalStateForUser(const std::string& user_id);
88 // Returns the user's preferences.
89 static UserSettings GetUserSettings(const std::string& user_id);
91 // Returns the identifier for the device.
92 static std::string GetDeviceId();
94 // Returns the EasyUnlockService type.
95 virtual Type GetType() const = 0;
97 // Returns the user currently associated with the service.
98 virtual std::string GetUserEmail() const = 0;
100 // Launches Easy Unlock setup app.
101 virtual void LaunchSetup() = 0;
103 // Gets/Sets/Clears the permit access for the local device.
104 virtual const base::DictionaryValue* GetPermitAccess() const = 0;
105 virtual void SetPermitAccess(const base::DictionaryValue& permit) = 0;
106 virtual void ClearPermitAccess() = 0;
108 // Gets/Sets the remote devices list.
109 virtual const base::ListValue* GetRemoteDevices() const = 0;
110 virtual void SetRemoteDevices(const base::ListValue& devices) = 0;
112 // Runs the flow for turning Easy unlock off.
113 virtual void RunTurnOffFlow() = 0;
115 // Resets the turn off flow if one is in progress.
116 virtual void ResetTurnOffFlow() = 0;
118 // Returns the current turn off flow status.
119 virtual TurnOffFlowStatus GetTurnOffFlowStatus() const = 0;
121 // Gets the challenge bytes for the user currently associated with the
122 // service.
123 virtual std::string GetChallenge() const = 0;
125 // Retrieved wrapped secret that should be used to unlock cryptohome for the
126 // user currently associated with the service. If the service does not support
127 // signin (i.e. service for a regular profile) or there is no secret available
128 // for the user, returns an empty string.
129 virtual std::string GetWrappedSecret() const = 0;
131 // Records metrics for Easy sign-in outcome for the given user.
132 virtual void RecordEasySignInOutcome(const std::string& user_id,
133 bool success) const = 0;
135 // Records metrics for password based flow for the given user.
136 virtual void RecordPasswordLoginEvent(const std::string& user_id) const = 0;
138 // Starts auto pairing.
139 typedef base::Callback<void(bool success, const std::string& error)>
140 AutoPairingResultCallback;
141 virtual void StartAutoPairing(const AutoPairingResultCallback& callback) = 0;
143 // Sets auto pairing result.
144 virtual void SetAutoPairingResult(bool success, const std::string& error) = 0;
146 // Sets the service up and schedules service initialization.
147 void Initialize(scoped_ptr<EasyUnlockAppManager> app_manager);
149 // Whether easy unlock is allowed to be used. If the controlling preference
150 // is set (from policy), this returns the preference value. Otherwise, it is
151 // permitted if the flag is enabled.
152 bool IsAllowed() const;
154 // Whether Easy Unlock is currently enabled for this user.
155 bool IsEnabled() const;
157 // Sets the hardlock state for the associated user.
158 void SetHardlockState(EasyUnlockScreenlockStateHandler::HardlockState state);
160 // Returns the hardlock state for the associated user.
161 EasyUnlockScreenlockStateHandler::HardlockState GetHardlockState() const;
163 // Gets the persisted hardlock state. Return true if there is persisted
164 // hardlock state and the value would be set to |state|. Otherwise,
165 // returns false and |state| is unchanged.
166 bool GetPersistedHardlockState(
167 EasyUnlockScreenlockStateHandler::HardlockState* state) const;
169 // Shows the hardlock or connecting state as initial UI before cryptohome
170 // keys checking and state update from the app.
171 void ShowInitialUserState();
173 // Updates the user pod on the signin/lock screen for the user associated with
174 // the service to reflect the provided screenlock state.
175 bool UpdateScreenlockState(proximity_auth::ScreenlockState state);
177 // Returns the screenlock state if it is available. Otherwise STATE_INACTIVE
178 // is returned.
179 proximity_auth::ScreenlockState GetScreenlockState();
181 // Starts an auth attempt for the user associated with the service. The
182 // attempt type (unlock vs. signin) will depend on the service type.
183 void AttemptAuth(const std::string& user_id);
185 // Similar to above but a callback is invoked after the auth attempt is
186 // finalized instead of default unlock/sign-in.
187 typedef EasyUnlockAuthAttempt::FinalizedCallback AttemptAuthCallback;
188 void AttemptAuth(const std::string& user_id,
189 const AttemptAuthCallback& callback);
191 // Finalizes the previously started auth attempt for easy unlock. If called on
192 // signin profile service, it will cancel the current auth attempt if one
193 // exists.
194 void FinalizeUnlock(bool success);
196 // Finalizes previously started auth attempt for easy signin. If called on
197 // regular profile service, it will cancel the current auth attempt if one
198 // exists.
199 void FinalizeSignin(const std::string& secret);
201 // Handles Easy Unlock auth failure for the user.
202 void HandleAuthFailure(const std::string& user_id);
204 // Checks the consistency between pairing data and cryptohome keys. Set
205 // hardlock state if the two do not match.
206 void CheckCryptohomeKeysAndMaybeHardlock();
208 // Marks the Easy Unlock screen lock state as the one associated with the
209 // trial run initiated by Easy Unlock app.
210 void SetTrialRun();
212 // Records that the user clicked on the lock icon during the trial run
213 // initiated by the Easy Unlock app.
214 void RecordClickOnLockIcon();
216 void AddObserver(EasyUnlockServiceObserver* observer);
217 void RemoveObserver(EasyUnlockServiceObserver* observer);
219 ChromeProximityAuthClient* proximity_auth_client() {
220 return &proximity_auth_client_;
223 protected:
224 explicit EasyUnlockService(Profile* profile);
225 ~EasyUnlockService() override;
227 // Does a service type specific initialization.
228 virtual void InitializeInternal() = 0;
230 // Does a service type specific shutdown. Called from |Shutdown|.
231 virtual void ShutdownInternal() = 0;
233 // Service type specific tests for whether the service is allowed. Returns
234 // false if service is not allowed. If true is returned, the service may still
235 // not be allowed if common tests fail (e.g. if Bluetooth is not available).
236 virtual bool IsAllowedInternal() const = 0;
238 // Called while processing a user gesture to unlock the screen using Easy
239 // Unlock, just before the screen is unlocked.
240 virtual void OnWillFinalizeUnlock(bool success) = 0;
242 // Called when the local device resumes after a suspend.
243 virtual void OnSuspendDone() = 0;
245 // KeyedService override:
246 void Shutdown() override;
248 // Exposes the profile to which the service is attached to subclasses.
249 const Profile* profile() const { return profile_; }
250 Profile* profile() { return profile_; }
252 // Opens an Easy Unlock Setup app window.
253 void OpenSetupApp();
255 // Reloads the Easy unlock component app if it's loaded and resets the lock
256 // screen state.
257 void ReloadAppAndLockScreen();
259 // Checks whether Easy unlock should be running and updates app state.
260 void UpdateAppState();
262 // Disables easy unlock app without affecting lock screen state.
263 // Used primarily by signin service when user logged in state changes to
264 // logged in but before screen gets unlocked. At this point service shutdown
265 // is imminent and the app can be safely unloaded, but, for esthetic reasons,
266 // the lock screen UI should remain unchanged until the screen unlocks.
267 void DisableAppWithoutResettingScreenlockState();
269 // Notifies the easy unlock app that the user state has been updated.
270 void NotifyUserUpdated();
272 // Notifies observers that the turn off flow status changed.
273 void NotifyTurnOffOperationStatusChanged();
275 // Resets the screenlock state set by this service.
276 void ResetScreenlockState();
278 // Updates |screenlock_state_handler_|'s hardlocked state.
279 void SetScreenlockHardlockedState(
280 EasyUnlockScreenlockStateHandler::HardlockState state);
282 const EasyUnlockScreenlockStateHandler* screenlock_state_handler() const {
283 return screenlock_state_handler_.get();
286 // Saves hardlock state for the given user. Update UI if the currently
287 // associated user is the same.
288 void SetHardlockStateForUser(
289 const std::string& user_id,
290 EasyUnlockScreenlockStateHandler::HardlockState state);
292 // Returns the authentication event for a recent password sign-in or unlock,
293 // according to the current state of the service.
294 EasyUnlockAuthEvent GetPasswordAuthEvent() const;
296 private:
297 // A class to detect whether a bluetooth adapter is present.
298 class BluetoothDetector;
300 // Initializes the service after EasyUnlockAppManager is ready.
301 void InitializeOnAppManagerReady();
303 // Gets |screenlock_state_handler_|. Returns NULL if Easy Unlock is not
304 // allowed. Otherwise, if |screenlock_state_handler_| is not set, an instance
305 // is created. Do not cache the returned value, as it may go away if Easy
306 // Unlock gets disabled.
307 EasyUnlockScreenlockStateHandler* GetScreenlockStateHandler();
309 // Callback when Bluetooth adapter present state changes.
310 void OnBluetoothAdapterPresentChanged();
312 #if defined(OS_CHROMEOS)
313 // Callback for get key operation from CheckCryptohomeKeysAndMaybeHardlock.
314 void OnCryptohomeKeysFetchedForChecking(
315 const std::string& user_id,
316 const std::set<std::string> paired_devices,
317 bool success,
318 const chromeos::EasyUnlockDeviceKeyDataList& key_data_list);
319 #endif
321 // Updates the service to state for handling system suspend.
322 void PrepareForSuspend();
324 void EnsureTpmKeyPresentIfNeeded();
326 Profile* const profile_;
328 ChromeProximityAuthClient proximity_auth_client_;
330 scoped_ptr<EasyUnlockAppManager> app_manager_;
332 // Created lazily in |GetScreenlockStateHandler|.
333 scoped_ptr<EasyUnlockScreenlockStateHandler> screenlock_state_handler_;
335 // The handler for the current auth attempt. Set iff an auth attempt is in
336 // progress.
337 scoped_ptr<EasyUnlockAuthAttempt> auth_attempt_;
339 scoped_ptr<BluetoothDetector> bluetooth_detector_;
341 // The proximity auth over Bluetooth Low Energy system. This is main entry
342 // point to bootstap Smart Lock to discover phones over Bluetooth Low
343 // Energy.
344 scoped_ptr<proximity_auth::ProximityAuthBleSystem> proximity_auth_ble_system_;
346 #if defined(OS_CHROMEOS)
347 // Monitors suspend and wake state of ChromeOS.
348 class PowerMonitor;
349 scoped_ptr<PowerMonitor> power_monitor_;
350 #endif
352 // Whether the service has been shut down.
353 bool shut_down_;
355 bool tpm_key_checked_;
357 base::ObserverList<EasyUnlockServiceObserver> observers_;
359 base::WeakPtrFactory<EasyUnlockService> weak_ptr_factory_;
361 DISALLOW_COPY_AND_ASSIGN(EasyUnlockService);
364 #endif // CHROME_BROWSER_SIGNIN_EASY_UNLOCK_SERVICE_H_