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