Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(262)

Side by Side Diff: chrome/browser/chromeos/login/easy_unlock/easy_unlock_key_manager.cc

Issue 808563004: Clean up Smart Lock cryptohome keys logic: (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/chromeos/login/easy_unlock/easy_unlock_key_manager.h" 5 #include "chrome/browser/chromeos/login/easy_unlock/easy_unlock_key_manager.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/stl_util.h" 9 #include "base/stl_util.h"
10 #include "base/strings/stringprintf.h" 10 #include "base/strings/stringprintf.h"
(...skipping 12 matching lines...) Expand all
23 const char kKeyPsk[] = "psk"; 23 const char kKeyPsk[] = "psk";
24 24
25 const char kKeyLabelPrefix[] = "easy-unlock-"; 25 const char kKeyLabelPrefix[] = "easy-unlock-";
26 26
27 const char kPermitPermitIdFormat[] = "permit://google.com/easyunlock/v1/%s"; 27 const char kPermitPermitIdFormat[] = "permit://google.com/easyunlock/v1/%s";
28 const char kPermitTypeLicence[] = "licence"; 28 const char kPermitTypeLicence[] = "licence";
29 29
30 } // namespace 30 } // namespace
31 31
32 EasyUnlockKeyManager::EasyUnlockKeyManager() 32 EasyUnlockKeyManager::EasyUnlockKeyManager()
33 : operation_id_(0), 33 : operation_in_progress_(false), weak_ptr_factory_(this) {
34 weak_ptr_factory_(this) {
35 } 34 }
36 35
37 EasyUnlockKeyManager::~EasyUnlockKeyManager() { 36 EasyUnlockKeyManager::~EasyUnlockKeyManager() {
38 STLDeleteContainerPairSecondPointers(get_keys_ops_.begin(),
39 get_keys_ops_.end());
40 } 37 }
41 38
42 void EasyUnlockKeyManager::RefreshKeys(const UserContext& user_context, 39 void EasyUnlockKeyManager::RefreshKeys(const UserContext& user_context,
43 const base::ListValue& remote_devices, 40 const base::ListValue& remote_devices,
44 const RefreshKeysCallback& callback) { 41 const RefreshKeysCallback& callback) {
45 // Must have the secret.
46 DCHECK(!user_context.GetKey()->GetSecret().empty());
47
48 EasyUnlockDeviceKeyDataList devices; 42 EasyUnlockDeviceKeyDataList devices;
49 if (!RemoteDeviceListToDeviceDataList(remote_devices, &devices)) 43 if (!RemoteDeviceListToDeviceDataList(remote_devices, &devices))
50 devices.clear(); 44 devices.clear();
51 45
52 // Only one pending request. 46 write_operation_queue_.push_back(
53 DCHECK(!HasPendingOperations()); 47 make_scoped_ptr(new EasyUnlockRefreshKeysOperation(
54 create_keys_op_.reset(new EasyUnlockCreateKeysOperation( 48 user_context, devices,
55 user_context, 49 base::Bind(&EasyUnlockKeyManager::OnKeysRefreshed,
56 devices, 50 weak_ptr_factory_.GetWeakPtr(), callback))));
57 base::Bind(&EasyUnlockKeyManager::OnKeysCreated, 51 RunNextOperation();
xiyuan 2014/12/17 00:23:14 What happens if there is a running operation? This
Tim Song 2014/12/17 02:05:14 Done. Sorry, I meant to check operation_in_progres
58 weak_ptr_factory_.GetWeakPtr(),
59 devices.size(),
60 callback)));
61 create_keys_op_->Start();
62 }
63
64 void EasyUnlockKeyManager::RemoveKeys(const UserContext& user_context,
65 size_t start_index,
66 const RemoveKeysCallback& callback) {
67 // Must have the secret.
68 DCHECK(!user_context.GetKey()->GetSecret().empty());
69 // Only one pending request.
70 DCHECK(!HasPendingOperations());
71
72 remove_keys_op_.reset(
73 new EasyUnlockRemoveKeysOperation(
74 user_context,
75 start_index,
76 base::Bind(&EasyUnlockKeyManager::OnKeysRemoved,
77 weak_ptr_factory_.GetWeakPtr(),
78 callback)));
79 remove_keys_op_->Start();
80 } 52 }
81 53
82 void EasyUnlockKeyManager::GetDeviceDataList( 54 void EasyUnlockKeyManager::GetDeviceDataList(
83 const UserContext& user_context, 55 const UserContext& user_context,
84 const GetDeviceDataListCallback& callback) { 56 const GetDeviceDataListCallback& callback) {
85 // Defer the get operation if there is pending write operations. 57 read_operation_queue_.push_back(
86 if (create_keys_op_ || remove_keys_op_) { 58 make_scoped_ptr(new EasyUnlockGetKeysOperation(
87 pending_ops_.push_back(base::Bind(&EasyUnlockKeyManager::GetDeviceDataList, 59 user_context, base::Bind(&EasyUnlockKeyManager::OnKeysFetched,
88 weak_ptr_factory_.GetWeakPtr(), 60 weak_ptr_factory_.GetWeakPtr(), callback))));
89 user_context, 61 RunNextOperation();
xiyuan 2014/12/17 00:23:14 same here.
Tim Song 2014/12/17 02:05:14 Done.
90 callback));
91 return;
92 }
93
94 const int op_id = GetNextOperationId();
95 scoped_ptr<EasyUnlockGetKeysOperation> op(new EasyUnlockGetKeysOperation(
96 user_context,
97 base::Bind(&EasyUnlockKeyManager::OnKeysFetched,
98 weak_ptr_factory_.GetWeakPtr(),
99 op_id,
100 callback)));
101 op->Start();
102 get_keys_ops_[op_id] = op.release();
103 } 62 }
104 63
105 // static 64 // static
106 void EasyUnlockKeyManager::DeviceDataToRemoteDeviceDictionary( 65 void EasyUnlockKeyManager::DeviceDataToRemoteDeviceDictionary(
107 const std::string& user_id, 66 const std::string& user_id,
108 const EasyUnlockDeviceKeyData& data, 67 const EasyUnlockDeviceKeyData& data,
109 base::DictionaryValue* dict) { 68 base::DictionaryValue* dict) {
110 dict->SetString(kKeyBluetoothAddress, data.bluetooth_address); 69 dict->SetString(kKeyBluetoothAddress, data.bluetooth_address);
111 dict->SetString(kKeyPsk, data.psk); 70 dict->SetString(kKeyPsk, data.psk);
112 scoped_ptr<base::DictionaryValue> permit_record(new base::DictionaryValue); 71 scoped_ptr<base::DictionaryValue> permit_record(new base::DictionaryValue);
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
174 133
175 data_list->swap(parsed_devices); 134 data_list->swap(parsed_devices);
176 return true; 135 return true;
177 } 136 }
178 137
179 // static 138 // static
180 std::string EasyUnlockKeyManager::GetKeyLabel(size_t key_index) { 139 std::string EasyUnlockKeyManager::GetKeyLabel(size_t key_index) {
181 return base::StringPrintf("%s%zu", kKeyLabelPrefix, key_index); 140 return base::StringPrintf("%s%zu", kKeyLabelPrefix, key_index);
182 } 141 }
183 142
184 bool EasyUnlockKeyManager::HasPendingOperations() const { 143 void EasyUnlockKeyManager::RunNextOperation() {
185 return create_keys_op_ || remove_keys_op_ || !get_keys_ops_.empty(); 144 // The front operation will be removed upon completion.
145 if (!write_operation_queue_.empty()) {
146 operation_in_progress_ = true;
147 write_operation_queue_.front()->Start();
148 } else if (!read_operation_queue_.empty()) {
149 operation_in_progress_ = true;
150 read_operation_queue_.front()->Start();
151 }
186 } 152 }
187 153
188 int EasyUnlockKeyManager::GetNextOperationId() { 154 void EasyUnlockKeyManager::OnKeysRefreshed(const RefreshKeysCallback& callback,
189 return ++operation_id_; 155 bool refresh_success) {
190 } 156 if (!callback.is_null())
157 callback.Run(refresh_success);
191 158
192 void EasyUnlockKeyManager::RunNextPendingOp() { 159 operation_in_progress_ = false;
193 if (pending_ops_.empty()) 160 DCHECK(!write_operation_queue_.empty());
194 return; 161 if (!write_operation_queue_.empty())
195 162 write_operation_queue_.pop_front();
196 pending_ops_.front().Run(); 163 RunNextOperation();
197 pending_ops_.pop_front();
198 }
199
200 void EasyUnlockKeyManager::OnKeysCreated(
201 size_t remove_start_index,
202 const RefreshKeysCallback& callback,
203 bool create_success) {
204 scoped_ptr<EasyUnlockCreateKeysOperation> op = create_keys_op_.Pass();
205 if (!callback.is_null())
206 callback.Run(create_success);
207
208 // Remove extra existing keys.
209 RemoveKeys(op->user_context(), remove_start_index, RemoveKeysCallback());
210 }
211
212 void EasyUnlockKeyManager::OnKeysRemoved(const RemoveKeysCallback& callback,
213 bool remove_success) {
214 scoped_ptr<EasyUnlockRemoveKeysOperation> op = remove_keys_op_.Pass();
215 if (!callback.is_null())
216 callback.Run(remove_success);
217
218 if (!HasPendingOperations())
219 RunNextPendingOp();
220 } 164 }
221 165
222 void EasyUnlockKeyManager::OnKeysFetched( 166 void EasyUnlockKeyManager::OnKeysFetched(
223 int op_id,
224 const GetDeviceDataListCallback& callback, 167 const GetDeviceDataListCallback& callback,
225 bool fetch_success, 168 bool fetch_success,
226 const EasyUnlockDeviceKeyDataList& fetched_data) { 169 const EasyUnlockDeviceKeyDataList& fetched_data) {
227 std::map<int, EasyUnlockGetKeysOperation*>::iterator it =
228 get_keys_ops_.find(op_id);
229 scoped_ptr<EasyUnlockGetKeysOperation> op;
230 if (it != get_keys_ops_.end()) {
231 op.reset(it->second);
232 get_keys_ops_.erase(it);
233 } else {
234 NOTREACHED();
235 }
236
237 if (!callback.is_null()) 170 if (!callback.is_null())
238 callback.Run(fetch_success, fetched_data); 171 callback.Run(fetch_success, fetched_data);
239 172
240 if (!HasPendingOperations()) 173 operation_in_progress_ = false;
241 RunNextPendingOp(); 174 DCHECK(!read_operation_queue_.empty());
175 if (!read_operation_queue_.empty())
176 read_operation_queue_.pop_front();
177 RunNextOperation();
242 } 178 }
243 179
244 } // namespace chromeos 180 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698