Chromium Code Reviews| Index: chrome_elf/chrome_elf_util.cc |
| diff --git a/chrome_elf/chrome_elf_util.cc b/chrome_elf/chrome_elf_util.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..7e372f867558ff7bda506431f7b72d80dbd5433a |
| --- /dev/null |
| +++ b/chrome_elf/chrome_elf_util.cc |
| @@ -0,0 +1,198 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome_elf/chrome_elf_util.h" |
| + |
| +#include <windows.h> |
| + |
| +#include "base/macros.h" |
| +#include "base/strings/string16.h" |
| + |
| +namespace { |
| + |
| +const wchar_t kRegPathClientState[] = L"Software\\Google\\Update\\ClientState"; |
| +const wchar_t kRegPathClientStateMedium[] = |
| + L"Software\\Google\\Update\\ClientStateMedium"; |
| +#if defined(GOOGLE_CHROME_BUILD) |
| +const wchar_t kRegPathChromePolicy[] = L"SOFTWARE\\Policies\\Google\\Chrome"; |
| +#else |
| +const wchar_t kRegPathChromePolicy[] = L"SOFTWARE\\Policies\\Chromium"; |
| +#endif // defined(GOOGLE_CHROME_BUILD) |
| + |
| +const wchar_t kRegValueUsageStats[] = L"usagestats"; |
| +const wchar_t kUninstallArgumentsField[] = L"UninstallArguments"; |
| +const wchar_t kMetricsReportingEnabled[] =L"MetricsReportingEnabled"; |
| + |
| +const wchar_t kAppGuidCanary[] = |
| + L"{4ea16ac7-fd5a-47c3-875b-dbf4a2008c20}"; |
| +const wchar_t kAppGuidGoogleChrome[] = |
| + L"{8A69D345-D564-463c-AFF1-A69D9E530F96}"; |
| +const wchar_t kAppGuidGoogleBinaries[] = |
| + L"{4DC8B4CA-1BDA-483e-B5FA-D3C12E15B62D}"; |
| + |
| +bool ReadKeyValueString(bool system_install, const wchar_t* key_path, |
| + const wchar_t* guid, const wchar_t* value_to_read, |
| + base::string16* value_out) { |
| + HKEY h_key = NULL; |
| + value_out->clear(); |
| + |
| + base::string16 full_key_path(key_path); |
| + full_key_path.append(1, L'\\'); |
| + full_key_path.append(guid); |
| + |
| + if (::RegOpenKeyEx(system_install ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER, |
| + full_key_path.c_str(), 0, |
| + KEY_QUERY_VALUE | KEY_WOW64_32KEY, &h_key) != |
| + ERROR_SUCCESS) { |
| + return false; |
| + } |
| + |
| + const size_t kMaxStringLength = 1024; |
| + wchar_t raw_value[kMaxStringLength] = {}; |
| + DWORD size = sizeof(raw_value); |
| + DWORD type = REG_SZ; |
| + LONG result = ::RegQueryValueEx(h_key, value_to_read, 0, &type, |
| + reinterpret_cast<LPBYTE>(raw_value), &size); |
| + |
| + if (result == ERROR_SUCCESS) { |
| + if (type != REG_SZ || (size & 1) != 0) { |
| + result = ERROR_NOT_SUPPORTED; |
| + } else if (size == 0) { |
| + *raw_value = L'\0'; |
| + } else if (raw_value[size/sizeof(wchar_t) - 1] != L'\0') { |
| + if ((size / sizeof(wchar_t)) < kMaxStringLength) |
| + raw_value[size / sizeof(wchar_t)] = L'\0'; |
| + else |
| + result = ERROR_MORE_DATA; |
| + } |
| + } |
| + |
| + *value_out = raw_value; |
|
grt (UTC plus 2)
2014/02/17 20:49:39
only make this assignment when result == ERROR_SUC
Cait (Slow)
2014/02/18 23:03:12
Done.
|
| + |
| + ::RegCloseKey(h_key); |
| + |
| + return result == ERROR_SUCCESS; |
| +} |
| + |
| +bool ReadKeyValueDW(bool system_install, const wchar_t* key_path, |
| + base::string16 guid, const wchar_t* value_to_read, |
| + DWORD* value_out) { |
| + HKEY h_key = NULL; |
| + *value_out = 0; |
| + |
| + base::string16 full_key_path(key_path); |
| + full_key_path.append(1, L'\\'); |
| + full_key_path.append(guid); |
| + |
| + if (::RegOpenKeyEx(system_install ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER, |
| + full_key_path.c_str(), 0, |
| + KEY_QUERY_VALUE | KEY_WOW64_32KEY, &h_key) != |
| + ERROR_SUCCESS) { |
| + return false; |
| + } |
| + |
| + DWORD size = sizeof(DWORD); |
|
grt (UTC plus 2)
2014/02/17 20:49:39
sizeof(*value_out)
Cait (Slow)
2014/02/18 23:03:12
Done.
|
| + DWORD type = REG_DWORD; |
| + LONG result = ::RegQueryValueEx(h_key, value_to_read, 0, &type, |
| + reinterpret_cast<BYTE*>(value_out), &size); |
| + |
| + ::RegCloseKey(h_key); |
| + |
| + return result == ERROR_SUCCESS; |
|
grt (UTC plus 2)
2014/02/17 20:49:39
result == ERROR_SUCCESS && size == sizeof(*value_o
Cait (Slow)
2014/02/18 23:03:12
Done.
|
| +} |
| + |
| +} // namespace |
| + |
| +bool IsCanary(const wchar_t* exe_path) { |
| + return wcsstr(exe_path, L"Chrome SxS\\Application") != NULL; |
| +} |
| + |
| +bool IsSystemInstall(const wchar_t* exe_path) { |
| + wchar_t program_dir[MAX_PATH] = {}; |
| + DWORD ret = ::GetEnvironmentVariable(L"PROGRAMFILES", program_dir, |
| + arraysize(program_dir)); |
| + if (ret && ret < MAX_PATH && !wcsncmp(exe_path, program_dir, ret)) |
| + return true; |
| + |
| + ret = ::GetEnvironmentVariable(L"PROGRAMFILES(X86)", program_dir, |
| + arraysize(program_dir)); |
| + if (ret && ret < MAX_PATH && !wcsncmp(exe_path, program_dir, ret)) |
| + return true; |
| + |
| + return false; |
| +} |
| + |
| +bool IsMultiInstall(bool is_system_install) { |
| + base::string16 args; |
| + if (!ReadKeyValueString(is_system_install, kRegPathClientState, |
| + kAppGuidGoogleChrome, kUninstallArgumentsField, |
| + &args)) { |
| + return false; |
| + } |
| + return args.find(L"--multi-install") != base::string16::npos; |
| +} |
| + |
| +bool AreUsageStatsEnabled(const wchar_t* exe_path) { |
| +#if !defined(GOOGLE_CHROME_BUILD) |
| + return false; |
| +#else |
| + bool enabled = true; |
| + bool controlled_by_policy = ReportingIsEnforcedByPolicy(&enabled); |
| + |
| + if (controlled_by_policy && !enabled) |
| + return false; |
| + |
| + // TODO(caitkp): If enabled by policy but manually disabled, what happens? |
| + |
| + bool system_install = IsSystemInstall(exe_path); |
| + base::string16 app_guid; |
| + |
| + if (IsCanary(exe_path)) { |
| + app_guid = kAppGuidCanary; |
| + } else { |
| + app_guid = IsMultiInstall(system_install) ? kAppGuidGoogleBinaries : |
| + kAppGuidGoogleChrome; |
| + } |
| + |
| + DWORD out_value = 0; |
| + if (system_install && |
| + ReadKeyValueDW(system_install, kRegPathClientStateMedium, app_guid, |
| + kRegValueUsageStats, &out_value)) { |
| + return out_value == 1; |
| + } |
| + |
| + return ReadKeyValueDW(system_install, kRegPathClientState, app_guid, |
| + kRegValueUsageStats, &out_value) && out_value == 1; |
| +#endif // defined(GOOGLE_CHROME_BUILD) |
| +} |
| + |
| +bool ReportingIsEnforcedByPolicy(bool* breakpad_enabled) { |
| + HKEY h_key = NULL; |
|
grt (UTC plus 2)
2014/02/17 20:49:39
don't prefix this with h_ (and elsewhere in this f
|
| + DWORD value = 0; |
| + BYTE* value_bytes = reinterpret_cast<BYTE*>(value); |
|
grt (UTC plus 2)
2014/02/17 20:49:39
value -> &value
Cait (Slow)
2014/02/18 23:03:12
Done.
|
| + DWORD size = sizeof(DWORD); |
|
grt (UTC plus 2)
2014/02/17 20:49:39
sizeof(value)
Cait (Slow)
2014/02/18 23:03:12
Done.
|
| + DWORD type = REG_DWORD; |
| + |
| + if (::RegOpenKeyEx(HKEY_LOCAL_MACHINE, kRegPathChromePolicy, 0, |
|
grt (UTC plus 2)
2014/02/17 20:49:39
be sure to close the key regardless as to whether
Cait (Slow)
2014/02/18 23:03:12
Done.
|
| + KEY_READ | KEY_WOW64_32KEY, &h_key) == ERROR_SUCCESS && |
|
grt (UTC plus 2)
2014/02/17 20:49:39
KEY_WOW64_32KEY is not called for here.
grt (UTC plus 2)
2014/02/17 20:49:39
KEY_READ -> KEY_QUERY_VALUE
Cait (Slow)
2014/02/18 23:03:12
Done.
Cait (Slow)
2014/02/18 23:03:12
Done.
|
| + ::RegQueryValueEx(h_key, kMetricsReportingEnabled, 0, &type, |
| + value_bytes, &size) == ERROR_SUCCESS) { |
| + *breakpad_enabled = value != 0; |
| + ::RegCloseKey(h_key); |
| + return true; |
| + } |
| + |
| + if (::RegOpenKeyEx(HKEY_CURRENT_USER, kRegPathChromePolicy, 0, |
| + KEY_READ | KEY_WOW64_32KEY, &h_key) == ERROR_SUCCESS && |
| + ::RegQueryValueEx(h_key, kMetricsReportingEnabled, 0, &type, |
| + value_bytes, &size) == ERROR_SUCCESS) { |
| + *breakpad_enabled = value != 0; |
| + ::RegCloseKey(h_key); |
| + return true; |
| + } |
| + |
| + return false; |
| +} |
| + |
| + |
|
grt (UTC plus 2)
2014/02/17 20:49:39
nit: remove extra newlines at end of file
Cait (Slow)
2014/02/18 23:03:12
Done.
|