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
3 // found in the LICENSE file.
5 #include "chrome/installer/util/set_reg_value_work_item.h"
7 #include "base/logging.h"
8 #include "base/strings/string_util.h"
9 #include "base/win/registry.h"
10 #include "chrome/installer/util/logging_installer.h"
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
) {
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));
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
) {
28 if (binary_data
.size() < sizeof(wchar_t)) {
33 str_value
->assign(reinterpret_cast<const wchar_t*>(&binary_data
[0]),
34 binary_data
.size() / sizeof(wchar_t));
36 // Trim off a trailing string terminator, if present.
37 if (!str_value
->empty()) {
38 auto iter
= str_value
->end();
40 str_value
->erase(iter
);
46 SetRegValueWorkItem::~SetRegValueWorkItem() {
49 SetRegValueWorkItem::SetRegValueWorkItem(HKEY predefined_root
,
50 const std::wstring
& key_path
,
52 const std::wstring
& value_name
,
53 const std::wstring
& value_data
,
55 : predefined_root_(predefined_root
),
57 value_name_(value_name
),
58 overwrite_(overwrite
),
59 wow64_access_(wow64_access
),
63 DCHECK(wow64_access
== 0 ||
64 wow64_access
== KEY_WOW64_32KEY
||
65 wow64_access
== KEY_WOW64_64KEY
);
66 StringToBinaryData(value_data
, &value_
);
69 SetRegValueWorkItem::SetRegValueWorkItem(HKEY predefined_root
,
70 const std::wstring
& key_path
,
72 const std::wstring
& value_name
,
75 : predefined_root_(predefined_root
),
77 value_name_(value_name
),
78 overwrite_(overwrite
),
79 wow64_access_(wow64_access
),
83 DCHECK(wow64_access
== 0 ||
84 wow64_access
== KEY_WOW64_32KEY
||
85 wow64_access
== KEY_WOW64_64KEY
);
86 const uint8
* data
= reinterpret_cast<const uint8
*>(&value_data
);
87 value_
.assign(data
, data
+ sizeof(value_data
));
90 SetRegValueWorkItem::SetRegValueWorkItem(HKEY predefined_root
,
91 const std::wstring
& key_path
,
93 const std::wstring
& value_name
,
96 : predefined_root_(predefined_root
),
98 value_name_(value_name
),
99 overwrite_(overwrite
),
100 wow64_access_(wow64_access
),
104 DCHECK(wow64_access
== 0 ||
105 wow64_access
== KEY_WOW64_32KEY
||
106 wow64_access
== KEY_WOW64_64KEY
);
107 const uint8
* data
= reinterpret_cast<const uint8
*>(&value_data
);
108 value_
.assign(data
, data
+ sizeof(value_data
));
111 SetRegValueWorkItem::SetRegValueWorkItem(
112 HKEY predefined_root
,
113 const std::wstring
& key_path
,
115 const std::wstring
& value_name
,
116 const GetValueFromExistingCallback
& get_value_callback
)
117 : predefined_root_(predefined_root
),
119 value_name_(value_name
),
120 get_value_callback_(get_value_callback
),
122 wow64_access_(wow64_access
),
126 DCHECK(wow64_access
== 0 ||
127 wow64_access
== KEY_WOW64_32KEY
||
128 wow64_access
== KEY_WOW64_64KEY
);
129 // Nothing to do, |get_value_callback| will fill |value_| later.
132 bool SetRegValueWorkItem::Do() {
133 LONG result
= ERROR_SUCCESS
;
134 base::win::RegKey key
;
135 if (status_
!= SET_VALUE
) {
136 // we already did something.
137 VLOG(1) << "multiple calls to Do()";
138 result
= ERROR_CANTWRITE
;
139 return ignore_failure_
;
142 status_
= VALUE_UNCHANGED
;
143 result
= key
.Open(predefined_root_
,
145 KEY_READ
| KEY_SET_VALUE
| wow64_access_
);
146 if (result
!= ERROR_SUCCESS
) {
147 VLOG(1) << "can not open " << key_path_
<< " error: " << result
;
148 return ignore_failure_
;
153 result
= key
.ReadValue(value_name_
.c_str(), NULL
, &size
, &type
);
154 // If the value exists but we don't want to overwrite then there's
155 // nothing more to do.
156 if ((result
!= ERROR_FILE_NOT_FOUND
) && !overwrite_
) {
160 // If there's something to be saved, save it.
161 if (result
== ERROR_SUCCESS
) {
163 previous_type_
= type
;
165 previous_value_
.resize(size
);
166 result
= key
.ReadValue(value_name_
.c_str(), &previous_value_
[0], &size
,
168 if (result
!= ERROR_SUCCESS
) {
169 previous_value_
.clear();
170 VLOG(1) << "Failed to save original value. Error: " << result
;
175 if (!get_value_callback_
.is_null()) {
176 // Although this could be made more generic, for now this assumes the
177 // |type_| of |value_| is REG_SZ.
178 DCHECK_EQ(static_cast<DWORD
>(REG_SZ
), type_
);
180 // Fill |previous_value_str| with the wstring representation of the binary
181 // data in |previous_value_| as long as it's of type REG_SZ (leave it empty
183 std::wstring previous_value_str
;
184 if (previous_type_
== REG_SZ
)
185 BinaryDataToString(previous_value_
, &previous_value_str
);
187 StringToBinaryData(get_value_callback_
.Run(previous_value_str
), &value_
);
190 result
= key
.WriteValue(value_name_
.c_str(), &value_
[0],
191 static_cast<DWORD
>(value_
.size()), type_
);
192 if (result
!= ERROR_SUCCESS
) {
193 VLOG(1) << "Failed to write value " << key_path_
<< " error: " << result
;
194 return ignore_failure_
;
197 status_
= previous_type_
? VALUE_OVERWRITTEN
: NEW_VALUE_CREATED
;
201 void SetRegValueWorkItem::Rollback() {
205 if (status_
== SET_VALUE
|| status_
== VALUE_ROLL_BACK
)
208 if (status_
== VALUE_UNCHANGED
) {
209 status_
= VALUE_ROLL_BACK
;
210 VLOG(1) << "rollback: setting unchanged, nothing to do";
214 base::win::RegKey key
;
215 LONG result
= key
.Open(
216 predefined_root_
, key_path_
.c_str(), KEY_SET_VALUE
| wow64_access_
);
217 if (result
!= ERROR_SUCCESS
) {
218 VLOG(1) << "rollback: can not open " << key_path_
<< " error: " << result
;
222 if (status_
== NEW_VALUE_CREATED
) {
223 result
= key
.DeleteValue(value_name_
.c_str());
224 VLOG(1) << "rollback: deleting " << value_name_
<< " error: " << result
;
225 } else if (status_
== VALUE_OVERWRITTEN
) {
226 const unsigned char* previous_value
=
227 previous_value_
.empty() ? NULL
: &previous_value_
[0];
228 result
= key
.WriteValue(value_name_
.c_str(), previous_value
,
229 static_cast<DWORD
>(previous_value_
.size()),
231 VLOG(1) << "rollback: restoring " << value_name_
<< " error: " << result
;
236 status_
= VALUE_ROLL_BACK
;