2020-07-06 20:14:57 +02:00
|
|
|
// Copyright 2017 The Chromium Embedded Framework Authors. Portions copyright
|
|
|
|
// 2011 The Chromium Authors. All rights reserved. Use of this source code is
|
|
|
|
// governed by a BSD-style license that can be found in the LICENSE file.
|
|
|
|
|
2024-04-30 17:45:07 +02:00
|
|
|
#include "cef/libcef/common/resource_util.h"
|
2020-07-06 20:14:57 +02:00
|
|
|
|
2022-01-24 18:58:02 +01:00
|
|
|
#if BUILDFLAG(IS_LINUX)
|
2020-07-06 20:14:57 +02:00
|
|
|
#include <dlfcn.h>
|
|
|
|
#endif
|
|
|
|
|
2024-04-30 03:09:17 +02:00
|
|
|
#include "base/base_paths.h"
|
2020-07-06 20:14:57 +02:00
|
|
|
#include "base/command_line.h"
|
|
|
|
#include "base/files/file_path.h"
|
2023-12-05 00:45:54 +01:00
|
|
|
#include "base/logging.h"
|
2020-07-06 20:14:57 +02:00
|
|
|
#include "base/path_service.h"
|
|
|
|
#include "chrome/common/chrome_paths.h"
|
|
|
|
#include "chrome/common/chrome_switches.h"
|
2024-04-30 03:09:17 +02:00
|
|
|
|
|
|
|
#if BUILDFLAG(IS_LINUX)
|
|
|
|
#include "base/environment.h"
|
|
|
|
#include "base/nix/xdg_util.h"
|
|
|
|
#endif
|
|
|
|
|
2024-05-02 20:29:09 +02:00
|
|
|
#if BUILDFLAG(IS_MAC)
|
|
|
|
#include "cef/libcef/common/util_mac.h"
|
|
|
|
#endif
|
|
|
|
|
2020-07-06 20:14:57 +02:00
|
|
|
namespace resource_util {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2022-01-24 18:58:02 +01:00
|
|
|
#if BUILDFLAG(IS_LINUX)
|
2020-07-06 20:14:57 +02:00
|
|
|
|
|
|
|
// Based on chrome/common/chrome_paths_linux.cc.
|
|
|
|
// See http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
|
2021-08-11 19:15:03 +02:00
|
|
|
// for a spec on where config files go. The net result on most systems is that
|
|
|
|
// we use "~/.config/cef_user_data".
|
2020-07-06 20:14:57 +02:00
|
|
|
bool GetDefaultUserDataDirectory(base::FilePath* result) {
|
|
|
|
std::unique_ptr<base::Environment> env(base::Environment::Create());
|
|
|
|
base::FilePath config_dir(base::nix::GetXDGDirectory(
|
|
|
|
env.get(), base::nix::kXdgConfigHomeEnvVar, base::nix::kDotConfigDir));
|
|
|
|
*result = config_dir.Append(FILE_PATH_LITERAL("cef_user_data"));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-01-24 18:58:02 +01:00
|
|
|
#elif BUILDFLAG(IS_MAC)
|
2020-07-06 20:14:57 +02:00
|
|
|
|
|
|
|
// Based on chrome/common/chrome_paths_mac.mm.
|
|
|
|
bool GetDefaultUserDataDirectory(base::FilePath* result) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!base::PathService::Get(base::DIR_APP_DATA, result)) {
|
2020-07-06 20:14:57 +02:00
|
|
|
return false;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2020-07-06 20:14:57 +02:00
|
|
|
*result = result->Append(FILE_PATH_LITERAL("CEF"));
|
|
|
|
*result = result->Append(FILE_PATH_LITERAL("User Data"));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-01-24 18:58:02 +01:00
|
|
|
#elif BUILDFLAG(IS_WIN)
|
2020-07-06 20:14:57 +02:00
|
|
|
|
|
|
|
// Based on chrome/common/chrome_paths_win.cc.
|
|
|
|
bool GetDefaultUserDataDirectory(base::FilePath* result) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!base::PathService::Get(base::DIR_LOCAL_APP_DATA, result)) {
|
2020-07-06 20:14:57 +02:00
|
|
|
return false;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2020-07-06 20:14:57 +02:00
|
|
|
*result = result->Append(FILE_PATH_LITERAL("CEF"));
|
|
|
|
*result = result->Append(FILE_PATH_LITERAL("User Data"));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
base::FilePath GetUserDataPath(CefSettings* settings,
|
|
|
|
const base::CommandLine* command_line) {
|
|
|
|
// |settings| will be non-nullptr in the main process only.
|
2021-04-07 00:09:45 +02:00
|
|
|
if (settings) {
|
2023-06-06 16:41:53 +02:00
|
|
|
CefString root_cache_path;
|
|
|
|
if (settings->root_cache_path.length > 0) {
|
|
|
|
root_cache_path = CefString(&settings->root_cache_path);
|
2021-04-07 00:09:45 +02:00
|
|
|
}
|
2023-06-06 16:41:53 +02:00
|
|
|
if (!root_cache_path.empty()) {
|
|
|
|
return base::FilePath(root_cache_path);
|
2023-12-05 00:45:54 +01:00
|
|
|
} else {
|
|
|
|
LOG(WARNING) << "Please customize CefSettings.root_cache_path for your "
|
|
|
|
"application. Use of the default value may lead to "
|
|
|
|
"unintended process singleton behavior.";
|
2021-04-07 00:09:45 +02:00
|
|
|
}
|
|
|
|
}
|
2020-07-06 20:14:57 +02:00
|
|
|
|
|
|
|
// This may be set for sub-processes.
|
|
|
|
base::FilePath result =
|
|
|
|
command_line->GetSwitchValuePath(switches::kUserDataDir);
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!result.empty()) {
|
2020-07-06 20:14:57 +02:00
|
|
|
return result;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2020-07-06 20:14:57 +02:00
|
|
|
|
2023-01-02 23:59:03 +01:00
|
|
|
if (GetDefaultUserDataDirectory(&result)) {
|
2020-07-06 20:14:57 +02:00
|
|
|
return result;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2020-07-06 20:14:57 +02:00
|
|
|
|
2023-01-02 23:59:03 +01:00
|
|
|
if (base::PathService::Get(base::DIR_TEMP, &result)) {
|
2020-07-06 20:14:57 +02:00
|
|
|
return result;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2020-07-06 20:14:57 +02:00
|
|
|
|
2023-05-08 17:07:57 +02:00
|
|
|
DCHECK(false);
|
2020-07-06 20:14:57 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2024-04-30 03:09:17 +02:00
|
|
|
} // namespace
|
|
|
|
|
2024-05-02 20:29:09 +02:00
|
|
|
#if BUILDFLAG(IS_MAC)
|
|
|
|
|
|
|
|
base::FilePath GetResourcesDir() {
|
|
|
|
return util_mac::GetFrameworkResourcesDirectory();
|
|
|
|
}
|
|
|
|
|
|
|
|
#else // !BUILDFLAG(IS_MAC)
|
|
|
|
|
|
|
|
base::FilePath GetResourcesDir() {
|
|
|
|
base::FilePath pak_dir;
|
|
|
|
base::PathService::Get(base::DIR_ASSETS, &pak_dir);
|
|
|
|
return pak_dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // !BUILDFLAG(IS_MAC)
|
|
|
|
|
2020-07-06 20:14:57 +02:00
|
|
|
void OverrideUserDataDir(CefSettings* settings,
|
|
|
|
const base::CommandLine* command_line) {
|
|
|
|
const base::FilePath& user_data_path =
|
|
|
|
GetUserDataPath(settings, command_line);
|
|
|
|
base::PathService::Override(chrome::DIR_USER_DATA, user_data_path);
|
|
|
|
|
|
|
|
// Path used for crash dumps.
|
|
|
|
base::PathService::Override(chrome::DIR_CRASH_DUMPS, user_data_path);
|
|
|
|
|
|
|
|
// Path used for spell checking dictionary files.
|
|
|
|
base::PathService::OverrideAndCreateIfNeeded(
|
2021-08-11 19:15:03 +02:00
|
|
|
chrome::DIR_APP_DICTIONARIES,
|
|
|
|
user_data_path.Append(FILE_PATH_LITERAL("Dictionaries")),
|
2020-07-06 20:14:57 +02:00
|
|
|
false, // May not be an absolute path.
|
|
|
|
true); // Create if necessary.
|
|
|
|
}
|
|
|
|
|
2022-01-24 18:58:02 +01:00
|
|
|
#if BUILDFLAG(IS_LINUX)
|
2020-07-06 20:14:57 +02:00
|
|
|
void OverrideAssetPath() {
|
|
|
|
Dl_info dl_info;
|
|
|
|
if (dladdr(reinterpret_cast<const void*>(&OverrideAssetPath), &dl_info)) {
|
|
|
|
base::FilePath path = base::FilePath(dl_info.dli_fname).DirName();
|
|
|
|
base::PathService::Override(base::DIR_ASSETS, path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
} // namespace resource_util
|