Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(119)

Side by Side Diff: chrome_elf/chrome_elf_util.cc

Issue 1841573002: [Chrome ELF] New NT registry API. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Clean up OverrideRegistry function. Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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_elf/chrome_elf_util.h" 5 #include "chrome_elf/chrome_elf_util.h"
6 6
7 #include <assert.h> 7 #include <assert.h>
8 #include <stddef.h>
8 #include <windows.h> 9 #include <windows.h>
9 #include <stddef.h>
10 10
11 #include "base/macros.h" 11 #include "base/macros.h"
12 #include "base/strings/string16.h" 12 #include "base/strings/string16.h"
13 #include "chrome_elf/chrome_elf_constants.h"
14 #include "chrome_elf/chrome_elf_reg.h"
13 15
14 ProcessType g_process_type = ProcessType::UNINITIALIZED; 16 ProcessType g_process_type = ProcessType::UNINITIALIZED;
15 17
16 namespace { 18 namespace {
17 19
18 const wchar_t kRegPathClientState[] = L"Software\\Google\\Update\\ClientState"; 20 const wchar_t kRegPathClientState[] = L"Software\\Google\\Update\\ClientState";
19 const wchar_t kRegPathClientStateMedium[] = 21 const wchar_t kRegPathClientStateMedium[] =
20 L"Software\\Google\\Update\\ClientStateMedium"; 22 L"Software\\Google\\Update\\ClientStateMedium";
21 #if defined(GOOGLE_CHROME_BUILD) 23 #if defined(GOOGLE_CHROME_BUILD)
22 const wchar_t kRegPathChromePolicy[] = L"SOFTWARE\\Policies\\Google\\Chrome"; 24 const wchar_t kRegPathChromePolicy[] = L"SOFTWARE\\Policies\\Google\\Chrome";
23 #else 25 #else
24 const wchar_t kRegPathChromePolicy[] = L"SOFTWARE\\Policies\\Chromium"; 26 const wchar_t kRegPathChromePolicy[] = L"SOFTWARE\\Policies\\Chromium";
25 #endif // defined(GOOGLE_CHROME_BUILD) 27 #endif // defined(GOOGLE_CHROME_BUILD)
26 28
27 const wchar_t kRegValueUsageStats[] = L"usagestats"; 29 const wchar_t kRegValueUsageStats[] = L"usagestats";
28 const wchar_t kUninstallArgumentsField[] = L"UninstallArguments"; 30 const wchar_t kUninstallArgumentsField[] = L"UninstallArguments";
29 const wchar_t kMetricsReportingEnabled[] =L"MetricsReportingEnabled"; 31 const wchar_t kMetricsReportingEnabled[] = L"MetricsReportingEnabled";
30 32
31 const wchar_t kAppGuidCanary[] = 33 const wchar_t kAppGuidCanary[] =
32 L"{4ea16ac7-fd5a-47c3-875b-dbf4a2008c20}"; 34 L"{4ea16ac7-fd5a-47c3-875b-dbf4a2008c20}";
33 const wchar_t kAppGuidGoogleChrome[] = 35 const wchar_t kAppGuidGoogleChrome[] =
34 L"{8A69D345-D564-463c-AFF1-A69D9E530F96}"; 36 L"{8A69D345-D564-463c-AFF1-A69D9E530F96}";
35 const wchar_t kAppGuidGoogleBinaries[] = 37 const wchar_t kAppGuidGoogleBinaries[] =
36 L"{4DC8B4CA-1BDA-483e-B5FA-D3C12E15B62D}"; 38 L"{4DC8B4CA-1BDA-483e-B5FA-D3C12E15B62D}";
37 39
38 bool ReadKeyValueString(bool system_install, const wchar_t* key_path,
39 const wchar_t* guid, const wchar_t* value_to_read,
40 base::string16* value_out) {
41 HKEY key = NULL;
42 value_out->clear();
43
44 base::string16 full_key_path(key_path);
45 full_key_path.append(1, L'\\');
46 full_key_path.append(guid);
47
48 if (::RegOpenKeyEx(system_install ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER,
49 full_key_path.c_str(), 0,
50 KEY_QUERY_VALUE | KEY_WOW64_32KEY, &key) !=
51 ERROR_SUCCESS) {
52 return false;
53 }
54
55 const size_t kMaxStringLength = 1024;
56 wchar_t raw_value[kMaxStringLength] = {};
57 DWORD size = sizeof(raw_value);
58 DWORD type = REG_SZ;
59 LONG result = ::RegQueryValueEx(key, value_to_read, 0, &type,
60 reinterpret_cast<LPBYTE>(raw_value), &size);
61
62 if (result == ERROR_SUCCESS) {
63 if (type != REG_SZ || (size & 1) != 0) {
64 result = ERROR_NOT_SUPPORTED;
65 } else if (size == 0) {
66 *raw_value = L'\0';
67 } else if (raw_value[size / sizeof(wchar_t) - 1] != L'\0') {
68 if ((size / sizeof(wchar_t)) < kMaxStringLength)
69 raw_value[size / sizeof(wchar_t)] = L'\0';
70 else
71 result = ERROR_MORE_DATA;
72 }
73 }
74
75 if (result == ERROR_SUCCESS)
76 *value_out = raw_value;
77
78 ::RegCloseKey(key);
79
80 return result == ERROR_SUCCESS;
81 }
82
83 bool ReadKeyValueDW(bool system_install, const wchar_t* key_path,
84 base::string16 guid, const wchar_t* value_to_read,
85 DWORD* value_out) {
86 HKEY key = NULL;
87 *value_out = 0;
88
89 base::string16 full_key_path(key_path);
90 full_key_path.append(1, L'\\');
91 full_key_path.append(guid);
92
93 if (::RegOpenKeyEx(system_install ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER,
94 full_key_path.c_str(), 0,
95 KEY_QUERY_VALUE | KEY_WOW64_32KEY, &key) !=
96 ERROR_SUCCESS) {
97 return false;
98 }
99
100 DWORD size = sizeof(*value_out);
101 DWORD type = REG_DWORD;
102 LONG result = ::RegQueryValueEx(key, value_to_read, 0, &type,
103 reinterpret_cast<BYTE*>(value_out), &size);
104
105 ::RegCloseKey(key);
106
107 return result == ERROR_SUCCESS && size == sizeof(*value_out);
108 }
109
110 } // namespace 40 } // namespace
111 41
112 bool IsCanary(const wchar_t* exe_path) { 42 bool IsCanary(const wchar_t* exe_path) {
113 return wcsstr(exe_path, L"Chrome SxS\\Application") != NULL; 43 return ::wcsstr(exe_path, L"Chrome SxS\\Application") != NULL;
114 } 44 }
115 45
116 bool IsSystemInstall(const wchar_t* exe_path) { 46 bool IsSystemInstall(const wchar_t* exe_path) {
117 wchar_t program_dir[MAX_PATH] = {}; 47 wchar_t program_dir[MAX_PATH] = {};
118 DWORD ret = ::GetEnvironmentVariable(L"PROGRAMFILES", program_dir, 48 DWORD ret = ::GetEnvironmentVariable(L"PROGRAMFILES", program_dir,
119 arraysize(program_dir)); 49 arraysize(program_dir));
120 if (ret && ret < MAX_PATH && !wcsncmp(exe_path, program_dir, ret)) 50 if (ret && ret < MAX_PATH && !::wcsncmp(exe_path, program_dir, ret))
121 return true; 51 return true;
122 52
123 ret = ::GetEnvironmentVariable(L"PROGRAMFILES(X86)", program_dir, 53 ret = ::GetEnvironmentVariable(L"PROGRAMFILES(X86)", program_dir,
124 arraysize(program_dir)); 54 arraysize(program_dir));
125 if (ret && ret < MAX_PATH && !wcsncmp(exe_path, program_dir, ret)) 55 if (ret && ret < MAX_PATH && !::wcsncmp(exe_path, program_dir, ret))
126 return true; 56 return true;
127 57
128 return false; 58 return false;
129 } 59 }
130 60
131 bool IsMultiInstall(bool is_system_install) { 61 bool IsMultiInstall(bool is_system_install) {
132 base::string16 args; 62 base::string16 args;
133 if (!ReadKeyValueString(is_system_install, kRegPathClientState, 63
134 kAppGuidGoogleChrome, kUninstallArgumentsField, 64 base::string16 full_key_path(kRegPathClientState);
135 &args)) { 65 full_key_path.append(1, L'\\');
66 full_key_path.append(kAppGuidGoogleChrome);
67 if (!nt::GetRegValue_SZ((is_system_install ? nt::HKLM : nt::HKCU),
68 full_key_path.c_str(), kUninstallArgumentsField,
69 &args))
136 return false; 70 return false;
137 } 71
138 return args.find(L"--multi-install") != base::string16::npos; 72 return (args.find(L"--multi-install") != base::string16::npos);
139 } 73 }
140 74
141 bool AreUsageStatsEnabled(const wchar_t* exe_path) { 75 bool AreUsageStatsEnabled(const wchar_t* exe_path) {
142 bool enabled = true; 76 bool enabled = true;
143 bool controlled_by_policy = ReportingIsEnforcedByPolicy(&enabled); 77 bool controlled_by_policy = ReportingIsEnforcedByPolicy(&enabled);
144 78
145 if (controlled_by_policy && !enabled) 79 if (controlled_by_policy && !enabled)
146 return false; 80 return false;
147 81
148 bool system_install = IsSystemInstall(exe_path); 82 bool system_install = IsSystemInstall(exe_path);
149 base::string16 app_guid; 83 base::string16 app_guid;
150 84
151 if (IsCanary(exe_path)) { 85 if (IsCanary(exe_path)) {
152 app_guid = kAppGuidCanary; 86 app_guid = kAppGuidCanary;
153 } else { 87 } else {
154 app_guid = IsMultiInstall(system_install) ? kAppGuidGoogleBinaries : 88 app_guid = IsMultiInstall(system_install) ? kAppGuidGoogleBinaries :
155 kAppGuidGoogleChrome; 89 kAppGuidGoogleChrome;
156 } 90 }
157 91
158 DWORD out_value = 0; 92 DWORD out_value = 0;
93
94 // If system_install, first try kRegPathClientStateMedium.
95 base::string16 full_key_path(kRegPathClientStateMedium);
96 full_key_path.append(1, L'\\');
97 full_key_path.append(app_guid);
159 if (system_install && 98 if (system_install &&
160 ReadKeyValueDW(system_install, kRegPathClientStateMedium, app_guid, 99 nt::GetRegValue_DWORD(nt::HKLM, full_key_path.c_str(),
161 kRegValueUsageStats, &out_value)) { 100 kRegValueUsageStats, &out_value)) {
162 return out_value == 1; 101 return (out_value == 1);
163 } 102 }
164 103
165 return ReadKeyValueDW(system_install, kRegPathClientState, app_guid, 104 // Second, try kRegPathClientState.
166 kRegValueUsageStats, &out_value) && out_value == 1; 105 full_key_path = kRegPathClientState;
106 full_key_path.append(1, L'\\');
107 full_key_path.append(app_guid);
108 return (nt::GetRegValue_DWORD((system_install ? nt::HKLM : nt::HKCU),
109 full_key_path.c_str(), kRegValueUsageStats,
110 &out_value) &&
111 out_value == 1);
167 } 112 }
168 113
169 bool ReportingIsEnforcedByPolicy(bool* breakpad_enabled) { 114 bool ReportingIsEnforcedByPolicy(bool* breakpad_enabled) {
170 HKEY key = NULL;
171 DWORD value = 0; 115 DWORD value = 0;
172 BYTE* value_bytes = reinterpret_cast<BYTE*>(&value);
173 DWORD size = sizeof(value);
174 DWORD type = REG_DWORD;
175 116
176 if (::RegOpenKeyEx(HKEY_LOCAL_MACHINE, kRegPathChromePolicy, 0, 117 // First, try HKLM.
177 KEY_QUERY_VALUE, &key) == ERROR_SUCCESS) { 118 if (nt::GetRegValue_DWORD(nt::HKLM, kRegPathChromePolicy,
178 if (::RegQueryValueEx(key, kMetricsReportingEnabled, 0, &type, 119 kMetricsReportingEnabled, &value)) {
179 value_bytes, &size) == ERROR_SUCCESS) { 120 *breakpad_enabled = (value != 0);
180 *breakpad_enabled = value != 0; 121 return true;
181 }
182 ::RegCloseKey(key);
183 return size == sizeof(value);
184 } 122 }
185 123
186 if (::RegOpenKeyEx(HKEY_CURRENT_USER, kRegPathChromePolicy, 0, 124 // Second, try HKCU.
187 KEY_QUERY_VALUE, &key) == ERROR_SUCCESS) { 125 if (nt::GetRegValue_DWORD(nt::HKCU, kRegPathChromePolicy,
188 if (::RegQueryValueEx(key, kMetricsReportingEnabled, 0, &type, 126 kMetricsReportingEnabled, &value)) {
189 value_bytes, &size) == ERROR_SUCCESS) { 127 *breakpad_enabled = (value != 0);
190 *breakpad_enabled = value != 0; 128 return true;
191 }
192 ::RegCloseKey(key);
193 return size == sizeof(value);
194 } 129 }
195 130
196 return false; 131 return false;
197 } 132 }
198 133
199 void InitializeProcessType() { 134 void InitializeProcessType() {
200 assert(g_process_type == ProcessType::UNINITIALIZED); 135 assert(g_process_type == ProcessType::UNINITIALIZED);
201 typedef bool (*IsSandboxedProcessFunc)(); 136 typedef bool (*IsSandboxedProcessFunc)();
202 IsSandboxedProcessFunc is_sandboxed_process_func = 137 IsSandboxedProcessFunc is_sandboxed_process_func =
203 reinterpret_cast<IsSandboxedProcessFunc>( 138 reinterpret_cast<IsSandboxedProcessFunc>(
(...skipping 11 matching lines...) Expand all
215 return; 150 return;
216 } 151 }
217 152
218 g_process_type = ProcessType::BROWSER_PROCESS; 153 g_process_type = ProcessType::BROWSER_PROCESS;
219 } 154 }
220 155
221 bool IsNonBrowserProcess() { 156 bool IsNonBrowserProcess() {
222 assert(g_process_type != ProcessType::UNINITIALIZED); 157 assert(g_process_type != ProcessType::UNINITIALIZED);
223 return g_process_type == ProcessType::NON_BROWSER_PROCESS; 158 return g_process_type == ProcessType::NON_BROWSER_PROCESS;
224 } 159 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698