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 "base/memory/scoped_ptr.h" |
13 #include "base/strings/pattern.h" | |
14 #include "base/strings/string_util.h" | |
scottmg
2016/04/27 23:29:24
Is this used?
ananta
2016/04/28 00:04:25
Removed
| |
15 #include "build/build_config.h" | |
16 | |
17 namespace install_static { | |
13 | 18 |
14 ProcessType g_process_type = ProcessType::UNINITIALIZED; | 19 ProcessType g_process_type = ProcessType::UNINITIALIZED; |
15 | 20 |
21 // Chrome channel display names. | |
22 // TODO(ananta) | |
23 // http://crbug.com/604923 | |
24 // These constants are defined in the chrome/installer directory as well. We | |
25 // need to unify them. | |
26 const wchar_t kChromeChannelUnknown[] = L"unknown"; | |
27 const wchar_t kChromeChannelCanary[] = L"canary"; | |
28 const wchar_t kChromeChannelDev[] = L"dev"; | |
29 const wchar_t kChromeChannelBeta[] = L"beta"; | |
30 const wchar_t kChromeChannelStable[] = L""; | |
31 const wchar_t kChromeChannelStableExplicit[] = L"stable"; | |
32 | |
16 namespace { | 33 namespace { |
17 | 34 |
18 const wchar_t kRegPathClientState[] = L"Software\\Google\\Update\\ClientState"; | 35 const wchar_t kRegPathClientState[] = L"Software\\Google\\Update\\ClientState"; |
19 const wchar_t kRegPathClientStateMedium[] = | 36 const wchar_t kRegPathClientStateMedium[] = |
20 L"Software\\Google\\Update\\ClientStateMedium"; | 37 L"Software\\Google\\Update\\ClientStateMedium"; |
21 #if defined(GOOGLE_CHROME_BUILD) | 38 #if defined(GOOGLE_CHROME_BUILD) |
22 const wchar_t kRegPathChromePolicy[] = L"SOFTWARE\\Policies\\Google\\Chrome"; | 39 const wchar_t kRegPathChromePolicy[] = L"SOFTWARE\\Policies\\Google\\Chrome"; |
23 #else | 40 #else |
24 const wchar_t kRegPathChromePolicy[] = L"SOFTWARE\\Policies\\Chromium"; | 41 const wchar_t kRegPathChromePolicy[] = L"SOFTWARE\\Policies\\Chromium"; |
25 #endif // defined(GOOGLE_CHROME_BUILD) | 42 #endif // defined(GOOGLE_CHROME_BUILD) |
26 | 43 |
27 const wchar_t kRegValueUsageStats[] = L"usagestats"; | 44 const wchar_t kRegValueUsageStats[] = L"usagestats"; |
28 const wchar_t kUninstallArgumentsField[] = L"UninstallArguments"; | 45 const wchar_t kUninstallArgumentsField[] = L"UninstallArguments"; |
29 const wchar_t kMetricsReportingEnabled[] = L"MetricsReportingEnabled"; | 46 const wchar_t kMetricsReportingEnabled[] = L"MetricsReportingEnabled"; |
30 | 47 |
48 const wchar_t kRegPathGoogleUpdate[] = L"Software\\Google\\Update"; | |
49 const wchar_t kRegGoogleUpdateVersion[] = L"version"; | |
50 | |
31 const wchar_t kAppGuidCanary[] = | 51 const wchar_t kAppGuidCanary[] = |
32 L"{4ea16ac7-fd5a-47c3-875b-dbf4a2008c20}"; | 52 L"{4ea16ac7-fd5a-47c3-875b-dbf4a2008c20}"; |
33 const wchar_t kAppGuidGoogleChrome[] = | 53 const wchar_t kAppGuidGoogleChrome[] = |
34 L"{8A69D345-D564-463c-AFF1-A69D9E530F96}"; | 54 L"{8A69D345-D564-463c-AFF1-A69D9E530F96}"; |
35 const wchar_t kAppGuidGoogleBinaries[] = | 55 const wchar_t kAppGuidGoogleBinaries[] = |
36 L"{4DC8B4CA-1BDA-483e-B5FA-D3C12E15B62D}"; | 56 L"{4DC8B4CA-1BDA-483e-B5FA-D3C12E15B62D}"; |
37 | 57 |
58 // TODO(ananta) | |
59 // http://crbug.com/604923 | |
60 // These constants are defined in the chrome/installer directory as well. We | |
61 // need to unify them. | |
62 #if defined(GOOGLE_CHROME_BUILD) | |
63 const wchar_t kSxSSuffix[] = L" SxS"; | |
64 const wchar_t kGoogleChromeInstallSubDir1[] = L"Google"; | |
65 const wchar_t kGoogleChromeInstallSubDir2[] = L"Chrome"; | |
66 const wchar_t kRegApField[] = L"ap"; | |
67 #else | |
68 const wchar_t kChromiumInstallSubDir[] = L"Chromium"; | |
69 #endif | |
70 const wchar_t kUserDataDirname[] = L"User Data"; | |
71 const wchar_t kBrowserCrashDumpMetricsSubKey[] = L"\\BrowserCrashDumpAttempts"; | |
72 | |
38 bool ReadKeyValueString(bool system_install, const wchar_t* key_path, | 73 bool ReadKeyValueString(bool system_install, const wchar_t* key_path, |
39 const wchar_t* guid, const wchar_t* value_to_read, | 74 const wchar_t* guid, const wchar_t* value_to_read, |
40 base::string16* value_out) { | 75 base::string16* value_out) { |
41 HKEY key = NULL; | 76 HKEY key = NULL; |
42 value_out->clear(); | 77 value_out->clear(); |
43 | 78 |
44 base::string16 full_key_path(key_path); | 79 base::string16 full_key_path(key_path); |
45 full_key_path.append(1, L'\\'); | 80 if (wcslen(guid) > 0) { |
46 full_key_path.append(guid); | 81 full_key_path.append(1, L'\\'); |
82 full_key_path.append(guid); | |
83 } | |
47 | 84 |
48 if (::RegOpenKeyEx(system_install ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER, | 85 if (::RegOpenKeyEx(system_install ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER, |
49 full_key_path.c_str(), 0, | 86 full_key_path.c_str(), 0, |
50 KEY_QUERY_VALUE | KEY_WOW64_32KEY, &key) != | 87 KEY_QUERY_VALUE | KEY_WOW64_32KEY, &key) != |
51 ERROR_SUCCESS) { | 88 ERROR_SUCCESS) { |
52 return false; | 89 return false; |
53 } | 90 } |
54 | 91 |
55 const size_t kMaxStringLength = 1024; | 92 const size_t kMaxStringLength = 1024; |
56 wchar_t raw_value[kMaxStringLength] = {}; | 93 wchar_t raw_value[kMaxStringLength] = {}; |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
100 DWORD size = sizeof(*value_out); | 137 DWORD size = sizeof(*value_out); |
101 DWORD type = REG_DWORD; | 138 DWORD type = REG_DWORD; |
102 LONG result = ::RegQueryValueEx(key, value_to_read, 0, &type, | 139 LONG result = ::RegQueryValueEx(key, value_to_read, 0, &type, |
103 reinterpret_cast<BYTE*>(value_out), &size); | 140 reinterpret_cast<BYTE*>(value_out), &size); |
104 | 141 |
105 ::RegCloseKey(key); | 142 ::RegCloseKey(key); |
106 | 143 |
107 return result == ERROR_SUCCESS && size == sizeof(*value_out); | 144 return result == ERROR_SUCCESS && size == sizeof(*value_out); |
108 } | 145 } |
109 | 146 |
147 struct LanguageAndCodePage { | |
148 WORD language; | |
149 WORD code_page; | |
150 }; | |
151 | |
152 bool GetLanguageAndCodePageFromVersionResource(const char* version_resource, | |
153 WORD* language, | |
154 WORD* code_page) { | |
155 if (!version_resource) | |
156 return false; | |
157 | |
158 LanguageAndCodePage* translation_info = nullptr; | |
159 uint32_t page_count = 0; | |
160 BOOL query_result = VerQueryValue(version_resource, | |
161 L"\\VarFileInfo\\Translation", | |
162 reinterpret_cast<void**>(&translation_info), | |
163 &page_count); | |
164 if (!query_result) | |
165 return false; | |
166 | |
167 *language = translation_info->language; | |
168 *code_page = translation_info->code_page; | |
169 return true; | |
170 } | |
171 | |
172 bool GetValueFromVersionResource(const char* version_resource, | |
173 const base::string16& name, | |
174 base::string16* value_str) { | |
175 DCHECK(value_str); | |
176 value_str->clear(); | |
177 | |
178 WORD language = 0; | |
179 WORD code_page = 0; | |
180 if (!GetLanguageAndCodePageFromVersionResource(version_resource, | |
181 &language, | |
182 &code_page)) { | |
183 return false; | |
184 } | |
185 | |
186 WORD lang_codepage[8] = {}; | |
187 size_t i = 0; | |
188 // Use the language and codepage | |
189 lang_codepage[i++] = language; | |
190 lang_codepage[i++] = code_page; | |
191 // Use the default language and codepage from the resource. | |
192 lang_codepage[i++] = ::GetUserDefaultLangID(); | |
193 lang_codepage[i++] = code_page; | |
194 // Use the language from the resource and Latin codepage (most common). | |
195 lang_codepage[i++] = language; | |
196 lang_codepage[i++] = 1252; | |
197 // Use the default language and Latin codepage (most common). | |
198 lang_codepage[i++] = ::GetUserDefaultLangID(); | |
199 lang_codepage[i++] = 1252; | |
200 | |
201 static_assert((arraysize(lang_codepage) % 2) == 0, | |
202 "Language code page size should be a multiple of 2"); | |
203 DCHECK_EQ(arraysize(lang_codepage), i); | |
204 | |
205 i = 0; | |
206 while (i < arraysize(lang_codepage)) { | |
207 wchar_t sub_block[MAX_PATH] = {}; | |
208 WORD language = lang_codepage[i++]; | |
209 WORD code_page = lang_codepage[i++]; | |
210 _snwprintf_s(sub_block, MAX_PATH, MAX_PATH, | |
211 L"\\StringFileInfo\\%04x%04x\\%ls", language, code_page, name.c_str()); | |
212 void* value = NULL; | |
213 uint32_t size = 0; | |
214 BOOL r = ::VerQueryValue(version_resource, sub_block, &value, &size); | |
215 if (r && value) { | |
216 value_str->assign(static_cast<wchar_t*>(value)); | |
217 return true; | |
218 } | |
219 } | |
220 return false; | |
221 } | |
222 | |
223 // Returns the executable path for the current process. | |
224 base::string16 GetCurrentProcessExePath() { | |
225 wchar_t exe_path[MAX_PATH] = {}; | |
226 if (GetModuleFileName(NULL, exe_path, arraysize(exe_path)) == 0) | |
227 return base::string16(); | |
228 return exe_path; | |
229 } | |
230 | |
231 // UTF8 to UTF16 and vice versa conversion helpers. We cannot use the base | |
232 // string conversion utilities here as they bring about a dependency on | |
233 // user32.dll which is not allowed in this file. | |
234 | |
235 // Convert a UTF16 string to an UTF8 string. | |
236 std::string utf16_to_utf8(const base::string16 &source) { | |
237 if (source.empty()) | |
238 return std::string(); | |
239 int size = ::WideCharToMultiByte(CP_UTF8, 0, &source[0], | |
240 static_cast<int>(source.size()), NULL, 0, NULL, NULL); | |
scottmg
2016/04/27 23:29:24
NULL -> nullptr and elsewhere.
ananta
2016/04/28 00:04:24
Done.
| |
241 std::string result(size, 0); | |
242 ::WideCharToMultiByte(CP_UTF8, 0, &source[0], | |
243 static_cast<int>(source.size()), &result[0], size, NULL, NULL); | |
scottmg
2016/04/27 23:29:24
Check that the return value is == size.
ananta
2016/04/28 00:04:24
Done.
| |
244 return result; | |
245 } | |
246 | |
247 // Convert a UTF8 string to a UTF16 string. | |
248 base::string16 utf8_to_string16(const std::string &source) { | |
249 if (source.empty()) | |
250 return base::string16(); | |
251 int size = ::MultiByteToWideChar(CP_UTF8, 0, &source[0], | |
252 static_cast<int>(source.size()), NULL, 0); | |
253 base::string16 result(size, 0); | |
254 ::MultiByteToWideChar(CP_UTF8, 0, &source[0], | |
255 static_cast<int>(source.size()), &result[0], size); | |
scottmg
2016/04/27 23:29:24
Check that the return value is == size.
ananta
2016/04/28 00:04:24
Done.
| |
256 return result; | |
257 } | |
258 | |
259 bool RecursiveDirectoryCreate(const base::string16& full_path) { | |
260 // If the path exists, we've succeeded if it's a directory, failed otherwise. | |
261 const wchar_t* full_path_str = full_path.c_str(); | |
262 DWORD file_attributes = ::GetFileAttributes(full_path_str); | |
263 if (file_attributes != INVALID_FILE_ATTRIBUTES) { | |
264 if ((file_attributes & FILE_ATTRIBUTE_DIRECTORY) != 0) { | |
265 DVLOG(1) << "RecursiveDirectoryCreate(" << full_path_str << "), " | |
266 << "directory already exists."; | |
267 return true; | |
268 } | |
269 DLOG(WARNING) << "RecursiveDirectoryCreate(" << full_path_str << "), " | |
270 << "conflicts with existing file."; | |
271 return false; | |
272 } | |
273 | |
274 // Invariant: Path does not exist as file or directory. | |
275 | |
276 // Attempt to create the parent recursively. This will immediately return | |
277 // true if it already exists, otherwise will create all required parent | |
278 // directories starting with the highest-level missing parent. | |
279 base::string16 parent_path; | |
280 std::size_t pos = full_path.find_last_of(L"/\\"); | |
281 if (pos != base::string16::npos) { | |
282 parent_path = full_path.substr(0, pos); | |
283 if (!RecursiveDirectoryCreate(parent_path)) { | |
284 DLOG(WARNING) << "Failed to create one of the parent directories."; | |
285 return false; | |
286 } | |
287 } | |
288 if (!::CreateDirectory(full_path_str, NULL)) { | |
289 DWORD error_code = ::GetLastError(); | |
290 if (error_code == ERROR_ALREADY_EXISTS) { | |
291 DWORD file_attributes = GetFileAttributes(full_path_str); | |
292 if ((file_attributes != INVALID_FILE_ATTRIBUTES) && | |
293 ((file_attributes & FILE_ATTRIBUTE_DIRECTORY) != 0)) { | |
294 // This error code ERROR_ALREADY_EXISTS doesn't indicate whether we | |
295 // were racing with someone creating the same directory, or a file | |
296 // with the same path. If the directory exists, we lost the | |
297 // race to create the same directory. | |
298 return true; | |
299 } else { | |
300 DLOG(WARNING) << "Failed to create directory " << full_path_str | |
301 << ", last error is " << error_code << "."; | |
302 return false; | |
303 } | |
304 } | |
305 } | |
306 return true; | |
307 } | |
308 | |
110 } // namespace | 309 } // namespace |
111 | 310 |
112 bool IsCanary(const wchar_t* exe_path) { | 311 bool IsCanary(const wchar_t* exe_path) { |
113 return wcsstr(exe_path, L"Chrome SxS\\Application") != NULL; | 312 return wcsstr(exe_path, L"Chrome SxS\\Application") != NULL; |
114 } | 313 } |
115 | 314 |
116 bool IsSystemInstall(const wchar_t* exe_path) { | 315 bool IsSystemInstall(const wchar_t* exe_path) { |
117 wchar_t program_dir[MAX_PATH] = {}; | 316 wchar_t program_dir[MAX_PATH] = {}; |
118 DWORD ret = ::GetEnvironmentVariable(L"PROGRAMFILES", program_dir, | 317 DWORD ret = ::GetEnvironmentVariable(L"PROGRAMFILES", program_dir, |
119 arraysize(program_dir)); | 318 arraysize(program_dir)); |
(...skipping 11 matching lines...) Expand all Loading... | |
131 bool IsMultiInstall(bool is_system_install) { | 330 bool IsMultiInstall(bool is_system_install) { |
132 base::string16 args; | 331 base::string16 args; |
133 if (!ReadKeyValueString(is_system_install, kRegPathClientState, | 332 if (!ReadKeyValueString(is_system_install, kRegPathClientState, |
134 kAppGuidGoogleChrome, kUninstallArgumentsField, | 333 kAppGuidGoogleChrome, kUninstallArgumentsField, |
135 &args)) { | 334 &args)) { |
136 return false; | 335 return false; |
137 } | 336 } |
138 return args.find(L"--multi-install") != base::string16::npos; | 337 return args.find(L"--multi-install") != base::string16::npos; |
139 } | 338 } |
140 | 339 |
141 bool AreUsageStatsEnabled(const wchar_t* exe_path) { | 340 bool GetCollectStatsConsent() { |
341 return GetCollectStatsConsentForExe(GetCurrentProcessExePath()); | |
342 } | |
343 | |
344 bool GetCollectStatsConsentForExe(const base::string16& exe_path) { | |
142 bool enabled = true; | 345 bool enabled = true; |
143 bool controlled_by_policy = ReportingIsEnforcedByPolicy(&enabled); | 346 bool controlled_by_policy = ReportingIsEnforcedByPolicy(&enabled); |
144 | 347 |
145 if (controlled_by_policy && !enabled) | 348 if (controlled_by_policy && !enabled) |
146 return false; | 349 return false; |
147 | 350 |
148 bool system_install = IsSystemInstall(exe_path); | 351 bool system_install = IsSystemInstall(exe_path.c_str()); |
149 base::string16 app_guid; | 352 base::string16 app_guid; |
150 | 353 |
151 if (IsCanary(exe_path)) { | 354 if (IsCanary(exe_path.c_str())) { |
152 app_guid = kAppGuidCanary; | 355 app_guid = kAppGuidCanary; |
153 } else { | 356 } else { |
154 app_guid = IsMultiInstall(system_install) ? kAppGuidGoogleBinaries : | 357 app_guid = IsMultiInstall(system_install) ? kAppGuidGoogleBinaries : |
155 kAppGuidGoogleChrome; | 358 kAppGuidGoogleChrome; |
156 } | 359 } |
157 | 360 |
158 DWORD out_value = 0; | 361 DWORD out_value = 0; |
159 if (system_install && | 362 if (system_install && |
160 ReadKeyValueDW(system_install, kRegPathClientStateMedium, app_guid, | 363 ReadKeyValueDW(system_install, kRegPathClientStateMedium, app_guid, |
161 kRegValueUsageStats, &out_value)) { | 364 kRegValueUsageStats, &out_value)) { |
162 return out_value == 1; | 365 return out_value == 1; |
163 } | 366 } |
164 | 367 |
165 return ReadKeyValueDW(system_install, kRegPathClientState, app_guid, | 368 return ReadKeyValueDW(system_install, kRegPathClientState, app_guid, |
166 kRegValueUsageStats, &out_value) && out_value == 1; | 369 kRegValueUsageStats, &out_value) && out_value == 1; |
167 } | 370 } |
168 | 371 |
169 bool ReportingIsEnforcedByPolicy(bool* breakpad_enabled) { | 372 bool ReportingIsEnforcedByPolicy(bool* breakpad_enabled) { |
170 HKEY key = NULL; | 373 HKEY key = NULL; |
171 DWORD value = 0; | 374 DWORD value = 0; |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
215 return; | 418 return; |
216 } | 419 } |
217 | 420 |
218 g_process_type = ProcessType::BROWSER_PROCESS; | 421 g_process_type = ProcessType::BROWSER_PROCESS; |
219 } | 422 } |
220 | 423 |
221 bool IsNonBrowserProcess() { | 424 bool IsNonBrowserProcess() { |
222 assert(g_process_type != ProcessType::UNINITIALIZED); | 425 assert(g_process_type != ProcessType::UNINITIALIZED); |
223 return g_process_type == ProcessType::NON_BROWSER_PROCESS; | 426 return g_process_type == ProcessType::NON_BROWSER_PROCESS; |
224 } | 427 } |
428 | |
429 bool GetDefaultUserDataDirectory(base::string16* result) { | |
430 scoped_ptr<wchar_t> user_data_dir_path; | |
431 | |
432 // This environment variable should be set on Windows 7 and up. | |
433 // If we fail to find this variable then we default to the temporary files | |
434 // path. | |
435 DWORD size = ::ExpandEnvironmentStrings(L"%LOCALAPPDATA%", nullptr, 0); | |
scottmg
2016/04/27 23:29:25
static const wchar_t kLocalAppData[] = L"%LOCALAPP
ananta
2016/04/28 00:04:24
Done.
| |
436 if (size) { | |
437 user_data_dir_path.reset(new wchar_t[size]); | |
438 if (::ExpandEnvironmentStrings(L"%LOCALAPPDATA%", | |
439 user_data_dir_path.get(), | |
440 size) != size) { | |
441 user_data_dir_path.reset(nullptr); | |
442 } | |
443 } | |
444 // We failed to find the %LOCALAPPDATA% folder. Fallback to the temporary | |
445 // files path. If we fail to find this we bail. | |
446 if (!user_data_dir_path.get()) { | |
447 size = ::GetTempPath(0, nullptr); | |
448 if (!size) | |
449 return false; | |
450 user_data_dir_path.reset(new wchar_t[size + 1]); | |
451 if (::GetTempPath(size + 1, user_data_dir_path.get()) != size) | |
452 return false; | |
453 } | |
454 | |
455 base::string16 install_sub_directory = GetChromeInstallSubDirectory(); | |
456 | |
457 result->append(user_data_dir_path.get()); | |
scottmg
2016/04/27 23:29:24
result = user_data_dir_path here to be more clear
ananta
2016/04/28 00:04:25
Done.
| |
458 result->append(L"\\"); | |
scottmg
2016/04/27 23:29:24
GetTempPath() says it returns with a trailing \, w
ananta
2016/04/28 00:04:25
Yeah. Added a check for that.
| |
459 result->append(install_sub_directory); | |
460 result->append(L"\\"); | |
461 result->append(kUserDataDirname); | |
462 return true; | |
463 } | |
464 | |
465 bool GetDefaultCrashDumpLocation(base::string16* crash_dir) { | |
466 // In order to be able to start crash handling very early, we do not rely on | |
467 // chrome's PathService entries (for DIR_CRASH_DUMPS) being available on | |
468 // Windows. See https://crbug.com/564398. | |
469 if (!GetDefaultUserDataDirectory(crash_dir)) | |
470 return false; | |
471 | |
472 // We have to make sure the user data dir exists on first run. See | |
473 // http://crbug.com/591504. | |
474 if ((::GetFileAttributes(crash_dir->c_str()) == INVALID_FILE_ATTRIBUTES) && | |
scottmg
2016/04/27 23:29:24
Probably no need to check that it exists first alr
ananta
2016/04/28 00:04:24
Done.
| |
475 (!RecursiveDirectoryCreate(crash_dir->c_str()))) { | |
476 return false; | |
477 } | |
478 crash_dir->append(L"\\CrashPad"); | |
scottmg
2016/04/27 23:29:24
"Crashpad", not "CrashPad".
ananta
2016/04/28 00:04:24
Done.
| |
479 return true; | |
480 } | |
481 | |
482 | |
483 std::string GetEnvironmentString(const std::string& variable_name) { | |
484 DWORD value_length = ::GetEnvironmentVariable( | |
485 utf8_to_string16(variable_name).c_str(), NULL, 0); | |
486 if (value_length == 0) | |
487 return std::string(); | |
488 std::unique_ptr<wchar_t[]> value(new wchar_t[value_length]); | |
489 ::GetEnvironmentVariable(utf8_to_string16(variable_name).c_str(), | |
490 value.get(), value_length); | |
491 return utf16_to_utf8(value.get()); | |
492 } | |
493 | |
494 bool SetEnvironmentString(const std::string& variable_name, | |
495 const std::string& new_value) { | |
496 return !!SetEnvironmentVariable(utf8_to_string16(variable_name).c_str(), | |
497 utf8_to_string16(new_value).c_str()); | |
498 } | |
499 | |
500 bool HasEnvironmentVariable(const std::string& variable_name) { | |
501 return !!::GetEnvironmentVariable(utf8_to_string16(variable_name).c_str(), | |
502 NULL, 0); | |
503 } | |
504 | |
505 bool GetExecutableVersionDetails(const base::string16& exe_path, | |
506 base::string16* product_name, | |
507 base::string16* version, | |
508 base::string16* special_build, | |
509 base::string16* channel_name) { | |
510 DCHECK(product_name); | |
511 DCHECK(version); | |
512 DCHECK(special_build); | |
513 DCHECK(channel_name); | |
514 | |
515 // Default values in case we don't find a version resource. | |
516 *product_name = L"Chrome"; | |
517 *version = L"0.0.0.0-devel"; | |
518 *channel_name = kChromeChannelUnknown; | |
519 special_build->clear(); | |
520 | |
521 DWORD dummy = 0; | |
522 DWORD length = ::GetFileVersionInfoSize(exe_path.c_str(), &dummy); | |
523 if (length) { | |
524 scoped_ptr<char> data(new char[length]); | |
525 if (::GetFileVersionInfo(exe_path.c_str(), dummy, length, | |
526 data.get())) { | |
527 GetValueFromVersionResource(data.get(), L"ProductVersion", version); | |
528 | |
529 base::string16 official_build; | |
530 GetValueFromVersionResource(data.get(), L"Official Build", | |
531 &official_build); | |
532 if (official_build != L"1") | |
533 version->append(L"-devel"); | |
534 GetValueFromVersionResource(data.get(), L"ProductShortName", | |
535 product_name); | |
536 GetValueFromVersionResource(data.get(), L"SpecialBuild", special_build); | |
537 } | |
538 } | |
539 GetChromeChannelName(!IsSystemInstall(exe_path.c_str()), channel_name); | |
540 return true; | |
541 } | |
542 | |
543 void GetChromeChannelName(bool is_per_user_install, | |
544 base::string16* channel_name) { | |
545 #if !defined(GOOGLE_CHROME_BUILD) | |
546 *channel_name = kChromeChannelUnknown; | |
547 return; | |
548 #else | |
549 channel_name->clear(); | |
550 // TODO(ananta) | |
551 // http://crbug.com/604923 | |
552 // Unify this with the chrome/installer/util/channel_info.h/.cc. | |
553 if (IsCanary(GetCurrentProcessExePath().c_str())) { | |
554 *channel_name = L"canary"; | |
555 } else { | |
556 base::string16 value; | |
557 if (ReadKeyValueString(!is_per_user_install, | |
558 kRegPathClientState, | |
559 kAppGuidGoogleChrome, | |
560 kRegApField, | |
561 &value)) { | |
562 static const wchar_t kChromeChannelBetaPattern[] = L"1?1-"; | |
563 static const wchar_t kChromeChannelBetaX64Pattern[] = L"*x64-beta*"; | |
564 static const wchar_t kChromeChannelDevPattern[] = L"2?0-d"; | |
565 static const wchar_t kChromeChannelDevX64Pattern[] = L"x64-dev"; | |
566 static const wchar_t kChromeStableMultiInstallPattern[] = L"*multi*"; | |
567 | |
568 std::transform(value.begin(), value.end(), value.begin(), ::tolower); | |
569 | |
570 // Channel names containing stable should be reported as an empty string. | |
571 if (value.find(kChromeChannelStableExplicit) != | |
572 base::string16::npos) { | |
573 return; | |
574 } | |
575 | |
576 if (base::MatchPattern(value, kChromeChannelDevPattern) || | |
577 base::MatchPattern(value, kChromeChannelDevX64Pattern)) { | |
578 channel_name->assign(kChromeChannelDev); | |
579 } | |
580 | |
581 if (base::MatchPattern(value, kChromeChannelBetaPattern) || | |
582 base::MatchPattern(value, kChromeChannelBetaPattern)) { | |
583 channel_name->assign(kChromeChannelBeta); | |
584 } | |
585 | |
586 if (value.find(kChromeStableMultiInstallPattern) != | |
587 base::string16::npos) { | |
588 *channel_name = L"-m"; | |
589 } | |
590 | |
591 // If we fail to find any matching pattern in the channel name then we | |
592 // default to empty which means stable. Not sure if this is ok. | |
593 // TODO(ananta) | |
594 // http://crbug.com/604923 | |
595 // Check if this is ok. | |
596 } else { | |
597 *channel_name = kChromeChannelUnknown; | |
598 } | |
599 } | |
600 #endif // GOOGLE_CHROME_BUILD | |
601 } | |
602 | |
603 std::string GetGoogleUpdateVersion() { | |
604 base::string16 update_version; | |
605 if (ReadKeyValueString(IsSystemInstall(GetCurrentProcessExePath().c_str()), | |
606 kRegPathGoogleUpdate, | |
607 L"", | |
608 kRegGoogleUpdateVersion, | |
609 &update_version)) { | |
610 return utf16_to_utf8(update_version); | |
611 } | |
612 return std::string(); | |
613 } | |
614 | |
615 base::string16 GetChromeInstallSubDirectory() { | |
616 base::string16 result; | |
617 #if defined(GOOGLE_CHROME_BUILD) | |
618 base::string16 sub_directory = kGoogleChromeInstallSubDir1; | |
619 sub_directory += L"\\"; | |
620 sub_directory += kGoogleChromeInstallSubDir2; | |
621 if (IsCanary(GetCurrentProcessExePath().c_str())) | |
622 sub_directory += kSxSSuffix; | |
623 result.append(sub_directory); | |
624 #else | |
625 result = result.append(kChromiumInstallSubDir); | |
626 #endif | |
627 return result; | |
628 } | |
629 | |
630 base::string16 GetBrowserCrashDumpAttemptsRegistryPath() { | |
631 base::string16 registry_path = L"Software"; | |
632 base::string16 install_sub_directory = GetChromeInstallSubDirectory(); | |
633 registry_path += install_sub_directory; | |
634 registry_path += kBrowserCrashDumpMetricsSubKey; | |
635 return registry_path; | |
636 } | |
637 | |
638 } // namespace install_static | |
OLD | NEW |