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::RefreshKeys(const UserContext& user_context, |
| 33 const base::ListValue& remote_devices, |
| 34 const CreateKeysCallback& callback) { |
| 35 // Must have the secret. |
| 36 DCHECK(!user_context.GetKey()->GetSecret().empty()); |
| 37 |
| 38 EasyUnlockDeviceKeyDataList devices; |
| 39 if (!RemoteDeviceListToDeviceDataList(remote_devices, &devices)) |
| 40 devices.clear(); |
| 41 |
| 42 // Remove existing keys before creating new ones. |
| 43 RemoveKeys(user_context, |
| 44 base::Bind(&EasyUnlockKeyManager::OnKeysRemovedForCreateKeys, |
| 45 weak_ptr_factory_.GetWeakPtr(), |
| 46 user_context, |
| 47 devices, |
| 48 callback)); |
| 49 } |
| 50 |
| 51 void EasyUnlockKeyManager::RemoveKeys(const UserContext& user_context, |
| 52 const RemoveKeysCallback& callback) { |
| 53 // Must have the secret. |
| 54 DCHECK(!user_context.GetKey()->GetSecret().empty()); |
| 55 // Only one pending request. |
| 56 DCHECK(!remove_keys_op_); |
| 57 |
| 58 remove_keys_op_.reset( |
| 59 new EasyUnlockRemoveKeysOperation( |
| 60 user_context, |
| 61 base::Bind(&EasyUnlockKeyManager::OnKeysRemoved, |
| 62 weak_ptr_factory_.GetWeakPtr(), |
| 63 callback))); |
| 64 remove_keys_op_->Start(); |
| 65 } |
| 66 |
| 67 void EasyUnlockKeyManager::GetDeviceDataList( |
| 68 const UserContext& user_context, |
| 69 const GetDeviceDataListCallback& callback) { |
| 70 // Only one pending request. |
| 71 DCHECK(!get_keys_op_); |
| 72 |
| 73 get_keys_op_.reset(new EasyUnlockGetKeysOperation( |
| 74 user_context, |
| 75 base::Bind(&EasyUnlockKeyManager::OnKeysFetched, |
| 76 weak_ptr_factory_.GetWeakPtr(), |
| 77 callback))); |
| 78 get_keys_op_->Start(); |
| 79 } |
| 80 |
| 81 // static |
| 82 void EasyUnlockKeyManager::DeviceDataToRemoteDeviceDictionary( |
| 83 const EasyUnlockDeviceKeyData& data, |
| 84 base::DictionaryValue* dict) { |
| 85 dict->SetString(kKeyBluetoothAddress, data.bluetooth_address); |
| 86 dict->SetString(kKeyPsk, data.psk); |
| 87 scoped_ptr<base::DictionaryValue> permit_record(new base::DictionaryValue); |
| 88 permit_record->SetString(kKeyId, data.public_key); |
| 89 dict->Set(kKeyPermitRecord, permit_record.release()); |
| 90 } |
| 91 |
| 92 // static |
| 93 bool EasyUnlockKeyManager::RemoteDeviceDictionaryToDeviceData( |
| 94 const base::DictionaryValue& dict, |
| 95 EasyUnlockDeviceKeyData* data) { |
| 96 return dict.GetString(kKeyBluetoothAddress, &data->bluetooth_address) && |
| 97 dict.GetString(kKeyPermitId, &data->public_key) && |
| 98 dict.GetString(kKeyPsk, &data->psk); |
| 99 } |
| 100 |
| 101 // static |
| 102 void EasyUnlockKeyManager::DeviceDataListToRemoteDeviceList( |
| 103 const EasyUnlockDeviceKeyDataList& data_list, |
| 104 base::ListValue* device_list) { |
| 105 device_list->Clear(); |
| 106 for (size_t i = 0; i < data_list.size(); ++i) { |
| 107 scoped_ptr<base::DictionaryValue> device_dict(new base::DictionaryValue); |
| 108 DeviceDataToRemoteDeviceDictionary(data_list[i], device_dict.get()); |
| 109 device_list->Append(device_dict.release()); |
| 110 } |
| 111 } |
| 112 |
| 113 // static |
| 114 bool EasyUnlockKeyManager::RemoteDeviceListToDeviceDataList( |
| 115 const base::ListValue& device_list, |
| 116 EasyUnlockDeviceKeyDataList* data_list) { |
| 117 EasyUnlockDeviceKeyDataList parsed_devices; |
| 118 for (base::ListValue::const_iterator it = device_list.begin(); |
| 119 it != device_list.end(); |
| 120 ++it) { |
| 121 const base::DictionaryValue* dict; |
| 122 if (!(*it)->GetAsDictionary(&dict) || !dict) |
| 123 return false; |
| 124 |
| 125 EasyUnlockDeviceKeyData data; |
| 126 if (!RemoteDeviceDictionaryToDeviceData(*dict, &data)) |
| 127 return false; |
| 128 |
| 129 parsed_devices.push_back(data); |
| 130 } |
| 131 |
| 132 data_list->swap(parsed_devices); |
| 133 return true; |
| 134 } |
| 135 |
| 136 // static |
| 137 std::string EasyUnlockKeyManager::GetKeyLabel(size_t key_index) { |
| 138 return base::StringPrintf("%s%zu", kKeyLabelPrefix, key_index); |
| 139 } |
| 140 |
| 141 void EasyUnlockKeyManager::OnKeysRemovedForCreateKeys( |
| 142 const UserContext& user_context, |
| 143 const EasyUnlockDeviceKeyDataList& devices, |
| 144 const CreateKeysCallback& callback, |
| 145 bool remove_success) { |
| 146 if (!remove_success) { |
| 147 callback.Run(false); |
| 148 return; |
| 149 } |
| 150 |
| 151 // Only one pending request. |
| 152 DCHECK(!create_keys_op_); |
| 153 create_keys_op_.reset(new EasyUnlockCreateKeysOperation( |
| 154 user_context, |
| 155 devices, |
| 156 base::Bind(&EasyUnlockKeyManager::OnKeysCreated, |
| 157 weak_ptr_factory_.GetWeakPtr(), |
| 158 callback))); |
| 159 create_keys_op_->Start(); |
| 160 } |
| 161 |
| 162 void EasyUnlockKeyManager::OnKeysCreated(const CreateKeysCallback& callback, |
| 163 bool create_success) { |
| 164 scoped_ptr<EasyUnlockCreateKeysOperation> op = create_keys_op_.Pass(); |
| 165 if (!callback.is_null()) |
| 166 callback.Run(create_success); |
| 167 } |
| 168 |
| 169 void EasyUnlockKeyManager::OnKeysRemoved(const RemoveKeysCallback& callback, |
| 170 bool remove_success) { |
| 171 scoped_ptr<EasyUnlockRemoveKeysOperation> op = remove_keys_op_.Pass(); |
| 172 if (!callback.is_null()) |
| 173 callback.Run(remove_success); |
| 174 } |
| 175 |
| 176 void EasyUnlockKeyManager::OnKeysFetched( |
| 177 const GetDeviceDataListCallback& callback, |
| 178 bool fetch_success, |
| 179 const EasyUnlockDeviceKeyDataList& fetched_data) { |
| 180 scoped_ptr<EasyUnlockGetKeysOperation> op = get_keys_op_.Pass(); |
| 181 if (!callback.is_null()) |
| 182 callback.Run(fetch_success, fetched_data); |
| 183 } |
| 184 |
| 185 } // namespace chromeos |
OLD | NEW |