Browse Source

refactor: use `process_util.h` helpers (#38604)

refactor: use process_util.h helpers

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Milan Burda <[email protected]>
trop[bot] 1 year ago
parent
commit
f933ce3755

+ 2 - 4
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,

+ 7 - 13
shell/app/electron_main_delegate.cc

@@ -316,9 +316,7 @@ absl::optional<int> 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<int> 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";

+ 6 - 9
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
 }

+ 3 - 7
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"
@@ -355,10 +354,7 @@ 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)
@@ -368,10 +364,10 @@ void NodeBindings::RegisterBuiltinBindings() {
 #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()

+ 19 - 8
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

+ 4 - 0
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