forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp_command.cc
147 lines (118 loc) · 4.9 KB
/
app_command.cc
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
// Copyright 2011 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif
#include "chrome/installer/util/app_command.h"
#include <windows.h>
#include <stddef.h>
#include "base/check.h"
#include "base/logging.h"
#include "base/strings/strcat.h"
#include "base/win/registry.h"
#include "chrome/install_static/install_util.h"
#include "chrome/installer/util/google_update_constants.h"
#include "chrome/installer/util/work_item_list.h"
namespace installer {
namespace {
// Returns the "`GetClientsKeyPath()`\\Commands\\`name`" registry key path used
// to register AppCommands with the updater.
std::wstring GetCommandKey(const std::wstring& name) {
return base::StrCat({install_static::GetClientsKeyPath(), L"\\",
google_update::kRegCommandsKey, L"\\", name});
}
} // namespace
// static
// Associate bool member variables with registry entries.
const AppCommand::NamedBoolVar AppCommand::kNameBoolVars[] = {
{&AppCommand::sends_pings_, google_update::kRegSendsPingsField},
{&AppCommand::is_web_accessible_, google_update::kRegWebAccessibleField},
{&AppCommand::is_auto_run_on_os_upgrade_,
google_update::kRegAutoRunOnOSUpgradeField},
{&AppCommand::is_run_as_user_, google_update::kRegRunAsUserField},
};
AppCommand::AppCommand()
: sends_pings_(false),
is_web_accessible_(false),
is_auto_run_on_os_upgrade_(false),
is_run_as_user_(false) {}
AppCommand::AppCommand(const std::wstring& command_name,
const std::wstring& command_line)
: command_name_(command_name),
command_line_(command_line),
sends_pings_(false),
is_web_accessible_(false),
is_auto_run_on_os_upgrade_(false),
is_run_as_user_(false) {}
AppCommand::AppCommand(AppCommand&&) = default;
AppCommand::AppCommand(const AppCommand&) = default;
AppCommand::~AppCommand() = default;
bool AppCommand::Initialize(HKEY root_key) {
DCHECK(!command_name_.empty());
base::win::RegKey key;
auto result = key.Open(root_key, GetCommandKey(command_name_).c_str(),
KEY_QUERY_VALUE | KEY_WOW64_32KEY);
if (result != ERROR_SUCCESS) {
::SetLastError(result);
PLOG(DFATAL) << "Error opening AppCommand: " << command_name_;
return false;
}
return Initialize(key);
}
bool AppCommand::Initialize(const base::win::RegKey& key) {
if (!key.Valid()) {
LOG(DFATAL) << "Cannot initialize an AppCommand from an invalid key.";
return false;
}
LONG result = ERROR_SUCCESS;
std::wstring cmd_line;
result = key.ReadValue(google_update::kRegCommandLineField, &cmd_line);
if (result != ERROR_SUCCESS) {
LOG(WARNING) << "Error reading " << google_update::kRegCommandLineField
<< " value from registry: " << result;
return false;
}
command_line_.swap(cmd_line);
for (size_t i = 0; i < std::size(kNameBoolVars); ++i) {
DWORD value = 0; // Set default to false.
// Note: ReadValueDW only modifies out param on success.
key.ReadValueDW(kNameBoolVars[i].name, &value);
this->*(kNameBoolVars[i].data) = (value != 0);
}
return true;
}
void AppCommand::AddCreateAppCommandWorkItems(const HKEY root_key,
WorkItemList* item_list) const {
DCHECK(!command_name_.empty());
DCHECK(!command_line_.empty());
const std::wstring command_path = GetCommandKey(command_name_);
item_list->AddCreateRegKeyWorkItem(root_key, command_path, KEY_WOW64_32KEY)
->set_log_message("creating AppCommand registry key");
item_list
->AddSetRegValueWorkItem(root_key, command_path, KEY_WOW64_32KEY,
google_update::kRegCommandLineField,
command_line_, true)
->set_log_message("setting AppCommand CommandLine registry value");
for (size_t i = 0; i < std::size(kNameBoolVars); ++i) {
const wchar_t* var_name = kNameBoolVars[i].name;
bool var_data = this->*(kNameBoolVars[i].data);
// Adds a work item to set |var_name| to DWORD 1 if |var_data| is true;
// adds a work item to remove |var_name| otherwise.
if (var_data) {
item_list->AddSetRegValueWorkItem(root_key, command_path, KEY_WOW64_32KEY,
var_name, static_cast<DWORD>(1), true);
} else {
item_list->AddDeleteRegValueWorkItem(root_key, command_path,
KEY_WOW64_32KEY, var_name);
}
}
}
void AppCommand::AddDeleteAppCommandWorkItems(const HKEY root_key,
WorkItemList* item_list) const {
DCHECK(!command_name_.empty());
item_list->AddDeleteRegKeyWorkItem(root_key, GetCommandKey(command_name_),
KEY_WOW64_32KEY);
}
} // namespace installer