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_CHROMEOS_LOGIN_EASY_UNLOCK_EASY_UNLOCK_KEY_MANAGER_H_
6 #define CHROME_BROWSER_CHROMEOS_LOGIN_EASY_UNLOCK_EASY_UNLOCK_KEY_MANAGER_H_
12 #include "base/callback.h"
13 #include "base/macros.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/stl_util.h"
16 #include "chrome/browser/chromeos/login/easy_unlock/easy_unlock_get_keys_operation.h"
17 #include "chrome/browser/chromeos/login/easy_unlock/easy_unlock_refresh_keys_operation.h"
18 #include "chrome/browser/chromeos/login/easy_unlock/easy_unlock_types.h"
21 class DictionaryValue
;
29 // A class to manage Easy unlock cryptohome keys.
30 class EasyUnlockKeyManager
{
32 typedef EasyUnlockRefreshKeysOperation::RefreshKeysCallback
34 typedef EasyUnlockGetKeysOperation::GetKeysCallback GetDeviceDataListCallback
;
36 EasyUnlockKeyManager();
37 ~EasyUnlockKeyManager();
39 // Nukes existing Easy unlock keys and creates new ones for the given
40 // |remote_devices| and the given |user_context|. |user_context| must have
41 // secret to allow keys to be created.
42 void RefreshKeys(const UserContext
& user_context
,
43 const base::ListValue
& remote_devices
,
44 const RefreshKeysCallback
& callback
);
46 // Retrieves the remote device data from cryptohome keys for the given
48 void GetDeviceDataList(const UserContext
& user_context
,
49 const GetDeviceDataListCallback
& callback
);
51 // Helpers to convert between DeviceData and remote device dictionary.
52 // DeviceDataToRemoteDeviceDictionary fills the remote device dictionary and
53 // always succeeds. RemoteDeviceDictionaryToDeviceData returns false if the
54 // conversion fails (missing required propery). Note that
55 // EasyUnlockDeviceKeyData contains a sub set of the remote device dictionary.
56 static void DeviceDataToRemoteDeviceDictionary(
57 const std::string
& user_id
,
58 const EasyUnlockDeviceKeyData
& data
,
59 base::DictionaryValue
* dict
);
60 static bool RemoteDeviceDictionaryToDeviceData(
61 const base::DictionaryValue
& dict
,
62 EasyUnlockDeviceKeyData
* data
);
64 // Helpers to convert between EasyUnlockDeviceKeyDataList and remote devices
66 static void DeviceDataListToRemoteDeviceList(
67 const std::string
& user_id
,
68 const EasyUnlockDeviceKeyDataList
& data_list
,
69 base::ListValue
* device_list
);
70 static bool RemoteDeviceListToDeviceDataList(
71 const base::ListValue
& device_list
,
72 EasyUnlockDeviceKeyDataList
* data_list
);
74 // Gets key label for the given key index.
75 static std::string
GetKeyLabel(size_t key_index
);
78 // Runs the next operation if there is one. We first run all the operations in
79 // the |write_operation_queue_| and then run all the operations in the
80 // |read_operation_queue_|.
81 void RunNextOperation();
83 // Called when the TPM key is ready to be used for creating Easy Unlock key
85 void RefreshKeysWithTpmKeyPresent(const UserContext
& user_context
,
86 base::ListValue
* remote_devices
,
87 const RefreshKeysCallback
& callback
);
89 // Returns true if there are pending operations.
90 bool HasPendingOperations() const;
92 // Callback invoked after refresh keys operation.
93 void OnKeysRefreshed(const RefreshKeysCallback
& callback
,
96 // Callback invoked after get keys op.
97 void OnKeysFetched(const GetDeviceDataListCallback
& callback
,
99 const EasyUnlockDeviceKeyDataList
& fetched_data
);
101 // Queued operations are stored as raw pointers, as scoped_ptrs may not behave
102 // nicely with std::deque.
103 using WriteOperationQueue
= std::deque
<EasyUnlockRefreshKeysOperation
*>;
104 using ReadOperationQueue
= std::deque
<EasyUnlockGetKeysOperation
*>;
105 WriteOperationQueue write_operation_queue_
;
106 ReadOperationQueue read_operation_queue_
;
108 // Scopes the raw operation pointers to the lifetime of this object.
109 STLElementDeleter
<WriteOperationQueue
> write_queue_deleter_
;
110 STLElementDeleter
<ReadOperationQueue
> read_queue_deleter_
;
112 // Stores the current operation in progress. At most one of these variables
113 // can be non-null at any time.
114 scoped_ptr
<EasyUnlockRefreshKeysOperation
> pending_write_operation_
;
115 scoped_ptr
<EasyUnlockGetKeysOperation
> pending_read_operation_
;
117 base::WeakPtrFactory
<EasyUnlockKeyManager
> weak_ptr_factory_
;
119 DISALLOW_COPY_AND_ASSIGN(EasyUnlockKeyManager
);
122 } // namespace chromeos
124 #endif // CHROME_BROWSER_CHROMEOS_LOGIN_EASY_UNLOCK_EASY_UNLOCK_KEY_MANAGER_H_