1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/installer/util/set_reg_value_work_item.h"
#include "base/logging.h"
#include "base/string_util.h"
#include "base/win/registry.h"
#include "chrome/installer/util/logging_installer.h"
SetRegValueWorkItem::~SetRegValueWorkItem() {
}
SetRegValueWorkItem::SetRegValueWorkItem(HKEY predefined_root,
const std::wstring& key_path,
const std::wstring& value_name,
const std::wstring& value_data,
bool overwrite)
: predefined_root_(predefined_root),
key_path_(key_path),
value_name_(value_name),
overwrite_(overwrite),
status_(SET_VALUE),
type_(REG_SZ),
previous_type_(0) {
const uint8* data = reinterpret_cast<const uint8*>(value_data.c_str());
value_.assign(data, data + (value_data.length() + 1) * sizeof(wchar_t));
}
SetRegValueWorkItem::SetRegValueWorkItem(HKEY predefined_root,
const std::wstring& key_path,
const std::wstring& value_name,
DWORD value_data,
bool overwrite)
: predefined_root_(predefined_root),
key_path_(key_path),
value_name_(value_name),
overwrite_(overwrite),
status_(SET_VALUE),
type_(REG_DWORD),
previous_type_(0) {
const uint8* data = reinterpret_cast<const uint8*>(&value_data);
value_.assign(data, data + sizeof(value_data));
}
SetRegValueWorkItem::SetRegValueWorkItem(HKEY predefined_root,
const std::wstring& key_path,
const std::wstring& value_name,
int64 value_data,
bool overwrite)
: predefined_root_(predefined_root),
key_path_(key_path),
value_name_(value_name),
overwrite_(overwrite),
status_(SET_VALUE),
type_(REG_QWORD),
previous_type_(0) {
const uint8* data = reinterpret_cast<const uint8*>(&value_data);
value_.assign(data, data + sizeof(value_data));
}
bool SetRegValueWorkItem::Do() {
LONG result = ERROR_SUCCESS;
base::win::RegKey key;
if (status_ != SET_VALUE) {
// we already did something.
VLOG(1) << "multiple calls to Do()";
result = ERROR_CANTWRITE;
return ignore_failure_;
}
status_ = VALUE_UNCHANGED;
result = key.Open(predefined_root_, key_path_.c_str(),
KEY_READ | KEY_SET_VALUE);
if (result != ERROR_SUCCESS) {
VLOG(1) << "can not open " << key_path_ << " error: " << result;
return ignore_failure_;
}
DWORD type = 0;
DWORD size = 0;
result = key.ReadValue(value_name_.c_str(), NULL, &size, &type);
// If the value exists but we don't want to overwrite then there's
// nothing more to do.
if ((result != ERROR_FILE_NOT_FOUND) && !overwrite_) {
return true;
}
// If there's something to be saved, save it.
if (result == ERROR_SUCCESS) {
if (!size) {
previous_type_ = type;
} else {
previous_value_.resize(size);
result = key.ReadValue(value_name_.c_str(), &previous_value_[0], &size,
&previous_type_);
if (result != ERROR_SUCCESS) {
previous_value_.clear();
VLOG(1) << "Failed to save original value. Error: " << result;
}
}
}
result = key.WriteValue(value_name_.c_str(), &value_[0],
static_cast<DWORD>(value_.size()), type_);
if (result != ERROR_SUCCESS) {
VLOG(1) << "Failed to write value " << key_path_ << " error: " << result;
return ignore_failure_;
}
status_ = previous_type_ ? VALUE_OVERWRITTEN : NEW_VALUE_CREATED;
return true;
}
void SetRegValueWorkItem::Rollback() {
if (ignore_failure_)
return;
if (status_ == SET_VALUE || status_ == VALUE_ROLL_BACK)
return;
if (status_ == VALUE_UNCHANGED) {
status_ = VALUE_ROLL_BACK;
VLOG(1) << "rollback: setting unchanged, nothing to do";
return;
}
base::win::RegKey key;
LONG result = key.Open(predefined_root_, key_path_.c_str(), KEY_SET_VALUE);
if (result != ERROR_SUCCESS) {
VLOG(1) << "rollback: can not open " << key_path_ << " error: " << result;
return;
}
if (status_ == NEW_VALUE_CREATED) {
result = key.DeleteValue(value_name_.c_str());
VLOG(1) << "rollback: deleting " << value_name_ << " error: " << result;
} else if (status_ == VALUE_OVERWRITTEN) {
const unsigned char* previous_value =
previous_value_.empty() ? NULL : &previous_value_[0];
result = key.WriteValue(value_name_.c_str(), previous_value,
static_cast<DWORD>(previous_value_.size()),
previous_type_);
VLOG(1) << "rollback: restoring " << value_name_ << " error: " << result;
} else {
NOTREACHED();
}
status_ = VALUE_ROLL_BACK;
}
|