From bb2ba35b51609e40c4cae2f3f904a04c795a7430 Mon Sep 17 00:00:00 2001 From: Milan Burda Date: Tue, 6 Jun 2023 10:19:13 +0200 Subject: [PATCH] refactor: use `process_util.h` helpers (#38574) refactor: use process_util.h helpers --- shell/app/electron_content_client.cc | 6 ++--- shell/app/electron_main_delegate.cc | 20 +++++--------- .../api/electron_api_crash_reporter.cc | 15 +++++------ shell/common/node_bindings.cc | 10 +++---- shell/common/process_util.cc | 27 +++++++++++++------ shell/common/process_util.h | 4 +++ 6 files changed, 41 insertions(+), 41 deletions(-) diff --git a/shell/app/electron_content_client.cc b/shell/app/electron_content_client.cc index 9d9646fba9ac..9a37ac4149f4 100644 --- a/shell/app/electron_content_client.cc +++ b/shell/app/electron_content_client.cc @@ -22,6 +22,7 @@ #include "ppapi/buildflags/buildflags.h" #include "shell/common/electron_paths.h" #include "shell/common/options_switches.h" +#include "shell/common/process_util.h" #include "third_party/widevine/cdm/buildflags.h" #include "ui/base/l10n/l10n_util.h" #include "ui/base/resource/resource_bundle.h" @@ -148,16 +149,13 @@ base::RefCountedMemory* ElectronContentClient::GetDataResourceBytes( } void ElectronContentClient::AddAdditionalSchemes(Schemes* schemes) { - auto* command_line = base::CommandLine::ForCurrentProcess(); - std::string process_type = - command_line->GetSwitchValueASCII(::switches::kProcessType); // Browser Process registration happens in // `api::Protocol::RegisterSchemesAsPrivileged` // // Renderer Process registration happens in `RendererClientBase` // // We use this for registration to network utility process - if (process_type == ::switches::kUtilityProcess) { + if (IsUtilityProcess()) { AppendDelimitedSwitchToVector(switches::kServiceWorkerSchemes, &schemes->service_worker_schemes); AppendDelimitedSwitchToVector(switches::kStandardSchemes, diff --git a/shell/app/electron_main_delegate.cc b/shell/app/electron_main_delegate.cc index 05ccf1a1b60c..ad771f290054 100644 --- a/shell/app/electron_main_delegate.cc +++ b/shell/app/electron_main_delegate.cc @@ -316,9 +316,7 @@ absl::optional ElectronMainDelegate::BasicStartupComplete() { void ElectronMainDelegate::PreSandboxStartup() { auto* command_line = base::CommandLine::ForCurrentProcess(); - - std::string process_type = - command_line->GetSwitchValueASCII(::switches::kProcessType); + std::string process_type = GetProcessType(); base::FilePath user_data_dir = command_line->GetSwitchValuePath(::switches::kUserDataDir); @@ -335,9 +333,9 @@ void ElectronMainDelegate::PreSandboxStartup() { // know the correct user-data directory. (And, further, accessing the // application name on Linux can cause glib calls that end up spawning // threads, which if done before the zygote is booted, causes a CHECK().) - logging::InitElectronLogging(*command_line, - /* is_preinit = */ process_type.empty() || - process_type == ::switches::kZygoteProcess); + logging::InitElectronLogging( + *command_line, + /* is_preinit = */ IsBrowserProcess() || IsZygoteProcess()); #endif #if !IS_MAS_BUILD() @@ -356,7 +354,7 @@ void ElectronMainDelegate::PreSandboxStartup() { // In the main process, we wait for JS to call crashReporter.start() before // initializing crashpad. If we're in the renderer, we want to initialize it // immediately at boot. - if (!process_type.empty()) { + if (!IsBrowserProcess()) { ElectronCrashReporterClient::Create(); crash_reporter::InitializeCrashpad(false, process_type); } @@ -364,7 +362,7 @@ void ElectronMainDelegate::PreSandboxStartup() { #if BUILDFLAG(IS_LINUX) // Zygote needs to call InitCrashReporter() in RunZygote(). - if (process_type != ::switches::kZygoteProcess && !process_type.empty()) { + if (!IsZygoteProcess() && !IsBrowserProcess()) { ElectronCrashReporterClient::Create(); if (command_line->HasSwitch( crash_reporter::switches::kCrashpadHandlerPid)) { @@ -414,12 +412,8 @@ absl::optional ElectronMainDelegate::PreBrowserMain() { } base::StringPiece ElectronMainDelegate::GetBrowserV8SnapshotFilename() { - const base::CommandLine* command_line = - base::CommandLine::ForCurrentProcess(); - std::string process_type = - command_line->GetSwitchValueASCII(::switches::kProcessType); bool load_browser_process_specific_v8_snapshot = - process_type.empty() && + IsBrowserProcess() && electron::fuses::IsLoadBrowserProcessSpecificV8SnapshotEnabled(); if (load_browser_process_specific_v8_snapshot) { return "browser_v8_context_snapshot.bin"; diff --git a/shell/browser/api/electron_api_crash_reporter.cc b/shell/browser/api/electron_api_crash_reporter.cc index a7ba78e86f95..3dd6102dab6c 100644 --- a/shell/browser/api/electron_api_crash_reporter.cc +++ b/shell/browser/api/electron_api_crash_reporter.cc @@ -28,6 +28,7 @@ #include "shell/common/gin_converters/time_converter.h" #include "shell/common/gin_helper/dictionary.h" #include "shell/common/node_includes.h" +#include "shell/common/process_util.h" #include "shell/common/thread_restrictions.h" #if !IS_MAS_BUILD() @@ -142,17 +143,13 @@ void Start(const std::string& submit_url, ElectronCrashReporterClient::Get()->SetShouldRateLimit(rate_limit); ElectronCrashReporterClient::Get()->SetShouldCompressUploads(compress); ElectronCrashReporterClient::Get()->SetGlobalAnnotations(global_extra); - auto* command_line = base::CommandLine::ForCurrentProcess(); - std::string process_type = - is_node_process - ? "node" - : command_line->GetSwitchValueASCII(::switches::kProcessType); + std::string process_type = is_node_process ? "node" : GetProcessType(); #if BUILDFLAG(IS_LINUX) for (const auto& pair : extra) electron::crash_keys::SetCrashKey(pair.first, pair.second); { electron::ScopedAllowBlockingForElectron allow_blocking; - ::crash_reporter::InitializeCrashpad(process_type.empty(), process_type); + ::crash_reporter::InitializeCrashpad(IsBrowserProcess(), process_type); } if (ignore_system_crash_handler) { crashpad::CrashpadInfo::GetCrashpadInfo() @@ -161,7 +158,7 @@ void Start(const std::string& submit_url, #elif BUILDFLAG(IS_MAC) for (const auto& pair : extra) electron::crash_keys::SetCrashKey(pair.first, pair.second); - ::crash_reporter::InitializeCrashpad(process_type.empty(), process_type); + ::crash_reporter::InitializeCrashpad(IsBrowserProcess(), process_type); if (ignore_system_crash_handler) { crashpad::CrashpadInfo::GetCrashpadInfo() ->set_system_crash_reporter_forwarding(crashpad::TriState::kDisabled); @@ -172,8 +169,8 @@ void Start(const std::string& submit_url, base::FilePath user_data_dir; base::PathService::Get(chrome::DIR_USER_DATA, &user_data_dir); ::crash_reporter::InitializeCrashpadWithEmbeddedHandler( - process_type.empty(), process_type, - base::WideToUTF8(user_data_dir.value()), base::FilePath()); + IsBrowserProcess(), process_type, base::WideToUTF8(user_data_dir.value()), + base::FilePath()); #endif #endif } diff --git a/shell/common/node_bindings.cc b/shell/common/node_bindings.cc index 671d7706d6cb..36a06d1ed454 100644 --- a/shell/common/node_bindings.cc +++ b/shell/common/node_bindings.cc @@ -23,7 +23,6 @@ #include "base/trace_event/trace_event.h" #include "content/public/browser/browser_thread.h" #include "content/public/common/content_paths.h" -#include "content/public/common/content_switches.h" #include "electron/buildflags/buildflags.h" #include "electron/fuses.h" #include "shell/browser/api/electron_api_app.h" @@ -350,20 +349,17 @@ NodeBindings::~NodeBindings() { void NodeBindings::RegisterBuiltinBindings() { #define V(modname) _register_##modname(); - auto* command_line = base::CommandLine::ForCurrentProcess(); - std::string process_type = - command_line->GetSwitchValueASCII(::switches::kProcessType); - if (process_type.empty()) { + if (IsBrowserProcess()) { ELECTRON_BROWSER_BINDINGS(V) #if BUILDFLAG(ENABLE_VIEWS_API) ELECTRON_VIEWS_BINDINGS(V) #endif } ELECTRON_COMMON_BINDINGS(V) - if (process_type == ::switches::kRendererProcess) { + if (IsRendererProcess()) { ELECTRON_RENDERER_BINDINGS(V) } - if (process_type == ::switches::kUtilityProcess) { + if (IsUtilityProcess()) { ELECTRON_UTILITY_BINDINGS(V) } #if DCHECK_IS_ON() diff --git a/shell/common/process_util.cc b/shell/common/process_util.cc index f54bdf6c3d36..c55573b8dc56 100644 --- a/shell/common/process_util.cc +++ b/shell/common/process_util.cc @@ -26,18 +26,29 @@ void EmitWarning(node::Environment* env, emit_warning.Run(warning_msg, warning_type, ""); } -bool IsBrowserProcess() { +std::string GetProcessType() { auto* command_line = base::CommandLine::ForCurrentProcess(); - std::string process_type = - command_line->GetSwitchValueASCII(switches::kProcessType); - return process_type.empty(); + return command_line->GetSwitchValueASCII(switches::kProcessType); +} + +bool IsBrowserProcess() { + static bool result = GetProcessType().empty(); + return result; } bool IsRendererProcess() { - auto* command_line = base::CommandLine::ForCurrentProcess(); - std::string process_type = - command_line->GetSwitchValueASCII(switches::kProcessType); - return process_type == switches::kRendererProcess; + static bool result = GetProcessType() == switches::kRendererProcess; + return result; +} + +bool IsUtilityProcess() { + static bool result = GetProcessType() == switches::kUtilityProcess; + return result; +} + +bool IsZygoteProcess() { + static bool result = GetProcessType() == switches::kZygoteProcess; + return result; } } // namespace electron diff --git a/shell/common/process_util.h b/shell/common/process_util.h index 8a93eb9488d4..fb4c6639153b 100644 --- a/shell/common/process_util.h +++ b/shell/common/process_util.h @@ -17,8 +17,12 @@ void EmitWarning(node::Environment* env, const std::string& warning_msg, const std::string& warning_type); +std::string GetProcessType(); + bool IsBrowserProcess(); bool IsRendererProcess(); +bool IsUtilityProcess(); +bool IsZygoteProcess(); } // namespace electron