Chromium Code Reviews| 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/strings/stringprintf.h" | |
| 10 #include "base/values.h" | |
| 11 | |
| 12 namespace chromeos { | |
| 13 | |
| 14 namespace { | |
| 15 | |
| 16 const char kKeyBluetoothAddress[] = "bluetoothAddress"; | |
| 17 const char kKeyPermitRecord[] = "permitRecord"; | |
| 18 const char kKeyId[] = "id"; | |
| 19 const char kKeyPermitId[] = "permitRecord.id"; | |
| 20 const char kKeyPsk[] = "psk"; | |
| 21 | |
| 22 const char kKeyLabelPrefix[] = "easy-unlock-"; | |
| 23 | |
| 24 } // namespace | |
| 25 | |
| 26 EasyUnlockKeyManager::EasyUnlockKeyManager() : weak_ptr_factory_(this) { | |
| 27 } | |
| 28 | |
| 29 EasyUnlockKeyManager::~EasyUnlockKeyManager() { | |
| 30 } | |
| 31 | |
| 32 void EasyUnlockKeyManager::CreateKeys( | |
| 33 const UserContext& user_context, | |
| 34 const base::ListValue& remote_devices, | |
| 35 const CreateKeysCallback& callback) { | |
| 36 // Must have the secret. | |
| 37 DCHECK(!user_context.GetKey()->GetSecret().empty()); | |
| 38 | |
| 39 EasyUnlockDeviceKeyDataList devices; | |
| 40 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
| |
| 41 LOG(ERROR) << "Error parsing remote device list."; | |
| 42 callback.Run(false); | |
| 43 return; | |
| 44 } | |
| 45 | |
| 46 // Only one pending request. | |
| 47 DCHECK(!create_keys_op_); | |
| 48 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.
| |
| 49 user_context, | |
| 50 devices, | |
| 51 base::Bind(&EasyUnlockKeyManager::OnKeysCreated, | |
| 52 weak_ptr_factory_.GetWeakPtr(), | |
| 53 callback))); | |
| 54 | |
| 55 // Remove existing keys before creating new ones. | |
| 56 RemoveKeys(user_context, | |
| 57 base::Bind(&EasyUnlockKeyManager::OnKeysRemovedForCreateKeys, | |
| 58 weak_ptr_factory_.GetWeakPtr(), | |
| 59 callback)); | |
| 60 } | |
| 61 | |
| 62 void EasyUnlockKeyManager::RemoveKeys(const UserContext& user_context, | |
| 63 const RemoveKeysCallback& callback) { | |
| 64 // Must have the secret. | |
| 65 DCHECK(!user_context.GetKey()->GetSecret().empty()); | |
| 66 // Only one pending request. | |
| 67 DCHECK(!remove_keys_op_); | |
| 68 | |
| 69 remove_keys_op_.reset( | |
| 70 new EasyUnlockRemoveKeysOperation( | |
| 71 user_context, | |
| 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) { | |
| 81 // Only one pending request. | |
| 82 DCHECK(!get_keys_op_); | |
| 83 | |
| 84 get_keys_op_.reset(new EasyUnlockGetKeysOperation( | |
| 85 user_context, | |
| 86 base::Bind(&EasyUnlockKeyManager::OnKeysFetched, | |
| 87 weak_ptr_factory_.GetWeakPtr(), | |
| 88 callback))); | |
| 89 get_keys_op_->Start(); | |
| 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 return dict.GetString(kKeyBluetoothAddress, &data->bluetooth_address) && | |
| 108 dict.GetString(kKeyPermitId, &data->public_key) && | |
| 109 dict.GetString(kKeyPsk, &data->psk); | |
| 110 } | |
| 111 | |
| 112 // static | |
| 113 void EasyUnlockKeyManager::DeviceDataListToRemoteDeviceList( | |
| 114 const EasyUnlockDeviceKeyDataList& data_list, | |
| 115 base::ListValue* device_list) { | |
| 116 device_list->Clear(); | |
| 117 for (size_t i = 0; i < data_list.size(); ++i) { | |
| 118 scoped_ptr<base::DictionaryValue> device_dict(new base::DictionaryValue); | |
| 119 DeviceDataToRemoteDeviceDictionary(data_list[i], device_dict.get()); | |
| 120 device_list->Append(device_dict.release()); | |
| 121 } | |
| 122 } | |
| 123 | |
| 124 // static | |
| 125 bool EasyUnlockKeyManager::RemoteDeviceListToDeviceDataList( | |
| 126 const base::ListValue& device_list, | |
| 127 EasyUnlockDeviceKeyDataList* data_list) { | |
| 128 EasyUnlockDeviceKeyDataList parsed_devices; | |
| 129 for (base::ListValue::const_iterator it = device_list.begin(); | |
| 130 it != device_list.end(); | |
| 131 ++it) { | |
| 132 const base::DictionaryValue* dict; | |
| 133 if (!(*it)->GetAsDictionary(&dict) || !dict) | |
| 134 return false; | |
| 135 | |
| 136 EasyUnlockDeviceKeyData data; | |
| 137 if (!RemoteDeviceDictionaryToDeviceData(*dict, &data)) | |
| 138 return false; | |
| 139 | |
| 140 parsed_devices.push_back(data); | |
| 141 } | |
| 142 | |
| 143 data_list->swap(parsed_devices); | |
| 144 return true; | |
| 145 } | |
| 146 | |
| 147 // static | |
| 148 std::string EasyUnlockKeyManager::GetKeyLabel(int key_index) { | |
| 149 return base::StringPrintf("%s%d", kKeyLabelPrefix, key_index); | |
| 150 } | |
| 151 | |
| 152 void EasyUnlockKeyManager::OnKeysRemovedForCreateKeys( | |
| 153 const CreateKeysCallback& callback, | |
| 154 bool remove_success) { | |
| 155 if (!remove_success) { | |
| 156 callback.Run(false); | |
| 157 return; | |
| 158 } | |
| 159 | |
| 160 // Only one pending request. | |
| 161 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.
| |
| 162 create_keys_op_->Start(); | |
| 163 } | |
| 164 | |
| 165 void EasyUnlockKeyManager::OnKeysCreated(const CreateKeysCallback& callback, | |
| 166 bool create_success) { | |
| 167 scoped_ptr<EasyUnlockCreateKeysOperation> op = create_keys_op_.Pass(); | |
| 168 if (!callback.is_null()) | |
| 169 callback.Run(create_success); | |
| 170 } | |
| 171 | |
| 172 void EasyUnlockKeyManager::OnKeysRemoved(const RemoveKeysCallback& callback, | |
| 173 bool remove_success) { | |
| 174 scoped_ptr<EasyUnlockRemoveKeysOperation> op = remove_keys_op_.Pass(); | |
| 175 if (!callback.is_null()) | |
| 176 callback.Run(remove_success); | |
| 177 } | |
| 178 | |
| 179 void EasyUnlockKeyManager::OnKeysFetched( | |
| 180 const GetDeviceDataListCallback& callback, | |
| 181 bool fetch_success, | |
| 182 const EasyUnlockDeviceKeyDataList& fetched_data) { | |
| 183 scoped_ptr<EasyUnlockGetKeysOperation> op = get_keys_op_.Pass(); | |
| 184 if (!callback.is_null()) | |
| 185 callback.Run(fetch_success, fetched_data); | |
| 186 } | |
| 187 | |
| 188 } // namespace chromeos | |
| OLD | NEW |