refactor: use PathProvider for user-data-dir and others (#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
This commit is contained in:
parent
8b945cb296
commit
ebf54d7cc0
16 changed files with 115 additions and 108 deletions
|
@ -441,9 +441,13 @@ int GetPathConstant(const std::string& name) {
|
|||
if (name == "appData")
|
||||
return DIR_APP_DATA;
|
||||
else if (name == "userData")
|
||||
return DIR_USER_DATA;
|
||||
return chrome::DIR_USER_DATA;
|
||||
else if (name == "cache")
|
||||
return DIR_CACHE;
|
||||
#if defined(OS_POSIX)
|
||||
return base::DIR_CACHE;
|
||||
#else
|
||||
return base::DIR_APP_DATA;
|
||||
#endif
|
||||
else if (name == "userCache")
|
||||
return DIR_USER_CACHE;
|
||||
else if (name == "logs")
|
||||
|
@ -930,8 +934,7 @@ void App::SetAppLogsPath(gin_helper::ErrorThrower thrower,
|
|||
}
|
||||
} else {
|
||||
base::FilePath path;
|
||||
if (base::PathService::Get(DIR_USER_DATA, &path)) {
|
||||
path = path.Append(base::FilePath::FromUTF8Unsafe(GetApplicationName()));
|
||||
if (base::PathService::Get(chrome::DIR_USER_DATA, &path)) {
|
||||
path = path.Append(base::FilePath::FromUTF8Unsafe("logs"));
|
||||
{
|
||||
base::ThreadRestrictions::ScopedAllowIO allow_io;
|
||||
|
@ -962,30 +965,10 @@ bool App::IsPackaged() {
|
|||
|
||||
base::FilePath App::GetPath(gin_helper::ErrorThrower thrower,
|
||||
const std::string& name) {
|
||||
bool succeed = false;
|
||||
base::FilePath path;
|
||||
|
||||
int key = GetPathConstant(name);
|
||||
if (key >= 0) {
|
||||
succeed = base::PathService::Get(key, &path);
|
||||
// If users try to get the logs path before setting a logs path,
|
||||
// set the path to a sensible default and then try to get it again
|
||||
if (!succeed && name == "logs") {
|
||||
SetAppLogsPath(thrower, absl::optional<base::FilePath>());
|
||||
succeed = base::PathService::Get(key, &path);
|
||||
}
|
||||
|
||||
#if defined(OS_WIN)
|
||||
// If we get the "recent" path before setting it, set it
|
||||
if (!succeed && name == "recent" &&
|
||||
platform_util::GetFolderPath(DIR_RECENT, &path)) {
|
||||
base::ThreadRestrictions::ScopedAllowIO allow_io;
|
||||
succeed = base::PathService::Override(DIR_RECENT, path);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
if (!succeed)
|
||||
if (key < 0 || !base::PathService::Get(key, &path))
|
||||
thrower.ThrowError("Failed to get '" + name + "' path");
|
||||
|
||||
return path;
|
||||
|
@ -999,20 +982,9 @@ void App::SetPath(gin_helper::ErrorThrower thrower,
|
|||
return;
|
||||
}
|
||||
|
||||
bool succeed = false;
|
||||
int key = GetPathConstant(name);
|
||||
if (key >= 0) {
|
||||
succeed =
|
||||
base::PathService::OverrideAndCreateIfNeeded(key, path, true, false);
|
||||
if (key == DIR_USER_DATA) {
|
||||
succeed |= base::PathService::OverrideAndCreateIfNeeded(
|
||||
chrome::DIR_USER_DATA, path, true, false);
|
||||
succeed |= base::PathService::Override(
|
||||
chrome::DIR_APP_DICTIONARIES,
|
||||
path.Append(base::FilePath::FromUTF8Unsafe("Dictionaries")));
|
||||
}
|
||||
}
|
||||
if (!succeed)
|
||||
if (key < 0 || !base::PathService::OverrideAndCreateIfNeeded(
|
||||
key, path, /* is_absolute = */ true, /* create = */ false))
|
||||
thrower.ThrowError("Failed to set path");
|
||||
}
|
||||
|
||||
|
@ -1082,7 +1054,7 @@ bool App::RequestSingleInstanceLock() {
|
|||
return true;
|
||||
|
||||
base::FilePath user_dir;
|
||||
base::PathService::Get(DIR_USER_DATA, &user_dir);
|
||||
base::PathService::Get(chrome::DIR_USER_DATA, &user_dir);
|
||||
|
||||
auto cb = base::BindRepeating(&App::OnSecondInstance, base::Unretained(this));
|
||||
|
||||
|
|
|
@ -172,7 +172,7 @@ void Start(const std::string& submit_url,
|
|||
for (const auto& pair : extra)
|
||||
electron::crash_keys::SetCrashKey(pair.first, pair.second);
|
||||
base::FilePath user_data_dir;
|
||||
base::PathService::Get(DIR_USER_DATA, &user_data_dir);
|
||||
base::PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
|
||||
::crash_reporter::InitializeCrashpadWithEmbeddedHandler(
|
||||
process_type.empty(), process_type,
|
||||
base::WideToUTF8(user_data_dir.value()), base::FilePath());
|
||||
|
|
3
shell/browser/browser.cc
Executable file → Normal file
3
shell/browser/browser.cc
Executable file → Normal file
|
@ -14,6 +14,7 @@
|
|||
#include "base/run_loop.h"
|
||||
#include "base/threading/thread_restrictions.h"
|
||||
#include "base/threading/thread_task_runner_handle.h"
|
||||
#include "chrome/common/chrome_paths.h"
|
||||
#include "shell/browser/browser_observer.h"
|
||||
#include "shell/browser/electron_browser_main_parts.h"
|
||||
#include "shell/browser/login_handler.h"
|
||||
|
@ -187,7 +188,7 @@ void Browser::DidFinishLaunching(base::DictionaryValue launch_info) {
|
|||
// Make sure the userData directory is created.
|
||||
base::ThreadRestrictions::ScopedAllowIO allow_io;
|
||||
base::FilePath user_data;
|
||||
if (base::PathService::Get(DIR_USER_DATA, &user_data))
|
||||
if (base::PathService::Get(chrome::DIR_USER_DATA, &user_data))
|
||||
base::CreateDirectoryAndGetError(user_data, nullptr);
|
||||
|
||||
is_ready_ = true;
|
||||
|
|
|
@ -11,6 +11,7 @@
|
|||
#include "base/command_line.h"
|
||||
#include "base/files/file_path.h"
|
||||
#include "base/path_service.h"
|
||||
#include "chrome/common/chrome_paths.h"
|
||||
#include "chrome/common/chrome_switches.h"
|
||||
#include "components/os_crypt/os_crypt.h"
|
||||
#include "components/prefs/in_memory_pref_store.h"
|
||||
|
@ -104,7 +105,7 @@ void BrowserProcessImpl::PostEarlyInitialization() {
|
|||
// is the only key that needs it
|
||||
if (electron::fuses::IsCookieEncryptionEnabled()) {
|
||||
base::FilePath prefs_path;
|
||||
CHECK(base::PathService::Get(electron::DIR_USER_DATA, &prefs_path));
|
||||
CHECK(base::PathService::Get(chrome::DIR_USER_DATA, &prefs_path));
|
||||
prefs_path = prefs_path.Append(FILE_PATH_LITERAL("Local State"));
|
||||
base::ThreadRestrictions::ScopedAllowIO allow_io;
|
||||
scoped_refptr<JsonPrefStore> user_pref_store =
|
||||
|
|
|
@ -1023,7 +1023,7 @@ void ElectronBrowserClient::OnNetworkServiceCreated(
|
|||
std::vector<base::FilePath>
|
||||
ElectronBrowserClient::GetNetworkContextsParentDirectory() {
|
||||
base::FilePath user_data_dir;
|
||||
base::PathService::Get(DIR_USER_DATA, &user_data_dir);
|
||||
base::PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
|
||||
DCHECK(!user_data_dir.empty());
|
||||
|
||||
return {user_data_dir};
|
||||
|
@ -1416,7 +1416,7 @@ std::string ElectronBrowserClient::GetApplicationLocale() {
|
|||
|
||||
base::FilePath ElectronBrowserClient::GetFontLookupTableCacheDir() {
|
||||
base::FilePath user_data_dir;
|
||||
base::PathService::Get(DIR_USER_DATA, &user_data_dir);
|
||||
base::PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
|
||||
DCHECK(!user_data_dir.empty());
|
||||
return user_data_dir.Append(FILE_PATH_LITERAL("FontLookupTableCache"));
|
||||
}
|
||||
|
|
|
@ -117,14 +117,10 @@ ElectronBrowserContext::ElectronBrowserContext(const std::string& partition,
|
|||
base::StringToInt(command_line->GetSwitchValueASCII(switches::kDiskCacheSize),
|
||||
&max_cache_size_);
|
||||
|
||||
if (!base::PathService::Get(DIR_USER_DATA, &path_)) {
|
||||
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(DIR_USER_DATA, path_);
|
||||
base::PathService::Override(chrome::DIR_USER_DATA, path_);
|
||||
base::PathService::Override(
|
||||
chrome::DIR_APP_DICTIONARIES,
|
||||
path_.Append(base::FilePath::FromUTF8Unsafe("Dictionaries")));
|
||||
}
|
||||
|
||||
if (!in_memory && !partition.empty())
|
||||
|
|
|
@ -61,7 +61,6 @@
|
|||
|
||||
#if defined(OS_LINUX)
|
||||
#include "base/environment.h"
|
||||
#include "base/nix/xdg_util.h"
|
||||
#include "base/threading/thread_task_runner_handle.h"
|
||||
#include "ui/gtk/gtk_ui_factory.h"
|
||||
#include "ui/gtk/gtk_util.h"
|
||||
|
@ -155,16 +154,6 @@ std::u16string MediaStringProvider(media::MessageId id) {
|
|||
}
|
||||
|
||||
#if defined(OS_LINUX)
|
||||
void OverrideLinuxAppDataPath() {
|
||||
base::FilePath path;
|
||||
if (base::PathService::Get(DIR_APP_DATA, &path))
|
||||
return;
|
||||
auto env = base::Environment::Create();
|
||||
path = base::nix::GetXDGDirectory(env.get(), base::nix::kXdgConfigHomeEnvVar,
|
||||
base::nix::kDotConfigDir);
|
||||
base::PathService::Override(DIR_APP_DATA, path);
|
||||
}
|
||||
|
||||
// GTK does not provide a way to check if current theme is dark, so we compare
|
||||
// the text and background luminosity to get a result.
|
||||
// This trick comes from FireFox.
|
||||
|
@ -232,10 +221,6 @@ int ElectronBrowserMainParts::GetExitCode() const {
|
|||
|
||||
int ElectronBrowserMainParts::PreEarlyInitialization() {
|
||||
field_trial_list_ = std::make_unique<base::FieldTrialList>(nullptr);
|
||||
#if defined(OS_LINUX)
|
||||
OverrideLinuxAppDataPath();
|
||||
#endif
|
||||
|
||||
#if defined(OS_POSIX)
|
||||
HandleSIGCHLD();
|
||||
#endif
|
||||
|
|
|
@ -12,6 +12,7 @@
|
|||
#include "base/path_service.h"
|
||||
#include "chrome/browser/browser_process.h"
|
||||
#include "chrome/browser/net/chrome_mojo_proxy_resolver_factory.h"
|
||||
#include "chrome/common/chrome_paths.h"
|
||||
#include "chrome/common/chrome_switches.h"
|
||||
#include "components/os_crypt/os_crypt.h"
|
||||
#include "content/public/browser/browser_thread.h"
|
||||
|
@ -257,7 +258,7 @@ void SystemNetworkContextManager::OnNetworkServiceCreated(
|
|||
command_line.GetSwitchValueASCII(::switches::kPasswordStore);
|
||||
config->should_use_preference =
|
||||
command_line.HasSwitch(::switches::kEnableEncryptionSelection);
|
||||
base::PathService::Get(electron::DIR_USER_DATA, &config->user_data_path);
|
||||
base::PathService::Get(chrome::DIR_USER_DATA, &config->user_data_path);
|
||||
network_service->SetCryptConfig(std::move(config));
|
||||
#else
|
||||
network_service->SetEncryptionKey(OSCrypt::GetRawEncryptionKey());
|
||||
|
|
|
@ -14,6 +14,7 @@
|
|||
#include "base/strings/string_number_conversions.h"
|
||||
#include "base/strings/stringprintf.h"
|
||||
#include "base/strings/utf_string_conversions.h"
|
||||
#include "chrome/common/chrome_paths.h"
|
||||
#include "content/public/browser/devtools_agent_host.h"
|
||||
#include "content/public/browser/devtools_frontend_host.h"
|
||||
#include "content/public/browser/devtools_socket_factory.h"
|
||||
|
@ -89,7 +90,7 @@ const char kBrowserCloseMethod[] = "Browser.close";
|
|||
// static
|
||||
void DevToolsManagerDelegate::StartHttpHandler() {
|
||||
base::FilePath user_dir;
|
||||
base::PathService::Get(DIR_USER_DATA, &user_dir);
|
||||
base::PathService::Get(chrome::DIR_USER_DATA, &user_dir);
|
||||
content::DevToolsAgentHost::StartRemoteDebuggingServer(
|
||||
CreateSocketFactory(), user_dir, base::FilePath());
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue