From bec47f54f419f6b910da8bc999a52d91e3b9d53b Mon Sep 17 00:00:00 2001 From: Jeremy Rose Date: Wed, 14 Jul 2021 13:10:37 -0700 Subject: [PATCH] fix: use correct userData path when unbundled (#30113) --- shell/app/electron_main_delegate.cc | 9 ++++--- shell/browser/browser.cc | 8 +++--- shell/browser/electron_browser_context.cc | 6 +---- shell/common/application_info.cc | 30 +++++++++-------------- shell/common/application_info.h | 7 +++--- shell/common/application_info_linux.cc | 4 +-- spec-main/api-app-spec.ts | 4 +++ 7 files changed, 32 insertions(+), 36 deletions(-) diff --git a/shell/app/electron_main_delegate.cc b/shell/app/electron_main_delegate.cc index 1c22e2f4fafe..5647b7c8111b 100644 --- a/shell/app/electron_main_delegate.cc +++ b/shell/app/electron_main_delegate.cc @@ -123,7 +123,8 @@ bool ElectronPathProvider(int key, base::FilePath* result) { case chrome::DIR_USER_DATA: if (!base::PathService::Get(DIR_APP_DATA, &cur)) return false; - cur = cur.Append(base::FilePath::FromUTF8Unsafe(GetApplicationName())); + cur = cur.Append(base::FilePath::FromUTF8Unsafe( + GetPossiblyOverriddenApplicationName())); create_dir = true; break; case DIR_CRASH_DUMPS: @@ -153,7 +154,8 @@ bool ElectronPathProvider(int key, base::FilePath* result) { #endif if (!base::PathService::Get(parent_key, &cur)) return false; - cur = cur.Append(base::FilePath::FromUTF8Unsafe(GetApplicationName())); + cur = cur.Append(base::FilePath::FromUTF8Unsafe( + GetPossiblyOverriddenApplicationName())); create_dir = true; break; } @@ -178,7 +180,8 @@ bool ElectronPathProvider(int key, base::FilePath* result) { return false; cur = cur.Append(FILE_PATH_LITERAL("Library")); cur = cur.Append(FILE_PATH_LITERAL("Logs")); - cur = cur.Append(base::FilePath::FromUTF8Unsafe(GetApplicationName())); + cur = cur.Append(base::FilePath::FromUTF8Unsafe( + GetPossiblyOverriddenApplicationName())); #else if (!base::PathService::Get(chrome::DIR_USER_DATA, &cur)) return false; diff --git a/shell/browser/browser.cc b/shell/browser/browser.cc index dbd23d4b3cb0..fd8bb0b72603 100644 --- a/shell/browser/browser.cc +++ b/shell/browser/browser.cc @@ -136,25 +136,25 @@ void Browser::Shutdown() { } std::string Browser::GetVersion() const { - std::string ret = GetOverriddenApplicationVersion(); + std::string ret = OverriddenApplicationVersion(); if (ret.empty()) ret = GetExecutableFileVersion(); return ret; } void Browser::SetVersion(const std::string& version) { - OverrideApplicationVersion(version); + OverriddenApplicationVersion() = version; } std::string Browser::GetName() const { - std::string ret = GetOverriddenApplicationName(); + std::string ret = OverriddenApplicationName(); if (ret.empty()) ret = GetExecutableFileProductName(); return ret; } void Browser::SetName(const std::string& name) { - OverrideApplicationName(name); + OverriddenApplicationName() = name; } int Browser::GetBadgeCount() { diff --git a/shell/browser/electron_browser_context.cc b/shell/browser/electron_browser_context.cc index 47af4b70f1e8..eec49da36e92 100644 --- a/shell/browser/electron_browser_context.cc +++ b/shell/browser/electron_browser_context.cc @@ -118,11 +118,7 @@ ElectronBrowserContext::ElectronBrowserContext(const std::string& partition, base::StringToInt(command_line->GetSwitchValueASCII(switches::kDiskCacheSize), &max_cache_size_); - if (!base::PathService::Get(chrome::DIR_USER_DATA, &path_)) { - base::PathService::Get(DIR_APP_DATA, &path_); - path_ = path_.Append(base::FilePath::FromUTF8Unsafe(GetApplicationName())); - base::PathService::Override(chrome::DIR_USER_DATA, path_); - } + CHECK(base::PathService::Get(chrome::DIR_USER_DATA, &path_)); if (!in_memory && !partition.empty()) path_ = path_.Append(FILE_PATH_LITERAL("Partitions")) diff --git a/shell/common/application_info.cc b/shell/common/application_info.cc index 8fc44ff634ba..1af2aa377670 100644 --- a/shell/common/application_info.cc +++ b/shell/common/application_info.cc @@ -13,27 +13,21 @@ namespace electron { -namespace { - -base::NoDestructor g_overridden_application_name; -base::NoDestructor g_overridden_application_version; - -} // namespace - -// name -void OverrideApplicationName(const std::string& name) { - *g_overridden_application_name = name; -} -std::string GetOverriddenApplicationName() { - return *g_overridden_application_name; +std::string& OverriddenApplicationName() { + static base::NoDestructor overridden_application_name; + return *overridden_application_name; } -// version -void OverrideApplicationVersion(const std::string& version) { - *g_overridden_application_version = version; +std::string& OverriddenApplicationVersion() { + static base::NoDestructor overridden_application_version; + return *overridden_application_version; } -std::string GetOverriddenApplicationVersion() { - return *g_overridden_application_version; + +std::string GetPossiblyOverriddenApplicationName() { + std::string ret = OverriddenApplicationName(); + if (!ret.empty()) + return ret; + return GetApplicationName(); } std::string GetApplicationUserAgent() { diff --git a/shell/common/application_info.h b/shell/common/application_info.h index a79443ec31ac..9ad849cc413f 100644 --- a/shell/common/application_info.h +++ b/shell/common/application_info.h @@ -13,11 +13,10 @@ namespace electron { -void OverrideApplicationName(const std::string& name); -std::string GetOverriddenApplicationName(); +std::string& OverriddenApplicationName(); +std::string& OverriddenApplicationVersion(); -void OverrideApplicationVersion(const std::string& version); -std::string GetOverriddenApplicationVersion(); +std::string GetPossiblyOverriddenApplicationName(); std::string GetApplicationName(); std::string GetApplicationVersion(); diff --git a/shell/common/application_info_linux.cc b/shell/common/application_info_linux.cc index 80de26530f8a..d7eb9d72875b 100644 --- a/shell/common/application_info_linux.cc +++ b/shell/common/application_info_linux.cc @@ -33,7 +33,7 @@ namespace electron { std::string GetApplicationName() { // attempt #1: the string set in app.setName() - std::string ret = GetOverriddenApplicationName(); + std::string ret = OverriddenApplicationName(); // attempt #2: the 'Name' entry from .desktop file's [Desktop] section if (ret.empty()) { @@ -64,7 +64,7 @@ std::string GetApplicationVersion() { // try to use the string set in app.setVersion() if (ret.empty()) - ret = GetOverriddenApplicationVersion(); + ret = OverriddenApplicationVersion(); // no known version number; return some safe fallback if (ret.empty()) { diff --git a/spec-main/api-app-spec.ts b/spec-main/api-app-spec.ts index 5eccba83f615..35cb4a54b0df 100644 --- a/spec-main/api-app-spec.ts +++ b/spec-main/api-app-spec.ts @@ -924,6 +924,10 @@ describe('app module', () => { expect(app.getPath('recent')).to.equal('C:\\fake-path'); }); } + + it('uses the app name in getPath(userData)', () => { + expect(app.getPath('userData')).to.include(app.name); + }); }); describe('setPath(name, path)', () => {