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 <assert.h> |
8 #include <memory> | |
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 "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"; |
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(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) { |
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) != |
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] = {}; |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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 { | |
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; | |
168 uint32_t page_count = 0; | |
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; | |
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; | |
215 while (i < arraysize(lang_codepage)) { | |
216 wchar_t sub_block[MAX_PATH] = {}; | |
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()); | |
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] = {}; | |
235 if (GetModuleFileName(NULL, exe_path, arraysize(exe_path)) == 0) | |
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); | |
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); | |
scottmg
2016/04/28 21:34:00
extra space here after (
ananta
2016/04/28 22:54:39
Added an extra space before the inner ) for consis
| |
282 return true; | |
283 } | |
284 Trace(L"%hs( %ls directory conflicts with an existing file)\n", | |
scottmg
2016/04/28 21:34:00
same
ananta
2016/04/28 22:54:39
Ditto
| |
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) | |
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, | |
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 match one character or a '*' | |
scottmg
2016/04/28 21:34:00
I think not supporting '.' (only ? and *) would be
ananta
2016/04/28 22:54:39
Done.
| |
354 // which matches 0 or more instances of any character or a set of | |
355 // characters. | |
356 bool MatchPattern(const base::string16& source, | |
scottmg
2016/04/28 21:34:00
Gonna need some unittests for this function. I'm h
ananta
2016/04/28 22:54:39
Renamed to MatchPatternImpl. This is called from p
| |
357 const base::string16& pattern, | |
358 size_t source_index, | |
359 size_t pattern_index) { | |
360 if (source.empty() && pattern.empty()) | |
361 return true; | |
362 | |
363 if (source_index > source.length() || pattern_index > pattern.length()) | |
364 return false; | |
365 | |
366 // If we reached the end of both strings, then we are done. | |
367 if ((source_index == source.length()) && | |
368 (pattern_index == pattern.length())) { | |
369 return true; | |
370 } | |
371 | |
372 // If the current character in the pattern is a '*' then make sure that | |
373 // characters after the pattern are present in the source string. This | |
374 // assumes that you won't have two consecutive '*' characters in the pattern. | |
375 if ((pattern[pattern_index] == L'*') && | |
376 (pattern_index + 1 < pattern.length()) && | |
377 (source_index >= source.length())) { | |
378 return false; | |
379 } | |
380 | |
381 // If the pattern contains wildcard characters '?' or '.' or there is a match | |
382 // then move ahead in both strings. | |
383 if ((pattern[pattern_index] == L'?') || (pattern[pattern_index] == L'.') || | |
384 (pattern[pattern_index] == source[source_index])) { | |
385 return MatchPattern(source, pattern, source_index + 1, pattern_index + 1); | |
386 } | |
387 | |
388 // If we have a '*' then there are two possibilities | |
389 // 1. We consider current character of source. | |
390 // 2. We ignore current character of source. | |
391 if (pattern[pattern_index] == L'*') { | |
392 return MatchPattern(source, pattern, source_index + 1, pattern_index) || | |
393 MatchPattern(source, pattern, source_index, pattern_index + 1); | |
394 } | |
395 return false; | |
396 } | |
397 | |
110 } // namespace | 398 } // namespace |
111 | 399 |
112 bool IsCanary(const wchar_t* exe_path) { | 400 bool IsCanary(const wchar_t* exe_path) { |
113 return wcsstr(exe_path, L"Chrome SxS\\Application") != NULL; | 401 return wcsstr(exe_path, L"Chrome SxS\\Application") != NULL; |
114 } | 402 } |
115 | 403 |
116 bool IsSystemInstall(const wchar_t* exe_path) { | 404 bool IsSystemInstall(const wchar_t* exe_path) { |
117 wchar_t program_dir[MAX_PATH] = {}; | 405 wchar_t program_dir[MAX_PATH] = {}; |
118 DWORD ret = ::GetEnvironmentVariable(L"PROGRAMFILES", program_dir, | 406 DWORD ret = ::GetEnvironmentVariable(L"PROGRAMFILES", program_dir, |
119 arraysize(program_dir)); | 407 arraysize(program_dir)); |
(...skipping 11 matching lines...) Expand all Loading... | |
131 bool IsMultiInstall(bool is_system_install) { | 419 bool IsMultiInstall(bool is_system_install) { |
132 base::string16 args; | 420 base::string16 args; |
133 if (!ReadKeyValueString(is_system_install, kRegPathClientState, | 421 if (!ReadKeyValueString(is_system_install, kRegPathClientState, |
134 kAppGuidGoogleChrome, kUninstallArgumentsField, | 422 kAppGuidGoogleChrome, kUninstallArgumentsField, |
135 &args)) { | 423 &args)) { |
136 return false; | 424 return false; |
137 } | 425 } |
138 return args.find(L"--multi-install") != base::string16::npos; | 426 return args.find(L"--multi-install") != base::string16::npos; |
139 } | 427 } |
140 | 428 |
141 bool AreUsageStatsEnabled(const wchar_t* exe_path) { | 429 bool GetCollectStatsConsent() { |
142 bool enabled = true; | 430 return GetCollectStatsConsentImpl(GetCurrentProcessExePath()); |
143 bool controlled_by_policy = ReportingIsEnforcedByPolicy(&enabled); | 431 } |
144 | 432 |
145 if (controlled_by_policy && !enabled) | 433 bool GetCollectStatsConsentForTesting(const base::string16& exe_path) { |
146 return false; | 434 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 } | 435 } |
168 | 436 |
169 bool ReportingIsEnforcedByPolicy(bool* breakpad_enabled) { | 437 bool ReportingIsEnforcedByPolicy(bool* breakpad_enabled) { |
170 HKEY key = NULL; | 438 HKEY key = NULL; |
171 DWORD value = 0; | 439 DWORD value = 0; |
172 BYTE* value_bytes = reinterpret_cast<BYTE*>(&value); | 440 BYTE* value_bytes = reinterpret_cast<BYTE*>(&value); |
173 DWORD size = sizeof(value); | 441 DWORD size = sizeof(value); |
174 DWORD type = REG_DWORD; | 442 DWORD type = REG_DWORD; |
175 | 443 |
176 if (::RegOpenKeyEx(HKEY_LOCAL_MACHINE, kRegPathChromePolicy, 0, | 444 if (::RegOpenKeyEx(HKEY_LOCAL_MACHINE, kRegPathChromePolicy, 0, |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
215 return; | 483 return; |
216 } | 484 } |
217 | 485 |
218 g_process_type = ProcessType::BROWSER_PROCESS; | 486 g_process_type = ProcessType::BROWSER_PROCESS; |
219 } | 487 } |
220 | 488 |
221 bool IsNonBrowserProcess() { | 489 bool IsNonBrowserProcess() { |
222 assert(g_process_type != ProcessType::UNINITIALIZED); | 490 assert(g_process_type != ProcessType::UNINITIALIZED); |
223 return g_process_type == ProcessType::NON_BROWSER_PROCESS; | 491 return g_process_type == ProcessType::NON_BROWSER_PROCESS; |
224 } | 492 } |
493 | |
494 bool GetDefaultUserDataDirectory(base::string16* result) { | |
495 static const wchar_t kLocalAppData[] = L"%LOCALAPPDATA%"; | |
496 | |
497 std::unique_ptr<wchar_t> user_data_dir_path; | |
498 | |
499 // This environment variable should be set on Windows 7 and up. | |
500 // If we fail to find this variable then we default to the temporary files | |
501 // path. | |
502 DWORD size = ::ExpandEnvironmentStrings(kLocalAppData, nullptr, 0); | |
503 if (size) { | |
504 user_data_dir_path.reset(new wchar_t[size]); | |
505 if (::ExpandEnvironmentStrings(kLocalAppData, | |
506 user_data_dir_path.get(), | |
507 size) != size) { | |
508 user_data_dir_path.reset(nullptr); | |
509 } | |
510 } | |
511 // We failed to find the %LOCALAPPDATA% folder. Fallback to the temporary | |
512 // files path. If we fail to find this we bail. | |
513 if (!user_data_dir_path.get()) { | |
514 size = ::GetTempPath(0, nullptr); | |
515 if (!size) | |
516 return false; | |
517 user_data_dir_path.reset(new wchar_t[size + 1]); | |
518 if (::GetTempPath(size + 1, user_data_dir_path.get()) != size) | |
519 return false; | |
520 } | |
521 | |
522 base::string16 install_sub_directory = GetChromeInstallSubDirectory(); | |
523 | |
524 *result = user_data_dir_path.get(); | |
525 if ((*result)[result->length() - 1] != L'\\') | |
526 result->append(L"\\"); | |
527 result->append(install_sub_directory); | |
528 result->append(L"\\"); | |
529 result->append(kUserDataDirname); | |
530 return true; | |
531 } | |
532 | |
533 bool GetDefaultCrashDumpLocation(base::string16* crash_dir) { | |
534 // In order to be able to start crash handling very early, we do not rely on | |
535 // chrome's PathService entries (for DIR_CRASH_DUMPS) being available on | |
536 // Windows. See https://crbug.com/564398. | |
537 if (!GetDefaultUserDataDirectory(crash_dir)) | |
538 return false; | |
539 | |
540 // We have to make sure the user data dir exists on first run. See | |
541 // http://crbug.com/591504. | |
542 if (!RecursiveDirectoryCreate(crash_dir->c_str())) | |
543 return false; | |
544 crash_dir->append(L"\\Crashpad"); | |
545 return true; | |
546 } | |
547 | |
548 | |
549 std::string GetEnvironmentString(const std::string& variable_name) { | |
550 DWORD value_length = ::GetEnvironmentVariable( | |
551 utf8_to_string16(variable_name).c_str(), NULL, 0); | |
552 if (value_length == 0) | |
553 return std::string(); | |
554 std::unique_ptr<wchar_t[]> value(new wchar_t[value_length]); | |
555 ::GetEnvironmentVariable(utf8_to_string16(variable_name).c_str(), | |
556 value.get(), value_length); | |
557 return utf16_to_utf8(value.get()); | |
558 } | |
559 | |
560 bool SetEnvironmentString(const std::string& variable_name, | |
561 const std::string& new_value) { | |
562 return !!SetEnvironmentVariable(utf8_to_string16(variable_name).c_str(), | |
563 utf8_to_string16(new_value).c_str()); | |
564 } | |
565 | |
566 bool HasEnvironmentVariable(const std::string& variable_name) { | |
567 return !!::GetEnvironmentVariable(utf8_to_string16(variable_name).c_str(), | |
568 NULL, 0); | |
569 } | |
570 | |
571 bool GetExecutableVersionDetails(const base::string16& exe_path, | |
572 base::string16* product_name, | |
573 base::string16* version, | |
574 base::string16* special_build, | |
575 base::string16* channel_name) { | |
576 assert(product_name); | |
577 assert(version); | |
578 assert(special_build); | |
579 assert(channel_name); | |
580 | |
581 // Default values in case we don't find a version resource. | |
582 *product_name = L"Chrome"; | |
583 *version = L"0.0.0.0-devel"; | |
584 *channel_name = kChromeChannelUnknown; | |
585 special_build->clear(); | |
586 | |
587 DWORD dummy = 0; | |
588 DWORD length = ::GetFileVersionInfoSize(exe_path.c_str(), &dummy); | |
589 if (length) { | |
590 std::unique_ptr<char> data(new char[length]); | |
591 if (::GetFileVersionInfo(exe_path.c_str(), dummy, length, | |
592 data.get())) { | |
593 GetValueFromVersionResource(data.get(), L"ProductVersion", version); | |
594 | |
595 base::string16 official_build; | |
596 GetValueFromVersionResource(data.get(), L"Official Build", | |
597 &official_build); | |
598 if (official_build != L"1") | |
599 version->append(L"-devel"); | |
600 GetValueFromVersionResource(data.get(), L"ProductShortName", | |
601 product_name); | |
602 GetValueFromVersionResource(data.get(), L"SpecialBuild", special_build); | |
603 } | |
604 } | |
605 GetChromeChannelName(!IsSystemInstall(exe_path.c_str()), channel_name); | |
606 return true; | |
607 } | |
608 | |
609 void GetChromeChannelName(bool is_per_user_install, | |
610 base::string16* channel_name) { | |
611 #if !defined(GOOGLE_CHROME_BUILD) | |
612 *channel_name = kChromeChannelUnknown; | |
613 return; | |
614 #else | |
615 channel_name->clear(); | |
616 // TODO(ananta) | |
617 // http://crbug.com/604923 | |
618 // Unify this with the chrome/installer/util/channel_info.h/.cc. | |
619 if (IsCanary(GetCurrentProcessExePath().c_str())) { | |
620 *channel_name = L"canary"; | |
621 } else { | |
622 base::string16 value; | |
623 if (ReadKeyValueString(!is_per_user_install, | |
624 kRegPathClientState, | |
625 kAppGuidGoogleChrome, | |
626 kRegApField, | |
627 &value)) { | |
628 static const wchar_t kChromeChannelBetaPattern[] = L"1?1-"; | |
629 static const wchar_t kChromeChannelBetaX64Pattern[] = L"*x64-beta*"; | |
630 static const wchar_t kChromeChannelDevPattern[] = L"2?0-d"; | |
631 static const wchar_t kChromeChannelDevX64Pattern[] = L"x64-dev"; | |
632 static const wchar_t kChromeStableMultiInstallPattern[] = L"*multi*"; | |
633 | |
634 std::transform(value.begin(), value.end(), value.begin(), ::tolower); | |
635 | |
636 // Channel names containing stable should be reported as an empty string. | |
637 if (value.find(kChromeChannelStableExplicit) != | |
638 base::string16::npos) { | |
639 return; | |
640 } | |
641 | |
642 if (MatchPattern(value, kChromeChannelDevPattern, 0, 0) || | |
643 MatchPattern(value, kChromeChannelDevX64Pattern, 0, 0)) { | |
644 channel_name->assign(kChromeChannelDev); | |
645 } | |
646 | |
647 if (MatchPattern(value, kChromeChannelBetaPattern, 0, 0) || | |
648 MatchPattern(value, kChromeChannelBetaPattern, 0, 0)) { | |
649 channel_name->assign(kChromeChannelBeta); | |
650 } | |
651 | |
652 if (value.find(kChromeStableMultiInstallPattern) != | |
653 base::string16::npos) { | |
654 *channel_name = L"-m"; | |
655 } | |
656 | |
657 // If we fail to find any matching pattern in the channel name then we | |
658 // default to empty which means stable. Not sure if this is ok. | |
659 // TODO(ananta) | |
660 // http://crbug.com/604923 | |
661 // Check if this is ok. | |
662 } else { | |
663 *channel_name = kChromeChannelUnknown; | |
664 } | |
665 } | |
666 #endif // GOOGLE_CHROME_BUILD | |
667 } | |
668 | |
669 std::string GetGoogleUpdateVersion() { | |
670 base::string16 update_version; | |
671 if (ReadKeyValueString(IsSystemInstall(GetCurrentProcessExePath().c_str()), | |
672 kRegPathGoogleUpdate, | |
673 L"", | |
674 kRegGoogleUpdateVersion, | |
675 &update_version)) { | |
676 return utf16_to_utf8(update_version); | |
677 } | |
678 return std::string(); | |
679 } | |
680 | |
681 base::string16 GetChromeInstallSubDirectory() { | |
682 base::string16 result; | |
683 #if defined(GOOGLE_CHROME_BUILD) | |
684 base::string16 sub_directory = kGoogleChromeInstallSubDir1; | |
685 sub_directory += L"\\"; | |
686 sub_directory += kGoogleChromeInstallSubDir2; | |
687 if (IsCanary(GetCurrentProcessExePath().c_str())) | |
688 sub_directory += kSxSSuffix; | |
689 result.append(sub_directory); | |
690 #else | |
691 result = result.append(kChromiumInstallSubDir); | |
692 #endif | |
693 return result; | |
694 } | |
695 | |
696 base::string16 GetBrowserCrashDumpAttemptsRegistryPath() { | |
697 base::string16 registry_path = L"Software"; | |
698 base::string16 install_sub_directory = GetChromeInstallSubDirectory(); | |
699 registry_path += install_sub_directory; | |
700 registry_path += kBrowserCrashDumpMetricsSubKey; | |
701 return registry_path; | |
702 } | |
703 | |
704 } // namespace install_static | |
OLD | NEW |