Skip to content

Commit ebf54d7

Browse files
authored
refactor: use PathProvider for user-data-dir and others (electron#29649)
* refactor: use PathProvider for user-data-dir and others * consolidate logic for DIR_RECENT and DIR_APP_LOGS into path provider * fix bad include * remove debugging code * fix build on mac * fix build on win * create app logs dir on both mac and non-mac
1 parent 8b945cb commit ebf54d7

16 files changed

+115
-108
lines changed

default_app/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ function loadApplicationPackage (packagePath: string) {
109109

110110
try {
111111
const filePath = Module._resolveFilename(packagePath, module, true);
112-
app._setDefaultAppPaths(appPath || path.dirname(filePath));
112+
app.setAppPath(appPath || path.dirname(filePath));
113113
} catch (e) {
114114
showErrorMessage(`Unable to find Electron app at ${packagePath}\n\n${e.message}`);
115115
return;

lib/browser/api/app.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import * as fs from 'fs';
2-
import * as path from 'path';
32

43
import { Menu } from 'electron/main';
54

@@ -58,19 +57,6 @@ Object.defineProperty(app, 'applicationMenu', {
5857
// The native implementation is not provided on non-windows platforms
5958
app.setAppUserModelId = app.setAppUserModelId || (() => {});
6059

61-
app._setDefaultAppPaths = (packagePath) => {
62-
// Set the user path according to application's name.
63-
app.setPath('userData', path.join(app.getPath('appData'), app.name!));
64-
app.setPath('userCache', path.join(app.getPath('cache'), app.name!));
65-
app.setAppPath(packagePath);
66-
67-
// Add support for --user-data-dir=
68-
if (app.commandLine.hasSwitch('user-data-dir')) {
69-
const userDataDir = app.commandLine.getSwitchValue('user-data-dir');
70-
if (path.isAbsolute(userDataDir)) app.setPath('userData', userDataDir);
71-
}
72-
};
73-
7460
if (process.platform === 'darwin') {
7561
const setDockMenu = app.dock!.setMenu;
7662
app.dock!.setMenu = (menu) => {

lib/browser/init.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ if (packageJson.v8Flags != null) {
129129
require('v8').setFlagsFromString(packageJson.v8Flags);
130130
}
131131

132-
app._setDefaultAppPaths(packagePath);
132+
app.setAppPath(packagePath);
133133

134134
// Load the chrome devtools support.
135135
require('@electron/internal/browser/devtools');

shell/app/electron_crash_reporter_client.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ bool ElectronCrashReporterClient::GetCrashDumpLocation(
165165
#if defined(OS_MAC) || defined(OS_LINUX)
166166
bool ElectronCrashReporterClient::GetCrashMetricsLocation(
167167
base::FilePath* metrics_dir) {
168-
return base::PathService::Get(electron::DIR_USER_DATA, metrics_dir);
168+
return base::PathService::Get(chrome::DIR_USER_DATA, metrics_dir);
169169
}
170170
#endif // OS_MAC || OS_LINUX
171171

shell/app/electron_main_delegate.cc

Lines changed: 87 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@
88
#include <memory>
99
#include <string>
1010

11+
#include "base/base_switches.h"
1112
#include "base/command_line.h"
12-
#include "base/debug/stack_trace.h"
1313
#include "base/environment.h"
1414
#include "base/files/file_util.h"
1515
#include "base/logging.h"
1616
#include "base/mac/bundle_locations.h"
1717
#include "base/path_service.h"
1818
#include "base/strings/string_split.h"
1919
#include "chrome/common/chrome_paths.h"
20+
#include "chrome/common/chrome_switches.h"
2021
#include "components/content_settings/core/common/content_settings_pattern.h"
2122
#include "content/public/common/content_switches.h"
2223
#include "electron/buildflags/buildflags.h"
@@ -29,8 +30,10 @@
2930
#include "shell/browser/electron_gpu_client.h"
3031
#include "shell/browser/feature_list.h"
3132
#include "shell/browser/relauncher.h"
33+
#include "shell/common/application_info.h"
3234
#include "shell/common/electron_paths.h"
3335
#include "shell/common/options_switches.h"
36+
#include "shell/common/platform_util.h"
3437
#include "shell/renderer/electron_renderer_client.h"
3538
#include "shell/renderer/electron_sandboxed_renderer_client.h"
3639
#include "shell/utility/electron_content_utility_client.h"
@@ -47,6 +50,7 @@
4750
#endif
4851

4952
#if defined(OS_LINUX)
53+
#include "base/nix/xdg_util.h"
5054
#include "components/crash/core/app/breakpad_linux.h"
5155
#include "v8/include/v8-wasm-trap-handler-posix.h"
5256
#include "v8/include/v8.h"
@@ -107,28 +111,89 @@ void InvalidParameterHandler(const wchar_t*,
107111

108112
// TODO(nornagon): move path provider overriding to its own file in
109113
// shell/common
110-
bool GetDefaultCrashDumpsPath(base::FilePath* path) {
114+
bool ElectronPathProvider(int key, base::FilePath* result) {
115+
bool create_dir = false;
111116
base::FilePath cur;
112-
if (!base::PathService::Get(DIR_USER_DATA, &cur))
113-
return false;
117+
switch (key) {
118+
case chrome::DIR_USER_DATA:
119+
if (!base::PathService::Get(DIR_APP_DATA, &cur))
120+
return false;
121+
cur = cur.Append(base::FilePath::FromUTF8Unsafe(GetApplicationName()));
122+
create_dir = true;
123+
break;
124+
case DIR_CRASH_DUMPS:
125+
if (!base::PathService::Get(chrome::DIR_USER_DATA, &cur))
126+
return false;
114127
#if defined(OS_MAC) || defined(OS_WIN)
115-
cur = cur.Append(FILE_PATH_LITERAL("Crashpad"));
128+
cur = cur.Append(FILE_PATH_LITERAL("Crashpad"));
129+
#else
130+
cur = cur.Append(FILE_PATH_LITERAL("Crash Reports"));
131+
#endif
132+
create_dir = true;
133+
break;
134+
case chrome::DIR_APP_DICTIONARIES:
135+
// TODO(nornagon): can we just default to using Chrome's logic here?
136+
if (!base::PathService::Get(chrome::DIR_USER_DATA, &cur))
137+
return false;
138+
cur = cur.Append(base::FilePath::FromUTF8Unsafe("Dictionaries"));
139+
create_dir = true;
140+
break;
141+
case DIR_USER_CACHE: {
142+
#if defined(OS_POSIX)
143+
int parent_key = base::DIR_CACHE;
116144
#else
117-
cur = cur.Append(FILE_PATH_LITERAL("Crash Reports"));
145+
// On Windows, there's no OS-level centralized location for caches, so
146+
// store the cache in the app data directory.
147+
int parent_key = base::DIR_APP_DATA;
118148
#endif
149+
if (!base::PathService::Get(parent_key, &cur))
150+
return false;
151+
cur = cur.Append(base::FilePath::FromUTF8Unsafe(GetApplicationName()));
152+
create_dir = true;
153+
break;
154+
}
155+
#if defined(OS_LINUX)
156+
case DIR_APP_DATA: {
157+
auto env = base::Environment::Create();
158+
cur = base::nix::GetXDGDirectory(
159+
env.get(), base::nix::kXdgConfigHomeEnvVar, base::nix::kDotConfigDir);
160+
break;
161+
}
162+
#endif
163+
#if defined(OS_WIN)
164+
case DIR_RECENT:
165+
if (!platform_util::GetFolderPath(DIR_RECENT, &cur))
166+
return false;
167+
create_dir = true;
168+
break;
169+
#endif
170+
case DIR_APP_LOGS:
171+
#if defined(OS_MAC)
172+
if (!base::PathService::Get(base::DIR_HOME, &cur))
173+
return false;
174+
cur = cur.Append(FILE_PATH_LITERAL("Library"));
175+
cur = cur.Append(FILE_PATH_LITERAL("Logs"));
176+
cur = cur.Append(base::FilePath::FromUTF8Unsafe(GetApplicationName()));
177+
#else
178+
if (!base::PathService::Get(chrome::DIR_USER_DATA, &cur))
179+
return false;
180+
cur = cur.Append(base::FilePath::FromUTF8Unsafe("logs"));
181+
#endif
182+
create_dir = true;
183+
break;
184+
default:
185+
return false;
186+
}
187+
119188
// TODO(bauerb): http://crbug.com/259796
120189
base::ThreadRestrictions::ScopedAllowIO allow_io;
121-
if (!base::PathExists(cur) && !base::CreateDirectory(cur))
190+
if (create_dir && !base::PathExists(cur) && !base::CreateDirectory(cur)) {
122191
return false;
123-
*path = cur;
124-
return true;
125-
}
126-
127-
bool ElectronPathProvider(int key, base::FilePath* path) {
128-
if (key == DIR_CRASH_DUMPS) {
129-
return GetDefaultCrashDumpsPath(path);
130192
}
131-
return false;
193+
194+
*result = cur;
195+
196+
return true;
132197
}
133198

134199
void RegisterPathProvider() {
@@ -281,6 +346,13 @@ void ElectronMainDelegate::PreSandboxStartup() {
281346
std::string process_type =
282347
command_line->GetSwitchValueASCII(::switches::kProcessType);
283348

349+
base::FilePath user_data_dir =
350+
command_line->GetSwitchValuePath(::switches::kUserDataDir);
351+
if (!user_data_dir.empty()) {
352+
base::PathService::OverrideAndCreateIfNeeded(chrome::DIR_USER_DATA,
353+
user_data_dir, false, true);
354+
}
355+
284356
#if !defined(MAS_BUILD)
285357
crash_reporter::InitializeCrashKeys();
286358
#endif

shell/browser/api/electron_api_app.cc

Lines changed: 11 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -441,9 +441,13 @@ int GetPathConstant(const std::string& name) {
441441
if (name == "appData")
442442
return DIR_APP_DATA;
443443
else if (name == "userData")
444-
return DIR_USER_DATA;
444+
return chrome::DIR_USER_DATA;
445445
else if (name == "cache")
446-
return DIR_CACHE;
446+
#if defined(OS_POSIX)
447+
return base::DIR_CACHE;
448+
#else
449+
return base::DIR_APP_DATA;
450+
#endif
447451
else if (name == "userCache")
448452
return DIR_USER_CACHE;
449453
else if (name == "logs")
@@ -930,8 +934,7 @@ void App::SetAppLogsPath(gin_helper::ErrorThrower thrower,
930934
}
931935
} else {
932936
base::FilePath path;
933-
if (base::PathService::Get(DIR_USER_DATA, &path)) {
934-
path = path.Append(base::FilePath::FromUTF8Unsafe(GetApplicationName()));
937+
if (base::PathService::Get(chrome::DIR_USER_DATA, &path)) {
935938
path = path.Append(base::FilePath::FromUTF8Unsafe("logs"));
936939
{
937940
base::ThreadRestrictions::ScopedAllowIO allow_io;
@@ -962,30 +965,10 @@ bool App::IsPackaged() {
962965

963966
base::FilePath App::GetPath(gin_helper::ErrorThrower thrower,
964967
const std::string& name) {
965-
bool succeed = false;
966968
base::FilePath path;
967969

968970
int key = GetPathConstant(name);
969-
if (key >= 0) {
970-
succeed = base::PathService::Get(key, &path);
971-
// If users try to get the logs path before setting a logs path,
972-
// set the path to a sensible default and then try to get it again
973-
if (!succeed && name == "logs") {
974-
SetAppLogsPath(thrower, absl::optional<base::FilePath>());
975-
succeed = base::PathService::Get(key, &path);
976-
}
977-
978-
#if defined(OS_WIN)
979-
// If we get the "recent" path before setting it, set it
980-
if (!succeed && name == "recent" &&
981-
platform_util::GetFolderPath(DIR_RECENT, &path)) {
982-
base::ThreadRestrictions::ScopedAllowIO allow_io;
983-
succeed = base::PathService::Override(DIR_RECENT, path);
984-
}
985-
#endif
986-
}
987-
988-
if (!succeed)
971+
if (key < 0 || !base::PathService::Get(key, &path))
989972
thrower.ThrowError("Failed to get '" + name + "' path");
990973

991974
return path;
@@ -999,20 +982,9 @@ void App::SetPath(gin_helper::ErrorThrower thrower,
999982
return;
1000983
}
1001984

1002-
bool succeed = false;
1003985
int key = GetPathConstant(name);
1004-
if (key >= 0) {
1005-
succeed =
1006-
base::PathService::OverrideAndCreateIfNeeded(key, path, true, false);
1007-
if (key == DIR_USER_DATA) {
1008-
succeed |= base::PathService::OverrideAndCreateIfNeeded(
1009-
chrome::DIR_USER_DATA, path, true, false);
1010-
succeed |= base::PathService::Override(
1011-
chrome::DIR_APP_DICTIONARIES,
1012-
path.Append(base::FilePath::FromUTF8Unsafe("Dictionaries")));
1013-
}
1014-
}
1015-
if (!succeed)
986+
if (key < 0 || !base::PathService::OverrideAndCreateIfNeeded(
987+
key, path, /* is_absolute = */ true, /* create = */ false))
1016988
thrower.ThrowError("Failed to set path");
1017989
}
1018990

@@ -1082,7 +1054,7 @@ bool App::RequestSingleInstanceLock() {
10821054
return true;
10831055

10841056
base::FilePath user_dir;
1085-
base::PathService::Get(DIR_USER_DATA, &user_dir);
1057+
base::PathService::Get(chrome::DIR_USER_DATA, &user_dir);
10861058

10871059
auto cb = base::BindRepeating(&App::OnSecondInstance, base::Unretained(this));
10881060

shell/browser/api/electron_api_crash_reporter.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ void Start(const std::string& submit_url,
172172
for (const auto& pair : extra)
173173
electron::crash_keys::SetCrashKey(pair.first, pair.second);
174174
base::FilePath user_data_dir;
175-
base::PathService::Get(DIR_USER_DATA, &user_data_dir);
175+
base::PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
176176
::crash_reporter::InitializeCrashpadWithEmbeddedHandler(
177177
process_type.empty(), process_type,
178178
base::WideToUTF8(user_data_dir.value()), base::FilePath());

shell/browser/browser.cc

100755100644
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "base/run_loop.h"
1515
#include "base/threading/thread_restrictions.h"
1616
#include "base/threading/thread_task_runner_handle.h"
17+
#include "chrome/common/chrome_paths.h"
1718
#include "shell/browser/browser_observer.h"
1819
#include "shell/browser/electron_browser_main_parts.h"
1920
#include "shell/browser/login_handler.h"
@@ -187,7 +188,7 @@ void Browser::DidFinishLaunching(base::DictionaryValue launch_info) {
187188
// Make sure the userData directory is created.
188189
base::ThreadRestrictions::ScopedAllowIO allow_io;
189190
base::FilePath user_data;
190-
if (base::PathService::Get(DIR_USER_DATA, &user_data))
191+
if (base::PathService::Get(chrome::DIR_USER_DATA, &user_data))
191192
base::CreateDirectoryAndGetError(user_data, nullptr);
192193

193194
is_ready_ = true;

shell/browser/browser_process_impl.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "base/command_line.h"
1212
#include "base/files/file_path.h"
1313
#include "base/path_service.h"
14+
#include "chrome/common/chrome_paths.h"
1415
#include "chrome/common/chrome_switches.h"
1516
#include "components/os_crypt/os_crypt.h"
1617
#include "components/prefs/in_memory_pref_store.h"
@@ -104,7 +105,7 @@ void BrowserProcessImpl::PostEarlyInitialization() {
104105
// is the only key that needs it
105106
if (electron::fuses::IsCookieEncryptionEnabled()) {
106107
base::FilePath prefs_path;
107-
CHECK(base::PathService::Get(electron::DIR_USER_DATA, &prefs_path));
108+
CHECK(base::PathService::Get(chrome::DIR_USER_DATA, &prefs_path));
108109
prefs_path = prefs_path.Append(FILE_PATH_LITERAL("Local State"));
109110
base::ThreadRestrictions::ScopedAllowIO allow_io;
110111
scoped_refptr<JsonPrefStore> user_pref_store =

shell/browser/electron_browser_client.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,7 +1023,7 @@ void ElectronBrowserClient::OnNetworkServiceCreated(
10231023
std::vector<base::FilePath>
10241024
ElectronBrowserClient::GetNetworkContextsParentDirectory() {
10251025
base::FilePath user_data_dir;
1026-
base::PathService::Get(DIR_USER_DATA, &user_data_dir);
1026+
base::PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
10271027
DCHECK(!user_data_dir.empty());
10281028

10291029
return {user_data_dir};
@@ -1416,7 +1416,7 @@ std::string ElectronBrowserClient::GetApplicationLocale() {
14161416

14171417
base::FilePath ElectronBrowserClient::GetFontLookupTableCacheDir() {
14181418
base::FilePath user_data_dir;
1419-
base::PathService::Get(DIR_USER_DATA, &user_data_dir);
1419+
base::PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
14201420
DCHECK(!user_data_dir.empty());
14211421
return user_data_dir.Append(FILE_PATH_LITERAL("FontLookupTableCache"));
14221422
}

0 commit comments

Comments
 (0)