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.
|
|
|
|
|
2014-03-16 00:30:26 +00:00
|
|
|
#include "atom/browser/atom_browser_context.h"
|
2013-04-15 16:25:08 +00:00
|
|
|
|
2018-10-04 18:08:56 +00:00
|
|
|
#include <utility>
|
|
|
|
|
2016-08-31 00:08:32 +00:00
|
|
|
#include "atom/browser/atom_blob_reader.h"
|
2019-05-01 23:34:42 +00:00
|
|
|
#include "atom/browser/atom_browser_client.h"
|
2018-08-13 23:16:04 +00:00
|
|
|
#include "atom/browser/atom_browser_main_parts.h"
|
2015-06-22 11:43:49 +00:00
|
|
|
#include "atom/browser/atom_download_manager_delegate.h"
|
2018-10-24 10:49:10 +00:00
|
|
|
#include "atom/browser/atom_paths.h"
|
2016-08-26 22:30:02 +00:00
|
|
|
#include "atom/browser/atom_permission_manager.h"
|
2018-10-04 18:08:56 +00:00
|
|
|
#include "atom/browser/cookie_change_notifier.h"
|
|
|
|
#include "atom/browser/net/resolve_proxy_helper.h"
|
|
|
|
#include "atom/browser/pref_store_delegate.h"
|
2018-08-21 15:51:04 +00:00
|
|
|
#include "atom/browser/special_storage_policy.h"
|
2018-10-19 13:50:30 +00:00
|
|
|
#include "atom/browser/ui/inspectable_web_contents_impl.h"
|
2015-02-04 23:17:28 +00:00
|
|
|
#include "atom/browser/web_view_manager.h"
|
2018-10-19 18:51:43 +00:00
|
|
|
#include "atom/browser/zoom_level_delegate.h"
|
2018-10-24 10:49:10 +00:00
|
|
|
#include "atom/common/application_info.h"
|
2018-08-13 23:16:04 +00:00
|
|
|
#include "atom/common/options_switches.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"
|
2016-01-21 08:21:37 +00:00
|
|
|
#include "base/path_service.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"
|
2018-10-04 18:08:56 +00:00
|
|
|
#include "content/public/browser/storage_partition.h"
|
|
|
|
#include "net/base/escape.h"
|
2019-03-26 01:10:48 +00:00
|
|
|
#include "services/network/public/cpp/features.h"
|
2018-10-04 18:08:56 +00:00
|
|
|
|
|
|
|
using content::BrowserThread;
|
2013-09-20 08:47:47 +00:00
|
|
|
|
2014-08-13 09:40:31 +00:00
|
|
|
namespace atom {
|
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) {
|
|
|
|
return net::EscapePath(base::ToLowerASCII(input));
|
|
|
|
}
|
|
|
|
|
2014-09-23 04:13:46 +00:00
|
|
|
} // namespace
|
|
|
|
|
2018-10-04 18:08:56 +00:00
|
|
|
// static
|
|
|
|
AtomBrowserContext::BrowserContextMap AtomBrowserContext::browser_context_map_;
|
|
|
|
|
2016-10-17 10:33:24 +00:00
|
|
|
AtomBrowserContext::AtomBrowserContext(const std::string& partition,
|
|
|
|
bool in_memory,
|
|
|
|
const base::DictionaryValue& options)
|
2018-10-04 18:08:56 +00:00
|
|
|
: base::RefCountedDeleteOnSequence<AtomBrowserContext>(
|
2018-10-08 21:29:58 +00:00
|
|
|
base::ThreadTaskRunnerHandle::Get()),
|
2018-10-04 18:08:56 +00:00
|
|
|
in_memory_pref_store_(nullptr),
|
|
|
|
storage_policy_(new SpecialStoragePolicy),
|
|
|
|
in_memory_(in_memory),
|
|
|
|
weak_factory_(this) {
|
2019-05-01 23:34:42 +00:00
|
|
|
user_agent_ = AtomBrowserClient::Get()->GetUserAgent();
|
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);
|
|
|
|
options.GetBoolean("cache", &use_cache_);
|
|
|
|
|
|
|
|
base::StringToInt(command_line->GetSwitchValueASCII(switches::kDiskCacheSize),
|
|
|
|
&max_cache_size_);
|
|
|
|
|
2018-10-24 10:49:10 +00:00
|
|
|
if (!base::PathService::Get(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_);
|
2018-10-04 18:08:56 +00:00
|
|
|
}
|
2018-08-14 21:07:53 +00:00
|
|
|
|
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
|
|
|
|
2018-10-04 18:08:56 +00:00
|
|
|
content::BrowserContext::Initialize(this, path_);
|
|
|
|
|
|
|
|
// Initialize Pref Registry.
|
2016-07-26 11:04:04 +00:00
|
|
|
InitPrefs();
|
2018-10-04 18:08:56 +00:00
|
|
|
|
2019-03-26 01:10:48 +00:00
|
|
|
if (!base::FeatureList::IsEnabled(network::features::kNetworkService)) {
|
|
|
|
proxy_config_monitor_ = std::make_unique<ProxyConfigMonitor>(prefs_.get());
|
|
|
|
io_handle_ =
|
|
|
|
new URLRequestContextGetter::Handle(weak_factory_.GetWeakPtr());
|
|
|
|
}
|
|
|
|
|
2018-10-04 18:08:56 +00:00
|
|
|
cookie_change_notifier_ = std::make_unique<CookieChangeNotifier>(this);
|
2018-10-11 13:52:12 +00:00
|
|
|
|
|
|
|
BrowserContextDependencyManager::GetInstance()->MarkBrowserContextLive(this);
|
2016-06-22 06:46:46 +00:00
|
|
|
}
|
|
|
|
|
2018-08-14 21:07:53 +00:00
|
|
|
AtomBrowserContext::~AtomBrowserContext() {
|
2018-10-04 18:08:56 +00:00
|
|
|
DCHECK_CURRENTLY_ON(BrowserThread::UI);
|
|
|
|
NotifyWillBeDestroyed(this);
|
|
|
|
ShutdownStoragePartitions();
|
2019-03-26 01:10:48 +00:00
|
|
|
|
|
|
|
BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE,
|
|
|
|
std::move(resource_context_));
|
|
|
|
|
|
|
|
if (!base::FeatureList::IsEnabled(network::features::kNetworkService))
|
|
|
|
io_handle_->ShutdownOnUIThread();
|
|
|
|
|
2018-10-11 13:52:12 +00:00
|
|
|
// Notify any keyed services of browser context destruction.
|
|
|
|
BrowserContextDependencyManager::GetInstance()->DestroyBrowserContextServices(
|
|
|
|
this);
|
2018-10-04 18:08:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AtomBrowserContext::InitPrefs() {
|
|
|
|
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);
|
|
|
|
|
|
|
|
auto registry = WrapRefCounted(new PrefRegistrySimple);
|
|
|
|
|
|
|
|
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);
|
2018-10-19 13:50:30 +00:00
|
|
|
InspectableWebContentsImpl::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());
|
|
|
|
|
|
|
|
prefs_ = prefs_factory.Create(
|
|
|
|
registry.get(),
|
|
|
|
std::make_unique<PrefStoreDelegate>(weak_factory_.GetWeakPtr()));
|
|
|
|
prefs_->UpdateCommandLinePrefStore(new ValueMapPrefStore);
|
2018-08-14 21:07:53 +00:00
|
|
|
}
|
2016-06-22 06:46:46 +00:00
|
|
|
|
2016-06-22 06:57:51 +00:00
|
|
|
void AtomBrowserContext::SetUserAgent(const std::string& user_agent) {
|
|
|
|
user_agent_ = user_agent;
|
|
|
|
}
|
|
|
|
|
2018-10-04 18:08:56 +00:00
|
|
|
net::URLRequestContextGetter* AtomBrowserContext::CreateRequestContext(
|
|
|
|
content::ProtocolHandlerMap* protocol_handlers,
|
|
|
|
content::URLRequestInterceptorScopedVector protocol_interceptors) {
|
2019-03-26 01:10:48 +00:00
|
|
|
if (!base::FeatureList::IsEnabled(network::features::kNetworkService)) {
|
|
|
|
return io_handle_
|
|
|
|
->CreateMainRequestContextGetter(protocol_handlers,
|
|
|
|
std::move(protocol_interceptors))
|
|
|
|
.get();
|
|
|
|
} else {
|
|
|
|
NOTREACHED();
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-10-04 18:08:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
net::URLRequestContextGetter* AtomBrowserContext::CreateMediaRequestContext() {
|
2019-03-26 01:10:48 +00:00
|
|
|
if (!base::FeatureList::IsEnabled(network::features::kNetworkService)) {
|
|
|
|
return io_handle_->GetMainRequestContextGetter().get();
|
|
|
|
} else {
|
|
|
|
NOTREACHED();
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-10-04 18:08:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
net::URLRequestContextGetter* AtomBrowserContext::GetRequestContext() {
|
2019-03-26 01:10:48 +00:00
|
|
|
if (!base::FeatureList::IsEnabled(network::features::kNetworkService)) {
|
|
|
|
return GetDefaultStoragePartition(this)->GetURLRequestContext();
|
|
|
|
} else {
|
|
|
|
NOTREACHED();
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-10-04 18:08:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
network::mojom::NetworkContextPtr AtomBrowserContext::GetNetworkContext() {
|
2019-03-26 01:10:48 +00:00
|
|
|
if (!base::FeatureList::IsEnabled(network::features::kNetworkService)) {
|
|
|
|
return io_handle_->GetNetworkContext();
|
|
|
|
} else {
|
|
|
|
NOTREACHED();
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-10-04 18:08:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
base::FilePath AtomBrowserContext::GetPath() const {
|
|
|
|
return path_;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AtomBrowserContext::IsOffTheRecord() const {
|
|
|
|
return in_memory_;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AtomBrowserContext::CanUseHttpCache() const {
|
|
|
|
return use_cache_;
|
|
|
|
}
|
|
|
|
|
|
|
|
int AtomBrowserContext::GetMaxCacheSize() const {
|
|
|
|
return max_cache_size_;
|
|
|
|
}
|
|
|
|
|
|
|
|
content::ResourceContext* AtomBrowserContext::GetResourceContext() {
|
2019-03-26 01:10:48 +00:00
|
|
|
if (!base::FeatureList::IsEnabled(network::features::kNetworkService)) {
|
|
|
|
return io_handle_->GetResourceContext();
|
|
|
|
} else {
|
|
|
|
if (!resource_context_)
|
|
|
|
resource_context_.reset(new content::ResourceContext);
|
|
|
|
return resource_context_.get();
|
|
|
|
}
|
2018-10-04 18:08:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string AtomBrowserContext::GetMediaDeviceIDSalt() {
|
|
|
|
if (!media_device_id_salt_.get())
|
2018-10-19 18:51:43 +00:00
|
|
|
media_device_id_salt_.reset(new MediaDeviceIDSalt(prefs_.get()));
|
2018-10-04 18:08:56 +00:00
|
|
|
return media_device_id_salt_->GetSalt();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<content::ZoomLevelDelegate>
|
|
|
|
AtomBrowserContext::CreateZoomLevelDelegate(
|
|
|
|
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*
|
|
|
|
AtomBrowserContext::GetDownloadManagerDelegate() {
|
|
|
|
if (!download_manager_delegate_.get()) {
|
2018-04-17 22:41:47 +00:00
|
|
|
auto* download_manager = content::BrowserContext::GetDownloadManager(this);
|
2015-06-22 11:43:49 +00:00
|
|
|
download_manager_delegate_.reset(
|
|
|
|
new AtomDownloadManagerDelegate(download_manager));
|
|
|
|
}
|
|
|
|
return download_manager_delegate_.get();
|
|
|
|
}
|
|
|
|
|
2014-10-22 14:55:13 +00:00
|
|
|
content::BrowserPluginGuestManager* AtomBrowserContext::GetGuestManager() {
|
|
|
|
if (!guest_manager_)
|
2015-09-05 02:43:30 +00:00
|
|
|
guest_manager_.reset(new WebViewManager);
|
2014-10-22 14:55:13 +00:00
|
|
|
return guest_manager_.get();
|
|
|
|
}
|
|
|
|
|
2018-10-02 21:53:10 +00:00
|
|
|
content::PermissionControllerDelegate*
|
|
|
|
AtomBrowserContext::GetPermissionControllerDelegate() {
|
2016-01-31 19:13:29 +00:00
|
|
|
if (!permission_manager_.get())
|
|
|
|
permission_manager_.reset(new AtomPermissionManager);
|
2016-01-30 11:19:18 +00:00
|
|
|
return permission_manager_.get();
|
|
|
|
}
|
|
|
|
|
2018-08-21 15:51:04 +00:00
|
|
|
storage::SpecialStoragePolicy* AtomBrowserContext::GetSpecialStoragePolicy() {
|
|
|
|
return storage_policy_.get();
|
|
|
|
}
|
|
|
|
|
2018-08-14 21:07:53 +00:00
|
|
|
std::string AtomBrowserContext::GetUserAgent() const {
|
|
|
|
return user_agent_;
|
|
|
|
}
|
|
|
|
|
2016-08-31 00:08:32 +00:00
|
|
|
AtomBlobReader* AtomBrowserContext::GetBlobReader() {
|
|
|
|
if (!blob_reader_.get()) {
|
|
|
|
content::ChromeBlobStorageContext* blob_context =
|
|
|
|
content::ChromeBlobStorageContext::GetFor(this);
|
2018-04-12 08:06:23 +00:00
|
|
|
blob_reader_.reset(new AtomBlobReader(blob_context));
|
2016-08-31 00:08:32 +00:00
|
|
|
}
|
|
|
|
return blob_reader_.get();
|
|
|
|
}
|
|
|
|
|
2018-10-04 18:08:56 +00:00
|
|
|
content::PushMessagingService* AtomBrowserContext::GetPushMessagingService() {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
content::SSLHostStateDelegate* AtomBrowserContext::GetSSLHostStateDelegate() {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
content::BackgroundFetchDelegate*
|
|
|
|
AtomBrowserContext::GetBackgroundFetchDelegate() {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
content::BackgroundSyncController*
|
|
|
|
AtomBrowserContext::GetBackgroundSyncController() {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
content::BrowsingDataRemoverDelegate*
|
|
|
|
AtomBrowserContext::GetBrowsingDataRemoverDelegate() {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-01-22 19:50:25 +00:00
|
|
|
content::ClientHintsControllerDelegate*
|
|
|
|
AtomBrowserContext::GetClientHintsControllerDelegate() {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-10-04 18:08:56 +00:00
|
|
|
net::URLRequestContextGetter*
|
|
|
|
AtomBrowserContext::CreateRequestContextForStoragePartition(
|
|
|
|
const base::FilePath& partition_path,
|
|
|
|
bool in_memory,
|
|
|
|
content::ProtocolHandlerMap* protocol_handlers,
|
|
|
|
content::URLRequestInterceptorScopedVector request_interceptors) {
|
|
|
|
NOTREACHED();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
net::URLRequestContextGetter*
|
|
|
|
AtomBrowserContext::CreateMediaRequestContextForStoragePartition(
|
|
|
|
const base::FilePath& partition_path,
|
|
|
|
bool in_memory) {
|
|
|
|
NOTREACHED();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
ResolveProxyHelper* AtomBrowserContext::GetResolveProxyHelper() {
|
|
|
|
if (!resolve_proxy_helper_) {
|
|
|
|
resolve_proxy_helper_ = base::MakeRefCounted<ResolveProxyHelper>(this);
|
|
|
|
}
|
|
|
|
return resolve_proxy_helper_.get();
|
|
|
|
}
|
|
|
|
|
2015-09-05 12:54:36 +00:00
|
|
|
// static
|
2016-07-12 12:39:54 +00:00
|
|
|
scoped_refptr<AtomBrowserContext> AtomBrowserContext::From(
|
2018-04-18 01:55:30 +00:00
|
|
|
const std::string& partition,
|
|
|
|
bool in_memory,
|
2016-07-12 12:53:19 +00:00
|
|
|
const base::DictionaryValue& options) {
|
2018-10-04 18:08:56 +00:00
|
|
|
PartitionKey key(partition, in_memory);
|
|
|
|
auto* browser_context = browser_context_map_[key].get();
|
2016-07-12 12:39:54 +00:00
|
|
|
if (browser_context)
|
2018-10-04 18:08:56 +00:00
|
|
|
return scoped_refptr<AtomBrowserContext>(browser_context);
|
2016-07-12 12:39:54 +00:00
|
|
|
|
2018-10-04 18:08:56 +00:00
|
|
|
auto* new_context = new AtomBrowserContext(partition, in_memory, options);
|
|
|
|
browser_context_map_[key] = new_context->GetWeakPtr();
|
|
|
|
return scoped_refptr<AtomBrowserContext>(new_context);
|
2015-09-05 12:54:36 +00:00
|
|
|
}
|
|
|
|
|
2016-07-12 12:39:54 +00:00
|
|
|
} // namespace atom
|