| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "remoting/host/pairing_registry_delegate_win.h" | 5 #include "remoting/host/pairing_registry_delegate_win.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/json/json_string_value_serializer.h" | 9 #include "base/json/json_string_value_serializer.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/memory/ptr_util.h" |
| 11 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
| 12 #include "base/values.h" | 13 #include "base/values.h" |
| 13 #include "base/win/registry.h" | 14 #include "base/win/registry.h" |
| 14 | 15 |
| 15 namespace remoting { | 16 namespace remoting { |
| 16 | 17 |
| 17 namespace { | 18 namespace { |
| 18 | 19 |
| 19 // Duplicates a registry key handle (returned by RegCreateXxx/RegOpenXxx). | 20 // Duplicates a registry key handle (returned by RegCreateXxx/RegOpenXxx). |
| 20 // The returned handle cannot be inherited and has the same permissions as | 21 // The returned handle cannot be inherited and has the same permissions as |
| (...skipping 10 matching lines...) Expand all Loading... |
| 31 PLOG(ERROR) << "Failed to duplicate a registry key handle"; | 32 PLOG(ERROR) << "Failed to duplicate a registry key handle"; |
| 32 return false; | 33 return false; |
| 33 } | 34 } |
| 34 | 35 |
| 35 dest->Set(reinterpret_cast<HKEY>(handle)); | 36 dest->Set(reinterpret_cast<HKEY>(handle)); |
| 36 return true; | 37 return true; |
| 37 } | 38 } |
| 38 | 39 |
| 39 // Reads value |value_name| from |key| as a JSON string and returns it as | 40 // Reads value |value_name| from |key| as a JSON string and returns it as |
| 40 // |base::Value|. | 41 // |base::Value|. |
| 41 scoped_ptr<base::DictionaryValue> ReadValue(const base::win::RegKey& key, | 42 std::unique_ptr<base::DictionaryValue> ReadValue(const base::win::RegKey& key, |
| 42 const wchar_t* value_name) { | 43 const wchar_t* value_name) { |
| 43 // presubmit: allow wstring | 44 // presubmit: allow wstring |
| 44 std::wstring value_json; | 45 std::wstring value_json; |
| 45 LONG result = key.ReadValue(value_name, &value_json); | 46 LONG result = key.ReadValue(value_name, &value_json); |
| 46 if (result != ERROR_SUCCESS) { | 47 if (result != ERROR_SUCCESS) { |
| 47 SetLastError(result); | 48 SetLastError(result); |
| 48 PLOG(ERROR) << "Cannot read value '" << value_name << "'"; | 49 PLOG(ERROR) << "Cannot read value '" << value_name << "'"; |
| 49 return nullptr; | 50 return nullptr; |
| 50 } | 51 } |
| 51 | 52 |
| 52 // Parse the value. | 53 // Parse the value. |
| 53 std::string value_json_utf8 = base::WideToUTF8(value_json); | 54 std::string value_json_utf8 = base::WideToUTF8(value_json); |
| 54 JSONStringValueDeserializer deserializer(value_json_utf8); | 55 JSONStringValueDeserializer deserializer(value_json_utf8); |
| 55 int error_code; | 56 int error_code; |
| 56 std::string error_message; | 57 std::string error_message; |
| 57 scoped_ptr<base::Value> value = | 58 std::unique_ptr<base::Value> value = |
| 58 deserializer.Deserialize(&error_code, &error_message); | 59 deserializer.Deserialize(&error_code, &error_message); |
| 59 if (!value) { | 60 if (!value) { |
| 60 LOG(ERROR) << "Failed to parse '" << value_name << "': " << error_message | 61 LOG(ERROR) << "Failed to parse '" << value_name << "': " << error_message |
| 61 << " (" << error_code << ")."; | 62 << " (" << error_code << ")."; |
| 62 return nullptr; | 63 return nullptr; |
| 63 } | 64 } |
| 64 | 65 |
| 65 if (!value->IsType(base::Value::TYPE_DICTIONARY)) { | 66 if (!value->IsType(base::Value::TYPE_DICTIONARY)) { |
| 66 LOG(ERROR) << "Failed to parse '" << value_name << "': not a dictionary."; | 67 LOG(ERROR) << "Failed to parse '" << value_name << "': not a dictionary."; |
| 67 return nullptr; | 68 return nullptr; |
| 68 } | 69 } |
| 69 | 70 |
| 70 return make_scoped_ptr(static_cast<base::DictionaryValue*>(value.release())); | 71 return base::WrapUnique(static_cast<base::DictionaryValue*>(value.release())); |
| 71 } | 72 } |
| 72 | 73 |
| 73 // Serializes |value| into a JSON string and writes it as value |value_name| | 74 // Serializes |value| into a JSON string and writes it as value |value_name| |
| 74 // under |key|. | 75 // under |key|. |
| 75 bool WriteValue(base::win::RegKey& key, | 76 bool WriteValue(base::win::RegKey& key, |
| 76 const wchar_t* value_name, | 77 const wchar_t* value_name, |
| 77 scoped_ptr<base::DictionaryValue> value) { | 78 std::unique_ptr<base::DictionaryValue> value) { |
| 78 std::string value_json_utf8; | 79 std::string value_json_utf8; |
| 79 JSONStringValueSerializer serializer(&value_json_utf8); | 80 JSONStringValueSerializer serializer(&value_json_utf8); |
| 80 if (!serializer.Serialize(*value)) { | 81 if (!serializer.Serialize(*value)) { |
| 81 LOG(ERROR) << "Failed to serialize '" << value_name << "'"; | 82 LOG(ERROR) << "Failed to serialize '" << value_name << "'"; |
| 82 return false; | 83 return false; |
| 83 } | 84 } |
| 84 | 85 |
| 85 // presubmit: allow wstring | 86 // presubmit: allow wstring |
| 86 std::wstring value_json = base::UTF8ToWide(value_json_utf8); | 87 std::wstring value_json = base::UTF8ToWide(value_json_utf8); |
| 87 LONG result = key.WriteValue(value_name, value_json.c_str()); | 88 LONG result = key.WriteValue(value_name, value_json.c_str()); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 114 return false; | 115 return false; |
| 115 | 116 |
| 116 if (privileged) { | 117 if (privileged) { |
| 117 if (!DuplicateKeyHandle(privileged, &privileged_)) | 118 if (!DuplicateKeyHandle(privileged, &privileged_)) |
| 118 return false; | 119 return false; |
| 119 } | 120 } |
| 120 | 121 |
| 121 return true; | 122 return true; |
| 122 } | 123 } |
| 123 | 124 |
| 124 scoped_ptr<base::ListValue> PairingRegistryDelegateWin::LoadAll() { | 125 std::unique_ptr<base::ListValue> PairingRegistryDelegateWin::LoadAll() { |
| 125 scoped_ptr<base::ListValue> pairings(new base::ListValue()); | 126 std::unique_ptr<base::ListValue> pairings(new base::ListValue()); |
| 126 | 127 |
| 127 // Enumerate and parse all values under the unprivileged key. | 128 // Enumerate and parse all values under the unprivileged key. |
| 128 DWORD count = unprivileged_.GetValueCount(); | 129 DWORD count = unprivileged_.GetValueCount(); |
| 129 for (DWORD index = 0; index < count; ++index) { | 130 for (DWORD index = 0; index < count; ++index) { |
| 130 // presubmit: allow wstring | 131 // presubmit: allow wstring |
| 131 std::wstring value_name; | 132 std::wstring value_name; |
| 132 LONG result = unprivileged_.GetValueNameAt(index, &value_name); | 133 LONG result = unprivileged_.GetValueNameAt(index, &value_name); |
| 133 if (result != ERROR_SUCCESS) { | 134 if (result != ERROR_SUCCESS) { |
| 134 SetLastError(result); | 135 SetLastError(result); |
| 135 PLOG(ERROR) << "Cannot get the name of value " << index; | 136 PLOG(ERROR) << "Cannot get the name of value " << index; |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 | 180 |
| 180 return success; | 181 return success; |
| 181 } | 182 } |
| 182 | 183 |
| 183 PairingRegistry::Pairing PairingRegistryDelegateWin::Load( | 184 PairingRegistry::Pairing PairingRegistryDelegateWin::Load( |
| 184 const std::string& client_id) { | 185 const std::string& client_id) { |
| 185 // presubmit: allow wstring | 186 // presubmit: allow wstring |
| 186 std::wstring value_name = base::UTF8ToWide(client_id); | 187 std::wstring value_name = base::UTF8ToWide(client_id); |
| 187 | 188 |
| 188 // Read unprivileged fields first. | 189 // Read unprivileged fields first. |
| 189 scoped_ptr<base::DictionaryValue> pairing = ReadValue(unprivileged_, | 190 std::unique_ptr<base::DictionaryValue> pairing = |
| 190 value_name.c_str()); | 191 ReadValue(unprivileged_, value_name.c_str()); |
| 191 if (!pairing) | 192 if (!pairing) |
| 192 return PairingRegistry::Pairing(); | 193 return PairingRegistry::Pairing(); |
| 193 | 194 |
| 194 // Read the shared secret. | 195 // Read the shared secret. |
| 195 if (privileged_.Valid()) { | 196 if (privileged_.Valid()) { |
| 196 scoped_ptr<base::DictionaryValue> secret = ReadValue(privileged_, | 197 std::unique_ptr<base::DictionaryValue> secret = |
| 197 value_name.c_str()); | 198 ReadValue(privileged_, value_name.c_str()); |
| 198 if (!secret) | 199 if (!secret) |
| 199 return PairingRegistry::Pairing(); | 200 return PairingRegistry::Pairing(); |
| 200 | 201 |
| 201 // Merge the two dictionaries. | 202 // Merge the two dictionaries. |
| 202 pairing->MergeDictionary(secret.get()); | 203 pairing->MergeDictionary(secret.get()); |
| 203 } | 204 } |
| 204 | 205 |
| 205 return PairingRegistry::Pairing::CreateFromValue(*pairing); | 206 return PairingRegistry::Pairing::CreateFromValue(*pairing); |
| 206 } | 207 } |
| 207 | 208 |
| 208 bool PairingRegistryDelegateWin::Save(const PairingRegistry::Pairing& pairing) { | 209 bool PairingRegistryDelegateWin::Save(const PairingRegistry::Pairing& pairing) { |
| 209 if (!privileged_.Valid()) { | 210 if (!privileged_.Valid()) { |
| 210 LOG(ERROR) << "Cannot save pairing entry '" << pairing.client_id() | 211 LOG(ERROR) << "Cannot save pairing entry '" << pairing.client_id() |
| 211 << "': the pairing registry privileged key is invalid."; | 212 << "': the pairing registry privileged key is invalid."; |
| 212 return false; | 213 return false; |
| 213 } | 214 } |
| 214 | 215 |
| 215 // Convert pairing to JSON. | 216 // Convert pairing to JSON. |
| 216 scoped_ptr<base::DictionaryValue> pairing_json = pairing.ToValue(); | 217 std::unique_ptr<base::DictionaryValue> pairing_json = pairing.ToValue(); |
| 217 | 218 |
| 218 // Extract the shared secret to a separate dictionary. | 219 // Extract the shared secret to a separate dictionary. |
| 219 scoped_ptr<base::Value> secret_key; | 220 std::unique_ptr<base::Value> secret_key; |
| 220 CHECK(pairing_json->Remove(PairingRegistry::kSharedSecretKey, &secret_key)); | 221 CHECK(pairing_json->Remove(PairingRegistry::kSharedSecretKey, &secret_key)); |
| 221 scoped_ptr<base::DictionaryValue> secret_json(new base::DictionaryValue()); | 222 std::unique_ptr<base::DictionaryValue> secret_json( |
| 223 new base::DictionaryValue()); |
| 222 secret_json->Set(PairingRegistry::kSharedSecretKey, secret_key.release()); | 224 secret_json->Set(PairingRegistry::kSharedSecretKey, secret_key.release()); |
| 223 | 225 |
| 224 // presubmit: allow wstring | 226 // presubmit: allow wstring |
| 225 std::wstring value_name = base::UTF8ToWide(pairing.client_id()); | 227 std::wstring value_name = base::UTF8ToWide(pairing.client_id()); |
| 226 | 228 |
| 227 // Write pairing to the registry. | 229 // Write pairing to the registry. |
| 228 if (!WriteValue(privileged_, value_name.c_str(), std::move(secret_json)) || | 230 if (!WriteValue(privileged_, value_name.c_str(), std::move(secret_json)) || |
| 229 !WriteValue(unprivileged_, value_name.c_str(), std::move(pairing_json))) { | 231 !WriteValue(unprivileged_, value_name.c_str(), std::move(pairing_json))) { |
| 230 return false; | 232 return false; |
| 231 } | 233 } |
| (...skipping 24 matching lines...) Expand all Loading... |
| 256 result != ERROR_FILE_NOT_FOUND && | 258 result != ERROR_FILE_NOT_FOUND && |
| 257 result != ERROR_PATH_NOT_FOUND) { | 259 result != ERROR_PATH_NOT_FOUND) { |
| 258 SetLastError(result); | 260 SetLastError(result); |
| 259 PLOG(ERROR) << "Cannot delete pairing entry '" << client_id << "'"; | 261 PLOG(ERROR) << "Cannot delete pairing entry '" << client_id << "'"; |
| 260 return false; | 262 return false; |
| 261 } | 263 } |
| 262 | 264 |
| 263 return true; | 265 return true; |
| 264 } | 266 } |
| 265 | 267 |
| 266 scoped_ptr<PairingRegistry::Delegate> CreatePairingRegistryDelegate() { | 268 std::unique_ptr<PairingRegistry::Delegate> CreatePairingRegistryDelegate() { |
| 267 return make_scoped_ptr(new PairingRegistryDelegateWin()); | 269 return base::WrapUnique(new PairingRegistryDelegateWin()); |
| 268 } | 270 } |
| 269 | 271 |
| 270 } // namespace remoting | 272 } // namespace remoting |
| OLD | NEW |