refactor: DRY in App::SetAppLogPath() (#48452)

This commit is contained in:
Charles Kerr
2025-10-05 12:32:28 -05:00
committed by GitHub
parent 38e491689a
commit a1ca9a8d55
3 changed files with 37 additions and 47 deletions

View File

@@ -870,28 +870,33 @@ void App::SetAppPath(const base::FilePath& app_path) {
app_path_ = app_path;
}
#if !BUILDFLAG(IS_MAC)
void App::SetAppLogsPath(gin_helper::ErrorThrower thrower,
std::optional<base::FilePath> custom_path) {
if (custom_path.has_value()) {
if (!custom_path->IsAbsolute()) {
thrower.ThrowError("Path must be absolute");
return;
}
{
ScopedAllowBlockingForElectron allow_blocking;
base::PathService::Override(DIR_APP_LOGS, custom_path.value());
}
} else {
base::FilePath path;
if (base::PathService::Get(chrome::DIR_USER_DATA, &path)) {
path = path.Append(base::FilePath::FromUTF8Unsafe("logs"));
{
ScopedAllowBlockingForElectron allow_blocking;
base::PathService::Override(DIR_APP_LOGS, path);
}
}
void App::SetAppLogsPath(gin::Arguments* const args) {
base::FilePath path;
// if caller provided a path, it must be absolute
if (args->GetNext(&path) && !path.IsAbsolute()) {
args->ThrowTypeError("Path must be absolute");
return;
}
// if caller did not provide a path, then use a default one
if (path.empty()) {
path = GetDefaultAppLogPath();
}
ScopedAllowBlockingForElectron allow_blocking;
base::PathService::Override(DIR_APP_LOGS, path);
}
#if !BUILDFLAG(IS_MAC)
// static
// default to `${DIR_USER_DATA}/logs`
base::FilePath App::GetDefaultAppLogPath() {
base::FilePath path;
if (base::PathService::Get(chrome::DIR_USER_DATA, &path)) {
path = path.Append(base::FilePath::FromUTF8Unsafe("logs"));
}
return path;
}
#endif

View File

@@ -178,6 +178,8 @@ class App final : public gin::Wrappable<App>,
const content::ChildProcessTerminationInfo& info) override;
private:
[[nodiscard]] static base::FilePath GetDefaultAppLogPath();
void BrowserChildProcessCrashedOrKilled(
const content::ChildProcessData& data,
const content::ChildProcessTerminationInfo& info);
@@ -190,8 +192,7 @@ class App final : public gin::Wrappable<App>,
const std::string& name = std::string());
void ChildProcessDisconnected(content::ChildProcessId pid);
void SetAppLogsPath(gin_helper::ErrorThrower thrower,
std::optional<base::FilePath> custom_path);
void SetAppLogsPath(gin::Arguments* args);
// Get/Set the pre-defined path in PathService.
base::FilePath GetPath(gin_helper::ErrorThrower thrower,

View File

@@ -17,30 +17,14 @@
namespace electron::api {
void App::SetAppLogsPath(gin_helper::ErrorThrower thrower,
std::optional<base::FilePath> custom_path) {
if (custom_path.has_value()) {
if (!custom_path->IsAbsolute()) {
thrower.ThrowError("Path must be absolute");
return;
}
{
ScopedAllowBlockingForElectron allow_blocking;
base::PathService::Override(DIR_APP_LOGS, custom_path.value());
}
} else {
NSString* bundle_name =
[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"];
NSString* logs_path =
[NSString stringWithFormat:@"Library/Logs/%@", bundle_name];
NSString* library_path =
[NSHomeDirectory() stringByAppendingPathComponent:logs_path];
{
ScopedAllowBlockingForElectron allow_blocking;
base::PathService::Override(DIR_APP_LOGS,
base::FilePath([library_path UTF8String]));
}
}
base::FilePath App::GetDefaultAppLogPath() {
NSString* bundle_name =
[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"];
NSString* logs_path =
[NSString stringWithFormat:@"Library/Logs/%@", bundle_name];
NSString* library_path =
[NSHomeDirectory() stringByAppendingPathComponent:logs_path];
return base::FilePath{[library_path UTF8String]};
}
void App::SetActivationPolicy(gin_helper::ErrorThrower thrower,