OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/installer/util/registry_key_backup.h" | 5 #include "chrome/installer/util/registry_key_backup.h" |
6 | 6 |
| 7 #include <stdint.h> |
| 8 |
7 #include <algorithm> | 9 #include <algorithm> |
8 #include <map> | 10 #include <map> |
9 #include <utility> | 11 #include <utility> |
10 #include <vector> | 12 #include <vector> |
11 | 13 |
12 #include "base/logging.h" | 14 #include "base/logging.h" |
13 #include "base/win/registry.h" | 15 #include "base/win/registry.h" |
14 | 16 |
15 using base::win::RegKey; | 17 using base::win::RegKey; |
16 | 18 |
17 namespace { | 19 namespace { |
18 | 20 |
19 const REGSAM kKeyReadNoNotify = (KEY_READ) & ~(KEY_NOTIFY); | 21 const REGSAM kKeyReadNoNotify = (KEY_READ) & ~(KEY_NOTIFY); |
20 | 22 |
21 // A container for a registry value. | 23 // A container for a registry value. |
22 class ValueData { | 24 class ValueData { |
23 public: | 25 public: |
24 ValueData(); | 26 ValueData(); |
25 ~ValueData(); | 27 ~ValueData(); |
26 | 28 |
27 // Initializes this object with a name (the first |name_size| characters in | 29 // Initializes this object with a name (the first |name_size| characters in |
28 // |name_buffer|, |type|, and data (the first |data_size| bytes in |data|). | 30 // |name_buffer|, |type|, and data (the first |data_size| bytes in |data|). |
29 void Initialize(const wchar_t* name_buffer, DWORD name_size, | 31 void Initialize(const wchar_t* name_buffer, |
30 DWORD type, const uint8* data, DWORD data_size); | 32 DWORD name_size, |
| 33 DWORD type, |
| 34 const uint8_t* data, |
| 35 DWORD data_size); |
31 | 36 |
32 // The possibly empty name of this value. | 37 // The possibly empty name of this value. |
33 const std::wstring& name_str() const { return name_; } | 38 const std::wstring& name_str() const { return name_; } |
34 | 39 |
35 // The name of this value, or NULL for the default (unnamed) value. | 40 // The name of this value, or NULL for the default (unnamed) value. |
36 const wchar_t* name() const { return name_.empty() ? NULL : name_.c_str(); } | 41 const wchar_t* name() const { return name_.empty() ? NULL : name_.c_str(); } |
37 | 42 |
38 // The type of this value. | 43 // The type of this value. |
39 DWORD type() const { return type_; } | 44 DWORD type() const { return type_; } |
40 | 45 |
41 // A pointer to a buffer of |data_len()| bytes containing the value's data, | 46 // A pointer to a buffer of |data_len()| bytes containing the value's data, |
42 // or NULL if the value has no data. | 47 // or NULL if the value has no data. |
43 const uint8* data() const { return data_.empty() ? NULL : &data_[0]; } | 48 const uint8_t* data() const { return data_.empty() ? NULL : &data_[0]; } |
44 | 49 |
45 // The size, in bytes, of the value's data. | 50 // The size, in bytes, of the value's data. |
46 DWORD data_len() const { return static_cast<DWORD>(data_.size()); } | 51 DWORD data_len() const { return static_cast<DWORD>(data_.size()); } |
47 | 52 |
48 private: | 53 private: |
49 // This value's name, or the empty string if this is the default (unnamed) | 54 // This value's name, or the empty string if this is the default (unnamed) |
50 // value. | 55 // value. |
51 std::wstring name_; | 56 std::wstring name_; |
52 // This value's data. | 57 // This value's data. |
53 std::vector<uint8> data_; | 58 std::vector<uint8_t> data_; |
54 // This value's type (e.g., REG_DWORD, REG_SZ, REG_QWORD, etc). | 59 // This value's type (e.g., REG_DWORD, REG_SZ, REG_QWORD, etc). |
55 DWORD type_; | 60 DWORD type_; |
56 | 61 |
57 // Copy constructible and assignable for use in STL containers. | 62 // Copy constructible and assignable for use in STL containers. |
58 }; | 63 }; |
59 | 64 |
60 } // namespace | 65 } // namespace |
61 | 66 |
62 // A container for a registry key, its values, and its subkeys. | 67 // A container for a registry key, its values, and its subkeys. |
63 class RegistryKeyBackup::KeyData { | 68 class RegistryKeyBackup::KeyData { |
(...skipping 21 matching lines...) Expand all Loading... |
85 | 90 |
86 // Copy constructible and assignable for use in STL containers. | 91 // Copy constructible and assignable for use in STL containers. |
87 }; | 92 }; |
88 | 93 |
89 ValueData::ValueData() : type_(REG_NONE) { | 94 ValueData::ValueData() : type_(REG_NONE) { |
90 } | 95 } |
91 | 96 |
92 ValueData::~ValueData() { | 97 ValueData::~ValueData() { |
93 } | 98 } |
94 | 99 |
95 void ValueData::Initialize( | 100 void ValueData::Initialize(const wchar_t* name_buffer, |
96 const wchar_t* name_buffer, | 101 DWORD name_size, |
97 DWORD name_size, | 102 DWORD type, |
98 DWORD type, | 103 const uint8_t* data, |
99 const uint8* data, | 104 DWORD data_size) { |
100 DWORD data_size) { | |
101 name_.assign(name_buffer, name_size); | 105 name_.assign(name_buffer, name_size); |
102 type_ = type; | 106 type_ = type; |
103 data_.assign(data, data + data_size); | 107 data_.assign(data, data + data_size); |
104 } | 108 } |
105 | 109 |
106 RegistryKeyBackup::KeyData::KeyData() { | 110 RegistryKeyBackup::KeyData::KeyData() { |
107 } | 111 } |
108 | 112 |
109 RegistryKeyBackup::KeyData::~KeyData() { | 113 RegistryKeyBackup::KeyData::~KeyData() { |
110 } | 114 } |
(...skipping 14 matching lines...) Expand all Loading... |
125 if (result != ERROR_SUCCESS) { | 129 if (result != ERROR_SUCCESS) { |
126 LOG(ERROR) << "Failed getting info of key to backup, result: " << result; | 130 LOG(ERROR) << "Failed getting info of key to backup, result: " << result; |
127 return false; | 131 return false; |
128 } | 132 } |
129 DWORD max_name_len = std::max(max_subkey_name_len, max_value_name_len) + 1; | 133 DWORD max_name_len = std::max(max_subkey_name_len, max_value_name_len) + 1; |
130 std::vector<wchar_t> name_buffer(max_name_len); | 134 std::vector<wchar_t> name_buffer(max_name_len); |
131 | 135 |
132 // Backup the values. | 136 // Backup the values. |
133 if (num_values != 0) { | 137 if (num_values != 0) { |
134 values.reserve(num_values); | 138 values.reserve(num_values); |
135 std::vector<uint8> value_buffer(max_value_len != 0 ? max_value_len : 1); | 139 std::vector<uint8_t> value_buffer(max_value_len != 0 ? max_value_len : 1); |
136 DWORD name_size = 0; | 140 DWORD name_size = 0; |
137 DWORD value_type = REG_NONE; | 141 DWORD value_type = REG_NONE; |
138 DWORD value_size = 0; | 142 DWORD value_size = 0; |
139 | 143 |
140 for (DWORD i = 0; i < num_values; ) { | 144 for (DWORD i = 0; i < num_values; ) { |
141 name_size = static_cast<DWORD>(name_buffer.size()); | 145 name_size = static_cast<DWORD>(name_buffer.size()); |
142 value_size = static_cast<DWORD>(value_buffer.size()); | 146 value_size = static_cast<DWORD>(value_buffer.size()); |
143 result = RegEnumValue(key.Handle(), i, &name_buffer[0], &name_size, | 147 result = RegEnumValue(key.Handle(), i, &name_buffer[0], &name_size, |
144 NULL, &value_type, &value_buffer[0], &value_size); | 148 NULL, &value_type, &value_buffer[0], &value_size); |
145 switch (result) { | 149 switch (result) { |
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
319 } else { | 323 } else { |
320 success = key_data_->WriteTo(&dest_key); | 324 success = key_data_->WriteTo(&dest_key); |
321 LOG_IF(ERROR, !success) << "Failed to write key data."; | 325 LOG_IF(ERROR, !success) << "Failed to write key data."; |
322 } | 326 } |
323 } else { | 327 } else { |
324 success = true; | 328 success = true; |
325 } | 329 } |
326 | 330 |
327 return success; | 331 return success; |
328 } | 332 } |
OLD | NEW |