From c393a5b46e49e17ed064cf6074ea43a06cffaf41 Mon Sep 17 00:00:00 2001 From: caitkp Date: Thu, 14 May 2015 12:56:33 -0700 Subject: Reland Issue 1132473003: Cache IsNonBrowserProcess so we only take a loader-lock the first time. BUG=485656, 477137 Review URL: https://codereview.chromium.org/1144433004 Cr-Commit-Position: refs/heads/master@{#329911} --- .../blacklist/test/blacklist_test_main_dll.cc | 5 ++++- chrome_elf/chrome_elf_main.cc | 2 ++ chrome_elf/chrome_elf_util.cc | 26 +++++++++++++++++----- chrome_elf/chrome_elf_util.h | 13 +++++++++++ chrome_elf/chrome_elf_util_unittest.cc | 6 +++++ 5 files changed, 45 insertions(+), 7 deletions(-) (limited to 'chrome_elf') diff --git a/chrome_elf/blacklist/test/blacklist_test_main_dll.cc b/chrome_elf/blacklist/test/blacklist_test_main_dll.cc index 54e5eb8..8c01d55 100644 --- a/chrome_elf/blacklist/test/blacklist_test_main_dll.cc +++ b/chrome_elf/blacklist/test/blacklist_test_main_dll.cc @@ -5,11 +5,14 @@ #include #include "chrome_elf/blacklist/blacklist.h" +#include "chrome_elf/chrome_elf_util.h" -extern "C" void InitBlacklistTestDll() {} +extern "C" void InitBlacklistTestDll() { +} BOOL APIENTRY DllMain(HMODULE module, DWORD reason, LPVOID reserved) { if (reason == DLL_PROCESS_ATTACH) { + InitializeProcessType(); blacklist::Initialize(true); // force always on, no beacon } diff --git a/chrome_elf/chrome_elf_main.cc b/chrome_elf/chrome_elf_main.cc index 989493a..52212cc 100644 --- a/chrome_elf/chrome_elf_main.cc +++ b/chrome_elf/chrome_elf_main.cc @@ -8,6 +8,7 @@ #include "chrome_elf/blacklist/blacklist.h" #include "chrome_elf/breakpad.h" +#include "chrome_elf/chrome_elf_util.h" #include "chrome_elf/ntdll_cache.h" void SignalChromeElf() { @@ -16,6 +17,7 @@ void SignalChromeElf() { BOOL APIENTRY DllMain(HMODULE module, DWORD reason, LPVOID reserved) { if (reason == DLL_PROCESS_ATTACH) { + InitializeProcessType(); InitializeCrashReporting(); __try { diff --git a/chrome_elf/chrome_elf_util.cc b/chrome_elf/chrome_elf_util.cc index a547d0b..7e2e710 100644 --- a/chrome_elf/chrome_elf_util.cc +++ b/chrome_elf/chrome_elf_util.cc @@ -4,11 +4,14 @@ #include "chrome_elf/chrome_elf_util.h" +#include #include #include "base/macros.h" #include "base/strings/string16.h" +ProcessType g_process_type = ProcessType::UNINITIALIZED; + namespace { const wchar_t kRegPathClientState[] = L"Software\\Google\\Update\\ClientState"; @@ -192,18 +195,29 @@ bool ReportingIsEnforcedByPolicy(bool* breakpad_enabled) { return false; } -bool IsNonBrowserProcess() { +void InitializeProcessType() { + assert(g_process_type == ProcessType::UNINITIALIZED); typedef bool (*IsSandboxedProcessFunc)(); IsSandboxedProcessFunc is_sandboxed_process_func = reinterpret_cast( GetProcAddress(GetModuleHandle(NULL), "IsSandboxedProcess")); - bool is_sandboxed_process = - is_sandboxed_process_func && is_sandboxed_process_func(); + if (is_sandboxed_process_func && is_sandboxed_process_func()) { + g_process_type = ProcessType::NON_BROWSER_PROCESS; + return; + } // TODO(robertshield): Drop the command line check when we drop support for // enabling chrome_elf in unsandboxed processes. - wchar_t* command_line = GetCommandLine(); - bool has_process_type_flag = command_line && wcsstr(command_line, L"--type"); + const wchar_t* command_line = GetCommandLine(); + if (command_line && wcsstr(command_line, L"--type")) { + g_process_type = ProcessType::NON_BROWSER_PROCESS; + return; + } - return (has_process_type_flag || is_sandboxed_process); + g_process_type = ProcessType::BROWSER_PROCESS; +} + +bool IsNonBrowserProcess() { + assert(g_process_type != ProcessType::UNINITIALIZED); + return g_process_type == ProcessType::NON_BROWSER_PROCESS; } diff --git a/chrome_elf/chrome_elf_util.h b/chrome_elf/chrome_elf_util.h index e87dc7f..673943d 100644 --- a/chrome_elf/chrome_elf_util.h +++ b/chrome_elf/chrome_elf_util.h @@ -7,6 +7,12 @@ #include "base/strings/string16.h" +enum class ProcessType { + UNINITIALIZED, + NON_BROWSER_PROCESS, + BROWSER_PROCESS, +}; + // Returns true if |exe_path| points to a Chrome installed in an SxS // installation. bool IsCanary(const wchar_t* exe_path); @@ -24,8 +30,15 @@ bool AreUsageStatsEnabled(const wchar_t* exe_path); // if stats collecting is permitted by this policy and false if not. bool ReportingIsEnforcedByPolicy(bool* breakpad_enabled); +// Initializes |g_process_type| which stores whether or not the current process +// is the main browser process. +void InitializeProcessType(); + // Returns true if invoked in a Chrome process other than the main browser // process. False otherwise. bool IsNonBrowserProcess(); +// Caches the |ProcessType| of the current process. +extern ProcessType g_process_type; + #endif // CHROME_ELF_CHROME_ELF_UTIL_H_ diff --git a/chrome_elf/chrome_elf_util_unittest.cc b/chrome_elf/chrome_elf_util_unittest.cc index ad83801..565785f 100644 --- a/chrome_elf/chrome_elf_util_unittest.cc +++ b/chrome_elf/chrome_elf_util_unittest.cc @@ -48,6 +48,12 @@ TEST(ChromeElfUtilTest, SystemInstallTest) { EXPECT_FALSE(IsSystemInstall(kChromeUserExePath)); } +TEST(ChromeElfUtilTest, BrowserProcessTest) { + EXPECT_EQ(ProcessType::UNINITIALIZED, g_process_type); + InitializeProcessType(); + EXPECT_FALSE(IsNonBrowserProcess()); +} + // Parameterized test with paramters: // 1: product: "canary" or "google" // 2: install level: "user" or "system" -- cgit v1.1