OLD | NEW |
---|---|
(Empty) | |
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 #include "chrome/browser/chromeos/login/easy_unlock/easy_unlock_key_manager.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/logging.h" | |
9 #include "base/stl_util.h" | |
10 #include "base/strings/stringprintf.h" | |
11 #include "base/values.h" | |
12 | |
13 namespace chromeos { | |
14 | |
15 namespace { | |
16 | |
17 const char kKeyBluetoothAddress[] = "bluetoothAddress"; | |
18 const char kKeyPermitRecord[] = "permitRecord"; | |
19 const char kKeyId[] = "id"; | |
20 const char kKeyPermitId[] = "permitRecord.id"; | |
21 const char kKeyPsk[] = "psk"; | |
22 | |
23 const char kKeyLabelPrefix[] = "easy-unlock-"; | |
24 | |
25 } // namespace | |
26 | |
27 EasyUnlockKeyManager::EasyUnlockKeyManager() | |
28 : operation_id_(0), | |
29 weak_ptr_factory_(this) { | |
30 } | |
31 | |
32 EasyUnlockKeyManager::~EasyUnlockKeyManager() { | |
33 STLDeleteContainerPairSecondPointers(get_keys_ops_.begin(), | |
34 get_keys_ops_.end()); | |
35 } | |
36 | |
37 void EasyUnlockKeyManager::RefreshKeys(const UserContext& user_context, | |
38 const base::ListValue& remote_devices, | |
39 const CreateKeysCallback& callback) { | |
40 // Must have the secret. | |
41 DCHECK(!user_context.GetKey()->GetSecret().empty()); | |
42 | |
43 EasyUnlockDeviceKeyDataList devices; | |
44 if (!RemoteDeviceListToDeviceDataList(remote_devices, &devices)) | |
45 devices.clear(); | |
46 | |
47 // Only one pending request. | |
48 DCHECK(!create_keys_op_); | |
49 create_keys_op_.reset(new EasyUnlockCreateKeysOperation( | |
50 user_context, | |
51 devices, | |
52 base::Bind(&EasyUnlockKeyManager::OnKeysCreated, | |
53 weak_ptr_factory_.GetWeakPtr(), | |
54 user_context, | |
55 devices, | |
56 callback))); | |
57 create_keys_op_->Start(); | |
58 } | |
59 | |
60 void EasyUnlockKeyManager::RemoveKeys(const UserContext& user_context, | |
61 size_t start_index, | |
62 const RemoveKeysCallback& callback) { | |
63 // Must have the secret. | |
64 DCHECK(!user_context.GetKey()->GetSecret().empty()); | |
65 // Only one pending request. | |
66 DCHECK(!remove_keys_op_); | |
67 | |
68 remove_keys_op_.reset( | |
69 new EasyUnlockRemoveKeysOperation( | |
70 user_context, | |
71 start_index, | |
72 base::Bind(&EasyUnlockKeyManager::OnKeysRemoved, | |
73 weak_ptr_factory_.GetWeakPtr(), | |
74 callback))); | |
75 remove_keys_op_->Start(); | |
76 } | |
77 | |
78 void EasyUnlockKeyManager::GetDeviceDataList( | |
79 const UserContext& user_context, | |
80 const GetDeviceDataListCallback& callback) { | |
tbarzic
2014/09/10 19:01:11
should this be throttled if there is create_keys_o
xiyuan
2014/09/10 20:37:13
This should not happen (at least not for the same
tbarzic
2014/09/10 20:43:39
sg
| |
81 const int op_id = GetNextOperationId(); | |
82 scoped_ptr<EasyUnlockGetKeysOperation> op(new EasyUnlockGetKeysOperation( | |
83 user_context, | |
84 base::Bind(&EasyUnlockKeyManager::OnKeysFetched, | |
85 weak_ptr_factory_.GetWeakPtr(), | |
86 op_id, | |
87 callback))); | |
88 op->Start(); | |
89 get_keys_ops_[op_id] = op.release(); | |
90 } | |
91 | |
92 // static | |
93 void EasyUnlockKeyManager::DeviceDataToRemoteDeviceDictionary( | |
94 const EasyUnlockDeviceKeyData& data, | |
95 base::DictionaryValue* dict) { | |
96 dict->SetString(kKeyBluetoothAddress, data.bluetooth_address); | |
97 dict->SetString(kKeyPsk, data.psk); | |
98 scoped_ptr<base::DictionaryValue> permit_record(new base::DictionaryValue); | |
99 permit_record->SetString(kKeyId, data.public_key); | |
100 dict->Set(kKeyPermitRecord, permit_record.release()); | |
101 } | |
102 | |
103 // static | |
104 bool EasyUnlockKeyManager::RemoteDeviceDictionaryToDeviceData( | |
105 const base::DictionaryValue& dict, | |
106 EasyUnlockDeviceKeyData* data) { | |
107 std::string bluetooth_address; | |
108 std::string public_key; | |
109 std::string psk; | |
110 | |
111 if (!dict.GetString(kKeyBluetoothAddress, &bluetooth_address) || | |
112 !dict.GetString(kKeyPermitId, &public_key) || | |
113 !dict.GetString(kKeyPsk, &psk)) { | |
114 return false; | |
115 } | |
116 | |
117 data->bluetooth_address.swap(bluetooth_address); | |
118 data->public_key.swap(public_key); | |
119 data->psk.swap(psk); | |
120 return true; | |
121 } | |
122 | |
123 // static | |
124 void EasyUnlockKeyManager::DeviceDataListToRemoteDeviceList( | |
125 const EasyUnlockDeviceKeyDataList& data_list, | |
126 base::ListValue* device_list) { | |
127 device_list->Clear(); | |
128 for (size_t i = 0; i < data_list.size(); ++i) { | |
129 scoped_ptr<base::DictionaryValue> device_dict(new base::DictionaryValue); | |
130 DeviceDataToRemoteDeviceDictionary(data_list[i], device_dict.get()); | |
131 device_list->Append(device_dict.release()); | |
132 } | |
133 } | |
134 | |
135 // static | |
136 bool EasyUnlockKeyManager::RemoteDeviceListToDeviceDataList( | |
137 const base::ListValue& device_list, | |
138 EasyUnlockDeviceKeyDataList* data_list) { | |
139 EasyUnlockDeviceKeyDataList parsed_devices; | |
140 for (base::ListValue::const_iterator it = device_list.begin(); | |
141 it != device_list.end(); | |
142 ++it) { | |
143 const base::DictionaryValue* dict; | |
144 if (!(*it)->GetAsDictionary(&dict) || !dict) | |
145 return false; | |
146 | |
147 EasyUnlockDeviceKeyData data; | |
148 if (!RemoteDeviceDictionaryToDeviceData(*dict, &data)) | |
149 return false; | |
150 | |
151 parsed_devices.push_back(data); | |
152 } | |
153 | |
154 data_list->swap(parsed_devices); | |
155 return true; | |
156 } | |
157 | |
158 // static | |
159 std::string EasyUnlockKeyManager::GetKeyLabel(size_t key_index) { | |
160 return base::StringPrintf("%s%zu", kKeyLabelPrefix, key_index); | |
161 } | |
162 | |
163 int EasyUnlockKeyManager::GetNextOperationId() { | |
164 return ++operation_id_; | |
165 } | |
166 | |
167 void EasyUnlockKeyManager::OnKeysCreated( | |
168 const UserContext& user_context, | |
169 const EasyUnlockDeviceKeyDataList& devices, | |
170 const CreateKeysCallback& callback, | |
171 bool create_success) { | |
172 scoped_ptr<EasyUnlockCreateKeysOperation> op = create_keys_op_.Pass(); | |
173 if (!callback.is_null()) | |
174 callback.Run(create_success); | |
175 | |
176 // Remove extra existing keys. | |
177 RemoveKeys(user_context, devices.size(), RemoveKeysCallback()); | |
178 } | |
179 | |
180 void EasyUnlockKeyManager::OnKeysRemoved(const RemoveKeysCallback& callback, | |
181 bool remove_success) { | |
182 scoped_ptr<EasyUnlockRemoveKeysOperation> op = remove_keys_op_.Pass(); | |
183 if (!callback.is_null()) | |
184 callback.Run(remove_success); | |
185 } | |
186 | |
187 void EasyUnlockKeyManager::OnKeysFetched( | |
188 int op_id, | |
189 const GetDeviceDataListCallback& callback, | |
190 bool fetch_success, | |
191 const EasyUnlockDeviceKeyDataList& fetched_data) { | |
192 std::map<int, EasyUnlockGetKeysOperation*>::iterator it = | |
193 get_keys_ops_.find(op_id); | |
194 scoped_ptr<EasyUnlockGetKeysOperation> op; | |
195 if (it != get_keys_ops_.end()) { | |
196 op.reset(it->second); | |
197 get_keys_ops_.erase(it); | |
198 } else { | |
199 NOTREACHED(); | |
200 } | |
201 | |
202 if (!callback.is_null()) | |
203 callback.Run(fetch_success, fetched_data); | |
204 } | |
205 | |
206 } // namespace chromeos | |
OLD | NEW |