Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(53)

Side by Side Diff: chrome/installer/util/set_reg_value_work_item.cc

Issue 1220473002: Introduce a SetRegValueWorkItem overload that accepts a callback instead (...) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: review:grt Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/set_reg_value_work_item.h" 5 #include "chrome/installer/util/set_reg_value_work_item.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/strings/string_util.h" 8 #include "base/strings/string_util.h"
9 #include "base/win/registry.h" 9 #include "base/win/registry.h"
10 #include "chrome/installer/util/logging_installer.h" 10 #include "chrome/installer/util/logging_installer.h"
11 11
12 namespace {
13
14 // Transforms |str_value| into the byte-by-byte representation of its underlying
15 // string, stores the result in |binary_data|.
16 void StringToBinaryData(const std::wstring& str_value,
17 std::vector<uint8>* binary_data) {
18 DCHECK(binary_data);
19 const uint8* data = reinterpret_cast<const uint8*>(str_value.c_str());
20 binary_data->assign(data, data + (str_value.length() + 1) * sizeof(wchar_t));
21 }
22
23 // Transforms |binary_data| into its wstring representation (assuming
24 // |binary_data| is a sequence of wchar_t's).
25 void BinaryDataToString(const std::vector<uint8>& binary_data,
26 std::wstring* str_value) {
27 DCHECK(str_value);
28 if (binary_data.size() < sizeof(wchar_t)) {
29 str_value->clear();
30 return;
31 }
32
33 // The length of the string contained in |binary_data|. This is at least one
34 // per the above condition and may contain a null character if |binary_data|
35 // is null-terminated.
36 const size_t str_len_maybe_with_null = binary_data.size() / sizeof(wchar_t);
37
38 str_value->assign(reinterpret_cast<const wchar_t*>(&binary_data[0]),
39 str_len_maybe_with_null);
40
41 // If the last character in the assigned buffer was a null-character, remove
42 // it (C++ strings are not NULL terminated).
grt (UTC plus 2) 2015/06/30 20:26:42 this comment is a bit misleading: while the termin
gab 2015/07/01 02:55:19 sgtm, done.
43 if ((*str_value)[str_len_maybe_with_null - 1] == L'\0')
gab 2015/06/29 20:11:01 Think this is safe or should I check the resulting
grt (UTC plus 2) 2015/06/30 20:26:42 I think it's safe. str_len_maybe_with_null >= 1, s
gab 2015/07/01 02:55:19 Well I was thinking if somehow this ends up constr
grt (UTC plus 2) 2015/07/01 15:37:16 Ah, I see. You can do away with str_len_maybe_with
gab 2015/07/02 01:59:05 I like that, done.
44 str_value->erase(str_len_maybe_with_null - 1);
45 }
46
47 } // namespace
48
12 SetRegValueWorkItem::~SetRegValueWorkItem() { 49 SetRegValueWorkItem::~SetRegValueWorkItem() {
13 } 50 }
14 51
15 SetRegValueWorkItem::SetRegValueWorkItem(HKEY predefined_root, 52 SetRegValueWorkItem::SetRegValueWorkItem(HKEY predefined_root,
16 const std::wstring& key_path, 53 const std::wstring& key_path,
17 REGSAM wow64_access, 54 REGSAM wow64_access,
18 const std::wstring& value_name, 55 const std::wstring& value_name,
19 const std::wstring& value_data, 56 const std::wstring& value_data,
20 bool overwrite) 57 bool overwrite)
21 : predefined_root_(predefined_root), 58 : predefined_root_(predefined_root),
22 key_path_(key_path), 59 key_path_(key_path),
23 value_name_(value_name), 60 value_name_(value_name),
24 overwrite_(overwrite), 61 overwrite_(overwrite),
25 wow64_access_(wow64_access), 62 wow64_access_(wow64_access),
26 status_(SET_VALUE), 63 status_(SET_VALUE),
27 type_(REG_SZ), 64 type_(REG_SZ),
28 previous_type_(0) { 65 previous_type_(0) {
29 DCHECK(wow64_access == 0 || 66 DCHECK(wow64_access == 0 ||
30 wow64_access == KEY_WOW64_32KEY || 67 wow64_access == KEY_WOW64_32KEY ||
31 wow64_access == KEY_WOW64_64KEY); 68 wow64_access == KEY_WOW64_64KEY);
32 const uint8* data = reinterpret_cast<const uint8*>(value_data.c_str()); 69 StringToBinaryData(value_data, &value_);
33 value_.assign(data, data + (value_data.length() + 1) * sizeof(wchar_t));
34 } 70 }
35 71
36 SetRegValueWorkItem::SetRegValueWorkItem(HKEY predefined_root, 72 SetRegValueWorkItem::SetRegValueWorkItem(HKEY predefined_root,
37 const std::wstring& key_path, 73 const std::wstring& key_path,
38 REGSAM wow64_access, 74 REGSAM wow64_access,
39 const std::wstring& value_name, 75 const std::wstring& value_name,
40 DWORD value_data, 76 DWORD value_data,
41 bool overwrite) 77 bool overwrite)
42 : predefined_root_(predefined_root), 78 : predefined_root_(predefined_root),
43 key_path_(key_path), 79 key_path_(key_path),
(...skipping 24 matching lines...) Expand all
68 status_(SET_VALUE), 104 status_(SET_VALUE),
69 type_(REG_QWORD), 105 type_(REG_QWORD),
70 previous_type_(0) { 106 previous_type_(0) {
71 DCHECK(wow64_access == 0 || 107 DCHECK(wow64_access == 0 ||
72 wow64_access == KEY_WOW64_32KEY || 108 wow64_access == KEY_WOW64_32KEY ||
73 wow64_access == KEY_WOW64_64KEY); 109 wow64_access == KEY_WOW64_64KEY);
74 const uint8* data = reinterpret_cast<const uint8*>(&value_data); 110 const uint8* data = reinterpret_cast<const uint8*>(&value_data);
75 value_.assign(data, data + sizeof(value_data)); 111 value_.assign(data, data + sizeof(value_data));
76 } 112 }
77 113
114 SetRegValueWorkItem::SetRegValueWorkItem(
115 HKEY predefined_root,
116 const std::wstring& key_path,
117 REGSAM wow64_access,
118 const std::wstring& value_name,
119 const GetValueFromExistingCallback& get_value_callback)
120 : predefined_root_(predefined_root),
121 key_path_(key_path),
122 value_name_(value_name),
123 get_value_callback_(get_value_callback),
124 overwrite_(true),
125 wow64_access_(wow64_access),
126 status_(SET_VALUE),
127 type_(REG_SZ),
128 previous_type_(0) {
129 DCHECK(wow64_access == 0 ||
130 wow64_access == KEY_WOW64_32KEY ||
131 wow64_access == KEY_WOW64_64KEY);
132 // Nothing to do, |get_value_callback| will fill |value_| later.
133 }
134
78 bool SetRegValueWorkItem::Do() { 135 bool SetRegValueWorkItem::Do() {
79 LONG result = ERROR_SUCCESS; 136 LONG result = ERROR_SUCCESS;
80 base::win::RegKey key; 137 base::win::RegKey key;
81 if (status_ != SET_VALUE) { 138 if (status_ != SET_VALUE) {
82 // we already did something. 139 // we already did something.
83 VLOG(1) << "multiple calls to Do()"; 140 VLOG(1) << "multiple calls to Do()";
84 result = ERROR_CANTWRITE; 141 result = ERROR_CANTWRITE;
85 return ignore_failure_; 142 return ignore_failure_;
86 } 143 }
87 144
(...skipping 23 matching lines...) Expand all
111 previous_value_.resize(size); 168 previous_value_.resize(size);
112 result = key.ReadValue(value_name_.c_str(), &previous_value_[0], &size, 169 result = key.ReadValue(value_name_.c_str(), &previous_value_[0], &size,
113 &previous_type_); 170 &previous_type_);
114 if (result != ERROR_SUCCESS) { 171 if (result != ERROR_SUCCESS) {
115 previous_value_.clear(); 172 previous_value_.clear();
116 VLOG(1) << "Failed to save original value. Error: " << result; 173 VLOG(1) << "Failed to save original value. Error: " << result;
117 } 174 }
118 } 175 }
119 } 176 }
120 177
178 if (!get_value_callback_.is_null()) {
179 // Although this could be made more generic, for now this assumes the
180 // |type_| of |value_| is REG_SZ.
181 DCHECK(type_ == REG_SZ);
182
183 // Fill |previous_value_str| with the wstring representation of the binary
184 // data in |previous_value_| as long as it's of type REG_SZ (leave it empty
185 // otherwise).
186 std::wstring previous_value_str;
187 if (previous_type_ == REG_SZ)
188 BinaryDataToString(previous_value_, &previous_value_str);
189
190 StringToBinaryData(get_value_callback_.Run(previous_value_str), &value_);
191 }
192
121 result = key.WriteValue(value_name_.c_str(), &value_[0], 193 result = key.WriteValue(value_name_.c_str(), &value_[0],
122 static_cast<DWORD>(value_.size()), type_); 194 static_cast<DWORD>(value_.size()), type_);
123 if (result != ERROR_SUCCESS) { 195 if (result != ERROR_SUCCESS) {
124 VLOG(1) << "Failed to write value " << key_path_ << " error: " << result; 196 VLOG(1) << "Failed to write value " << key_path_ << " error: " << result;
125 return ignore_failure_; 197 return ignore_failure_;
126 } 198 }
127 199
128 status_ = previous_type_ ? VALUE_OVERWRITTEN : NEW_VALUE_CREATED; 200 status_ = previous_type_ ? VALUE_OVERWRITTEN : NEW_VALUE_CREATED;
129 return true; 201 return true;
130 } 202 }
(...skipping 28 matching lines...) Expand all
159 result = key.WriteValue(value_name_.c_str(), previous_value, 231 result = key.WriteValue(value_name_.c_str(), previous_value,
160 static_cast<DWORD>(previous_value_.size()), 232 static_cast<DWORD>(previous_value_.size()),
161 previous_type_); 233 previous_type_);
162 VLOG(1) << "rollback: restoring " << value_name_ << " error: " << result; 234 VLOG(1) << "rollback: restoring " << value_name_ << " error: " << result;
163 } else { 235 } else {
164 NOTREACHED(); 236 NOTREACHED();
165 } 237 }
166 238
167 status_ = VALUE_ROLL_BACK; 239 status_ = VALUE_ROLL_BACK;
168 } 240 }
OLDNEW
« no previous file with comments | « chrome/installer/util/set_reg_value_work_item.h ('k') | chrome/installer/util/set_reg_value_work_item_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698