electron/brightray/browser/browser_context.cc

238 lines
7.5 KiB
C++
Raw Normal View History

2013-03-13 19:12:05 +00:00
// Copyright (c) 2012 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-CHROMIUM file.
#include "brightray/browser/browser_context.h"
2013-03-13 19:12:05 +00:00
#include "base/files/file_path.h"
#include "base/path_service.h"
#include "base/strings/string_util.h"
#include "base/threading/thread_restrictions.h"
#include "brightray/browser/brightray_paths.h"
#include "brightray/browser/browser_client.h"
#include "brightray/browser/inspectable_web_contents_impl.h"
#include "brightray/browser/network_delegate.h"
#include "brightray/browser/special_storage_policy.h"
#include "brightray/browser/zoom_level_delegate.h"
#include "brightray/common/application_info.h"
#include "components/prefs/json_pref_store.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
#include "components/prefs/pref_service_factory.h"
2013-03-13 19:12:05 +00:00
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/resource_context.h"
#include "content/public/browser/storage_partition.h"
#include "net/base/escape.h"
2013-03-13 19:12:05 +00:00
2014-08-13 07:09:26 +00:00
using content::BrowserThread;
2013-03-13 19:12:05 +00:00
namespace brightray {
namespace {
// Convert string to lower case and escape it.
std::string MakePartitionName(const std::string& input) {
2015-12-07 11:55:01 +00:00
return net::EscapePath(base::ToLowerASCII(input));
}
} // namespace
2013-03-13 19:12:05 +00:00
class BrowserContext::ResourceContext : public content::ResourceContext {
public:
2013-03-13 19:12:05 +00:00
ResourceContext() : getter_(nullptr) {}
void set_url_request_context_getter(URLRequestContextGetter* getter) {
getter_ = getter;
}
private:
2015-06-05 04:07:27 +00:00
net::HostResolver* GetHostResolver() override {
2013-03-13 19:12:05 +00:00
return getter_->host_resolver();
}
2015-06-05 04:07:27 +00:00
net::URLRequestContext* GetRequestContext() override {
2013-03-13 19:12:05 +00:00
return getter_->GetURLRequestContext();
}
URLRequestContextGetter* getter_;
};
2015-09-05 14:34:42 +00:00
// static
BrowserContext::BrowserContextMap BrowserContext::browser_context_map_;
// static
2018-04-18 01:56:12 +00:00
scoped_refptr<BrowserContext> BrowserContext::Get(const std::string& partition,
bool in_memory) {
2015-09-05 14:34:42 +00:00
PartitionKey key(partition, in_memory);
if (browser_context_map_[key].get())
2017-12-18 00:23:02 +00:00
return WrapRefCounted(browser_context_map_[key].get());
2015-09-05 14:34:42 +00:00
return nullptr;
2015-09-05 14:34:42 +00:00
}
BrowserContext::BrowserContext(const std::string& partition, bool in_memory)
: in_memory_(in_memory),
2015-09-05 14:34:42 +00:00
resource_context_(new ResourceContext),
2015-10-12 13:09:39 +00:00
storage_policy_(new SpecialStoragePolicy),
2015-09-05 14:34:42 +00:00
weak_factory_(this) {
2015-01-19 00:59:57 +00:00
if (!PathService::Get(DIR_USER_DATA, &path_)) {
2015-01-19 01:19:25 +00:00
PathService::Get(DIR_APP_DATA, &path_);
2015-01-19 00:59:57 +00:00
path_ = path_.Append(base::FilePath::FromUTF8Unsafe(GetApplicationName()));
PathService::Override(DIR_USER_DATA, path_);
}
if (!in_memory_ && !partition.empty())
path_ = path_.Append(FILE_PATH_LITERAL("Partitions"))
.Append(base::FilePath::FromUTF8Unsafe(
MakePartitionName(partition)));
2016-05-23 03:36:34 +00:00
content::BrowserContext::Initialize(this, path_);
browser_context_map_[PartitionKey(partition, in_memory)] = GetWeakPtr();
}
2016-07-04 06:29:43 +00:00
BrowserContext::~BrowserContext() {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
2017-02-10 17:27:50 +00:00
NotifyWillBeDestroyed(this);
ShutdownStoragePartitions();
if (BrowserThread::IsMessageLoopValid(BrowserThread::IO)) {
BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE,
resource_context_.release());
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
base::BindOnce(&URLRequestContextGetter::NotifyContextShutdownOnIO,
base::RetainedRef(url_request_getter_)));
}
2016-07-04 06:29:43 +00:00
}
void BrowserContext::InitPrefs() {
auto prefs_path = GetPath().Append(FILE_PATH_LITERAL("Preferences"));
Fix some flaky tests in CI (#12153) * Guard whole InitPrefs with ScopedAllowIO Saw a crash: 0 0x7f8d2f7d918d base::debug::StackTrace::StackTrace() 1 0x7f8d2f7d755c base::debug::StackTrace::StackTrace() 2 0x7f8d2f867caa logging::LogMessage::~LogMessage() 3 0x7f8d2fa157c7 base::ThreadRestrictions::AssertIOAllowed() 4 0x7f8d2f83453a base::OpenFile() 5 0x7f8d2f82a967 base::ReadFileToStringWithMaxSize() 6 0x7f8d2f82ad44 base::ReadFileToString() 7 0x7f8d2f846f73 JSONFileValueDeserializer::ReadFileToString() 8 0x7f8d2f84738c JSONFileValueDeserializer::Deserialize() 9 0x7f8d35a5d1f6 <unknown> 10 0x7f8d35a5c217 JsonPrefStore::ReadPrefs() 11 0x7f8d35a87d3e PrefService::InitFromStorage() 12 0x7f8d35a87c60 PrefService::PrefService() 13 0x7f8d35a91a10 PrefServiceFactory::Create() 14 0x000000e86e1b brightray::BrowserContext::InitPrefs() 15 0x000000c2bd64 atom::AtomBrowserContext::AtomBrowserContext() 16 0x000000c320db atom::AtomBrowserContext::From() 17 0x000000b4b8b5 atom::api::Session::FromPartition() * Fix done being called twice in setInterval test The callback passed to browser process is called asyncly, so it is possible that multiple callbacks has already been scheduled before we can clearInternval. * Fix failing test when dir name has special chars The pdfSource is not escaped while parsedURL.search is. * Call done with Error instead of string * Fix crash caused by not removing input observer Solve crash: 0 libcontent.dylib content::RenderWidgetHostImpl::DispatchInputEventWithLatencyInfo(blink::WebInputEvent const&, ui::LatencyInfo*) + 214 1 libcontent.dylib content::RenderWidgetHostImpl::ForwardMouseEventWithLatencyInfo(blink::WebMouseEvent const&, ui::LatencyInfo const&) + 1350 2 libcontent.dylib content::RenderWidgetHostViewMac::ProcessMouseEvent(blink::WebMouseEvent const&, ui::LatencyInfo const&) + 44 3 libcontent.dylib content::RenderWidgetHostInputEventRouter::RouteMouseEvent(content::RenderWidgetHostViewBase*, blink::WebMouseEvent*, ui::LatencyInfo const&) + 1817 * Print detailed error * Run tests after server is ready
2018-03-07 05:40:27 +00:00
base::ThreadRestrictions::ScopedAllowIO allow_io;
PrefServiceFactory prefs_factory;
scoped_refptr<JsonPrefStore> pref_store =
base::MakeRefCounted<JsonPrefStore>(prefs_path);
Fix some flaky tests in CI (#12153) * Guard whole InitPrefs with ScopedAllowIO Saw a crash: 0 0x7f8d2f7d918d base::debug::StackTrace::StackTrace() 1 0x7f8d2f7d755c base::debug::StackTrace::StackTrace() 2 0x7f8d2f867caa logging::LogMessage::~LogMessage() 3 0x7f8d2fa157c7 base::ThreadRestrictions::AssertIOAllowed() 4 0x7f8d2f83453a base::OpenFile() 5 0x7f8d2f82a967 base::ReadFileToStringWithMaxSize() 6 0x7f8d2f82ad44 base::ReadFileToString() 7 0x7f8d2f846f73 JSONFileValueDeserializer::ReadFileToString() 8 0x7f8d2f84738c JSONFileValueDeserializer::Deserialize() 9 0x7f8d35a5d1f6 <unknown> 10 0x7f8d35a5c217 JsonPrefStore::ReadPrefs() 11 0x7f8d35a87d3e PrefService::InitFromStorage() 12 0x7f8d35a87c60 PrefService::PrefService() 13 0x7f8d35a91a10 PrefServiceFactory::Create() 14 0x000000e86e1b brightray::BrowserContext::InitPrefs() 15 0x000000c2bd64 atom::AtomBrowserContext::AtomBrowserContext() 16 0x000000c320db atom::AtomBrowserContext::From() 17 0x000000b4b8b5 atom::api::Session::FromPartition() * Fix done being called twice in setInterval test The callback passed to browser process is called asyncly, so it is possible that multiple callbacks has already been scheduled before we can clearInternval. * Fix failing test when dir name has special chars The pdfSource is not escaped while parsedURL.search is. * Call done with Error instead of string * Fix crash caused by not removing input observer Solve crash: 0 libcontent.dylib content::RenderWidgetHostImpl::DispatchInputEventWithLatencyInfo(blink::WebInputEvent const&, ui::LatencyInfo*) + 214 1 libcontent.dylib content::RenderWidgetHostImpl::ForwardMouseEventWithLatencyInfo(blink::WebMouseEvent const&, ui::LatencyInfo const&) + 1350 2 libcontent.dylib content::RenderWidgetHostViewMac::ProcessMouseEvent(blink::WebMouseEvent const&, ui::LatencyInfo const&) + 44 3 libcontent.dylib content::RenderWidgetHostInputEventRouter::RouteMouseEvent(content::RenderWidgetHostViewBase*, blink::WebMouseEvent*, ui::LatencyInfo const&) + 1817 * Print detailed error * Run tests after server is ready
2018-03-07 05:40:27 +00:00
pref_store->ReadPrefs(); // Synchronous.
prefs_factory.set_user_prefs(pref_store);
2017-12-18 00:23:02 +00:00
auto registry = WrapRefCounted(new PrefRegistrySimple);
RegisterInternalPrefs(registry.get());
RegisterPrefs(registry.get());
prefs_ = prefs_factory.Create(registry.get());
2013-03-13 19:12:05 +00:00
}
void BrowserContext::RegisterInternalPrefs(PrefRegistrySimple* registry) {
InspectableWebContentsImpl::RegisterPrefs(registry);
MediaDeviceIDSalt::RegisterPrefs(registry);
2017-01-30 11:55:46 +00:00
ZoomLevelDelegate::RegisterPrefs(registry);
}
2016-07-04 06:29:43 +00:00
URLRequestContextGetter* BrowserContext::GetRequestContext() {
return static_cast<URLRequestContextGetter*>(
GetDefaultStoragePartition(this)->GetURLRequestContext());
}
net::URLRequestContextGetter* BrowserContext::CreateRequestContext(
content::ProtocolHandlerMap* protocol_handlers,
2014-08-31 10:43:01 +00:00
content::URLRequestInterceptorScopedVector protocol_interceptors) {
DCHECK(!url_request_getter_.get());
url_request_getter_ = new URLRequestContextGetter(
2018-04-18 01:56:12 +00:00
this, static_cast<NetLog*>(BrowserClient::Get()->GetNetLog()), GetPath(),
in_memory_, BrowserThread::GetTaskRunnerForThread(BrowserThread::IO),
protocol_handlers, std::move(protocol_interceptors));
2013-03-13 19:12:05 +00:00
resource_context_->set_url_request_context_getter(url_request_getter_.get());
return url_request_getter_.get();
}
std::unique_ptr<net::NetworkDelegate> BrowserContext::CreateNetworkDelegate() {
return std::make_unique<NetworkDelegate>();
}
std::string BrowserContext::GetMediaDeviceIDSalt() {
if (!media_device_id_salt_.get())
media_device_id_salt_.reset(new MediaDeviceIDSalt(prefs_.get()));
return media_device_id_salt_->GetSalt();
}
base::FilePath BrowserContext::GetPath() const {
return path_;
2013-03-13 19:12:05 +00:00
}
std::unique_ptr<content::ZoomLevelDelegate>
BrowserContext::CreateZoomLevelDelegate(const base::FilePath& partition_path) {
2017-01-30 11:55:46 +00:00
if (!IsOffTheRecord()) {
return std::make_unique<ZoomLevelDelegate>(prefs(), partition_path);
2017-01-30 11:55:46 +00:00
}
return std::unique_ptr<content::ZoomLevelDelegate>();
2015-03-09 02:56:45 +00:00
}
2013-03-13 19:12:05 +00:00
bool BrowserContext::IsOffTheRecord() const {
return in_memory_;
2013-03-13 19:12:05 +00:00
}
content::ResourceContext* BrowserContext::GetResourceContext() {
return resource_context_.get();
}
content::DownloadManagerDelegate* BrowserContext::GetDownloadManagerDelegate() {
return nullptr;
2013-03-13 19:12:05 +00:00
}
2014-08-31 10:43:01 +00:00
content::BrowserPluginGuestManager* BrowserContext::GetGuestManager() {
return nullptr;
2013-03-13 19:12:05 +00:00
}
2014-12-05 22:31:02 +00:00
storage::SpecialStoragePolicy* BrowserContext::GetSpecialStoragePolicy() {
2015-10-12 13:09:39 +00:00
return storage_policy_.get();
2013-03-13 19:12:05 +00:00
}
2014-08-31 10:43:01 +00:00
content::PushMessagingService* BrowserContext::GetPushMessagingService() {
return nullptr;
2014-07-27 10:44:39 +00:00
}
2014-10-11 08:38:27 +00:00
content::SSLHostStateDelegate* BrowserContext::GetSSLHostStateDelegate() {
return nullptr;
}
2015-05-22 06:50:41 +00:00
content::PermissionManager* BrowserContext::GetPermissionManager() {
if (!permission_manager_.get())
permission_manager_.reset(new PermissionManager);
return permission_manager_.get();
}
2018-04-18 01:56:12 +00:00
content::BackgroundFetchDelegate* BrowserContext::GetBackgroundFetchDelegate() {
return nullptr;
}
content::BackgroundSyncController*
BrowserContext::GetBackgroundSyncController() {
2016-03-08 14:28:28 +00:00
return nullptr;
}
content::BrowsingDataRemoverDelegate*
BrowserContext::GetBrowsingDataRemoverDelegate() {
return nullptr;
}
net::URLRequestContextGetter*
BrowserContext::CreateRequestContextForStoragePartition(
const base::FilePath& partition_path,
bool in_memory,
content::ProtocolHandlerMap* protocol_handlers,
content::URLRequestInterceptorScopedVector request_interceptors) {
return nullptr;
}
2018-04-18 01:56:12 +00:00
net::URLRequestContextGetter* BrowserContext::CreateMediaRequestContext() {
2016-07-04 06:06:05 +00:00
return url_request_getter_.get();
}
net::URLRequestContextGetter*
BrowserContext::CreateMediaRequestContextForStoragePartition(
const base::FilePath& partition_path,
bool in_memory) {
return nullptr;
}
} // namespace brightray