Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / installer / util / set_reg_value_work_item.cc
bloba78873844237ec3b687a43f169ef8cb6a1194fc1
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"
12 namespace {
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));
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;
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();
39 if (*--iter == L'\0')
40 str_value->erase(iter);
44 } // namespace
46 SetRegValueWorkItem::~SetRegValueWorkItem() {
49 SetRegValueWorkItem::SetRegValueWorkItem(HKEY predefined_root,
50 const std::wstring& key_path,
51 REGSAM wow64_access,
52 const std::wstring& value_name,
53 const std::wstring& value_data,
54 bool overwrite)
55 : predefined_root_(predefined_root),
56 key_path_(key_path),
57 value_name_(value_name),
58 overwrite_(overwrite),
59 wow64_access_(wow64_access),
60 type_(REG_SZ),
61 previous_type_(0),
62 status_(SET_VALUE) {
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,
71 REGSAM wow64_access,
72 const std::wstring& value_name,
73 DWORD value_data,
74 bool overwrite)
75 : predefined_root_(predefined_root),
76 key_path_(key_path),
77 value_name_(value_name),
78 overwrite_(overwrite),
79 wow64_access_(wow64_access),
80 type_(REG_DWORD),
81 previous_type_(0),
82 status_(SET_VALUE) {
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,
92 REGSAM wow64_access,
93 const std::wstring& value_name,
94 int64 value_data,
95 bool overwrite)
96 : predefined_root_(predefined_root),
97 key_path_(key_path),
98 value_name_(value_name),
99 overwrite_(overwrite),
100 wow64_access_(wow64_access),
101 type_(REG_QWORD),
102 previous_type_(0),
103 status_(SET_VALUE) {
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,
114 REGSAM wow64_access,
115 const std::wstring& value_name,
116 const GetValueFromExistingCallback& get_value_callback)
117 : predefined_root_(predefined_root),
118 key_path_(key_path),
119 value_name_(value_name),
120 get_value_callback_(get_value_callback),
121 overwrite_(true),
122 wow64_access_(wow64_access),
123 type_(REG_SZ),
124 previous_type_(0),
125 status_(SET_VALUE) {
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_,
144 key_path_.c_str(),
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_;
151 DWORD type = 0;
152 DWORD size = 0;
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_) {
157 return true;
160 // If there's something to be saved, save it.
161 if (result == ERROR_SUCCESS) {
162 if (!size) {
163 previous_type_ = type;
164 } else {
165 previous_value_.resize(size);
166 result = key.ReadValue(value_name_.c_str(), &previous_value_[0], &size,
167 &previous_type_);
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
182 // otherwise).
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;
198 return true;
201 void SetRegValueWorkItem::Rollback() {
202 if (ignore_failure_)
203 return;
205 if (status_ == SET_VALUE || status_ == VALUE_ROLL_BACK)
206 return;
208 if (status_ == VALUE_UNCHANGED) {
209 status_ = VALUE_ROLL_BACK;
210 VLOG(1) << "rollback: setting unchanged, nothing to do";
211 return;
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;
219 return;
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()),
230 previous_type_);
231 VLOG(1) << "rollback: restoring " << value_name_ << " error: " << result;
232 } else {
233 NOTREACHED();
236 status_ = VALUE_ROLL_BACK;