From e9f457820079f3982bb54b31a9fc5e30bcf2c021 Mon Sep 17 00:00:00 2001 From: Martin Rodriguez Reboredo Date: Sun, 23 Oct 2022 09:51:47 -0300 Subject: [PATCH] feat: implement resources-path flag in shell --- docs/api/command-line-switches.md | 9 +++++++++ shell/browser/electron_browser_client.cc | 7 +++++++ shell/common/node_bindings.cc | 15 +++++++++++++++ shell/common/options_switches.cc | 3 +++ shell/common/options_switches.h | 1 + 5 files changed, 35 insertions(+) diff --git a/docs/api/command-line-switches.md b/docs/api/command-line-switches.md index e99f223b2f5fc..a207cee74b5a3 100644 --- a/docs/api/command-line-switches.md +++ b/docs/api/command-line-switches.md @@ -204,6 +204,15 @@ authentication [per Chromium issue](https://bugs.chromium.org/p/chromium/issues/ Enables remote debugging over HTTP on the specified `port`. +### --resources-path=`path` + +Sets the resources path of an Electron application to the specified directory, +this means that `process.resourcesPath` will point to it. If it's absent or an +invalid path is passed then it falls back to the default value of the `resources` +path relative to the Electron binary and emits an error message to the log if +the path doesn't exist. It's ignored if the application is packaged or the +`embeddedAsarIntegrityValidation` fuse is enabled. + ### --v=`log_level` Gives the default maximal active V-logging level; 0 is the default. Normally diff --git a/shell/browser/electron_browser_client.cc b/shell/browser/electron_browser_client.cc index 2882575fa3c57..57d90056fd2c9 100644 --- a/shell/browser/electron_browser_client.cc +++ b/shell/browser/electron_browser_client.cc @@ -555,6 +555,13 @@ void ElectronBrowserClient::AppendExtraCommandLineSwitches( command_line->AppendSwitchPath(switches::kAppPath, app_path); } + base::FilePath resources_path = + base::CommandLine::ForCurrentProcess()->GetSwitchValuePath( + switches::kResourcesPath); + if (!resources_path.empty()) { + command_line->AppendSwitchPath(switches::kResourcesPath, resources_path); + } + auto env = base::Environment::Create(); if (env->HasVar("ELECTRON_PROFILE_INIT_SCRIPTS")) { command_line->AppendSwitch("profile-electron-init"); diff --git a/shell/common/node_bindings.cc b/shell/common/node_bindings.cc index b22f0328cd651..33a1297b52e26 100644 --- a/shell/common/node_bindings.cc +++ b/shell/common/node_bindings.cc @@ -15,6 +15,7 @@ #include "base/base_paths.h" #include "base/command_line.h" #include "base/environment.h" +#include "base/files/file_util.h" #include "base/path_service.h" #include "base/run_loop.h" #include "base/strings/string_split.h" @@ -37,6 +38,7 @@ #include "shell/common/gin_helper/microtasks_scope.h" #include "shell/common/mac/main_application_bundle.h" #include "shell/common/node_includes.h" +#include "shell/common/options_switches.h" #include "third_party/blink/renderer/bindings/core/v8/v8_initializer.h" // nogncheck #include "third_party/electron_node/src/debug_utils.h" @@ -308,6 +310,19 @@ namespace electron { namespace { base::FilePath GetResourcesPath() { + if (!electron::fuses::IsEmbeddedAsarIntegrityValidationEnabled() && + !electron::api::App::IsPackaged()) { + base::FilePath resources_path = + base::CommandLine::ForCurrentProcess()->GetSwitchValuePath( + switches::kResourcesPath); + if (!resources_path.empty()) { + if (base::PathExists(resources_path)) { + return resources_path; + } + LOG(ERROR) << "--resources-path points to a non-existent directory"; + resources_path.clear(); + } + } #if BUILDFLAG(IS_MAC) return MainApplicationBundlePath().Append("Contents").Append("Resources"); #else diff --git a/shell/common/options_switches.cc b/shell/common/options_switches.cc index 57b66f2e2d313..a4e0cc5186120 100644 --- a/shell/common/options_switches.cc +++ b/shell/common/options_switches.cc @@ -241,6 +241,9 @@ const char kAppUserModelId[] = "app-user-model-id"; // The application path const char kAppPath[] = "app-path"; +// The resources path +const char kResourcesPath[] = "resources-path"; + // The command line switch versions of the options. const char kScrollBounce[] = "scroll-bounce"; diff --git a/shell/common/options_switches.h b/shell/common/options_switches.h index b0199569dbcad..90d1d4eb6dba3 100644 --- a/shell/common/options_switches.h +++ b/shell/common/options_switches.h @@ -118,6 +118,7 @@ extern const char kCORSSchemes[]; extern const char kStreamingSchemes[]; extern const char kAppUserModelId[]; extern const char kAppPath[]; +extern const char kResourcesPath[]; extern const char kScrollBounce[]; extern const char kNodeIntegrationInWorker[];