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

Unified Diff: chrome_elf/chrome_elf_util.cc

Issue 1841573002: [Chrome ELF] New NT registry API. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Clean up OverrideRegistry function. Created 4 years, 8 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 side-by-side diff with in-line comments
Download patch
Index: chrome_elf/chrome_elf_util.cc
diff --git a/chrome_elf/chrome_elf_util.cc b/chrome_elf/chrome_elf_util.cc
index a3fd727e9d2ebdf54d795bf3bd3b76e0d97bca86..29c42ee338471345709d7d09ae8854d743c2a065 100644
--- a/chrome_elf/chrome_elf_util.cc
+++ b/chrome_elf/chrome_elf_util.cc
@@ -5,11 +5,13 @@
#include "chrome_elf/chrome_elf_util.h"
#include <assert.h>
-#include <windows.h>
#include <stddef.h>
+#include <windows.h>
#include "base/macros.h"
#include "base/strings/string16.h"
+#include "chrome_elf/chrome_elf_constants.h"
+#include "chrome_elf/chrome_elf_reg.h"
ProcessType g_process_type = ProcessType::UNINITIALIZED;
@@ -26,7 +28,7 @@ const wchar_t kRegPathChromePolicy[] = L"SOFTWARE\\Policies\\Chromium";
const wchar_t kRegValueUsageStats[] = L"usagestats";
const wchar_t kUninstallArgumentsField[] = L"UninstallArguments";
-const wchar_t kMetricsReportingEnabled[] =L"MetricsReportingEnabled";
+const wchar_t kMetricsReportingEnabled[] = L"MetricsReportingEnabled";
const wchar_t kAppGuidCanary[] =
L"{4ea16ac7-fd5a-47c3-875b-dbf4a2008c20}";
@@ -35,94 +37,22 @@ const wchar_t kAppGuidGoogleChrome[] =
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 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, &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(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;
- }
- }
-
- if (result == ERROR_SUCCESS)
- *value_out = raw_value;
-
- ::RegCloseKey(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 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, &key) !=
- ERROR_SUCCESS) {
- return false;
- }
-
- DWORD size = sizeof(*value_out);
- DWORD type = REG_DWORD;
- LONG result = ::RegQueryValueEx(key, value_to_read, 0, &type,
- reinterpret_cast<BYTE*>(value_out), &size);
-
- ::RegCloseKey(key);
-
- return result == ERROR_SUCCESS && size == sizeof(*value_out);
-}
-
} // namespace
bool IsCanary(const wchar_t* exe_path) {
- return wcsstr(exe_path, L"Chrome SxS\\Application") != NULL;
+ 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))
+ 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))
+ if (ret && ret < MAX_PATH && !::wcsncmp(exe_path, program_dir, ret))
return true;
return false;
@@ -130,12 +60,16 @@ bool IsSystemInstall(const wchar_t* exe_path) {
bool IsMultiInstall(bool is_system_install) {
base::string16 args;
- if (!ReadKeyValueString(is_system_install, kRegPathClientState,
- kAppGuidGoogleChrome, kUninstallArgumentsField,
- &args)) {
+
+ base::string16 full_key_path(kRegPathClientState);
+ full_key_path.append(1, L'\\');
+ full_key_path.append(kAppGuidGoogleChrome);
+ if (!nt::GetRegValue_SZ((is_system_install ? nt::HKLM : nt::HKCU),
+ full_key_path.c_str(), kUninstallArgumentsField,
+ &args))
return false;
- }
- return args.find(L"--multi-install") != base::string16::npos;
+
+ return (args.find(L"--multi-install") != base::string16::npos);
}
bool AreUsageStatsEnabled(const wchar_t* exe_path) {
@@ -156,41 +90,42 @@ bool AreUsageStatsEnabled(const wchar_t* exe_path) {
}
DWORD out_value = 0;
+
+ // If system_install, first try kRegPathClientStateMedium.
+ base::string16 full_key_path(kRegPathClientStateMedium);
+ full_key_path.append(1, L'\\');
+ full_key_path.append(app_guid);
if (system_install &&
- ReadKeyValueDW(system_install, kRegPathClientStateMedium, app_guid,
- kRegValueUsageStats, &out_value)) {
- return out_value == 1;
+ nt::GetRegValue_DWORD(nt::HKLM, full_key_path.c_str(),
+ kRegValueUsageStats, &out_value)) {
+ return (out_value == 1);
}
- return ReadKeyValueDW(system_install, kRegPathClientState, app_guid,
- kRegValueUsageStats, &out_value) && out_value == 1;
+ // Second, try kRegPathClientState.
+ full_key_path = kRegPathClientState;
+ full_key_path.append(1, L'\\');
+ full_key_path.append(app_guid);
+ return (nt::GetRegValue_DWORD((system_install ? nt::HKLM : nt::HKCU),
+ full_key_path.c_str(), kRegValueUsageStats,
+ &out_value) &&
+ out_value == 1);
}
bool ReportingIsEnforcedByPolicy(bool* breakpad_enabled) {
- HKEY key = NULL;
DWORD value = 0;
- BYTE* value_bytes = reinterpret_cast<BYTE*>(&value);
- DWORD size = sizeof(value);
- DWORD type = REG_DWORD;
-
- if (::RegOpenKeyEx(HKEY_LOCAL_MACHINE, kRegPathChromePolicy, 0,
- KEY_QUERY_VALUE, &key) == ERROR_SUCCESS) {
- if (::RegQueryValueEx(key, kMetricsReportingEnabled, 0, &type,
- value_bytes, &size) == ERROR_SUCCESS) {
- *breakpad_enabled = value != 0;
- }
- ::RegCloseKey(key);
- return size == sizeof(value);
+
+ // First, try HKLM.
+ if (nt::GetRegValue_DWORD(nt::HKLM, kRegPathChromePolicy,
+ kMetricsReportingEnabled, &value)) {
+ *breakpad_enabled = (value != 0);
+ return true;
}
- if (::RegOpenKeyEx(HKEY_CURRENT_USER, kRegPathChromePolicy, 0,
- KEY_QUERY_VALUE, &key) == ERROR_SUCCESS) {
- if (::RegQueryValueEx(key, kMetricsReportingEnabled, 0, &type,
- value_bytes, &size) == ERROR_SUCCESS) {
- *breakpad_enabled = value != 0;
- }
- ::RegCloseKey(key);
- return size == sizeof(value);
+ // Second, try HKCU.
+ if (nt::GetRegValue_DWORD(nt::HKCU, kRegPathChromePolicy,
+ kMetricsReportingEnabled, &value)) {
+ *breakpad_enabled = (value != 0);
+ return true;
}
return false;

Powered by Google App Engine
This is Rietveld 408576698