Chromium Code Reviews| Index: chrome/browser/chromeos/login/easy_unlock/easy_unlock_key_manager.cc |
| diff --git a/chrome/browser/chromeos/login/easy_unlock/easy_unlock_key_manager.cc b/chrome/browser/chromeos/login/easy_unlock/easy_unlock_key_manager.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..3cc4d93f4f060790f072bb0dc0ccdbe3ec1d935c |
| --- /dev/null |
| +++ b/chrome/browser/chromeos/login/easy_unlock/easy_unlock_key_manager.cc |
| @@ -0,0 +1,188 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/chromeos/login/easy_unlock/easy_unlock_key_manager.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/logging.h" |
| +#include "base/strings/stringprintf.h" |
| +#include "base/values.h" |
| + |
| +namespace chromeos { |
| + |
| +namespace { |
| + |
| +const char kKeyBluetoothAddress[] = "bluetoothAddress"; |
| +const char kKeyPermitRecord[] = "permitRecord"; |
| +const char kKeyId[] = "id"; |
| +const char kKeyPermitId[] = "permitRecord.id"; |
| +const char kKeyPsk[] = "psk"; |
| + |
| +const char kKeyLabelPrefix[] = "easy-unlock-"; |
| + |
| +} // namespace |
| + |
| +EasyUnlockKeyManager::EasyUnlockKeyManager() : weak_ptr_factory_(this) { |
| +} |
| + |
| +EasyUnlockKeyManager::~EasyUnlockKeyManager() { |
| +} |
| + |
| +void EasyUnlockKeyManager::CreateKeys( |
| + const UserContext& user_context, |
| + const base::ListValue& remote_devices, |
| + const CreateKeysCallback& callback) { |
| + // Must have the secret. |
| + DCHECK(!user_context.GetKey()->GetSecret().empty()); |
| + |
| + EasyUnlockDeviceKeyDataList devices; |
| + if (!RemoteDeviceListToDeviceDataList(remote_devices, &devices)) { |
|
tbarzic
2014/09/09 19:32:21
Should we remove keys even in case the remote_devi
xiyuan
2014/09/09 20:25:30
Makes sense. Updated to code to use empty |devices
|
| + LOG(ERROR) << "Error parsing remote device list."; |
| + callback.Run(false); |
| + return; |
| + } |
| + |
| + // Only one pending request. |
| + DCHECK(!create_keys_op_); |
| + create_keys_op_.reset(new EasyUnlockCreateKeysOperation( |
|
tbarzic
2014/09/09 19:32:21
Would it make sence to do this in OnKeysRemovedFor
xiyuan
2014/09/09 20:25:30
Done.
|
| + user_context, |
| + devices, |
| + base::Bind(&EasyUnlockKeyManager::OnKeysCreated, |
| + weak_ptr_factory_.GetWeakPtr(), |
| + callback))); |
| + |
| + // Remove existing keys before creating new ones. |
| + RemoveKeys(user_context, |
| + base::Bind(&EasyUnlockKeyManager::OnKeysRemovedForCreateKeys, |
| + weak_ptr_factory_.GetWeakPtr(), |
| + callback)); |
| +} |
| + |
| +void EasyUnlockKeyManager::RemoveKeys(const UserContext& user_context, |
| + const RemoveKeysCallback& callback) { |
| + // Must have the secret. |
| + DCHECK(!user_context.GetKey()->GetSecret().empty()); |
| + // Only one pending request. |
| + DCHECK(!remove_keys_op_); |
| + |
| + remove_keys_op_.reset( |
| + new EasyUnlockRemoveKeysOperation( |
| + user_context, |
| + base::Bind(&EasyUnlockKeyManager::OnKeysRemoved, |
| + weak_ptr_factory_.GetWeakPtr(), |
| + callback))); |
| + remove_keys_op_->Start(); |
| +} |
| + |
| +void EasyUnlockKeyManager::GetDeviceDataList( |
| + const UserContext& user_context, |
| + const GetDeviceDataListCallback& callback) { |
| + // Only one pending request. |
| + DCHECK(!get_keys_op_); |
| + |
| + get_keys_op_.reset(new EasyUnlockGetKeysOperation( |
| + user_context, |
| + base::Bind(&EasyUnlockKeyManager::OnKeysFetched, |
| + weak_ptr_factory_.GetWeakPtr(), |
| + callback))); |
| + get_keys_op_->Start(); |
| +} |
| + |
| +// static |
| +void EasyUnlockKeyManager::DeviceDataToRemoteDeviceDictionary( |
| + const EasyUnlockDeviceKeyData& data, |
| + base::DictionaryValue* dict) { |
| + dict->SetString(kKeyBluetoothAddress, data.bluetooth_address); |
| + dict->SetString(kKeyPsk, data.psk); |
| + scoped_ptr<base::DictionaryValue> permit_record(new base::DictionaryValue); |
| + permit_record->SetString(kKeyId, data.public_key); |
| + dict->Set(kKeyPermitRecord, permit_record.release()); |
| +} |
| + |
| +// static |
| +bool EasyUnlockKeyManager::RemoteDeviceDictionaryToDeviceData( |
| + const base::DictionaryValue& dict, |
| + EasyUnlockDeviceKeyData* data) { |
| + return dict.GetString(kKeyBluetoothAddress, &data->bluetooth_address) && |
| + dict.GetString(kKeyPermitId, &data->public_key) && |
| + dict.GetString(kKeyPsk, &data->psk); |
| +} |
| + |
| +// static |
| +void EasyUnlockKeyManager::DeviceDataListToRemoteDeviceList( |
| + const EasyUnlockDeviceKeyDataList& data_list, |
| + base::ListValue* device_list) { |
| + device_list->Clear(); |
| + for (size_t i = 0; i < data_list.size(); ++i) { |
| + scoped_ptr<base::DictionaryValue> device_dict(new base::DictionaryValue); |
| + DeviceDataToRemoteDeviceDictionary(data_list[i], device_dict.get()); |
| + device_list->Append(device_dict.release()); |
| + } |
| +} |
| + |
| +// static |
| +bool EasyUnlockKeyManager::RemoteDeviceListToDeviceDataList( |
| + const base::ListValue& device_list, |
| + EasyUnlockDeviceKeyDataList* data_list) { |
| + EasyUnlockDeviceKeyDataList parsed_devices; |
| + for (base::ListValue::const_iterator it = device_list.begin(); |
| + it != device_list.end(); |
| + ++it) { |
| + const base::DictionaryValue* dict; |
| + if (!(*it)->GetAsDictionary(&dict) || !dict) |
| + return false; |
| + |
| + EasyUnlockDeviceKeyData data; |
| + if (!RemoteDeviceDictionaryToDeviceData(*dict, &data)) |
| + return false; |
| + |
| + parsed_devices.push_back(data); |
| + } |
| + |
| + data_list->swap(parsed_devices); |
| + return true; |
| +} |
| + |
| +// static |
| +std::string EasyUnlockKeyManager::GetKeyLabel(int key_index) { |
| + return base::StringPrintf("%s%d", kKeyLabelPrefix, key_index); |
| +} |
| + |
| +void EasyUnlockKeyManager::OnKeysRemovedForCreateKeys( |
| + const CreateKeysCallback& callback, |
| + bool remove_success) { |
| + if (!remove_success) { |
| + callback.Run(false); |
| + return; |
| + } |
| + |
| + // Only one pending request. |
| + DCHECK(!create_keys_op_); |
|
tbarzic
2014/09/09 19:32:21
I think you meant DCHECK(create_leys_op_)
xiyuan
2014/09/09 20:25:30
Yep.
|
| + create_keys_op_->Start(); |
| +} |
| + |
| +void EasyUnlockKeyManager::OnKeysCreated(const CreateKeysCallback& callback, |
| + bool create_success) { |
| + scoped_ptr<EasyUnlockCreateKeysOperation> op = create_keys_op_.Pass(); |
| + if (!callback.is_null()) |
| + callback.Run(create_success); |
| +} |
| + |
| +void EasyUnlockKeyManager::OnKeysRemoved(const RemoveKeysCallback& callback, |
| + bool remove_success) { |
| + scoped_ptr<EasyUnlockRemoveKeysOperation> op = remove_keys_op_.Pass(); |
| + if (!callback.is_null()) |
| + callback.Run(remove_success); |
| +} |
| + |
| +void EasyUnlockKeyManager::OnKeysFetched( |
| + const GetDeviceDataListCallback& callback, |
| + bool fetch_success, |
| + const EasyUnlockDeviceKeyDataList& fetched_data) { |
| + scoped_ptr<EasyUnlockGetKeysOperation> op = get_keys_op_.Pass(); |
| + if (!callback.is_null()) |
| + callback.Run(fetch_success, fetched_data); |
| +} |
| + |
| +} // namespace chromeos |