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.
4
5#ifndef CHROME_BROWSER_SIGNIN_EASY_UNLOCK_SERVICE_SIGNIN_CHROMEOS_H_
6#define CHROME_BROWSER_SIGNIN_EASY_UNLOCK_SERVICE_SIGNIN_CHROMEOS_H_
7
8#include <map>
9#include <string>
10
11#include "base/macros.h"
12#include "base/memory/scoped_ptr.h"
13#include "base/memory/weak_ptr.h"
14#include "base/values.h"
15#include "chrome/browser/chromeos/login/easy_unlock/easy_unlock_types.h"
16#include "chrome/browser/signin/easy_unlock_service.h"
17#include "chrome/browser/signin/screenlock_bridge.h"
18#include "chromeos/login/login_state.h"
19
20// EasyUnlockService instance that should be used for signin profile.
21class EasyUnlockServiceSignin : public EasyUnlockService,
22                                public ScreenlockBridge::Observer,
23                                public chromeos::LoginState::Observer {
24 public:
25  explicit EasyUnlockServiceSignin(Profile* profile);
26  virtual ~EasyUnlockServiceSignin();
27
28 private:
29  // The load state of a user's cryptohome key data.
30  enum UserDataState {
31    // Initial state, the key data is empty and not being loaded.
32    USER_DATA_STATE_INITIAL,
33    // The key data is empty, but being loaded.
34    USER_DATA_STATE_LOADING,
35    // The key data has been loaded.
36    USER_DATA_STATE_LOADED
37  };
38
39  // Structure containing a user's key data loaded from cryptohome.
40  struct UserData {
41    UserData();
42    ~UserData();
43
44    // The loading state of the data.
45    UserDataState state;
46
47    // The data as returned from cryptohome.
48    chromeos::EasyUnlockDeviceKeyDataList devices;
49
50    // The list of remote device dictionaries understood by Easy unlock app.
51    // This will be returned by |GetRemoteDevices| method.
52    base::ListValue remote_devices_value;
53
54   private:
55    DISALLOW_COPY_AND_ASSIGN(UserData);
56  };
57
58  // EasyUnlockService implementation:
59  virtual EasyUnlockService::Type GetType() const OVERRIDE;
60  virtual std::string GetUserEmail() const OVERRIDE;
61  virtual void LaunchSetup() OVERRIDE;
62  virtual const base::DictionaryValue* GetPermitAccess() const OVERRIDE;
63  virtual void SetPermitAccess(const base::DictionaryValue& permit) OVERRIDE;
64  virtual void ClearPermitAccess() OVERRIDE;
65  virtual const base::ListValue* GetRemoteDevices() const OVERRIDE;
66  virtual void SetRemoteDevices(const base::ListValue& devices) OVERRIDE;
67  virtual void ClearRemoteDevices() OVERRIDE;
68  virtual void RunTurnOffFlow() OVERRIDE;
69  virtual void ResetTurnOffFlow() OVERRIDE;
70  virtual TurnOffFlowStatus GetTurnOffFlowStatus() const OVERRIDE;
71  virtual std::string GetChallenge() const OVERRIDE;
72  virtual std::string GetWrappedSecret() const OVERRIDE;
73  virtual void RecordEasySignInOutcome(const std::string& user_id,
74                                       bool success) const OVERRIDE;
75  virtual void RecordPasswordLoginEvent(
76      const std::string& user_id) const OVERRIDE;
77  virtual void InitializeInternal() OVERRIDE;
78  virtual void ShutdownInternal() OVERRIDE;
79  virtual bool IsAllowedInternal() OVERRIDE;
80
81  // ScreenlockBridge::Observer implementation:
82  virtual void OnScreenDidLock() OVERRIDE;
83  virtual void OnScreenDidUnlock() OVERRIDE;
84  virtual void OnFocusedUserChanged(const std::string& user_id) OVERRIDE;
85
86  // chromeos::LoginState::Observer implementation:
87  virtual void LoggedInStateChanged() OVERRIDE;
88
89  // Loads the device data associated with the user's Easy unlock keys from
90  // crypthome.
91  void LoadCurrentUserDataIfNeeded();
92
93  // Callback invoked when the user's device data is loaded from cryptohome.
94  void OnUserDataLoaded(
95      const std::string& user_id,
96      bool success,
97      const chromeos::EasyUnlockDeviceKeyDataList& data);
98
99  // If the device data has been loaded for the current user, returns it.
100  // Otherwise, returns NULL.
101  const UserData* FindLoadedDataForCurrentUser() const;
102
103  // User id of the user currently associated with the service.
104  std::string user_id_;
105
106  // Maps user ids to their fetched cryptohome key data.
107  std::map<std::string, UserData*> user_data_;
108
109  // Whether failed attempts to load user data should be retried.
110  // This is to handle case where cryptohome daemon is not started in time the
111  // service attempts to load some data. Retries will be allowed only until the
112  // first data load finishes (even if it fails).
113  bool allow_cryptohome_backoff_;
114
115  // Whether the service has been successfully initialized, and has not been
116  // shut down.
117  bool service_active_;
118
119  base::WeakPtrFactory<EasyUnlockServiceSignin> weak_ptr_factory_;
120
121  DISALLOW_COPY_AND_ASSIGN(EasyUnlockServiceSignin);
122};
123
124#endif  // CHROME_BROWSER_SIGNIN_EASY_UNLOCK_SERVICE_SIGNIN_CHROMEOS_H_
125