Chromium Code Reviews| Index: chrome/installer/util/registry_key_backup.cc |
| diff --git a/chrome/installer/util/registry_key_backup.cc b/chrome/installer/util/registry_key_backup.cc |
| index e3494614250aca9343994711739b7db9c78472c4..38608dfa66164beb4f50a1ba37eba8ccf0aea8e6 100644 |
| --- a/chrome/installer/util/registry_key_backup.cc |
| +++ b/chrome/installer/util/registry_key_backup.cc |
| @@ -6,6 +6,7 @@ |
| #include <algorithm> |
| #include <limits> |
| +#include <map> |
| #include <vector> |
| #include "base/logging.h" |
| @@ -14,47 +15,11 @@ |
| using base::win::RegKey; |
| namespace { |
| -const REGSAM kKeyReadNoNotify = (KEY_READ) & ~(KEY_NOTIFY); |
| -} // namespace |
| - |
| -// A container for a registry key, its values, and its subkeys. |
| -class RegistryKeyBackup::KeyData { |
| - public: |
| - KeyData(); |
| - ~KeyData(); |
| - |
| - // Initializes this object by reading the values and subkeys of |key|. |
| - // Security descriptors are not backed up. Returns true if the operation was |
| - // successful; false otherwise, in which case the state of this object is not |
| - // modified. |
| - bool Initialize(const RegKey& key); |
| - |
| - // Writes the contents of this object to |key|, which must have been opened |
| - // with at least REG_SET_VALUE and KEY_CREATE_SUB_KEY access rights. Returns |
| - // true if the operation was successful; false otherwise, in which case the |
| - // contents of |key| may have been modified. |
| - bool WriteTo(RegKey* key) const; |
| - private: |
| - class ValueData; |
| - |
| - // The values of this key. |
| - scoped_array<ValueData> values_; |
| - // The names of this key's sub-keys (the data for subkey_names_[i] is in |
| - // subkeys_[i]). |
| - scoped_array<std::wstring> subkey_names_; |
| - // The key data of this key's sub-keys. |
| - scoped_array<KeyData> subkeys_; |
| - // The number of values for this key. |
| - DWORD num_values_; |
| - // The number of subkeys for this key. |
| - DWORD num_subkeys_; |
| - |
| - DISALLOW_COPY_AND_ASSIGN(KeyData); |
| -}; |
| +const REGSAM kKeyReadNoNotify = (KEY_READ) & ~(KEY_NOTIFY); |
| // A container for a registry value. |
| -class RegistryKeyBackup::KeyData::ValueData { |
| +class ValueData { |
| public: |
| ValueData(); |
| ~ValueData(); |
| @@ -89,18 +54,45 @@ class RegistryKeyBackup::KeyData::ValueData { |
| // This value's type (e.g., REG_DWORD, REG_SZ, REG_QWORD, etc). |
| DWORD type_; |
| - DISALLOW_COPY_AND_ASSIGN(ValueData); |
| + // Explicitly copyable for STL containers. |
|
tommi (sloooow) - chröme
2013/04/16 11:22:09
nit: Isn't it rather implicit since it's not calle
grt (UTC plus 2)
2013/04/16 14:28:25
how about:
// CopyConstructible and Assignable f
dcheng
2013/04/16 18:49:53
Done.
|
| }; |
| -RegistryKeyBackup::KeyData::ValueData::ValueData() |
| - : type_(REG_NONE) { |
| +} // namespace |
| + |
| +// A container for a registry key, its values, and its subkeys. |
| +class RegistryKeyBackup::KeyData { |
| + public: |
| + KeyData(); |
| + ~KeyData(); |
| + |
| + // Initializes this object by reading the values and subkeys of |key|. |
| + // Security descriptors are not backed up. Returns true if the operation was |
| + // successful; false otherwise, in which case the state of this object is not |
| + // modified. |
| + bool Initialize(const RegKey& key); |
| + |
| + // Writes the contents of this object to |key|, which must have been opened |
| + // with at least REG_SET_VALUE and KEY_CREATE_SUB_KEY access rights. Returns |
| + // true if the operation was successful; false otherwise, in which case the |
| + // contents of |key| may have been modified. |
| + bool WriteTo(RegKey* key) const; |
| + |
| + private: |
| + // The values of this key. |
| + std::vector<ValueData> values_; |
| + // Map of subkey names to the corresponding KeyData. |
| + std::map<std::wstring, KeyData> subkeys_; |
| + |
| + // Explicitly copyable for STL containers. |
|
tommi (sloooow) - chröme
2013/04/16 11:22:09
nit: implicit, since the copy ctor etc are compile
grt (UTC plus 2)
2013/04/16 14:28:25
how about:
// CopyConstructible and Assignable f
dcheng
2013/04/16 18:49:53
Done.
|
| +}; |
| + |
| +ValueData::ValueData() : type_(REG_NONE) { |
| } |
| -RegistryKeyBackup::KeyData::ValueData::~ValueData() |
| -{ |
| +ValueData::~ValueData() { |
| } |
| -void RegistryKeyBackup::KeyData::ValueData::Initialize( |
| +void ValueData::Initialize( |
| const wchar_t* name_buffer, |
| DWORD name_size, |
| DWORD type, |
| @@ -111,9 +103,7 @@ void RegistryKeyBackup::KeyData::ValueData::Initialize( |
| data_.assign(data, data + data_size); |
| } |
| -RegistryKeyBackup::KeyData::KeyData() |
| - : num_values_(0), |
| - num_subkeys_(0) { |
| +RegistryKeyBackup::KeyData::KeyData() { |
| } |
| RegistryKeyBackup::KeyData::~KeyData() |
| @@ -121,9 +111,8 @@ RegistryKeyBackup::KeyData::~KeyData() |
| } |
| bool RegistryKeyBackup::KeyData::Initialize(const RegKey& key) { |
| - scoped_array<ValueData> values; |
| - scoped_array<std::wstring> subkey_names; |
| - scoped_array<KeyData> subkeys; |
| + std::vector<ValueData> values; |
| + std::map<std::wstring, KeyData> subkeys; |
| DWORD num_subkeys = 0; |
| DWORD max_subkey_name_len = 0; |
| @@ -149,7 +138,6 @@ bool RegistryKeyBackup::KeyData::Initialize(const RegKey& key) { |
| // Backup the values. |
| if (num_values != 0) { |
|
grt (UTC plus 2)
2013/04/16 14:28:25
inside this block:
values.reserve(num_values);
dcheng
2013/04/16 18:49:53
Done.
|
| - values.reset(new ValueData[num_values]); |
| scoped_array<uint8> value_buffer(new uint8[max_value_len]); |
|
grt (UTC plus 2)
2013/04/16 14:28:25
std::vector<uint8> value_buffer(max_value_len ? ma
dcheng
2013/04/16 18:49:53
Done.
|
| DWORD name_size = 0; |
| DWORD value_type = REG_NONE; |
| @@ -165,8 +153,9 @@ bool RegistryKeyBackup::KeyData::Initialize(const RegKey& key) { |
| num_values = i; |
| break; |
| case ERROR_SUCCESS: |
| - values[i].Initialize(name_buffer.get(), name_size, value_type, |
| - value_buffer.get(), value_size); |
| + values.push_back(ValueData()); |
| + values.back().Initialize(name_buffer.get(), name_size, value_type, |
|
grt (UTC plus 2)
2013/04/16 14:28:25
name_buffer.get() -> &name_buffer[0]
dcheng
2013/04/16 18:49:53
Done.
|
| + value_buffer.get(), value_size); |
|
grt (UTC plus 2)
2013/04/16 14:28:25
value_buffer.get() -> &value_buffer[0]
dcheng
2013/04/16 18:49:53
Done.
|
| ++i; |
| break; |
| case ERROR_MORE_DATA: |
| @@ -199,8 +188,6 @@ bool RegistryKeyBackup::KeyData::Initialize(const RegKey& key) { |
| // Backup the subkeys. |
| if (num_subkeys != 0) { |
| - subkey_names.reset(new std::wstring[num_subkeys]); |
| - subkeys.reset(new KeyData[num_subkeys]); |
| DWORD name_size = 0; |
| // Get the names of them. |
| @@ -213,7 +200,7 @@ bool RegistryKeyBackup::KeyData::Initialize(const RegKey& key) { |
| num_subkeys = i; |
| break; |
| case ERROR_SUCCESS: |
| - subkey_names[i].assign(name_buffer.get(), name_size); |
| + subkeys[std::wstring(name_buffer.get(), name_size)] = KeyData(); |
|
grt (UTC plus 2)
2013/04/16 14:28:25
name_buffer.get() -> &name_buffer[0]
grt (UTC plus 2)
2013/04/16 14:28:25
nit: " = KeyData()" constructs an extra object tha
dcheng
2013/04/16 18:49:53
That's true, but it's kind of non-obvious. I've ch
dcheng
2013/04/16 18:49:53
Done.
|
| ++i; |
| break; |
| case ERROR_MORE_DATA: |
| @@ -237,26 +224,24 @@ bool RegistryKeyBackup::KeyData::Initialize(const RegKey& key) { |
| // Get their values. |
| RegKey subkey; |
| - for (DWORD i = 0; i < num_subkeys; ++i) { |
| - result = subkey.Open(key.Handle(), subkey_names[i].c_str(), |
| + for (std::map<std::wstring, KeyData>::iterator it = subkeys.begin(); |
| + it != subkeys.end(); ++it) { |
| + result = subkey.Open(key.Handle(), it->first.c_str(), |
| kKeyReadNoNotify); |
|
grt (UTC plus 2)
2013/04/16 14:28:25
nit: move to previous line
dcheng
2013/04/16 18:49:53
Done.
|
| if (result != ERROR_SUCCESS) { |
| - LOG(ERROR) << "Failed opening subkey \"" << subkey_names[i] |
| + LOG(ERROR) << "Failed opening subkey \"" << it->first |
| << "\" for backup, result: " << result; |
| return false; |
| } |
| - if (!subkeys[i].Initialize(subkey)) { |
| - LOG(ERROR) << "Failed backing up subkey \"" << subkey_names[i] << "\""; |
| + if (!it->second.Initialize(subkey)) { |
| + LOG(ERROR) << "Failed backing up subkey \"" << it->first << "\""; |
| return false; |
| } |
| } |
| } |
| values_.swap(values); |
| - subkey_names_.swap(subkey_names); |
| subkeys_.swap(subkeys); |
| - num_values_ = num_values; |
| - num_subkeys_ = num_subkeys; |
| return true; |
| } |
| @@ -267,8 +252,9 @@ bool RegistryKeyBackup::KeyData::WriteTo(RegKey* key) const { |
| LONG result = ERROR_SUCCESS; |
| // Write the values. |
| - for (DWORD i = 0; i < num_values_; ++i) { |
| - const ValueData& value = values_[i]; |
| + for (std::vector<ValueData>::const_iterator it = values_.begin(); |
| + it != values_.end(); ++it) { |
| + const ValueData& value = *it; |
| result = RegSetValueEx(key->Handle(), value.name(), 0, value.type(), |
| value.data(), value.data_len()); |
| if (result != ERROR_SUCCESS) { |
| @@ -280,8 +266,9 @@ bool RegistryKeyBackup::KeyData::WriteTo(RegKey* key) const { |
| // Write the subkeys. |
| RegKey subkey; |
| - for (DWORD i = 0; i < num_subkeys_; ++i) { |
| - const std::wstring& name = subkey_names_[i]; |
| + for (std::map<std::wstring, KeyData>::const_iterator it = subkeys_.begin(); |
| + it != subkeys_.end(); ++it) { |
| + const std::wstring& name = it->first; |
| result = subkey.Create(key->Handle(), name.c_str(), KEY_WRITE); |
| if (result != ERROR_SUCCESS) { |
| @@ -289,7 +276,7 @@ bool RegistryKeyBackup::KeyData::WriteTo(RegKey* key) const { |
| << result; |
| return false; |
| } |
| - if (!subkeys_[i].WriteTo(&subkey)) { |
| + if (!it->second.WriteTo(&subkey)) { |
| LOG(ERROR) << "Failed writing subkey \"" << name << "\", result: " |
| << result; |
| return false; |