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 <algorithm> | 7 #include <algorithm> |
8 #include <limits> | 8 #include <limits> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/win/registry.h" | 12 #include "base/win/registry.h" |
13 | 13 |
14 using base::win::RegKey; | 14 using base::win::RegKey; |
15 | 15 |
16 namespace { | 16 namespace { |
17 const REGSAM kKeyReadNoNotify = (KEY_READ) & ~(KEY_NOTIFY); | 17 const REGSAM kKeyReadNoNotify = (KEY_READ) & ~(KEY_NOTIFY); |
18 } // namespace | 18 } // namespace |
19 | 19 |
20 // A container for a registry key, its values, and its subkeys. | 20 // A container for a registry key, its values, and its subkeys. |
21 class RegistryKeyBackup::KeyData { | 21 class RegistryKeyBackup::KeyData { |
22 public: | 22 public: |
23 KeyData(); | 23 KeyData(); |
24 ~KeyData(); | 24 ~KeyData(); |
| 25 |
| 26 // Initializes this object by reading the values and subkeys of |key|. |
| 27 // Security descriptors are not backed up. Returns true if the operation was |
| 28 // successful; false otherwise, in which case the state of this object is not |
| 29 // modified. |
25 bool Initialize(const RegKey& key); | 30 bool Initialize(const RegKey& key); |
| 31 |
| 32 // Writes the contents of this object to |key|, which must have been opened |
| 33 // with at least REG_SET_VALUE and KEY_CREATE_SUB_KEY access rights. Returns |
| 34 // true if the operation was successful; false otherwise, in which case the |
| 35 // contents of |key| may have been modified. |
26 bool WriteTo(RegKey* key) const; | 36 bool WriteTo(RegKey* key) const; |
27 | 37 |
28 private: | 38 private: |
29 class ValueData; | 39 class ValueData; |
30 | 40 |
| 41 // The values of this key. |
31 scoped_array<ValueData> values_; | 42 scoped_array<ValueData> values_; |
| 43 // The names of this key's sub-keys (the data for subkey_names_[i] is in |
| 44 // subkeys_[i]). |
32 scoped_array<std::wstring> subkey_names_; | 45 scoped_array<std::wstring> subkey_names_; |
| 46 // The key data of this key's sub-keys. |
33 scoped_array<KeyData> subkeys_; | 47 scoped_array<KeyData> subkeys_; |
| 48 // The number of values for this key. |
34 DWORD num_values_; | 49 DWORD num_values_; |
| 50 // The number of subkeys for this key. |
35 DWORD num_subkeys_; | 51 DWORD num_subkeys_; |
36 | 52 |
37 DISALLOW_COPY_AND_ASSIGN(KeyData); | 53 DISALLOW_COPY_AND_ASSIGN(KeyData); |
38 }; | 54 }; |
39 | 55 |
40 // A container for a registry value. | 56 // A container for a registry value. |
41 class RegistryKeyBackup::KeyData::ValueData { | 57 class RegistryKeyBackup::KeyData::ValueData { |
42 public: | 58 public: |
43 ValueData(); | 59 ValueData(); |
44 ~ValueData(); | 60 ~ValueData(); |
| 61 |
| 62 // Initializes this object with a name (the first |name_size| characters in |
| 63 // |name_buffer|, |type|, and data (the first |data_size| bytes in |data|). |
45 void Initialize(const wchar_t* name_buffer, DWORD name_size, | 64 void Initialize(const wchar_t* name_buffer, DWORD name_size, |
46 DWORD type, const uint8* data, DWORD data_size); | 65 DWORD type, const uint8* data, DWORD data_size); |
| 66 |
| 67 // The possibly empty name of this value. |
47 const std::wstring& name_str() const { return name_; } | 68 const std::wstring& name_str() const { return name_; } |
| 69 |
| 70 // The name of this value, or NULL for the default (unnamed) value. |
48 const wchar_t* name() const { return name_.empty() ? NULL : name_.c_str(); } | 71 const wchar_t* name() const { return name_.empty() ? NULL : name_.c_str(); } |
| 72 |
| 73 // The type of this value. |
49 DWORD type() const { return type_; } | 74 DWORD type() const { return type_; } |
| 75 |
| 76 // A pointer to a buffer of |data_len()| bytes containing the value's data, |
| 77 // or NULL if the value has no data. |
50 const uint8* data() const { return data_.empty() ? NULL : &data_[0]; } | 78 const uint8* data() const { return data_.empty() ? NULL : &data_[0]; } |
| 79 |
| 80 // The size, in bytes, of the value's data. |
51 DWORD data_len() const { return static_cast<DWORD>(data_.size()); } | 81 DWORD data_len() const { return static_cast<DWORD>(data_.size()); } |
52 | 82 |
53 private: | 83 private: |
| 84 // This value's name, or the empty string if this is the default (unnamed) |
| 85 // value. |
54 std::wstring name_; | 86 std::wstring name_; |
| 87 // This value's data. |
55 std::vector<uint8> data_; | 88 std::vector<uint8> data_; |
| 89 // This value's type (e.g., REG_DWORD, REG_SZ, REG_QWORD, etc). |
56 DWORD type_; | 90 DWORD type_; |
57 | 91 |
58 DISALLOW_COPY_AND_ASSIGN(ValueData); | 92 DISALLOW_COPY_AND_ASSIGN(ValueData); |
59 }; | 93 }; |
60 | 94 |
61 RegistryKeyBackup::KeyData::ValueData::ValueData() | 95 RegistryKeyBackup::KeyData::ValueData::ValueData() |
62 : type_(REG_NONE) { | 96 : type_(REG_NONE) { |
63 } | 97 } |
64 | 98 |
65 RegistryKeyBackup::KeyData::ValueData::~ValueData() | 99 RegistryKeyBackup::KeyData::ValueData::~ValueData() |
(...skipping 13 matching lines...) Expand all Loading... |
79 | 113 |
80 RegistryKeyBackup::KeyData::KeyData() | 114 RegistryKeyBackup::KeyData::KeyData() |
81 : num_values_(0), | 115 : num_values_(0), |
82 num_subkeys_(0) { | 116 num_subkeys_(0) { |
83 } | 117 } |
84 | 118 |
85 RegistryKeyBackup::KeyData::~KeyData() | 119 RegistryKeyBackup::KeyData::~KeyData() |
86 { | 120 { |
87 } | 121 } |
88 | 122 |
89 // Initializes this object by reading the values and subkeys of |key|. | |
90 // Security descriptors are not backed up. | |
91 bool RegistryKeyBackup::KeyData::Initialize(const RegKey& key) { | 123 bool RegistryKeyBackup::KeyData::Initialize(const RegKey& key) { |
92 scoped_array<ValueData> values; | 124 scoped_array<ValueData> values; |
93 scoped_array<std::wstring> subkey_names; | 125 scoped_array<std::wstring> subkey_names; |
94 scoped_array<KeyData> subkeys; | 126 scoped_array<KeyData> subkeys; |
95 | 127 |
96 DWORD num_subkeys = 0; | 128 DWORD num_subkeys = 0; |
97 DWORD max_subkey_name_len = 0; | 129 DWORD max_subkey_name_len = 0; |
98 DWORD num_values = 0; | 130 DWORD num_values = 0; |
99 DWORD max_value_name_len = 0; | 131 DWORD max_value_name_len = 0; |
100 DWORD max_value_len = 0; | 132 DWORD max_value_len = 0; |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
222 | 254 |
223 values_.swap(values); | 255 values_.swap(values); |
224 subkey_names_.swap(subkey_names); | 256 subkey_names_.swap(subkey_names); |
225 subkeys_.swap(subkeys); | 257 subkeys_.swap(subkeys); |
226 num_values_ = num_values; | 258 num_values_ = num_values; |
227 num_subkeys_ = num_subkeys; | 259 num_subkeys_ = num_subkeys; |
228 | 260 |
229 return true; | 261 return true; |
230 } | 262 } |
231 | 263 |
232 // Writes the values and subkeys of this object into |key|. | |
233 bool RegistryKeyBackup::KeyData::WriteTo(RegKey* key) const { | 264 bool RegistryKeyBackup::KeyData::WriteTo(RegKey* key) const { |
234 DCHECK(key); | 265 DCHECK(key); |
235 | 266 |
236 LONG result = ERROR_SUCCESS; | 267 LONG result = ERROR_SUCCESS; |
237 | 268 |
238 // Write the values. | 269 // Write the values. |
239 for (DWORD i = 0; i < num_values_; ++i) { | 270 for (DWORD i = 0; i < num_values_; ++i) { |
240 const ValueData& value = values_[i]; | 271 const ValueData& value = values_[i]; |
241 result = RegSetValueEx(key->Handle(), value.name(), 0, value.type(), | 272 result = RegSetValueEx(key->Handle(), value.name(), 0, value.type(), |
242 value.data(), value.data_len()); | 273 value.data(), value.data_len()); |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
312 } else { | 343 } else { |
313 success = key_data_->WriteTo(&dest_key); | 344 success = key_data_->WriteTo(&dest_key); |
314 LOG_IF(ERROR, !success) << "Failed to write key data."; | 345 LOG_IF(ERROR, !success) << "Failed to write key data."; |
315 } | 346 } |
316 } else { | 347 } else { |
317 success = true; | 348 success = true; |
318 } | 349 } |
319 | 350 |
320 return success; | 351 return success; |
321 } | 352 } |
OLD | NEW |