From abe2fd8c2cf134de5f977f59829503abc46c04f6 Mon Sep 17 00:00:00 2001 From: "trop[bot]" <37223003+trop[bot]@users.noreply.github.com> Date: Mon, 2 Feb 2026 09:38:46 +0100 Subject: [PATCH] refactor: address PathProvider TODO (#49599) Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com> Co-authored-by: Shelley Vohr --- filenames.gni | 1 + shell/app/electron_main_delegate.cc | 97 ----------------------- shell/common/electron_paths.cc | 117 ++++++++++++++++++++++++++++ shell/common/electron_paths.h | 4 + 4 files changed, 122 insertions(+), 97 deletions(-) create mode 100644 shell/common/electron_paths.cc diff --git a/filenames.gni b/filenames.gni index 21b57205fe..3c351e260a 100644 --- a/filenames.gni +++ b/filenames.gni @@ -590,6 +590,7 @@ filenames = { "shell/common/electron_command_line.cc", "shell/common/electron_command_line.h", "shell/common/electron_constants.h", + "shell/common/electron_paths.cc", "shell/common/electron_paths.h", "shell/common/gin_converters/accelerator_converter.cc", "shell/common/gin_converters/accelerator_converter.h", diff --git a/shell/app/electron_main_delegate.cc b/shell/app/electron_main_delegate.cc index 07d21ab529..278ae4c7be 100644 --- a/shell/app/electron_main_delegate.cc +++ b/shell/app/electron_main_delegate.cc @@ -43,13 +43,10 @@ #include "shell/browser/electron_gpu_client.h" #include "shell/browser/feature_list.h" #include "shell/browser/relauncher.h" -#include "shell/common/application_info.h" #include "shell/common/electron_paths.h" #include "shell/common/logging.h" #include "shell/common/options_switches.h" -#include "shell/common/platform_util.h" #include "shell/common/process_util.h" -#include "shell/common/thread_restrictions.h" #include "shell/renderer/electron_renderer_client.h" #include "shell/renderer/electron_sandboxed_renderer_client.h" #include "shell/utility/electron_content_utility_client.h" @@ -121,100 +118,6 @@ void InvalidParameterHandler(const wchar_t*, } #endif -// TODO(nornagon): move path provider overriding to its own file in -// shell/common -bool ElectronPathProvider(int key, base::FilePath* result) { - bool create_dir = false; - base::FilePath cur; - switch (key) { - case chrome::DIR_USER_DATA: - if (!base::PathService::Get(DIR_APP_DATA, &cur)) - return false; - cur = cur.Append(base::FilePath::FromUTF8Unsafe( - GetPossiblyOverriddenApplicationName())); - create_dir = true; - break; - case DIR_CRASH_DUMPS: - if (!base::PathService::Get(chrome::DIR_USER_DATA, &cur)) - return false; - cur = cur.Append(FILE_PATH_LITERAL("Crashpad")); - create_dir = true; - break; - case chrome::DIR_APP_DICTIONARIES: - // TODO(nornagon): can we just default to using Chrome's logic here? - if (!base::PathService::Get(DIR_SESSION_DATA, &cur)) - return false; - cur = cur.Append(base::FilePath::FromUTF8Unsafe("Dictionaries")); - create_dir = true; - break; - case DIR_SESSION_DATA: - // By default and for backward, equivalent to DIR_USER_DATA. - return base::PathService::Get(chrome::DIR_USER_DATA, result); - case DIR_USER_CACHE: { -#if BUILDFLAG(IS_POSIX) - int parent_key = base::DIR_CACHE; -#else - // On Windows, there's no OS-level centralized location for caches, so - // store the cache in the app data directory. - int parent_key = base::DIR_ROAMING_APP_DATA; -#endif - if (!base::PathService::Get(parent_key, &cur)) - return false; - cur = cur.Append(base::FilePath::FromUTF8Unsafe( - GetPossiblyOverriddenApplicationName())); - create_dir = true; - break; - } -#if BUILDFLAG(IS_LINUX) - case DIR_APP_DATA: { - auto env = base::Environment::Create(); - cur = base::nix::GetXDGDirectory( - env.get(), base::nix::kXdgConfigHomeEnvVar, base::nix::kDotConfigDir); - break; - } -#endif -#if BUILDFLAG(IS_WIN) - case DIR_RECENT: - if (!platform_util::GetFolderPath(DIR_RECENT, &cur)) - return false; - create_dir = true; - break; -#endif - case DIR_APP_LOGS: -#if BUILDFLAG(IS_MAC) - if (!base::PathService::Get(base::DIR_HOME, &cur)) - return false; - cur = cur.Append(FILE_PATH_LITERAL("Library")); - cur = cur.Append(FILE_PATH_LITERAL("Logs")); - cur = cur.Append(base::FilePath::FromUTF8Unsafe( - GetPossiblyOverriddenApplicationName())); -#else - if (!base::PathService::Get(chrome::DIR_USER_DATA, &cur)) - return false; - cur = cur.Append(base::FilePath::FromUTF8Unsafe("logs")); -#endif - create_dir = true; - break; - default: - return false; - } - - // TODO(bauerb): http://crbug.com/259796 - ScopedAllowBlockingForElectron allow_blocking; - if (create_dir && !base::PathExists(cur) && !base::CreateDirectory(cur)) { - return false; - } - - *result = cur; - - return true; -} - -void RegisterPathProvider() { - base::PathService::RegisterProvider(ElectronPathProvider, PATH_START, - PATH_END); -} - void ValidateV8Snapshot(v8::StartupData* data) { if (data->data && electron::fuses::IsEmbeddedAsarIntegrityValidationEnabled()) { diff --git a/shell/common/electron_paths.cc b/shell/common/electron_paths.cc new file mode 100644 index 0000000000..7fa29ed1e5 --- /dev/null +++ b/shell/common/electron_paths.cc @@ -0,0 +1,117 @@ +// Copyright (c) 2026 Microsoft GmbH. +// Use of this source code is governed by the MIT license that can be +// found in the LICENSE file. + +#include "shell/common/electron_paths.h" + +#include "base/environment.h" +#include "base/files/file_util.h" +#include "base/path_service.h" +#include "chrome/common/chrome_paths.h" +#include "shell/common/application_info.h" +#include "shell/common/platform_util.h" +#include "shell/common/thread_restrictions.h" + +#if BUILDFLAG(IS_LINUX) +#include "base/nix/xdg_util.h" +#endif + +namespace electron { + +namespace { + +bool ElectronPathProvider(int key, base::FilePath* result) { + bool create_dir = false; + base::FilePath cur; + switch (key) { + case chrome::DIR_USER_DATA: + if (!base::PathService::Get(DIR_APP_DATA, &cur)) + return false; + cur = cur.Append(base::FilePath::FromUTF8Unsafe( + GetPossiblyOverriddenApplicationName())); + create_dir = true; + break; + case DIR_CRASH_DUMPS: + if (!base::PathService::Get(chrome::DIR_USER_DATA, &cur)) + return false; + cur = cur.Append(FILE_PATH_LITERAL("Crashpad")); + create_dir = true; + break; + case chrome::DIR_APP_DICTIONARIES: + // TODO(nornagon): can we just default to using Chrome's logic here? + if (!base::PathService::Get(DIR_SESSION_DATA, &cur)) + return false; + cur = cur.Append(base::FilePath::FromUTF8Unsafe("Dictionaries")); + create_dir = true; + break; + case DIR_SESSION_DATA: + // By default and for backward, equivalent to DIR_USER_DATA. + return base::PathService::Get(chrome::DIR_USER_DATA, result); + case DIR_USER_CACHE: { +#if BUILDFLAG(IS_POSIX) + int parent_key = base::DIR_CACHE; +#else + // On Windows, there's no OS-level centralized location for caches, so + // store the cache in the app data directory. + int parent_key = base::DIR_ROAMING_APP_DATA; +#endif + if (!base::PathService::Get(parent_key, &cur)) + return false; + cur = cur.Append(base::FilePath::FromUTF8Unsafe( + GetPossiblyOverriddenApplicationName())); + create_dir = true; + break; + } +#if BUILDFLAG(IS_LINUX) + case DIR_APP_DATA: { + auto env = base::Environment::Create(); + cur = base::nix::GetXDGDirectory( + env.get(), base::nix::kXdgConfigHomeEnvVar, base::nix::kDotConfigDir); + break; + } +#endif +#if BUILDFLAG(IS_WIN) + case DIR_RECENT: + if (!platform_util::GetFolderPath(DIR_RECENT, &cur)) + return false; + create_dir = true; + break; +#endif + case DIR_APP_LOGS: +#if BUILDFLAG(IS_MAC) + if (!base::PathService::Get(base::DIR_HOME, &cur)) + return false; + cur = cur.Append(FILE_PATH_LITERAL("Library")); + cur = cur.Append(FILE_PATH_LITERAL("Logs")); + cur = cur.Append(base::FilePath::FromUTF8Unsafe( + GetPossiblyOverriddenApplicationName())); +#else + if (!base::PathService::Get(chrome::DIR_USER_DATA, &cur)) + return false; + cur = cur.Append(base::FilePath::FromUTF8Unsafe("logs")); +#endif + create_dir = true; + break; + default: + return false; + } + + // TODO(bauerb): http://crbug.com/259796 + ScopedAllowBlockingForElectron allow_blocking; + if (create_dir && !base::PathExists(cur) && !base::CreateDirectory(cur)) { + return false; + } + + *result = cur; + + return true; +} + +} // namespace + +void RegisterPathProvider() { + base::PathService::RegisterProvider(ElectronPathProvider, PATH_START, + PATH_END); +} + +} // namespace electron diff --git a/shell/common/electron_paths.h b/shell/common/electron_paths.h index c614f99294..2bc0440419 100644 --- a/shell/common/electron_paths.h +++ b/shell/common/electron_paths.h @@ -6,6 +6,7 @@ #define ELECTRON_SHELL_COMMON_ELECTRON_PATHS_H_ #include "base/base_paths.h" +#include "base/files/file_path.h" #if BUILDFLAG(IS_WIN) #include "base/base_paths_win.h" @@ -47,6 +48,9 @@ enum { static_assert(PATH_START < PATH_END, "invalid PATH boundaries"); +// Register the path provider with the base::PathService. +void RegisterPathProvider(); + } // namespace electron #endif // ELECTRON_SHELL_COMMON_ELECTRON_PATHS_H_