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

Side by Side Diff: chrome/install_static/install_util.cc

Issue 1913943003: Remove dependencies on chrome\installer from the ChromeCrashReporterClient class on Windows. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Update comment Created 4 years, 7 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.
grt (UTC plus 2) 2016/04/29 20:50:56 please add an OWNERS file to this dir with at leas
ananta 2016/04/29 22:49:24 Done. Added robertshield too. This file went in la
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/install_static/install_util.h" 5 #include "chrome/install_static/install_util.h"
6 6
7 #include <assert.h> 7 #include <assert.h>
8 #include <memory>
8 #include <windows.h> 9 #include <windows.h>
grt (UTC plus 2) 2016/04/29 20:50:56 https://google.github.io/styleguide/cppguide.html#
ananta 2016/04/29 22:49:24 Done.
9 #include <stddef.h>
10 10
11 #include "base/macros.h" 11 #include "base/macros.h"
grt (UTC plus 2) 2016/04/29 20:50:56 would a DEPS file in this directory help prevent a
ananta 2016/04/29 22:49:23 Thanks. Good point. Done
12 #include "base/strings/string16.h" 12 #include "build/build_config.h"
13
14 namespace install_static {
13 15
14 ProcessType g_process_type = ProcessType::UNINITIALIZED; 16 ProcessType g_process_type = ProcessType::UNINITIALIZED;
15 17
18 // Chrome channel display names.
19 // TODO(ananta)
20 // http://crbug.com/604923
21 // These constants are defined in the chrome/installer directory as well. We
22 // need to unify them.
23 const wchar_t kChromeChannelUnknown[] = L"unknown";
24 const wchar_t kChromeChannelCanary[] = L"canary";
25 const wchar_t kChromeChannelDev[] = L"dev";
26 const wchar_t kChromeChannelBeta[] = L"beta";
27 const wchar_t kChromeChannelStable[] = L"";
28 const wchar_t kChromeChannelStableExplicit[] = L"stable";
29
16 namespace { 30 namespace {
17 31
18 const wchar_t kRegPathClientState[] = L"Software\\Google\\Update\\ClientState"; 32 const wchar_t kRegPathClientState[] = L"Software\\Google\\Update\\ClientState";
grt (UTC plus 2) 2016/04/29 20:50:56 this is only for GOOGLE_CHROME_BUILD. Chromium bui
ananta 2016/04/29 22:49:23 Done.
ananta 2016/04/29 22:49:24 Done.
19 const wchar_t kRegPathClientStateMedium[] = 33 const wchar_t kRegPathClientStateMedium[] =
20 L"Software\\Google\\Update\\ClientStateMedium"; 34 L"Software\\Google\\Update\\ClientStateMedium";
21 #if defined(GOOGLE_CHROME_BUILD) 35 #if defined(GOOGLE_CHROME_BUILD)
22 const wchar_t kRegPathChromePolicy[] = L"SOFTWARE\\Policies\\Google\\Chrome"; 36 const wchar_t kRegPathChromePolicy[] = L"SOFTWARE\\Policies\\Google\\Chrome";
23 #else 37 #else
24 const wchar_t kRegPathChromePolicy[] = L"SOFTWARE\\Policies\\Chromium"; 38 const wchar_t kRegPathChromePolicy[] = L"SOFTWARE\\Policies\\Chromium";
25 #endif // defined(GOOGLE_CHROME_BUILD) 39 #endif // defined(GOOGLE_CHROME_BUILD)
26 40
27 const wchar_t kRegValueUsageStats[] = L"usagestats"; 41 const wchar_t kRegValueUsageStats[] = L"usagestats";
28 const wchar_t kUninstallArgumentsField[] = L"UninstallArguments"; 42 const wchar_t kUninstallArgumentsField[] = L"UninstallArguments";
29 const wchar_t kMetricsReportingEnabled[] = L"MetricsReportingEnabled"; 43 const wchar_t kMetricsReportingEnabled[] = L"MetricsReportingEnabled";
30 44
45 const wchar_t kRegPathGoogleUpdate[] = L"Software\\Google\\Update";
46 const wchar_t kRegGoogleUpdateVersion[] = L"version";
47
31 const wchar_t kAppGuidCanary[] = 48 const wchar_t kAppGuidCanary[] =
32 L"{4ea16ac7-fd5a-47c3-875b-dbf4a2008c20}"; 49 L"{4ea16ac7-fd5a-47c3-875b-dbf4a2008c20}";
33 const wchar_t kAppGuidGoogleChrome[] = 50 const wchar_t kAppGuidGoogleChrome[] =
34 L"{8A69D345-D564-463c-AFF1-A69D9E530F96}"; 51 L"{8A69D345-D564-463c-AFF1-A69D9E530F96}";
35 const wchar_t kAppGuidGoogleBinaries[] = 52 const wchar_t kAppGuidGoogleBinaries[] =
36 L"{4DC8B4CA-1BDA-483e-B5FA-D3C12E15B62D}"; 53 L"{4DC8B4CA-1BDA-483e-B5FA-D3C12E15B62D}";
37 54
55 // TODO(ananta)
56 // http://crbug.com/604923
57 // These constants are defined in the chrome/installer directory as well. We
58 // need to unify them.
59 #if defined(GOOGLE_CHROME_BUILD)
60 const wchar_t kSxSSuffix[] = L" SxS";
61 const wchar_t kGoogleChromeInstallSubDir1[] = L"Google";
62 const wchar_t kGoogleChromeInstallSubDir2[] = L"Chrome";
63 const wchar_t kRegApField[] = L"ap";
64 #else
65 const wchar_t kChromiumInstallSubDir[] = L"Chromium";
66 #endif
67 const wchar_t kUserDataDirname[] = L"User Data";
68 const wchar_t kBrowserCrashDumpMetricsSubKey[] = L"\\BrowserCrashDumpAttempts";
69
70 void Trace(const wchar_t* format_string, ...) {
71 static const int kMaxLogBufferSize = 1024;
72 static wchar_t buffer[kMaxLogBufferSize] = {};
73
74 va_list args = {};
75
76 va_start(args, format_string);
77 vswprintf(buffer, arraysize(buffer), format_string, args);
78 OutputDebugString(buffer);
79 va_end(args);
80 }
81
38 bool ReadKeyValueString(bool system_install, const wchar_t* key_path, 82 bool ReadKeyValueString(bool system_install, const wchar_t* key_path,
39 const wchar_t* guid, const wchar_t* value_to_read, 83 const wchar_t* guid, const wchar_t* value_to_read,
40 base::string16* value_out) { 84 base::string16* value_out) {
41 HKEY key = NULL; 85 HKEY key = NULL;
42 value_out->clear(); 86 value_out->clear();
43 87
44 base::string16 full_key_path(key_path); 88 base::string16 full_key_path(key_path);
45 full_key_path.append(1, L'\\'); 89 if (wcslen(guid) > 0) {
grt (UTC plus 2) 2016/04/29 20:50:56 if (guid && *guid) {
ananta 2016/04/29 22:49:23 Done.
ananta 2016/04/29 22:49:24 Done.
46 full_key_path.append(guid); 90 full_key_path.append(1, L'\\');
91 full_key_path.append(guid);
92 }
47 93
48 if (::RegOpenKeyEx(system_install ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER, 94 if (::RegOpenKeyEx(system_install ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER,
49 full_key_path.c_str(), 0, 95 full_key_path.c_str(), 0,
50 KEY_QUERY_VALUE | KEY_WOW64_32KEY, &key) != 96 KEY_QUERY_VALUE | KEY_WOW64_32KEY, &key) !=
grt (UTC plus 2) 2016/04/29 20:50:55 please note in a comment for this fn and the DW va
ananta 2016/04/29 22:49:23 Done.
ananta 2016/04/29 22:49:24 Done.
51 ERROR_SUCCESS) { 97 ERROR_SUCCESS) {
52 return false; 98 return false;
53 } 99 }
54 100
55 const size_t kMaxStringLength = 1024; 101 const size_t kMaxStringLength = 1024;
56 wchar_t raw_value[kMaxStringLength] = {}; 102 wchar_t raw_value[kMaxStringLength] = {};
57 DWORD size = sizeof(raw_value); 103 DWORD size = sizeof(raw_value);
58 DWORD type = REG_SZ; 104 DWORD type = REG_SZ;
59 LONG result = ::RegQueryValueEx(key, value_to_read, 0, &type, 105 LONG result = ::RegQueryValueEx(key, value_to_read, 0, &type,
60 reinterpret_cast<LPBYTE>(raw_value), &size); 106 reinterpret_cast<LPBYTE>(raw_value), &size);
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
100 DWORD size = sizeof(*value_out); 146 DWORD size = sizeof(*value_out);
101 DWORD type = REG_DWORD; 147 DWORD type = REG_DWORD;
102 LONG result = ::RegQueryValueEx(key, value_to_read, 0, &type, 148 LONG result = ::RegQueryValueEx(key, value_to_read, 0, &type,
103 reinterpret_cast<BYTE*>(value_out), &size); 149 reinterpret_cast<BYTE*>(value_out), &size);
104 150
105 ::RegCloseKey(key); 151 ::RegCloseKey(key);
106 152
107 return result == ERROR_SUCCESS && size == sizeof(*value_out); 153 return result == ERROR_SUCCESS && size == sizeof(*value_out);
108 } 154 }
109 155
156 struct LanguageAndCodePage {
grt (UTC plus 2) 2016/04/29 20:50:56 nit: move this into GetLanguageAndCodePageFromVers
ananta 2016/04/29 22:49:23 Done.
157 WORD language;
158 WORD code_page;
159 };
160
161 bool GetLanguageAndCodePageFromVersionResource(const char* version_resource,
162 WORD* language,
163 WORD* code_page) {
164 if (!version_resource)
165 return false;
166
167 LanguageAndCodePage* translation_info = nullptr;
grt (UTC plus 2) 2016/04/29 20:50:56 MSDN leads me to believe that this should be: DW
ananta 2016/04/29 22:49:23 This is based on https://msdn.microsoft.com/en-us/
grt (UTC plus 2) 2016/05/02 22:22:06 Okay, thanks for the pointer.
168 uint32_t page_count = 0;
grt (UTC plus 2) 2016/04/29 20:50:56 "count" is a bit of a misnomer since this is popul
ananta 2016/04/29 22:49:23 Renamed to data_size_in_bytes
169 BOOL query_result = VerQueryValue(version_resource,
170 L"\\VarFileInfo\\Translation",
171 reinterpret_cast<void**>(&translation_info),
172 &page_count);
173 if (!query_result)
174 return false;
175
176 *language = translation_info->language;
177 *code_page = translation_info->code_page;
178 return true;
179 }
180
181 bool GetValueFromVersionResource(const char* version_resource,
182 const base::string16& name,
183 base::string16* value_str) {
184 assert(value_str);
185 value_str->clear();
186
187 WORD language = 0;
188 WORD code_page = 0;
189 if (!GetLanguageAndCodePageFromVersionResource(version_resource,
190 &language,
191 &code_page)) {
192 return false;
grt (UTC plus 2) 2016/04/29 20:50:56 based on https://msdn.microsoft.com/en-us/library/
ananta 2016/04/29 22:49:24 We could possibly do that. The logic below is the
grt (UTC plus 2) 2016/05/02 22:22:06 Gotcha. Thinking about it more, why are we even do
ananta 2016/05/02 22:43:27 Added a TODO in the function. Will look into this
193 }
194
195 WORD lang_codepage[8] = {};
196 size_t i = 0;
197 // Use the language and codepage
198 lang_codepage[i++] = language;
199 lang_codepage[i++] = code_page;
200 // Use the default language and codepage from the resource.
201 lang_codepage[i++] = ::GetUserDefaultLangID();
202 lang_codepage[i++] = code_page;
203 // Use the language from the resource and Latin codepage (most common).
204 lang_codepage[i++] = language;
205 lang_codepage[i++] = 1252;
206 // Use the default language and Latin codepage (most common).
207 lang_codepage[i++] = ::GetUserDefaultLangID();
208 lang_codepage[i++] = 1252;
209
210 static_assert((arraysize(lang_codepage) % 2) == 0,
211 "Language code page size should be a multiple of 2");
212 assert(arraysize(lang_codepage) == i);
213
214 i = 0;
grt (UTC plus 2) 2016/04/29 20:50:56 call me crazy, but: for (i = 0; i < arraysize(la
ananta 2016/04/29 22:49:24 Done.
215 while (i < arraysize(lang_codepage)) {
216 wchar_t sub_block[MAX_PATH] = {};
grt (UTC plus 2) 2016/04/29 20:50:56 we don't really need to zero this out since it get
ananta 2016/04/29 22:49:23 Done.
217 WORD language = lang_codepage[i++];
218 WORD code_page = lang_codepage[i++];
219 _snwprintf_s(sub_block, MAX_PATH, MAX_PATH,
220 L"\\StringFileInfo\\%04x%04x\\%ls", language, code_page, name.c_str());
grt (UTC plus 2) 2016/04/29 20:50:55 WORD is an unsigned short, so %04hx
ananta 2016/04/29 22:49:23 Done. https://msdn.microsoft.com/en-us/library/win
221 void* value = nullptr;
222 uint32_t size = 0;
223 BOOL r = ::VerQueryValue(version_resource, sub_block, &value, &size);
224 if (r && value) {
225 value_str->assign(static_cast<wchar_t*>(value));
226 return true;
227 }
228 }
229 return false;
230 }
231
232 // Returns the executable path for the current process.
233 base::string16 GetCurrentProcessExePath() {
234 wchar_t exe_path[MAX_PATH] = {};
grt (UTC plus 2) 2016/04/29 20:50:55 is zeroing needed?
ananta 2016/04/29 22:49:23 I tend to always do it. Removed.
235 if (GetModuleFileName(NULL, exe_path, arraysize(exe_path)) == 0)
grt (UTC plus 2) 2016/04/29 20:50:56 nullptr
ananta 2016/04/29 22:49:24 Done.
236 return base::string16();
237 return exe_path;
238 }
239
240 // UTF8 to UTF16 and vice versa conversion helpers. We cannot use the base
241 // string conversion utilities here as they bring about a dependency on
242 // user32.dll which is not allowed in this file.
243
244 // Convert a UTF16 string to an UTF8 string.
245 std::string utf16_to_utf8(const base::string16 &source) {
246 if (source.empty())
247 return std::string();
248 int size = ::WideCharToMultiByte(CP_UTF8, 0, &source[0],
249 static_cast<int>(source.size()), nullptr, 0, nullptr, nullptr);
250 std::string result(size, 0);
251 if (::WideCharToMultiByte(CP_UTF8, 0, &source[0],
252 static_cast<int>(source.size()), &result[0], size, nullptr,
253 nullptr) != size) {
254 assert(false);
255 return std::string();
256 }
257 return result;
258 }
259
260 // Convert a UTF8 string to a UTF16 string.
261 base::string16 utf8_to_string16(const std::string &source) {
262 if (source.empty())
263 return base::string16();
264 int size = ::MultiByteToWideChar(CP_UTF8, 0, &source[0],
265 static_cast<int>(source.size()), nullptr, 0);
266 base::string16 result(size, 0);
grt (UTC plus 2) 2016/04/29 20:50:56 uber-nit: 0 -> L'\0' to make it super clear that t
ananta 2016/04/29 22:49:24 Done.
grt (UTC plus 2) 2016/05/02 22:22:06 Hah, you did it on the line above that I missed, b
ananta 2016/05/02 22:43:27 Sorry, fixed.
267 if (::MultiByteToWideChar(CP_UTF8, 0, &source[0],
268 static_cast<int>(source.size()), &result[0], size) != size) {
269 assert(false);
270 return base::string16();
271 }
272 return result;
273 }
274
275 bool RecursiveDirectoryCreate(const base::string16& full_path) {
276 // If the path exists, we've succeeded if it's a directory, failed otherwise.
277 const wchar_t* full_path_str = full_path.c_str();
278 DWORD file_attributes = ::GetFileAttributes(full_path_str);
279 if (file_attributes != INVALID_FILE_ATTRIBUTES) {
280 if ((file_attributes & FILE_ATTRIBUTE_DIRECTORY) != 0) {
281 Trace(L"%hs( %ls directory exists )\n", __FUNCTION__, full_path_str);
282 return true;
283 }
284 Trace(L"%hs( %ls directory conflicts with an existing file. )\n",
285 __FUNCTION__, full_path_str);
286 return false;
287 }
288
289 // Invariant: Path does not exist as file or directory.
290
291 // Attempt to create the parent recursively. This will immediately return
292 // true if it already exists, otherwise will create all required parent
293 // directories starting with the highest-level missing parent.
294 base::string16 parent_path;
295 std::size_t pos = full_path.find_last_of(L"/\\");
296 if (pos != base::string16::npos) {
297 parent_path = full_path.substr(0, pos);
298 if (!RecursiveDirectoryCreate(parent_path)) {
299 Trace(L"Failed to create one of the parent directories");
300 return false;
301 }
302 }
303 if (!::CreateDirectory(full_path_str, NULL)) {
304 DWORD error_code = ::GetLastError();
305 if (error_code == ERROR_ALREADY_EXISTS) {
306 DWORD file_attributes = GetFileAttributes(full_path_str);
307 if ((file_attributes != INVALID_FILE_ATTRIBUTES) &&
308 ((file_attributes & FILE_ATTRIBUTE_DIRECTORY) != 0)) {
309 // This error code ERROR_ALREADY_EXISTS doesn't indicate whether we
310 // were racing with someone creating the same directory, or a file
311 // with the same path. If the directory exists, we lost the
312 // race to create the same directory.
313 return true;
314 } else {
315 Trace(L"Failed to create directory %ls, last error is %d\n",
316 full_path_str, error_code);
317 return false;
318 }
319 }
320 }
321 return true;
322 }
323
324 bool GetCollectStatsConsentImpl(const base::string16& exe_path) {
325 bool enabled = true;
326 bool controlled_by_policy = ReportingIsEnforcedByPolicy(&enabled);
327
328 if (controlled_by_policy && !enabled)
grt (UTC plus 2) 2016/04/29 20:50:56 i think the right thing here is really: if (Repo
ananta 2016/04/29 22:49:24 Done.
329 return false;
330
331 bool system_install = IsSystemInstall(exe_path.c_str());
332 base::string16 app_guid;
333
334 if (IsCanary(exe_path.c_str())) {
335 app_guid = kAppGuidCanary;
336 } else {
337 app_guid = IsMultiInstall(system_install) ? kAppGuidGoogleBinaries :
338 kAppGuidGoogleChrome;
339 }
340
341 DWORD out_value = 0;
342 if (system_install &&
343 ReadKeyValueDW(system_install, kRegPathClientStateMedium, app_guid,
grt (UTC plus 2) 2016/04/29 20:50:55 nit: indentation. please use "git cl format".
ananta 2016/04/29 22:49:24 Done.
344 kRegValueUsageStats, &out_value)) {
345 return out_value == 1;
346 }
347
348 return ReadKeyValueDW(system_install, kRegPathClientState, app_guid,
349 kRegValueUsageStats, &out_value) && out_value == 1;
350 }
351
352 // Returns true if the |source| string matches the |pattern|. The pattern
353 // may contain wildcards like '?' which matches one character or a '*'
354 // which matches 0 or more characters.
355 // Please note that pattern matches the whole string. If you want to find
356 // something in the middle of the string then you need to specify the pattern
357 // as '*xyz*'.
358 // |source_index| is the index of the current character being matched in
359 // |source|.
360 // |pattern_index| is the index of the current pattern character in |pattern|
361 // which is matched with source.
362 bool MatchPatternImpl(const base::string16& source,
363 const base::string16& pattern,
364 size_t source_index,
365 size_t pattern_index) {
366 if (source.empty() && pattern.empty())
367 return true;
368
369 if (source_index > source.length() || pattern_index > pattern.length())
370 return false;
371
372 // If we reached the end of both strings, then we are done.
373 if ((source_index == source.length()) &&
374 (pattern_index == pattern.length())) {
375 return true;
376 }
377
378 // If the current character in the pattern is a '*' then make sure that
379 // characters after the pattern are present in the source string. This
380 // assumes that you won't have two consecutive '*' characters in the pattern.
381 if ((pattern[pattern_index] == L'*') &&
382 (pattern_index + 1 < pattern.length()) &&
383 (source_index >= source.length())) {
384 return false;
385 }
386
387 // If the pattern contains wildcard characters '?' or '.' or there is a match
388 // then move ahead in both strings.
389 if ((pattern[pattern_index] == L'?') ||
390 (pattern[pattern_index] == source[source_index])) {
391 return MatchPatternImpl(source, pattern, source_index + 1,
392 pattern_index + 1);
393 }
394
395 // If we have a '*' then there are two possibilities
396 // 1. We consider current character of source.
397 // 2. We ignore current character of source.
398 if (pattern[pattern_index] == L'*') {
399 return MatchPatternImpl(source, pattern, source_index + 1,
400 pattern_index) ||
401 MatchPatternImpl(source, pattern, source_index, pattern_index + 1);
402 }
403 return false;
404 }
405
110 } // namespace 406 } // namespace
111 407
112 bool IsCanary(const wchar_t* exe_path) { 408 bool IsCanary(const wchar_t* exe_path) {
grt (UTC plus 2) 2016/04/29 20:50:56 is it possible/does it make sense to use base::Str
ananta 2016/04/29 22:49:24 Sadly no. StringPiece16 comes with logging.h, whic
grt (UTC plus 2) 2016/05/02 22:22:06 Acknowledged.
113 return wcsstr(exe_path, L"Chrome SxS\\Application") != NULL; 409 return wcsstr(exe_path, L"Chrome SxS\\Application") != NULL;
114 } 410 }
115 411
116 bool IsSystemInstall(const wchar_t* exe_path) { 412 bool IsSystemInstall(const wchar_t* exe_path) {
117 wchar_t program_dir[MAX_PATH] = {}; 413 wchar_t program_dir[MAX_PATH] = {};
118 DWORD ret = ::GetEnvironmentVariable(L"PROGRAMFILES", program_dir, 414 DWORD ret = ::GetEnvironmentVariable(L"PROGRAMFILES", program_dir,
119 arraysize(program_dir)); 415 arraysize(program_dir));
120 if (ret && ret < MAX_PATH && !wcsncmp(exe_path, program_dir, ret)) 416 if (ret && ret < MAX_PATH && !wcsncmp(exe_path, program_dir, ret))
grt (UTC plus 2) 2016/04/29 20:50:56 wdyt of _wcsnicmp for path comparisons? i think we
grt (UTC plus 2) 2016/04/29 20:50:56 nit: i prefer arraysize(program_dir) over MAX_PATH
ananta 2016/04/29 22:49:23 Yeah. Done.
ananta 2016/04/29 22:49:24 Done.
121 return true; 417 return true;
122 418
123 ret = ::GetEnvironmentVariable(L"PROGRAMFILES(X86)", program_dir, 419 ret = ::GetEnvironmentVariable(L"PROGRAMFILES(X86)", program_dir,
124 arraysize(program_dir)); 420 arraysize(program_dir));
125 if (ret && ret < MAX_PATH && !wcsncmp(exe_path, program_dir, ret)) 421 if (ret && ret < MAX_PATH && !wcsncmp(exe_path, program_dir, ret))
126 return true; 422 return true;
127 423
128 return false; 424 return false;
129 } 425 }
130 426
131 bool IsMultiInstall(bool is_system_install) { 427 bool IsMultiInstall(bool is_system_install) {
132 base::string16 args; 428 base::string16 args;
133 if (!ReadKeyValueString(is_system_install, kRegPathClientState, 429 if (!ReadKeyValueString(is_system_install, kRegPathClientState,
134 kAppGuidGoogleChrome, kUninstallArgumentsField, 430 kAppGuidGoogleChrome, kUninstallArgumentsField,
135 &args)) { 431 &args)) {
136 return false; 432 return false;
137 } 433 }
138 return args.find(L"--multi-install") != base::string16::npos; 434 return args.find(L"--multi-install") != base::string16::npos;
139 } 435 }
140 436
141 bool AreUsageStatsEnabled(const wchar_t* exe_path) { 437 bool GetCollectStatsConsent() {
142 bool enabled = true; 438 return GetCollectStatsConsentImpl(GetCurrentProcessExePath());
143 bool controlled_by_policy = ReportingIsEnforcedByPolicy(&enabled); 439 }
144 440
145 if (controlled_by_policy && !enabled) 441 bool GetCollectStatsConsentForTesting(const base::string16& exe_path) {
146 return false; 442 return GetCollectStatsConsentImpl(exe_path);
147
148 bool system_install = IsSystemInstall(exe_path);
149 base::string16 app_guid;
150
151 if (IsCanary(exe_path)) {
152 app_guid = kAppGuidCanary;
153 } else {
154 app_guid = IsMultiInstall(system_install) ? kAppGuidGoogleBinaries :
155 kAppGuidGoogleChrome;
156 }
157
158 DWORD out_value = 0;
159 if (system_install &&
160 ReadKeyValueDW(system_install, kRegPathClientStateMedium, app_guid,
161 kRegValueUsageStats, &out_value)) {
162 return out_value == 1;
163 }
164
165 return ReadKeyValueDW(system_install, kRegPathClientState, app_guid,
166 kRegValueUsageStats, &out_value) && out_value == 1;
167 } 443 }
168 444
169 bool ReportingIsEnforcedByPolicy(bool* breakpad_enabled) { 445 bool ReportingIsEnforcedByPolicy(bool* breakpad_enabled) {
grt (UTC plus 2) 2016/04/29 20:50:55 breakpad_enabled -> metrics_reporting_enabled
ananta 2016/04/29 22:49:24 Done.
170 HKEY key = NULL; 446 HKEY key = NULL;
171 DWORD value = 0; 447 DWORD value = 0;
172 BYTE* value_bytes = reinterpret_cast<BYTE*>(&value); 448 BYTE* value_bytes = reinterpret_cast<BYTE*>(&value);
173 DWORD size = sizeof(value); 449 DWORD size = sizeof(value);
174 DWORD type = REG_DWORD; 450 DWORD type = REG_DWORD;
175 451
176 if (::RegOpenKeyEx(HKEY_LOCAL_MACHINE, kRegPathChromePolicy, 0, 452 if (::RegOpenKeyEx(HKEY_LOCAL_MACHINE, kRegPathChromePolicy, 0,
177 KEY_QUERY_VALUE, &key) == ERROR_SUCCESS) { 453 KEY_QUERY_VALUE, &key) == ERROR_SUCCESS) {
178 if (::RegQueryValueEx(key, kMetricsReportingEnabled, 0, &type, 454 if (::RegQueryValueEx(key, kMetricsReportingEnabled, 0, &type,
179 value_bytes, &size) == ERROR_SUCCESS) { 455 value_bytes, &size) == ERROR_SUCCESS) {
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
215 return; 491 return;
216 } 492 }
217 493
218 g_process_type = ProcessType::BROWSER_PROCESS; 494 g_process_type = ProcessType::BROWSER_PROCESS;
219 } 495 }
220 496
221 bool IsNonBrowserProcess() { 497 bool IsNonBrowserProcess() {
222 assert(g_process_type != ProcessType::UNINITIALIZED); 498 assert(g_process_type != ProcessType::UNINITIALIZED);
223 return g_process_type == ProcessType::NON_BROWSER_PROCESS; 499 return g_process_type == ProcessType::NON_BROWSER_PROCESS;
224 } 500 }
501
502 bool GetDefaultUserDataDirectory(base::string16* result) {
503 static const wchar_t kLocalAppData[] = L"%LOCALAPPDATA%";
504
505 std::unique_ptr<wchar_t> user_data_dir_path;
506
507 // This environment variable should be set on Windows 7 and up.
508 // If we fail to find this variable then we default to the temporary files
509 // path.
510 DWORD size = ::ExpandEnvironmentStrings(kLocalAppData, nullptr, 0);
511 if (size) {
512 user_data_dir_path.reset(new wchar_t[size]);
513 if (::ExpandEnvironmentStrings(kLocalAppData,
514 user_data_dir_path.get(),
515 size) != size) {
516 user_data_dir_path.reset(nullptr);
grt (UTC plus 2) 2016/04/29 20:50:56 nit: omit "nullptr"
ananta 2016/04/29 22:49:24 Done.
517 }
518 }
519 // We failed to find the %LOCALAPPDATA% folder. Fallback to the temporary
520 // files path. If we fail to find this we bail.
521 if (!user_data_dir_path.get()) {
522 size = ::GetTempPath(0, nullptr);
523 if (!size)
524 return false;
525 user_data_dir_path.reset(new wchar_t[size + 1]);
526 if (::GetTempPath(size + 1, user_data_dir_path.get()) != size)
527 return false;
528 }
529
530 base::string16 install_sub_directory = GetChromeInstallSubDirectory();
531
532 *result = user_data_dir_path.get();
533 if ((*result)[result->length() - 1] != L'\\')
534 result->append(L"\\");
535 result->append(install_sub_directory);
536 result->append(L"\\");
537 result->append(kUserDataDirname);
538 return true;
539 }
540
541 bool GetDefaultCrashDumpLocation(base::string16* crash_dir) {
542 // In order to be able to start crash handling very early, we do not rely on
543 // chrome's PathService entries (for DIR_CRASH_DUMPS) being available on
544 // Windows. See https://crbug.com/564398.
545 if (!GetDefaultUserDataDirectory(crash_dir))
546 return false;
547
548 // We have to make sure the user data dir exists on first run. See
549 // http://crbug.com/591504.
550 if (!RecursiveDirectoryCreate(crash_dir->c_str()))
551 return false;
552 crash_dir->append(L"\\Crashpad");
553 return true;
554 }
555
556
557 std::string GetEnvironmentString(const std::string& variable_name) {
558 DWORD value_length = ::GetEnvironmentVariable(
559 utf8_to_string16(variable_name).c_str(), NULL, 0);
560 if (value_length == 0)
561 return std::string();
562 std::unique_ptr<wchar_t[]> value(new wchar_t[value_length]);
563 ::GetEnvironmentVariable(utf8_to_string16(variable_name).c_str(),
564 value.get(), value_length);
565 return utf16_to_utf8(value.get());
566 }
567
568 bool SetEnvironmentString(const std::string& variable_name,
569 const std::string& new_value) {
570 return !!SetEnvironmentVariable(utf8_to_string16(variable_name).c_str(),
571 utf8_to_string16(new_value).c_str());
572 }
573
574 bool HasEnvironmentVariable(const std::string& variable_name) {
575 return !!::GetEnvironmentVariable(utf8_to_string16(variable_name).c_str(),
576 NULL, 0);
577 }
578
579 bool GetExecutableVersionDetails(const base::string16& exe_path,
580 base::string16* product_name,
581 base::string16* version,
582 base::string16* special_build,
583 base::string16* channel_name) {
584 assert(product_name);
585 assert(version);
586 assert(special_build);
587 assert(channel_name);
588
589 // Default values in case we don't find a version resource.
590 *product_name = L"Chrome";
591 *version = L"0.0.0.0-devel";
592 *channel_name = kChromeChannelUnknown;
593 special_build->clear();
594
595 DWORD dummy = 0;
596 DWORD length = ::GetFileVersionInfoSize(exe_path.c_str(), &dummy);
597 if (length) {
598 std::unique_ptr<char> data(new char[length]);
599 if (::GetFileVersionInfo(exe_path.c_str(), dummy, length,
600 data.get())) {
601 GetValueFromVersionResource(data.get(), L"ProductVersion", version);
602
603 base::string16 official_build;
604 GetValueFromVersionResource(data.get(), L"Official Build",
605 &official_build);
606 if (official_build != L"1")
607 version->append(L"-devel");
608 GetValueFromVersionResource(data.get(), L"ProductShortName",
609 product_name);
610 GetValueFromVersionResource(data.get(), L"SpecialBuild", special_build);
611 }
612 }
613 GetChromeChannelName(!IsSystemInstall(exe_path.c_str()), channel_name);
614 return true;
615 }
616
617 void GetChromeChannelName(bool is_per_user_install,
618 base::string16* channel_name) {
619 #if !defined(GOOGLE_CHROME_BUILD)
620 *channel_name = kChromeChannelUnknown;
621 return;
622 #else
623 channel_name->clear();
624 // TODO(ananta)
625 // http://crbug.com/604923
626 // Unify this with the chrome/installer/util/channel_info.h/.cc.
627 if (IsCanary(GetCurrentProcessExePath().c_str())) {
628 *channel_name = L"canary";
629 } else {
630 base::string16 value;
631 if (ReadKeyValueString(!is_per_user_install,
632 kRegPathClientState,
633 kAppGuidGoogleChrome,
634 kRegApField,
635 &value)) {
636 static const wchar_t kChromeChannelBetaPattern[] = L"1?1-";
637 static const wchar_t kChromeChannelBetaX64Pattern[] = L"*x64-beta*";
638 static const wchar_t kChromeChannelDevPattern[] = L"2?0-d";
639 static const wchar_t kChromeChannelDevX64Pattern[] = L"x64-dev";
640 static const wchar_t kChromeStableMultiInstallPattern[] = L"*multi*";
641
642 std::transform(value.begin(), value.end(), value.begin(), ::tolower);
643
644 // Channel names containing stable should be reported as an empty string.
645 if (value.find(kChromeChannelStableExplicit) !=
646 base::string16::npos) {
647 return;
648 }
649
650 if (MatchPattern(value, kChromeChannelDevPattern) ||
651 MatchPattern(value, kChromeChannelDevX64Pattern)) {
652 channel_name->assign(kChromeChannelDev);
653 }
654
655 if (MatchPattern(value, kChromeChannelBetaPattern) ||
656 MatchPattern(value, kChromeChannelBetaPattern)) {
657 channel_name->assign(kChromeChannelBeta);
658 }
659
660 if (value.find(kChromeStableMultiInstallPattern) !=
661 base::string16::npos) {
662 *channel_name = L"-m";
663 }
664
665 // If we fail to find any matching pattern in the channel name then we
666 // default to empty which means stable. Not sure if this is ok.
667 // TODO(ananta)
668 // http://crbug.com/604923
669 // Check if this is ok.
670 } else {
671 *channel_name = kChromeChannelUnknown;
672 }
673 }
674 #endif // GOOGLE_CHROME_BUILD
675 }
676
677 std::string GetGoogleUpdateVersion() {
678 base::string16 update_version;
679 if (ReadKeyValueString(IsSystemInstall(GetCurrentProcessExePath().c_str()),
680 kRegPathGoogleUpdate,
681 L"",
682 kRegGoogleUpdateVersion,
683 &update_version)) {
684 return utf16_to_utf8(update_version);
685 }
686 return std::string();
687 }
688
689 base::string16 GetChromeInstallSubDirectory() {
690 base::string16 result;
691 #if defined(GOOGLE_CHROME_BUILD)
692 base::string16 sub_directory = kGoogleChromeInstallSubDir1;
693 sub_directory += L"\\";
694 sub_directory += kGoogleChromeInstallSubDir2;
695 if (IsCanary(GetCurrentProcessExePath().c_str()))
696 sub_directory += kSxSSuffix;
697 result.append(sub_directory);
grt (UTC plus 2) 2016/04/29 20:50:56 nit: return result.append(sub_directory); #else
ananta 2016/04/29 22:49:24 Done.
698 #else
699 result = result.append(kChromiumInstallSubDir);
700 #endif
701 return result;
702 }
703
704 base::string16 GetBrowserCrashDumpAttemptsRegistryPath() {
705 base::string16 registry_path = L"Software";
grt (UTC plus 2) 2016/04/29 20:50:56 ? "Software\\" ?
ananta 2016/04/29 22:49:23 Thanks. Done.
706 base::string16 install_sub_directory = GetChromeInstallSubDirectory();
grt (UTC plus 2) 2016/04/29 20:50:56 nit: remove this variable and inline the call to G
ananta 2016/04/29 22:49:23 Done.
707 registry_path += install_sub_directory;
708 registry_path += kBrowserCrashDumpMetricsSubKey;
709 return registry_path;
710 }
711
712 bool MatchPattern(const base::string16& source,
713 const base::string16& pattern) {
714 assert(pattern.find(L"**") == base::string16::npos);
715 return MatchPatternImpl(source, pattern, 0, 0);
716 }
717
718 } // namespace install_static
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698