Chromium Code Reviews| OLD | NEW |
|---|---|
| 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/memory/scoped_ptr.h" | |
| 9 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
| 10 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
| 11 #include "base/values.h" | 12 #include "base/values.h" |
| 12 | 13 |
| 13 namespace chromeos { | 14 namespace chromeos { |
| 14 | 15 |
| 15 namespace { | 16 namespace { |
| 16 | 17 |
| 17 const char kKeyBluetoothAddress[] = "bluetoothAddress"; | 18 const char kKeyBluetoothAddress[] = "bluetoothAddress"; |
| 18 const char kKeyPermitRecord[] = "permitRecord"; | 19 const char kKeyPermitRecord[] = "permitRecord"; |
| 19 const char kKeyPermitId[] = "permitRecord.id"; | 20 const char kKeyPermitId[] = "permitRecord.id"; |
| 20 const char kKeyPermitPermitId[] = "permitRecord.permitId"; | 21 const char kKeyPermitPermitId[] = "permitRecord.permitId"; |
| 21 const char kKeyPermitData[] = "permitRecord.data"; | 22 const char kKeyPermitData[] = "permitRecord.data"; |
| 22 const char kKeyPermitType[] = "permitRecord.type"; | 23 const char kKeyPermitType[] = "permitRecord.type"; |
| 23 const char kKeyPsk[] = "psk"; | 24 const char kKeyPsk[] = "psk"; |
| 24 | 25 |
| 25 const char kKeyLabelPrefix[] = "easy-unlock-"; | 26 const char kKeyLabelPrefix[] = "easy-unlock-"; |
| 26 | 27 |
| 27 const char kPermitPermitIdFormat[] = "permit://google.com/easyunlock/v1/%s"; | 28 const char kPermitPermitIdFormat[] = "permit://google.com/easyunlock/v1/%s"; |
| 28 const char kPermitTypeLicence[] = "licence"; | 29 const char kPermitTypeLicence[] = "licence"; |
| 29 | 30 |
| 30 } // namespace | 31 } // namespace |
| 31 | 32 |
| 32 EasyUnlockKeyManager::EasyUnlockKeyManager() | 33 EasyUnlockKeyManager::EasyUnlockKeyManager() : weak_ptr_factory_(this) { |
| 33 : operation_id_(0), | |
| 34 weak_ptr_factory_(this) { | |
| 35 } | 34 } |
| 36 | 35 |
| 37 EasyUnlockKeyManager::~EasyUnlockKeyManager() { | 36 EasyUnlockKeyManager::~EasyUnlockKeyManager() { |
| 38 STLDeleteContainerPairSecondPointers(get_keys_ops_.begin(), | 37 while (!write_operation_queue_.empty()) { |
|
xiyuan
2014/12/17 03:26:33
How about include base/stl_util.h and do STLDelete
Tim Song
2014/12/17 20:57:57
Done. I used the base::STLElementDeleter to scope
| |
| 39 get_keys_ops_.end()); | 38 scoped_ptr<EasyUnlockRefreshKeysOperation> operation( |
| 39 make_scoped_ptr(write_operation_queue_.front())); | |
| 40 write_operation_queue_.pop_front(); | |
| 41 } | |
| 42 | |
| 43 while (!read_operation_queue_.empty()) { | |
| 44 scoped_ptr<EasyUnlockGetKeysOperation> operation( | |
| 45 make_scoped_ptr(read_operation_queue_.front())); | |
| 46 read_operation_queue_.pop_front(); | |
| 47 } | |
| 40 } | 48 } |
| 41 | 49 |
| 42 void EasyUnlockKeyManager::RefreshKeys(const UserContext& user_context, | 50 void EasyUnlockKeyManager::RefreshKeys(const UserContext& user_context, |
| 43 const base::ListValue& remote_devices, | 51 const base::ListValue& remote_devices, |
| 44 const RefreshKeysCallback& callback) { | 52 const RefreshKeysCallback& callback) { |
| 45 // Must have the secret. | |
| 46 DCHECK(!user_context.GetKey()->GetSecret().empty()); | |
| 47 | |
| 48 EasyUnlockDeviceKeyDataList devices; | 53 EasyUnlockDeviceKeyDataList devices; |
| 49 if (!RemoteDeviceListToDeviceDataList(remote_devices, &devices)) | 54 if (!RemoteDeviceListToDeviceDataList(remote_devices, &devices)) |
| 50 devices.clear(); | 55 devices.clear(); |
| 51 | 56 |
| 52 // Only one pending request. | 57 write_operation_queue_.push_back(new EasyUnlockRefreshKeysOperation( |
| 53 DCHECK(!HasPendingOperations()); | 58 user_context, devices, |
| 54 create_keys_op_.reset(new EasyUnlockCreateKeysOperation( | 59 base::Bind(&EasyUnlockKeyManager::OnKeysRefreshed, |
| 55 user_context, | 60 weak_ptr_factory_.GetWeakPtr(), callback))); |
| 56 devices, | 61 RunNextOperation(); |
| 57 base::Bind(&EasyUnlockKeyManager::OnKeysCreated, | |
| 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 } | 62 } |
| 81 | 63 |
| 82 void EasyUnlockKeyManager::GetDeviceDataList( | 64 void EasyUnlockKeyManager::GetDeviceDataList( |
| 83 const UserContext& user_context, | 65 const UserContext& user_context, |
| 84 const GetDeviceDataListCallback& callback) { | 66 const GetDeviceDataListCallback& callback) { |
| 85 // Defer the get operation if there is pending write operations. | 67 read_operation_queue_.push_back(new EasyUnlockGetKeysOperation( |
| 86 if (create_keys_op_ || remove_keys_op_) { | 68 user_context, base::Bind(&EasyUnlockKeyManager::OnKeysFetched, |
| 87 pending_ops_.push_back(base::Bind(&EasyUnlockKeyManager::GetDeviceDataList, | 69 weak_ptr_factory_.GetWeakPtr(), callback))); |
| 88 weak_ptr_factory_.GetWeakPtr(), | 70 RunNextOperation(); |
| 89 user_context, | |
| 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 } | 71 } |
| 104 | 72 |
| 105 // static | 73 // static |
| 106 void EasyUnlockKeyManager::DeviceDataToRemoteDeviceDictionary( | 74 void EasyUnlockKeyManager::DeviceDataToRemoteDeviceDictionary( |
| 107 const std::string& user_id, | 75 const std::string& user_id, |
| 108 const EasyUnlockDeviceKeyData& data, | 76 const EasyUnlockDeviceKeyData& data, |
| 109 base::DictionaryValue* dict) { | 77 base::DictionaryValue* dict) { |
| 110 dict->SetString(kKeyBluetoothAddress, data.bluetooth_address); | 78 dict->SetString(kKeyBluetoothAddress, data.bluetooth_address); |
| 111 dict->SetString(kKeyPsk, data.psk); | 79 dict->SetString(kKeyPsk, data.psk); |
| 112 scoped_ptr<base::DictionaryValue> permit_record(new base::DictionaryValue); | 80 scoped_ptr<base::DictionaryValue> permit_record(new base::DictionaryValue); |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 174 | 142 |
| 175 data_list->swap(parsed_devices); | 143 data_list->swap(parsed_devices); |
| 176 return true; | 144 return true; |
| 177 } | 145 } |
| 178 | 146 |
| 179 // static | 147 // static |
| 180 std::string EasyUnlockKeyManager::GetKeyLabel(size_t key_index) { | 148 std::string EasyUnlockKeyManager::GetKeyLabel(size_t key_index) { |
| 181 return base::StringPrintf("%s%zu", kKeyLabelPrefix, key_index); | 149 return base::StringPrintf("%s%zu", kKeyLabelPrefix, key_index); |
| 182 } | 150 } |
| 183 | 151 |
| 184 bool EasyUnlockKeyManager::HasPendingOperations() const { | 152 void EasyUnlockKeyManager::RunNextOperation() { |
| 185 return create_keys_op_ || remove_keys_op_ || !get_keys_ops_.empty(); | 153 if (pending_write_operation_ || pending_read_operation_) |
| 154 return; | |
| 155 | |
| 156 // The front operation will be removed upon completion. | |
| 157 if (!write_operation_queue_.empty()) { | |
| 158 pending_write_operation_ = make_scoped_ptr(write_operation_queue_.front()); | |
| 159 write_operation_queue_.pop_front(); | |
| 160 pending_write_operation_->Start(); | |
| 161 } else if (!read_operation_queue_.empty()) { | |
| 162 pending_read_operation_ = make_scoped_ptr(read_operation_queue_.front()); | |
| 163 read_operation_queue_.pop_front(); | |
| 164 pending_read_operation_->Start(); | |
| 165 } | |
| 186 } | 166 } |
| 187 | 167 |
| 188 int EasyUnlockKeyManager::GetNextOperationId() { | 168 void EasyUnlockKeyManager::OnKeysRefreshed(const RefreshKeysCallback& callback, |
| 189 return ++operation_id_; | 169 bool refresh_success) { |
| 190 } | 170 if (!callback.is_null()) |
| 171 callback.Run(refresh_success); | |
| 191 | 172 |
| 192 void EasyUnlockKeyManager::RunNextPendingOp() { | 173 DCHECK(pending_write_operation_); |
| 193 if (pending_ops_.empty()) | 174 pending_write_operation_.reset(); |
| 194 return; | 175 RunNextOperation(); |
| 195 | |
| 196 pending_ops_.front().Run(); | |
| 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 } | 176 } |
| 221 | 177 |
| 222 void EasyUnlockKeyManager::OnKeysFetched( | 178 void EasyUnlockKeyManager::OnKeysFetched( |
| 223 int op_id, | |
| 224 const GetDeviceDataListCallback& callback, | 179 const GetDeviceDataListCallback& callback, |
| 225 bool fetch_success, | 180 bool fetch_success, |
| 226 const EasyUnlockDeviceKeyDataList& fetched_data) { | 181 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()) | 182 if (!callback.is_null()) |
| 238 callback.Run(fetch_success, fetched_data); | 183 callback.Run(fetch_success, fetched_data); |
| 239 | 184 |
| 240 if (!HasPendingOperations()) | 185 DCHECK(pending_read_operation_); |
| 241 RunNextPendingOp(); | 186 pending_read_operation_.reset(); |
| 187 RunNextOperation(); | |
| 242 } | 188 } |
| 243 | 189 |
| 244 } // namespace chromeos | 190 } // namespace chromeos |
| OLD | NEW |