fix: use correct userData path when unbundled (#30113)

This commit is contained in:
Jeremy Rose 2021-07-14 13:10:37 -07:00 committed by GitHub
parent 4db7221c7d
commit bec47f54f4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 32 additions and 36 deletions

View file

@ -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;

View file

@ -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() {

View file

@ -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"))

View file

@ -13,27 +13,21 @@
namespace electron {
namespace {
base::NoDestructor<std::string> g_overridden_application_name;
base::NoDestructor<std::string> 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<std::string> 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<std::string> 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() {

View file

@ -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();

View file

@ -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()) {

View file

@ -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)', () => {