2014-10-31 18:17:05 +00:00
|
|
|
// Copyright (c) 2013 GitHub, Inc.
|
2014-04-25 09:49:37 +00:00
|
|
|
// Use of this source code is governed by the MIT license that can be
|
2013-04-15 16:25:08 +00:00
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2020-02-04 20:19:40 +00:00
|
|
|
#include "shell/browser/electron_browser_context.h"
|
2013-04-15 16:25:08 +00:00
|
|
|
|
2019-09-16 22:12:00 +00:00
|
|
|
#include <memory>
|
|
|
|
|
2018-10-04 18:08:56 +00:00
|
|
|
#include <utility>
|
|
|
|
|
2021-02-09 20:16:21 +00:00
|
|
|
#include "base/barrier_closure.h"
|
2021-06-17 21:17:25 +00:00
|
|
|
#include "base/base_paths.h"
|
2015-01-05 21:40:38 +00:00
|
|
|
#include "base/command_line.h"
|
2015-07-26 08:17:55 +00:00
|
|
|
#include "base/files/file_path.h"
|
2020-08-17 20:21:53 +00:00
|
|
|
#include "base/no_destructor.h"
|
2016-01-21 08:21:37 +00:00
|
|
|
#include "base/path_service.h"
|
2022-05-17 16:48:40 +00:00
|
|
|
#include "base/strings/escape.h"
|
2018-10-04 18:08:56 +00:00
|
|
|
#include "base/strings/string_util.h"
|
|
|
|
#include "base/threading/sequenced_task_runner_handle.h"
|
|
|
|
#include "base/threading/thread_restrictions.h"
|
2016-01-21 08:21:37 +00:00
|
|
|
#include "chrome/common/chrome_paths.h"
|
2015-07-26 08:17:55 +00:00
|
|
|
#include "chrome/common/pref_names.h"
|
2018-10-11 13:52:12 +00:00
|
|
|
#include "components/keyed_service/content/browser_context_dependency_manager.h"
|
2018-10-04 18:08:56 +00:00
|
|
|
#include "components/prefs/json_pref_store.h"
|
2016-06-15 11:31:29 +00:00
|
|
|
#include "components/prefs/pref_registry_simple.h"
|
2018-10-04 18:08:56 +00:00
|
|
|
#include "components/prefs/pref_service.h"
|
|
|
|
#include "components/prefs/pref_service_factory.h"
|
|
|
|
#include "components/prefs/value_map_pref_store.h"
|
|
|
|
#include "components/proxy_config/pref_proxy_config_tracker_impl.h"
|
|
|
|
#include "components/proxy_config/proxy_config_pref_names.h"
|
2019-03-05 05:08:55 +00:00
|
|
|
#include "content/browser/blob_storage/chrome_blob_storage_context.h" // nogncheck
|
2019-01-22 19:50:25 +00:00
|
|
|
#include "content/public/browser/browser_thread.h"
|
2021-02-09 20:16:21 +00:00
|
|
|
#include "content/public/browser/cors_origin_pattern_setter.h"
|
2022-08-22 21:15:32 +00:00
|
|
|
#include "content/public/browser/render_process_host.h"
|
2021-02-09 20:16:21 +00:00
|
|
|
#include "content/public/browser/shared_cors_origin_access_list.h"
|
2018-10-04 18:08:56 +00:00
|
|
|
#include "content/public/browser/storage_partition.h"
|
2022-08-22 21:15:32 +00:00
|
|
|
#include "content/public/browser/web_contents_media_capture_id.h"
|
|
|
|
#include "media/audio/audio_device_description.h"
|
2019-03-26 01:10:48 +00:00
|
|
|
#include "services/network/public/cpp/features.h"
|
2019-08-29 06:07:46 +00:00
|
|
|
#include "services/network/public/cpp/wrapper_shared_url_loader_factory.h"
|
2019-10-28 22:12:35 +00:00
|
|
|
#include "services/network/public/mojom/network_context.mojom.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/cookie_change_notifier.h"
|
2020-02-04 20:19:40 +00:00
|
|
|
#include "shell/browser/electron_browser_client.h"
|
|
|
|
#include "shell/browser/electron_browser_main_parts.h"
|
|
|
|
#include "shell/browser/electron_download_manager_delegate.h"
|
|
|
|
#include "shell/browser/electron_permission_manager.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/net/resolve_proxy_helper.h"
|
|
|
|
#include "shell/browser/pref_store_delegate.h"
|
2020-03-26 17:34:32 +00:00
|
|
|
#include "shell/browser/protocol_registry.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/special_storage_policy.h"
|
2020-07-15 18:27:42 +00:00
|
|
|
#include "shell/browser/ui/inspectable_web_contents.h"
|
2022-06-27 20:50:08 +00:00
|
|
|
#include "shell/browser/web_contents_permission_helper.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/web_view_manager.h"
|
|
|
|
#include "shell/browser/zoom_level_delegate.h"
|
|
|
|
#include "shell/common/application_info.h"
|
2020-05-07 20:31:26 +00:00
|
|
|
#include "shell/common/electron_paths.h"
|
2022-08-22 21:15:32 +00:00
|
|
|
#include "shell/common/gin_converters/frame_converter.h"
|
|
|
|
#include "shell/common/gin_helper/error_thrower.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/common/options_switches.h"
|
2022-08-22 21:15:32 +00:00
|
|
|
#include "third_party/blink/public/mojom/mediastream/media_stream.mojom.h"
|
2018-10-04 18:08:56 +00:00
|
|
|
|
2019-07-24 23:01:08 +00:00
|
|
|
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
|
|
|
|
#include "extensions/browser/browser_context_keyed_service_factories.h"
|
|
|
|
#include "extensions/browser/extension_pref_store.h"
|
|
|
|
#include "extensions/browser/extension_pref_value_map_factory.h"
|
|
|
|
#include "extensions/browser/extension_prefs.h"
|
|
|
|
#include "extensions/browser/pref_names.h"
|
|
|
|
#include "extensions/common/extension_api.h"
|
2020-02-03 22:01:10 +00:00
|
|
|
#include "shell/browser/extensions/electron_browser_context_keyed_service_factories.h"
|
|
|
|
#include "shell/browser/extensions/electron_extension_system.h"
|
|
|
|
#include "shell/browser/extensions/electron_extension_system_factory.h"
|
|
|
|
#include "shell/browser/extensions/electron_extensions_browser_client.h"
|
|
|
|
#include "shell/common/extensions/electron_extensions_client.h"
|
2019-07-24 23:01:08 +00:00
|
|
|
#endif // BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
|
|
|
|
|
2019-10-31 20:11:51 +00:00
|
|
|
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS) || \
|
|
|
|
BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
|
|
|
|
#include "components/pref_registry/pref_registry_syncable.h"
|
|
|
|
#include "components/user_prefs/user_prefs.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
|
|
|
|
#include "base/i18n/rtl.h"
|
|
|
|
#include "components/language/core/browser/language_prefs.h"
|
|
|
|
#include "components/spellcheck/browser/pref_names.h"
|
|
|
|
#include "components/spellcheck/common/spellcheck_common.h"
|
|
|
|
#endif
|
|
|
|
|
2018-10-04 18:08:56 +00:00
|
|
|
using content::BrowserThread;
|
2013-09-20 08:47:47 +00:00
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
namespace electron {
|
2013-09-20 08:47:47 +00:00
|
|
|
|
2014-09-23 04:13:46 +00:00
|
|
|
namespace {
|
|
|
|
|
2018-10-04 18:08:56 +00:00
|
|
|
// Convert string to lower case and escape it.
|
|
|
|
std::string MakePartitionName(const std::string& input) {
|
2022-05-17 16:48:40 +00:00
|
|
|
return base::EscapePath(base::ToLowerASCII(input));
|
2018-10-04 18:08:56 +00:00
|
|
|
}
|
|
|
|
|
2014-09-23 04:13:46 +00:00
|
|
|
} // namespace
|
|
|
|
|
2018-10-04 18:08:56 +00:00
|
|
|
// static
|
2020-08-17 20:21:53 +00:00
|
|
|
ElectronBrowserContext::BrowserContextMap&
|
|
|
|
ElectronBrowserContext::browser_context_map() {
|
|
|
|
static base::NoDestructor<ElectronBrowserContext::BrowserContextMap>
|
|
|
|
browser_context_map;
|
|
|
|
return *browser_context_map;
|
|
|
|
}
|
2018-10-04 18:08:56 +00:00
|
|
|
|
2020-02-04 20:19:40 +00:00
|
|
|
ElectronBrowserContext::ElectronBrowserContext(const std::string& partition,
|
|
|
|
bool in_memory,
|
2022-07-05 15:25:18 +00:00
|
|
|
base::Value::Dict options)
|
2021-06-08 02:00:05 +00:00
|
|
|
: storage_policy_(base::MakeRefCounted<SpecialStoragePolicy>()),
|
2021-06-15 00:37:55 +00:00
|
|
|
protocol_registry_(base::WrapUnique(new ProtocolRegistry)),
|
2018-10-04 18:08:56 +00:00
|
|
|
in_memory_(in_memory),
|
2021-03-16 19:43:09 +00:00
|
|
|
ssl_config_(network::mojom::SSLConfig::New()) {
|
2016-07-12 13:05:07 +00:00
|
|
|
// Read options.
|
2018-08-14 21:07:53 +00:00
|
|
|
base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
|
2018-10-04 18:08:56 +00:00
|
|
|
use_cache_ = !command_line->HasSwitch(switches::kDisableHttpCache);
|
2022-07-05 15:25:18 +00:00
|
|
|
if (auto use_cache_opt = options.FindBool("cache")) {
|
2022-01-10 22:31:39 +00:00
|
|
|
use_cache_ = use_cache_opt.value();
|
|
|
|
}
|
2018-10-04 18:08:56 +00:00
|
|
|
|
|
|
|
base::StringToInt(command_line->GetSwitchValueASCII(switches::kDiskCacheSize),
|
|
|
|
&max_cache_size_);
|
|
|
|
|
2022-05-09 14:26:57 +00:00
|
|
|
base::PathService::Get(DIR_SESSION_DATA, &path_);
|
2018-10-04 18:08:56 +00:00
|
|
|
if (!in_memory && !partition.empty())
|
|
|
|
path_ = path_.Append(FILE_PATH_LITERAL("Partitions"))
|
|
|
|
.Append(base::FilePath::FromUTF8Unsafe(
|
|
|
|
MakePartitionName(partition)));
|
2016-07-26 11:04:04 +00:00
|
|
|
|
2019-07-24 23:01:08 +00:00
|
|
|
BrowserContextDependencyManager::GetInstance()->MarkBrowserContextLive(this);
|
|
|
|
|
2018-10-04 18:08:56 +00:00
|
|
|
// Initialize Pref Registry.
|
2016-07-26 11:04:04 +00:00
|
|
|
InitPrefs();
|
2018-10-04 18:08:56 +00:00
|
|
|
|
|
|
|
cookie_change_notifier_ = std::make_unique<CookieChangeNotifier>(this);
|
2018-10-11 13:52:12 +00:00
|
|
|
|
2019-07-24 23:01:08 +00:00
|
|
|
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
|
2020-05-08 18:17:28 +00:00
|
|
|
if (!in_memory_) {
|
|
|
|
BrowserContextDependencyManager::GetInstance()
|
|
|
|
->CreateBrowserContextServices(this);
|
|
|
|
|
|
|
|
extension_system_ = static_cast<extensions::ElectronExtensionSystem*>(
|
|
|
|
extensions::ExtensionSystem::Get(this));
|
|
|
|
extension_system_->InitForRegularProfile(true /* extensions_enabled */);
|
|
|
|
extension_system_->FinishInitialization();
|
|
|
|
}
|
2019-07-24 23:01:08 +00:00
|
|
|
#endif
|
2016-06-22 06:46:46 +00:00
|
|
|
}
|
|
|
|
|
2020-02-04 20:19:40 +00:00
|
|
|
ElectronBrowserContext::~ElectronBrowserContext() {
|
2018-10-04 18:08:56 +00:00
|
|
|
DCHECK_CURRENTLY_ON(BrowserThread::UI);
|
2021-06-03 08:05:04 +00:00
|
|
|
NotifyWillBeDestroyed();
|
2020-01-13 22:55:58 +00:00
|
|
|
// Notify any keyed services of browser context destruction.
|
|
|
|
BrowserContextDependencyManager::GetInstance()->DestroyBrowserContextServices(
|
|
|
|
this);
|
2018-10-04 18:08:56 +00:00
|
|
|
ShutdownStoragePartitions();
|
2019-03-26 01:10:48 +00:00
|
|
|
|
2019-08-02 23:56:46 +00:00
|
|
|
BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE,
|
|
|
|
std::move(resource_context_));
|
2018-10-04 18:08:56 +00:00
|
|
|
}
|
|
|
|
|
2020-02-04 20:19:40 +00:00
|
|
|
void ElectronBrowserContext::InitPrefs() {
|
2018-10-04 18:08:56 +00:00
|
|
|
auto prefs_path = GetPath().Append(FILE_PATH_LITERAL("Preferences"));
|
|
|
|
base::ThreadRestrictions::ScopedAllowIO allow_io;
|
|
|
|
PrefServiceFactory prefs_factory;
|
|
|
|
scoped_refptr<JsonPrefStore> pref_store =
|
|
|
|
base::MakeRefCounted<JsonPrefStore>(prefs_path);
|
|
|
|
pref_store->ReadPrefs(); // Synchronous.
|
|
|
|
prefs_factory.set_user_prefs(pref_store);
|
|
|
|
|
2019-07-24 23:01:08 +00:00
|
|
|
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
|
2020-05-08 18:17:28 +00:00
|
|
|
if (!in_memory_) {
|
|
|
|
auto* ext_pref_store = new ExtensionPrefStore(
|
|
|
|
ExtensionPrefValueMapFactory::GetForBrowserContext(this),
|
|
|
|
IsOffTheRecord());
|
|
|
|
prefs_factory.set_extension_prefs(ext_pref_store);
|
|
|
|
}
|
2019-10-31 20:11:51 +00:00
|
|
|
#endif
|
2019-07-24 23:01:08 +00:00
|
|
|
|
2019-10-31 20:11:51 +00:00
|
|
|
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS) || \
|
|
|
|
BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
|
2021-06-08 02:00:05 +00:00
|
|
|
auto registry = base::MakeRefCounted<user_prefs::PrefRegistrySyncable>();
|
2019-07-24 23:01:08 +00:00
|
|
|
#else
|
2021-06-08 02:00:05 +00:00
|
|
|
auto registry = base::MakeRefCounted<PrefRegistrySimple>();
|
2019-07-24 23:01:08 +00:00
|
|
|
#endif
|
2018-10-04 18:08:56 +00:00
|
|
|
|
|
|
|
registry->RegisterFilePathPref(prefs::kSelectFileLastDirectory,
|
|
|
|
base::FilePath());
|
|
|
|
base::FilePath download_dir;
|
|
|
|
base::PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS, &download_dir);
|
|
|
|
registry->RegisterFilePathPref(prefs::kDownloadDefaultDirectory,
|
|
|
|
download_dir);
|
|
|
|
registry->RegisterDictionaryPref(prefs::kDevToolsFileSystemPaths);
|
2020-07-15 18:27:42 +00:00
|
|
|
InspectableWebContents::RegisterPrefs(registry.get());
|
2018-10-19 18:51:43 +00:00
|
|
|
MediaDeviceIDSalt::RegisterPrefs(registry.get());
|
|
|
|
ZoomLevelDelegate::RegisterPrefs(registry.get());
|
2018-10-04 18:08:56 +00:00
|
|
|
PrefProxyConfigTrackerImpl::RegisterPrefs(registry.get());
|
2019-07-24 23:01:08 +00:00
|
|
|
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
|
2020-05-08 18:17:28 +00:00
|
|
|
if (!in_memory_)
|
|
|
|
extensions::ExtensionPrefs::RegisterProfilePrefs(registry.get());
|
2019-07-24 23:01:08 +00:00
|
|
|
#endif
|
2018-10-04 18:08:56 +00:00
|
|
|
|
2019-10-31 20:11:51 +00:00
|
|
|
#if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
|
|
|
|
BrowserContextDependencyManager::GetInstance()
|
|
|
|
->RegisterProfilePrefsForServices(registry.get());
|
|
|
|
|
|
|
|
language::LanguagePrefs::RegisterProfilePrefs(registry.get());
|
|
|
|
#endif
|
|
|
|
|
2018-10-04 18:08:56 +00:00
|
|
|
prefs_ = prefs_factory.Create(
|
|
|
|
registry.get(),
|
|
|
|
std::make_unique<PrefStoreDelegate>(weak_factory_.GetWeakPtr()));
|
|
|
|
prefs_->UpdateCommandLinePrefStore(new ValueMapPrefStore);
|
2019-10-31 20:11:51 +00:00
|
|
|
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS) || \
|
|
|
|
BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
|
2019-07-24 23:01:08 +00:00
|
|
|
user_prefs::UserPrefs::Set(this, prefs_.get());
|
|
|
|
#endif
|
2019-10-31 20:11:51 +00:00
|
|
|
|
|
|
|
#if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
|
|
|
|
auto* current_dictionaries =
|
|
|
|
prefs()->Get(spellcheck::prefs::kSpellCheckDictionaries);
|
|
|
|
// No configured dictionaries, the default will be en-US
|
2022-02-25 18:17:35 +00:00
|
|
|
if (current_dictionaries->GetListDeprecated().empty()) {
|
2019-10-31 20:11:51 +00:00
|
|
|
std::string default_code = spellcheck::GetCorrespondingSpellCheckLanguage(
|
|
|
|
base::i18n::GetConfiguredLocale());
|
|
|
|
if (!default_code.empty()) {
|
2022-06-22 08:14:35 +00:00
|
|
|
base::Value::List language_codes;
|
2021-10-06 02:21:00 +00:00
|
|
|
language_codes.Append(default_code);
|
2022-06-22 08:14:35 +00:00
|
|
|
prefs()->Set(spellcheck::prefs::kSpellCheckDictionaries,
|
|
|
|
base::Value(std::move(language_codes)));
|
2019-10-31 20:11:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2018-08-14 21:07:53 +00:00
|
|
|
}
|
2016-06-22 06:46:46 +00:00
|
|
|
|
2020-02-04 20:19:40 +00:00
|
|
|
void ElectronBrowserContext::SetUserAgent(const std::string& user_agent) {
|
2016-06-22 06:57:51 +00:00
|
|
|
user_agent_ = user_agent;
|
|
|
|
}
|
|
|
|
|
2020-02-04 20:19:40 +00:00
|
|
|
base::FilePath ElectronBrowserContext::GetPath() {
|
2018-10-04 18:08:56 +00:00
|
|
|
return path_;
|
|
|
|
}
|
|
|
|
|
2020-02-04 20:19:40 +00:00
|
|
|
bool ElectronBrowserContext::IsOffTheRecord() {
|
2018-10-04 18:08:56 +00:00
|
|
|
return in_memory_;
|
|
|
|
}
|
|
|
|
|
2020-02-04 20:19:40 +00:00
|
|
|
bool ElectronBrowserContext::CanUseHttpCache() const {
|
2018-10-04 18:08:56 +00:00
|
|
|
return use_cache_;
|
|
|
|
}
|
|
|
|
|
2020-02-04 20:19:40 +00:00
|
|
|
int ElectronBrowserContext::GetMaxCacheSize() const {
|
2018-10-04 18:08:56 +00:00
|
|
|
return max_cache_size_;
|
|
|
|
}
|
|
|
|
|
2020-02-04 20:19:40 +00:00
|
|
|
content::ResourceContext* ElectronBrowserContext::GetResourceContext() {
|
2019-08-02 23:56:46 +00:00
|
|
|
if (!resource_context_)
|
2019-09-16 22:12:00 +00:00
|
|
|
resource_context_ = std::make_unique<content::ResourceContext>();
|
2019-08-02 23:56:46 +00:00
|
|
|
return resource_context_.get();
|
2018-10-04 18:08:56 +00:00
|
|
|
}
|
|
|
|
|
2020-02-04 20:19:40 +00:00
|
|
|
std::string ElectronBrowserContext::GetMediaDeviceIDSalt() {
|
2018-10-04 18:08:56 +00:00
|
|
|
if (!media_device_id_salt_.get())
|
2019-09-16 22:12:00 +00:00
|
|
|
media_device_id_salt_ = std::make_unique<MediaDeviceIDSalt>(prefs_.get());
|
2018-10-04 18:08:56 +00:00
|
|
|
return media_device_id_salt_->GetSalt();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<content::ZoomLevelDelegate>
|
2020-02-04 20:19:40 +00:00
|
|
|
ElectronBrowserContext::CreateZoomLevelDelegate(
|
2018-10-04 18:08:56 +00:00
|
|
|
const base::FilePath& partition_path) {
|
|
|
|
if (!IsOffTheRecord()) {
|
2018-10-19 18:51:43 +00:00
|
|
|
return std::make_unique<ZoomLevelDelegate>(prefs(), partition_path);
|
2018-10-04 18:08:56 +00:00
|
|
|
}
|
|
|
|
return std::unique_ptr<content::ZoomLevelDelegate>();
|
|
|
|
}
|
|
|
|
|
2015-06-22 11:43:49 +00:00
|
|
|
content::DownloadManagerDelegate*
|
2020-02-04 20:19:40 +00:00
|
|
|
ElectronBrowserContext::GetDownloadManagerDelegate() {
|
2015-06-22 11:43:49 +00:00
|
|
|
if (!download_manager_delegate_.get()) {
|
2021-05-19 23:15:47 +00:00
|
|
|
auto* download_manager = this->GetDownloadManager();
|
2019-09-16 22:12:00 +00:00
|
|
|
download_manager_delegate_ =
|
2020-02-04 20:19:40 +00:00
|
|
|
std::make_unique<ElectronDownloadManagerDelegate>(download_manager);
|
2015-06-22 11:43:49 +00:00
|
|
|
}
|
|
|
|
return download_manager_delegate_.get();
|
|
|
|
}
|
|
|
|
|
2020-02-04 20:19:40 +00:00
|
|
|
content::BrowserPluginGuestManager* ElectronBrowserContext::GetGuestManager() {
|
2014-10-22 14:55:13 +00:00
|
|
|
if (!guest_manager_)
|
2019-09-16 22:12:00 +00:00
|
|
|
guest_manager_ = std::make_unique<WebViewManager>();
|
2014-10-22 14:55:13 +00:00
|
|
|
return guest_manager_.get();
|
|
|
|
}
|
|
|
|
|
2021-10-06 02:21:00 +00:00
|
|
|
content::PlatformNotificationService*
|
|
|
|
ElectronBrowserContext::GetPlatformNotificationService() {
|
|
|
|
return ElectronBrowserClient::Get()->GetPlatformNotificationService();
|
|
|
|
}
|
|
|
|
|
2018-10-02 21:53:10 +00:00
|
|
|
content::PermissionControllerDelegate*
|
2020-02-04 20:19:40 +00:00
|
|
|
ElectronBrowserContext::GetPermissionControllerDelegate() {
|
2016-01-31 19:13:29 +00:00
|
|
|
if (!permission_manager_.get())
|
2020-02-04 20:19:40 +00:00
|
|
|
permission_manager_ = std::make_unique<ElectronPermissionManager>();
|
2016-01-30 11:19:18 +00:00
|
|
|
return permission_manager_.get();
|
|
|
|
}
|
|
|
|
|
2020-02-04 20:19:40 +00:00
|
|
|
storage::SpecialStoragePolicy*
|
|
|
|
ElectronBrowserContext::GetSpecialStoragePolicy() {
|
2018-08-21 15:51:04 +00:00
|
|
|
return storage_policy_.get();
|
|
|
|
}
|
|
|
|
|
2020-02-04 20:19:40 +00:00
|
|
|
std::string ElectronBrowserContext::GetUserAgent() const {
|
2022-06-13 16:35:42 +00:00
|
|
|
return user_agent_.value_or(ElectronBrowserClient::Get()->GetUserAgent());
|
|
|
|
}
|
|
|
|
|
2020-02-04 20:19:40 +00:00
|
|
|
predictors::PreconnectManager* ElectronBrowserContext::GetPreconnectManager() {
|
2019-08-26 16:47:32 +00:00
|
|
|
if (!preconnect_manager_.get()) {
|
2019-09-16 22:12:00 +00:00
|
|
|
preconnect_manager_ =
|
|
|
|
std::make_unique<predictors::PreconnectManager>(nullptr, this);
|
2019-08-26 16:47:32 +00:00
|
|
|
}
|
|
|
|
return preconnect_manager_.get();
|
|
|
|
}
|
|
|
|
|
2019-08-29 06:07:46 +00:00
|
|
|
scoped_refptr<network::SharedURLLoaderFactory>
|
2020-02-04 20:19:40 +00:00
|
|
|
ElectronBrowserContext::GetURLLoaderFactory() {
|
2019-08-29 06:07:46 +00:00
|
|
|
if (url_loader_factory_)
|
|
|
|
return url_loader_factory_;
|
|
|
|
|
2019-10-28 22:12:35 +00:00
|
|
|
mojo::PendingRemote<network::mojom::URLLoaderFactory> network_factory_remote;
|
|
|
|
mojo::PendingReceiver<network::mojom::URLLoaderFactory> factory_receiver =
|
|
|
|
network_factory_remote.InitWithNewPipeAndPassReceiver();
|
2019-08-29 06:07:46 +00:00
|
|
|
|
|
|
|
// Consult the embedder.
|
2019-09-18 19:58:00 +00:00
|
|
|
mojo::PendingRemote<network::mojom::TrustedURLLoaderHeaderClient>
|
|
|
|
header_client;
|
2020-02-04 20:19:40 +00:00
|
|
|
static_cast<content::ContentBrowserClient*>(ElectronBrowserClient::Get())
|
2019-08-29 06:07:46 +00:00
|
|
|
->WillCreateURLLoaderFactory(
|
|
|
|
this, nullptr, -1,
|
|
|
|
content::ContentBrowserClient::URLLoaderFactoryType::kNavigation,
|
2021-06-03 08:05:04 +00:00
|
|
|
url::Origin(), absl::nullopt, ukm::kInvalidSourceIdObj,
|
2020-10-16 01:30:41 +00:00
|
|
|
&factory_receiver, &header_client, nullptr, nullptr, nullptr);
|
2019-08-29 06:07:46 +00:00
|
|
|
|
|
|
|
network::mojom::URLLoaderFactoryParamsPtr params =
|
|
|
|
network::mojom::URLLoaderFactoryParams::New();
|
|
|
|
params->header_client = std::move(header_client);
|
|
|
|
params->process_id = network::mojom::kBrowserProcessId;
|
|
|
|
params->is_trusted = true;
|
|
|
|
params->is_corb_enabled = false;
|
|
|
|
// The tests of net module would fail if this setting is true, it seems that
|
|
|
|
// the non-NetworkService implementation always has web security enabled.
|
|
|
|
params->disable_web_security = false;
|
|
|
|
|
2021-05-04 03:13:46 +00:00
|
|
|
auto* storage_partition = GetDefaultStoragePartition();
|
2019-08-29 06:07:46 +00:00
|
|
|
storage_partition->GetNetworkContext()->CreateURLLoaderFactory(
|
2019-10-28 22:12:35 +00:00
|
|
|
std::move(factory_receiver), std::move(params));
|
2019-08-29 06:07:46 +00:00
|
|
|
url_loader_factory_ =
|
|
|
|
base::MakeRefCounted<network::WrapperSharedURLLoaderFactory>(
|
2019-10-28 22:12:35 +00:00
|
|
|
std::move(network_factory_remote));
|
2019-08-29 06:07:46 +00:00
|
|
|
return url_loader_factory_;
|
2019-11-14 18:01:18 +00:00
|
|
|
}
|
|
|
|
|
2020-02-04 20:19:40 +00:00
|
|
|
content::PushMessagingService*
|
|
|
|
ElectronBrowserContext::GetPushMessagingService() {
|
2018-10-04 18:08:56 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-02-04 20:19:40 +00:00
|
|
|
content::SSLHostStateDelegate*
|
|
|
|
ElectronBrowserContext::GetSSLHostStateDelegate() {
|
2018-10-04 18:08:56 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
content::BackgroundFetchDelegate*
|
2020-02-04 20:19:40 +00:00
|
|
|
ElectronBrowserContext::GetBackgroundFetchDelegate() {
|
2018-10-04 18:08:56 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
content::BackgroundSyncController*
|
2020-02-04 20:19:40 +00:00
|
|
|
ElectronBrowserContext::GetBackgroundSyncController() {
|
2018-10-04 18:08:56 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
content::BrowsingDataRemoverDelegate*
|
2020-02-04 20:19:40 +00:00
|
|
|
ElectronBrowserContext::GetBrowsingDataRemoverDelegate() {
|
2018-10-04 18:08:56 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-01-22 19:50:25 +00:00
|
|
|
content::ClientHintsControllerDelegate*
|
2020-02-04 20:19:40 +00:00
|
|
|
ElectronBrowserContext::GetClientHintsControllerDelegate() {
|
2019-01-22 19:50:25 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-10-28 22:12:35 +00:00
|
|
|
content::StorageNotificationService*
|
2020-02-04 20:19:40 +00:00
|
|
|
ElectronBrowserContext::GetStorageNotificationService() {
|
2019-10-28 22:12:35 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-02-04 20:19:40 +00:00
|
|
|
ResolveProxyHelper* ElectronBrowserContext::GetResolveProxyHelper() {
|
2018-10-04 18:08:56 +00:00
|
|
|
if (!resolve_proxy_helper_) {
|
|
|
|
resolve_proxy_helper_ = base::MakeRefCounted<ResolveProxyHelper>(this);
|
|
|
|
}
|
|
|
|
return resolve_proxy_helper_.get();
|
|
|
|
}
|
|
|
|
|
2020-09-23 20:22:10 +00:00
|
|
|
network::mojom::SSLConfigPtr ElectronBrowserContext::GetSSLConfig() {
|
|
|
|
return ssl_config_.Clone();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ElectronBrowserContext::SetSSLConfig(network::mojom::SSLConfigPtr config) {
|
|
|
|
ssl_config_ = std::move(config);
|
|
|
|
if (ssl_config_client_) {
|
|
|
|
ssl_config_client_->OnSSLConfigUpdated(ssl_config_.Clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ElectronBrowserContext::SetSSLConfigClient(
|
|
|
|
mojo::Remote<network::mojom::SSLConfigClient> client) {
|
|
|
|
ssl_config_client_ = std::move(client);
|
|
|
|
}
|
|
|
|
|
2022-08-22 21:15:32 +00:00
|
|
|
void ElectronBrowserContext::SetDisplayMediaRequestHandler(
|
|
|
|
DisplayMediaRequestHandler handler) {
|
|
|
|
display_media_request_handler_ = handler;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ElectronBrowserContext::DisplayMediaDeviceChosen(
|
|
|
|
const content::MediaStreamRequest& request,
|
|
|
|
content::MediaResponseCallback callback,
|
|
|
|
gin::Arguments* args) {
|
|
|
|
blink::mojom::StreamDevicesSetPtr stream_devices_set =
|
|
|
|
blink::mojom::StreamDevicesSet::New();
|
|
|
|
v8::Local<v8::Value> result;
|
|
|
|
if (!args->GetNext(&result) || result->IsNullOrUndefined()) {
|
|
|
|
std::move(callback).Run(
|
|
|
|
blink::mojom::StreamDevicesSet(),
|
|
|
|
blink::mojom::MediaStreamRequestResult::CAPTURE_FAILURE, nullptr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
gin_helper::Dictionary result_dict;
|
|
|
|
if (!gin::ConvertFromV8(args->isolate(), result, &result_dict)) {
|
|
|
|
gin_helper::ErrorThrower(args->isolate())
|
|
|
|
.ThrowTypeError(
|
|
|
|
"Display Media Request streams callback must be called with null "
|
|
|
|
"or a valid object");
|
|
|
|
std::move(callback).Run(
|
|
|
|
blink::mojom::StreamDevicesSet(),
|
|
|
|
blink::mojom::MediaStreamRequestResult::CAPTURE_FAILURE, nullptr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
stream_devices_set->stream_devices.emplace_back(
|
|
|
|
blink::mojom::StreamDevices::New());
|
|
|
|
blink::mojom::StreamDevices& devices = *stream_devices_set->stream_devices[0];
|
|
|
|
bool video_requested =
|
|
|
|
request.video_type != blink::mojom::MediaStreamType::NO_SERVICE;
|
|
|
|
bool audio_requested =
|
|
|
|
request.audio_type != blink::mojom::MediaStreamType::NO_SERVICE;
|
|
|
|
bool has_video = false;
|
|
|
|
if (video_requested && result_dict.Has("video")) {
|
|
|
|
gin_helper::Dictionary video_dict;
|
|
|
|
std::string id;
|
|
|
|
std::string name;
|
|
|
|
content::RenderFrameHost* rfh;
|
|
|
|
if (result_dict.Get("video", &video_dict) && video_dict.Get("id", &id) &&
|
|
|
|
video_dict.Get("name", &name)) {
|
|
|
|
devices.video_device =
|
|
|
|
blink::MediaStreamDevice(request.video_type, id, name);
|
|
|
|
} else if (result_dict.Get("video", &rfh)) {
|
|
|
|
devices.video_device = blink::MediaStreamDevice(
|
|
|
|
request.video_type,
|
|
|
|
content::WebContentsMediaCaptureId(rfh->GetProcess()->GetID(),
|
|
|
|
rfh->GetRoutingID())
|
|
|
|
.ToString(),
|
|
|
|
base::UTF16ToUTF8(
|
|
|
|
content::WebContents::FromRenderFrameHost(rfh)->GetTitle()));
|
|
|
|
} else {
|
|
|
|
gin_helper::ErrorThrower(args->isolate())
|
|
|
|
.ThrowTypeError(
|
|
|
|
"video must be a WebFrameMain or DesktopCapturerSource");
|
|
|
|
std::move(callback).Run(
|
|
|
|
blink::mojom::StreamDevicesSet(),
|
|
|
|
blink::mojom::MediaStreamRequestResult::CAPTURE_FAILURE, nullptr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
has_video = true;
|
|
|
|
}
|
|
|
|
if (audio_requested && result_dict.Has("audio")) {
|
|
|
|
gin_helper::Dictionary audio_dict;
|
|
|
|
std::string id;
|
|
|
|
std::string name;
|
|
|
|
content::RenderFrameHost* rfh;
|
|
|
|
// NB. this is not permitted by the documentation, but is left here as an
|
|
|
|
// "escape hatch" for providing an arbitrary name/id if needed in the
|
|
|
|
// future.
|
|
|
|
if (result_dict.Get("audio", &audio_dict) && audio_dict.Get("id", &id) &&
|
|
|
|
audio_dict.Get("name", &name)) {
|
|
|
|
devices.audio_device =
|
|
|
|
blink::MediaStreamDevice(request.audio_type, id, name);
|
|
|
|
} else if (result_dict.Get("audio", &rfh)) {
|
|
|
|
devices.audio_device = blink::MediaStreamDevice(
|
|
|
|
request.audio_type,
|
|
|
|
content::WebContentsMediaCaptureId(rfh->GetProcess()->GetID(),
|
|
|
|
rfh->GetRoutingID(),
|
|
|
|
/* disable_local_echo= */ true)
|
|
|
|
.ToString(),
|
|
|
|
"Tab audio");
|
|
|
|
} else if (result_dict.Get("audio", &id)) {
|
|
|
|
devices.audio_device =
|
|
|
|
blink::MediaStreamDevice(request.audio_type, id, "System audio");
|
|
|
|
} else {
|
|
|
|
gin_helper::ErrorThrower(args->isolate())
|
|
|
|
.ThrowTypeError(
|
|
|
|
"audio must be a WebFrameMain, \"loopback\" or "
|
|
|
|
"\"loopbackWithMute\"");
|
|
|
|
std::move(callback).Run(
|
|
|
|
blink::mojom::StreamDevicesSet(),
|
|
|
|
blink::mojom::MediaStreamRequestResult::CAPTURE_FAILURE, nullptr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((video_requested && !has_video)) {
|
|
|
|
gin_helper::ErrorThrower(args->isolate())
|
|
|
|
.ThrowTypeError(
|
|
|
|
"Video was requested, but no video stream was provided");
|
|
|
|
std::move(callback).Run(
|
|
|
|
blink::mojom::StreamDevicesSet(),
|
|
|
|
blink::mojom::MediaStreamRequestResult::CAPTURE_FAILURE, nullptr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::move(callback).Run(*stream_devices_set,
|
|
|
|
blink::mojom::MediaStreamRequestResult::OK, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ElectronBrowserContext::ChooseDisplayMediaDevice(
|
|
|
|
const content::MediaStreamRequest& request,
|
|
|
|
content::MediaResponseCallback callback) {
|
|
|
|
if (!display_media_request_handler_)
|
|
|
|
return false;
|
|
|
|
DisplayMediaResponseCallbackJs callbackJs =
|
|
|
|
base::BindOnce(&DisplayMediaDeviceChosen, request, std::move(callback));
|
|
|
|
display_media_request_handler_.Run(request, std::move(callbackJs));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-06-27 20:50:08 +00:00
|
|
|
void ElectronBrowserContext::GrantDevicePermission(
|
|
|
|
const url::Origin& origin,
|
|
|
|
const base::Value& device,
|
|
|
|
blink::PermissionType permission_type) {
|
|
|
|
granted_devices_[permission_type][origin].push_back(
|
|
|
|
std::make_unique<base::Value>(device.Clone()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ElectronBrowserContext::RevokeDevicePermission(
|
|
|
|
const url::Origin& origin,
|
|
|
|
const base::Value& device,
|
|
|
|
blink::PermissionType permission_type) {
|
|
|
|
const auto& current_devices_it = granted_devices_.find(permission_type);
|
|
|
|
if (current_devices_it == granted_devices_.end())
|
|
|
|
return;
|
|
|
|
|
|
|
|
const auto& origin_devices_it = current_devices_it->second.find(origin);
|
|
|
|
if (origin_devices_it == current_devices_it->second.end())
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (auto it = origin_devices_it->second.begin();
|
|
|
|
it != origin_devices_it->second.end();) {
|
|
|
|
if (DoesDeviceMatch(device, it->get(), permission_type)) {
|
|
|
|
it = origin_devices_it->second.erase(it);
|
|
|
|
} else {
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ElectronBrowserContext::DoesDeviceMatch(
|
|
|
|
const base::Value& device,
|
|
|
|
const base::Value* device_to_compare,
|
|
|
|
blink::PermissionType permission_type) {
|
|
|
|
if (permission_type ==
|
|
|
|
static_cast<blink::PermissionType>(
|
|
|
|
WebContentsPermissionHelper::PermissionType::HID)) {
|
|
|
|
if (device.GetDict().FindInt(kHidVendorIdKey) !=
|
|
|
|
device_to_compare->GetDict().FindInt(kHidVendorIdKey) ||
|
|
|
|
device.GetDict().FindInt(kHidProductIdKey) !=
|
|
|
|
device_to_compare->GetDict().FindInt(kHidProductIdKey)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto* serial_number =
|
|
|
|
device_to_compare->GetDict().FindString(kHidSerialNumberKey);
|
|
|
|
const auto* device_serial_number =
|
|
|
|
device.GetDict().FindString(kHidSerialNumberKey);
|
|
|
|
|
|
|
|
if (serial_number && device_serial_number &&
|
|
|
|
*device_serial_number == *serial_number)
|
|
|
|
return true;
|
|
|
|
} else if (permission_type ==
|
|
|
|
static_cast<blink::PermissionType>(
|
|
|
|
WebContentsPermissionHelper::PermissionType::SERIAL)) {
|
|
|
|
#if BUILDFLAG(IS_WIN)
|
|
|
|
const auto* instance_id = device.GetDict().FindString(kDeviceInstanceIdKey);
|
|
|
|
const auto* port_instance_id =
|
|
|
|
device_to_compare->GetDict().FindString(kDeviceInstanceIdKey);
|
|
|
|
if (instance_id && port_instance_id && *instance_id == *port_instance_id)
|
|
|
|
return true;
|
|
|
|
#else
|
|
|
|
const auto* serial_number = device.GetDict().FindString(kSerialNumberKey);
|
|
|
|
const auto* port_serial_number =
|
|
|
|
device_to_compare->GetDict().FindString(kSerialNumberKey);
|
|
|
|
if (device.GetDict().FindInt(kVendorIdKey) !=
|
|
|
|
device_to_compare->GetDict().FindInt(kVendorIdKey) ||
|
|
|
|
device.GetDict().FindInt(kProductIdKey) !=
|
|
|
|
device_to_compare->GetDict().FindInt(kProductIdKey) ||
|
|
|
|
(serial_number && port_serial_number &&
|
|
|
|
*port_serial_number != *serial_number)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if BUILDFLAG(IS_MAC)
|
|
|
|
const auto* usb_driver_key = device.GetDict().FindString(kUsbDriverKey);
|
|
|
|
const auto* port_usb_driver_key =
|
|
|
|
device_to_compare->GetDict().FindString(kUsbDriverKey);
|
|
|
|
if (usb_driver_key && port_usb_driver_key &&
|
|
|
|
*usb_driver_key != *port_usb_driver_key) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif // BUILDFLAG(IS_MAC)
|
|
|
|
return true;
|
|
|
|
#endif // BUILDFLAG(IS_WIN)
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ElectronBrowserContext::CheckDevicePermission(
|
|
|
|
const url::Origin& origin,
|
|
|
|
const base::Value& device,
|
|
|
|
blink::PermissionType permission_type) {
|
|
|
|
const auto& current_devices_it = granted_devices_.find(permission_type);
|
|
|
|
if (current_devices_it == granted_devices_.end())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const auto& origin_devices_it = current_devices_it->second.find(origin);
|
|
|
|
if (origin_devices_it == current_devices_it->second.end())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (const auto& device_to_compare : origin_devices_it->second) {
|
|
|
|
if (DoesDeviceMatch(device, device_to_compare.get(), permission_type))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-09-05 12:54:36 +00:00
|
|
|
// static
|
2020-08-17 20:21:53 +00:00
|
|
|
ElectronBrowserContext* ElectronBrowserContext::From(
|
2018-04-18 01:55:30 +00:00
|
|
|
const std::string& partition,
|
|
|
|
bool in_memory,
|
2022-07-05 15:25:18 +00:00
|
|
|
base::Value::Dict options) {
|
2018-10-04 18:08:56 +00:00
|
|
|
PartitionKey key(partition, in_memory);
|
2020-08-17 20:21:53 +00:00
|
|
|
ElectronBrowserContext* browser_context = browser_context_map()[key].get();
|
|
|
|
if (browser_context) {
|
|
|
|
return browser_context;
|
|
|
|
}
|
2016-07-12 12:39:54 +00:00
|
|
|
|
2019-10-30 05:30:59 +00:00
|
|
|
auto* new_context =
|
2020-02-04 20:19:40 +00:00
|
|
|
new ElectronBrowserContext(partition, in_memory, std::move(options));
|
2020-08-17 20:21:53 +00:00
|
|
|
browser_context_map()[key] =
|
|
|
|
std::unique_ptr<ElectronBrowserContext>(new_context);
|
|
|
|
return new_context;
|
2015-09-05 12:54:36 +00:00
|
|
|
}
|
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
} // namespace electron
|