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/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 void StringToBinaryData(const std::wstring& str_value, | |
grt (UTC plus 2)
2015/06/29 15:37:48
nit: add a comment
gab
2015/06/29 20:11:01
Done.
| |
15 std::vector<uint8>* binary_data) { | |
16 const uint8* data = reinterpret_cast<const uint8*>(str_value.c_str()); | |
17 binary_data->assign(data, data + (str_value.length() + 1) * sizeof(wchar_t)); | |
18 } | |
19 | |
20 } // namespace | |
21 | |
12 SetRegValueWorkItem::~SetRegValueWorkItem() { | 22 SetRegValueWorkItem::~SetRegValueWorkItem() { |
13 } | 23 } |
14 | 24 |
15 SetRegValueWorkItem::SetRegValueWorkItem(HKEY predefined_root, | 25 SetRegValueWorkItem::SetRegValueWorkItem(HKEY predefined_root, |
16 const std::wstring& key_path, | 26 const std::wstring& key_path, |
17 REGSAM wow64_access, | 27 REGSAM wow64_access, |
18 const std::wstring& value_name, | 28 const std::wstring& value_name, |
19 const std::wstring& value_data, | 29 const std::wstring& value_data, |
20 bool overwrite) | 30 bool overwrite) |
21 : predefined_root_(predefined_root), | 31 : predefined_root_(predefined_root), |
22 key_path_(key_path), | 32 key_path_(key_path), |
23 value_name_(value_name), | 33 value_name_(value_name), |
24 overwrite_(overwrite), | 34 overwrite_(overwrite), |
25 wow64_access_(wow64_access), | 35 wow64_access_(wow64_access), |
26 status_(SET_VALUE), | 36 status_(SET_VALUE), |
27 type_(REG_SZ), | 37 type_(REG_SZ), |
28 previous_type_(0) { | 38 previous_type_(0) { |
29 DCHECK(wow64_access == 0 || | 39 DCHECK(wow64_access == 0 || |
30 wow64_access == KEY_WOW64_32KEY || | 40 wow64_access == KEY_WOW64_32KEY || |
31 wow64_access == KEY_WOW64_64KEY); | 41 wow64_access == KEY_WOW64_64KEY); |
32 const uint8* data = reinterpret_cast<const uint8*>(value_data.c_str()); | 42 StringToBinaryData(value_data, &value_); |
33 value_.assign(data, data + (value_data.length() + 1) * sizeof(wchar_t)); | |
34 } | 43 } |
35 | 44 |
36 SetRegValueWorkItem::SetRegValueWorkItem(HKEY predefined_root, | 45 SetRegValueWorkItem::SetRegValueWorkItem(HKEY predefined_root, |
37 const std::wstring& key_path, | 46 const std::wstring& key_path, |
38 REGSAM wow64_access, | 47 REGSAM wow64_access, |
39 const std::wstring& value_name, | 48 const std::wstring& value_name, |
40 DWORD value_data, | 49 DWORD value_data, |
41 bool overwrite) | 50 bool overwrite) |
42 : predefined_root_(predefined_root), | 51 : predefined_root_(predefined_root), |
43 key_path_(key_path), | 52 key_path_(key_path), |
(...skipping 24 matching lines...) Expand all Loading... | |
68 status_(SET_VALUE), | 77 status_(SET_VALUE), |
69 type_(REG_QWORD), | 78 type_(REG_QWORD), |
70 previous_type_(0) { | 79 previous_type_(0) { |
71 DCHECK(wow64_access == 0 || | 80 DCHECK(wow64_access == 0 || |
72 wow64_access == KEY_WOW64_32KEY || | 81 wow64_access == KEY_WOW64_32KEY || |
73 wow64_access == KEY_WOW64_64KEY); | 82 wow64_access == KEY_WOW64_64KEY); |
74 const uint8* data = reinterpret_cast<const uint8*>(&value_data); | 83 const uint8* data = reinterpret_cast<const uint8*>(&value_data); |
75 value_.assign(data, data + sizeof(value_data)); | 84 value_.assign(data, data + sizeof(value_data)); |
76 } | 85 } |
77 | 86 |
87 SetRegValueWorkItem::SetRegValueWorkItem( | |
88 HKEY predefined_root, | |
89 const std::wstring& key_path, | |
90 REGSAM wow64_access, | |
91 const std::wstring& value_name, | |
92 const GetValueFromExistingCallback& get_value_callback) | |
93 : predefined_root_(predefined_root), | |
94 key_path_(key_path), | |
95 value_name_(value_name), | |
96 get_value_callback_(get_value_callback), | |
97 overwrite_(true), | |
98 wow64_access_(wow64_access), | |
99 status_(SET_VALUE), | |
100 type_(REG_SZ), | |
101 previous_type_(0) { | |
102 DCHECK(wow64_access == 0 || | |
103 wow64_access == KEY_WOW64_32KEY || | |
104 wow64_access == KEY_WOW64_64KEY); | |
105 // Nothing to do, |get_value_callback| will fill |value_| later. | |
106 } | |
107 | |
78 bool SetRegValueWorkItem::Do() { | 108 bool SetRegValueWorkItem::Do() { |
79 LONG result = ERROR_SUCCESS; | 109 LONG result = ERROR_SUCCESS; |
80 base::win::RegKey key; | 110 base::win::RegKey key; |
81 if (status_ != SET_VALUE) { | 111 if (status_ != SET_VALUE) { |
82 // we already did something. | 112 // we already did something. |
83 VLOG(1) << "multiple calls to Do()"; | 113 VLOG(1) << "multiple calls to Do()"; |
84 result = ERROR_CANTWRITE; | 114 result = ERROR_CANTWRITE; |
85 return ignore_failure_; | 115 return ignore_failure_; |
86 } | 116 } |
87 | 117 |
(...skipping 23 matching lines...) Expand all Loading... | |
111 previous_value_.resize(size); | 141 previous_value_.resize(size); |
112 result = key.ReadValue(value_name_.c_str(), &previous_value_[0], &size, | 142 result = key.ReadValue(value_name_.c_str(), &previous_value_[0], &size, |
113 &previous_type_); | 143 &previous_type_); |
114 if (result != ERROR_SUCCESS) { | 144 if (result != ERROR_SUCCESS) { |
115 previous_value_.clear(); | 145 previous_value_.clear(); |
116 VLOG(1) << "Failed to save original value. Error: " << result; | 146 VLOG(1) << "Failed to save original value. Error: " << result; |
117 } | 147 } |
118 } | 148 } |
119 } | 149 } |
120 | 150 |
151 if (!get_value_callback_.is_null()) { | |
152 // Although this could be made more generic, for now this assumes the | |
153 // |type_| of |value_| is REG_SZ. | |
154 DCHECK(type_ == REG_SZ); | |
grt (UTC plus 2)
2015/06/29 15:37:48
DCHECK_EQ(REG_SZ, type_);
gab
2015/06/29 20:11:01
I tried that put it would require a cast or:
d:\s
grt (UTC plus 2)
2015/06/30 20:26:42
I think the cast is worth it so that failures cont
gab
2015/07/01 02:55:19
Done.
| |
155 | |
156 const std::wstring previous_value_str( | |
157 (previous_type_ == REG_SZ && previous_value_.size() >= sizeof(wchar_t)) | |
158 ? reinterpret_cast<wchar_t*>(&previous_value_[0]) | |
grt (UTC plus 2)
2015/06/29 15:37:48
there is no guarantee that previous_value_ is null
gab
2015/06/29 20:11:01
Indeed, great great point sir!
grt (UTC plus 2)
2015/06/30 20:26:42
If the callback is meant to take the true registry
gab
2015/07/01 02:55:19
Acknowledged.
gab
2015/07/06 20:56:02
Actually registry.cc's ReadValue() [1] does assume
grt (UTC plus 2)
2015/07/10 19:10:52
That's a bug. The one in mini_installer.cc does so
| |
159 : L""); | |
160 StringToBinaryData(get_value_callback_.Run(previous_value_str), &value_); | |
161 } | |
162 | |
121 result = key.WriteValue(value_name_.c_str(), &value_[0], | 163 result = key.WriteValue(value_name_.c_str(), &value_[0], |
122 static_cast<DWORD>(value_.size()), type_); | 164 static_cast<DWORD>(value_.size()), type_); |
123 if (result != ERROR_SUCCESS) { | 165 if (result != ERROR_SUCCESS) { |
124 VLOG(1) << "Failed to write value " << key_path_ << " error: " << result; | 166 VLOG(1) << "Failed to write value " << key_path_ << " error: " << result; |
125 return ignore_failure_; | 167 return ignore_failure_; |
126 } | 168 } |
127 | 169 |
128 status_ = previous_type_ ? VALUE_OVERWRITTEN : NEW_VALUE_CREATED; | 170 status_ = previous_type_ ? VALUE_OVERWRITTEN : NEW_VALUE_CREATED; |
129 return true; | 171 return true; |
130 } | 172 } |
(...skipping 28 matching lines...) Expand all Loading... | |
159 result = key.WriteValue(value_name_.c_str(), previous_value, | 201 result = key.WriteValue(value_name_.c_str(), previous_value, |
160 static_cast<DWORD>(previous_value_.size()), | 202 static_cast<DWORD>(previous_value_.size()), |
161 previous_type_); | 203 previous_type_); |
162 VLOG(1) << "rollback: restoring " << value_name_ << " error: " << result; | 204 VLOG(1) << "rollback: restoring " << value_name_ << " error: " << result; |
163 } else { | 205 } else { |
164 NOTREACHED(); | 206 NOTREACHED(); |
165 } | 207 } |
166 | 208 |
167 status_ = VALUE_ROLL_BACK; | 209 status_ = VALUE_ROLL_BACK; |
168 } | 210 } |
OLD | NEW |