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

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

Powered by Google App Engine
This is Rietveld 408576698