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/scoped_ptr.h"
15 #include "base/memory/weak_ptr.h"
16 #include "chrome/browser/chromeos/login/easy_unlock/easy_unlock_create_keys_operation.h"
17 #include "chrome/browser/chromeos/login/easy_unlock/easy_unlock_get_keys_operation.h"
18 #include "chrome/browser/chromeos/login/easy_unlock/easy_unlock_remove_keys_operation.h"
19 #include "chrome/browser/chromeos/login/easy_unlock/easy_unlock_types.h"
22 class DictionaryValue;
30 // A class to manage Easy unlock cryptohome keys.
31 class EasyUnlockKeyManager {
33 typedef EasyUnlockCreateKeysOperation::CreateKeysCallback RefreshKeysCallback;
34 typedef EasyUnlockRemoveKeysOperation::RemoveKeysCallback RemoveKeysCallback;
35 typedef EasyUnlockGetKeysOperation::GetKeysCallback GetDeviceDataListCallback;
37 EasyUnlockKeyManager();
38 ~EasyUnlockKeyManager();
40 // Nukes existing Easy unlock keys and creates new ones for the given
41 // |remote_devices| and the given |user_context|. |user_context| must have
42 // secret to allow keys to be created.
43 void RefreshKeys(const UserContext& user_context,
44 const base::ListValue& remote_devices,
45 const RefreshKeysCallback& callback);
47 // Remove Easy unlock keys starting at the given index for the given
49 void RemoveKeys(const UserContext& user_context,
51 const RemoveKeysCallback& callback);
53 // Retrieves the remote device data from cryptohome keys for the given
55 void GetDeviceDataList(const UserContext& user_context,
56 const GetDeviceDataListCallback& callback);
58 // Helpers to convert between DeviceData and remote device dictionary.
59 // DeviceDataToRemoteDeviceDictionary fills the remote device dictionary and
60 // always succeeds. RemoteDeviceDictionaryToDeviceData returns false if the
61 // conversion fails (missing required propery). Note that
62 // EasyUnlockDeviceKeyData contains a sub set of the remote device dictionary.
63 static void DeviceDataToRemoteDeviceDictionary(
64 const std::string& user_id,
65 const EasyUnlockDeviceKeyData& data,
66 base::DictionaryValue* dict);
67 static bool RemoteDeviceDictionaryToDeviceData(
68 const base::DictionaryValue& dict,
69 EasyUnlockDeviceKeyData* data);
71 // Helpers to convert between EasyUnlockDeviceKeyDataList and remote devices
73 static void DeviceDataListToRemoteDeviceList(
74 const std::string& user_id,
75 const EasyUnlockDeviceKeyDataList& data_list,
76 base::ListValue* device_list);
77 static bool RemoteDeviceListToDeviceDataList(
78 const base::ListValue& device_list,
79 EasyUnlockDeviceKeyDataList* data_list);
81 // Gets key label for the given key index.
82 static std::string GetKeyLabel(size_t key_index);
85 // Returns true if there are pending operations.
86 bool HasPendingOperations() const;
88 // Returns the next operations id. Currently only used for get keys ops.
89 int GetNextOperationId();
91 // Runs the first pending op in |pending_ops_|. No-op if |pending_ops_| is
93 void RunNextPendingOp();
95 // Callback invoked after create keys op.
96 void OnKeysCreated(size_t remove_start_index,
97 const RefreshKeysCallback& callback,
100 // Callback invoked after remove keys op.
101 void OnKeysRemoved(const RemoveKeysCallback& callback, bool remove_success);
103 // Callback invoked after get keys op.
104 void OnKeysFetched(int op_id,
105 const GetDeviceDataListCallback& callback,
107 const EasyUnlockDeviceKeyDataList& fetched_data);
111 scoped_ptr<EasyUnlockCreateKeysOperation> create_keys_op_;
112 scoped_ptr<EasyUnlockRemoveKeysOperation> remove_keys_op_;
113 std::map<int, EasyUnlockGetKeysOperation*> get_keys_ops_;
115 std::deque<base::Closure> pending_ops_;
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_