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.
|
|
|
|
|
2017-05-18 22:58:12 +00:00
|
|
|
#include "brightray/browser/browser_context.h"
|
2013-03-13 19:12:05 +00:00
|
|
|
|
2018-09-13 00:25:56 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <utility>
|
|
|
|
|
2017-05-18 22:29:22 +00:00
|
|
|
#include "base/files/file_path.h"
|
|
|
|
#include "base/path_service.h"
|
|
|
|
#include "base/strings/string_util.h"
|
2017-12-09 17:29:28 +00:00
|
|
|
#include "base/threading/thread_restrictions.h"
|
2017-05-18 22:58:12 +00:00
|
|
|
#include "brightray/browser/brightray_paths.h"
|
|
|
|
#include "brightray/browser/browser_client.h"
|
|
|
|
#include "brightray/browser/inspectable_web_contents_impl.h"
|
|
|
|
#include "brightray/browser/zoom_level_delegate.h"
|
|
|
|
#include "brightray/common/application_info.h"
|
2016-04-27 02:12:52 +00:00
|
|
|
#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/storage_partition.h"
|
2015-09-05 11:46:55 +00:00
|
|
|
#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 {
|
|
|
|
|
2015-09-05 11:46:55 +00:00
|
|
|
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));
|
2015-09-05 11:46:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2018-08-14 21:07:53 +00:00
|
|
|
// static
|
|
|
|
void BrowserContextDeleter::Destruct(const BrowserContext* browser_context) {
|
|
|
|
browser_context->OnDestruct();
|
|
|
|
}
|
2018-08-13 23:16:04 +00:00
|
|
|
|
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
|
|
|
|
2016-07-12 12:39:23 +00:00
|
|
|
return nullptr;
|
2015-09-05 14:34:42 +00:00
|
|
|
}
|
|
|
|
|
2015-09-05 12:52:50 +00:00
|
|
|
BrowserContext::BrowserContext(const std::string& partition, bool in_memory)
|
2018-09-19 11:10:26 +00:00
|
|
|
: in_memory_(in_memory), weak_factory_(this) {
|
2018-09-15 00:09:42 +00:00
|
|
|
if (!base::PathService::Get(DIR_USER_DATA, &path_)) {
|
|
|
|
base::PathService::Get(DIR_APP_DATA, &path_);
|
2015-01-19 00:59:57 +00:00
|
|
|
path_ = path_.Append(base::FilePath::FromUTF8Unsafe(GetApplicationName()));
|
2018-09-15 00:09:42 +00:00
|
|
|
base::PathService::Override(DIR_USER_DATA, path_);
|
2015-01-19 00:59:57 +00:00
|
|
|
}
|
2013-10-07 19:21:43 +00:00
|
|
|
|
2015-09-05 12:52:50 +00:00
|
|
|
if (!in_memory_ && !partition.empty())
|
2015-09-05 11:46:55 +00:00
|
|
|
path_ = path_.Append(FILE_PATH_LITERAL("Partitions"))
|
2017-03-23 22:47:30 +00:00
|
|
|
.Append(base::FilePath::FromUTF8Unsafe(
|
|
|
|
MakePartitionName(partition)));
|
2016-05-23 03:36:34 +00:00
|
|
|
|
|
|
|
content::BrowserContext::Initialize(this, path_);
|
2016-07-12 12:39:23 +00:00
|
|
|
|
2018-08-14 21:07:53 +00:00
|
|
|
io_handle_ = new URLRequestContextGetter::Handle(GetWeakPtr());
|
|
|
|
|
2016-07-12 12:39:23 +00:00
|
|
|
browser_context_map_[PartitionKey(partition, in_memory)] = GetWeakPtr();
|
2015-10-06 07:19:38 +00:00
|
|
|
}
|
2015-08-26 21:40:02 +00:00
|
|
|
|
2016-07-04 06:29:43 +00:00
|
|
|
BrowserContext::~BrowserContext() {
|
2018-03-30 13:24:55 +00:00
|
|
|
DCHECK_CURRENTLY_ON(BrowserThread::UI);
|
2017-02-10 17:27:50 +00:00
|
|
|
NotifyWillBeDestroyed(this);
|
|
|
|
ShutdownStoragePartitions();
|
2018-08-14 21:07:53 +00:00
|
|
|
io_handle_->ShutdownOnUIThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BrowserContext::OnDestruct() const {
|
|
|
|
if (BrowserThread::CurrentlyOn(BrowserThread::UI)) {
|
|
|
|
delete this;
|
|
|
|
} else {
|
|
|
|
BrowserThread::DeleteSoon(BrowserThread::UI, FROM_HERE, this);
|
2018-08-13 23:16:04 +00:00
|
|
|
}
|
2016-07-04 06:29:43 +00:00
|
|
|
}
|
|
|
|
|
2015-10-06 07:19:38 +00:00
|
|
|
void BrowserContext::InitPrefs() {
|
2013-05-14 18:43:42 +00:00
|
|
|
auto prefs_path = GetPath().Append(FILE_PATH_LITERAL("Preferences"));
|
2018-03-07 05:40:27 +00:00
|
|
|
base::ThreadRestrictions::ScopedAllowIO allow_io;
|
2016-04-27 02:12:52 +00:00
|
|
|
PrefServiceFactory prefs_factory;
|
2017-12-09 17:29:28 +00:00
|
|
|
scoped_refptr<JsonPrefStore> pref_store =
|
|
|
|
base::MakeRefCounted<JsonPrefStore>(prefs_path);
|
2018-03-07 05:40:27 +00:00
|
|
|
pref_store->ReadPrefs(); // Synchronous.
|
2017-12-09 17:29:28 +00:00
|
|
|
prefs_factory.set_user_prefs(pref_store);
|
2013-03-29 21:28:49 +00:00
|
|
|
|
2017-12-18 00:23:02 +00:00
|
|
|
auto registry = WrapRefCounted(new PrefRegistrySimple);
|
2014-12-07 06:16:00 +00:00
|
|
|
RegisterInternalPrefs(registry.get());
|
|
|
|
RegisterPrefs(registry.get());
|
2013-03-29 21:28:49 +00:00
|
|
|
|
2014-12-07 06:16:00 +00:00
|
|
|
prefs_ = prefs_factory.Create(registry.get());
|
2013-03-13 19:12:05 +00:00
|
|
|
}
|
|
|
|
|
2013-04-08 17:53:53 +00:00
|
|
|
void BrowserContext::RegisterInternalPrefs(PrefRegistrySimple* registry) {
|
|
|
|
InspectableWebContentsImpl::RegisterPrefs(registry);
|
2016-12-03 11:12:48 +00:00
|
|
|
MediaDeviceIDSalt::RegisterPrefs(registry);
|
2017-01-30 11:55:46 +00:00
|
|
|
ZoomLevelDelegate::RegisterPrefs(registry);
|
2013-04-08 17:53:53 +00:00
|
|
|
}
|
|
|
|
|
2016-07-04 06:29:43 +00:00
|
|
|
URLRequestContextGetter* BrowserContext::GetRequestContext() {
|
|
|
|
return static_cast<URLRequestContextGetter*>(
|
|
|
|
GetDefaultStoragePartition(this)->GetURLRequestContext());
|
|
|
|
}
|
|
|
|
|
2013-11-17 22:42:56 +00:00
|
|
|
net::URLRequestContextGetter* BrowserContext::CreateRequestContext(
|
2014-06-26 20:27:22 +00:00
|
|
|
content::ProtocolHandlerMap* protocol_handlers,
|
2014-08-31 10:43:01 +00:00
|
|
|
content::URLRequestInterceptorScopedVector protocol_interceptors) {
|
2018-08-14 21:07:53 +00:00
|
|
|
return io_handle_
|
|
|
|
->CreateMainRequestContextGetter(protocol_handlers,
|
|
|
|
std::move(protocol_interceptors))
|
|
|
|
.get();
|
2013-07-17 14:21:33 +00:00
|
|
|
}
|
|
|
|
|
2017-08-04 21:15:28 +00:00
|
|
|
std::string BrowserContext::GetMediaDeviceIDSalt() {
|
2016-12-03 11:12:48 +00:00
|
|
|
if (!media_device_id_salt_.get())
|
|
|
|
media_device_id_salt_.reset(new MediaDeviceIDSalt(prefs_.get()));
|
2017-08-04 21:15:28 +00:00
|
|
|
return media_device_id_salt_->GetSalt();
|
2016-12-03 11:12:48 +00:00
|
|
|
}
|
|
|
|
|
2013-10-07 19:21:43 +00:00
|
|
|
base::FilePath BrowserContext::GetPath() const {
|
2013-06-04 18:17:16 +00:00
|
|
|
return path_;
|
2013-03-13 19:12:05 +00:00
|
|
|
}
|
|
|
|
|
2017-03-23 22:47:30 +00:00
|
|
|
std::unique_ptr<content::ZoomLevelDelegate>
|
|
|
|
BrowserContext::CreateZoomLevelDelegate(const base::FilePath& partition_path) {
|
2017-01-30 11:55:46 +00:00
|
|
|
if (!IsOffTheRecord()) {
|
2018-04-12 12:48:32 +00:00
|
|
|
return std::make_unique<ZoomLevelDelegate>(prefs(), partition_path);
|
2017-01-30 11:55:46 +00:00
|
|
|
}
|
2016-05-23 01:59:07 +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 {
|
2015-08-26 21:40:02 +00:00
|
|
|
return in_memory_;
|
2013-03-13 19:12:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
content::ResourceContext* BrowserContext::GetResourceContext() {
|
2018-08-14 21:07:53 +00:00
|
|
|
return io_handle_->GetResourceContext();
|
2013-03-13 19:12:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
content::DownloadManagerDelegate* BrowserContext::GetDownloadManagerDelegate() {
|
2014-12-17 21:13:19 +00:00
|
|
|
return nullptr;
|
2013-03-13 19:12:05 +00:00
|
|
|
}
|
|
|
|
|
2014-08-31 10:43:01 +00:00
|
|
|
content::BrowserPluginGuestManager* BrowserContext::GetGuestManager() {
|
2014-12-17 21:13:19 +00:00
|
|
|
return nullptr;
|
2013-03-13 19:12:05 +00:00
|
|
|
}
|
|
|
|
|
2014-08-31 10:43:01 +00:00
|
|
|
content::PushMessagingService* BrowserContext::GetPushMessagingService() {
|
2014-12-17 21:13:19 +00:00
|
|
|
return nullptr;
|
2014-07-27 10:44:39 +00:00
|
|
|
}
|
|
|
|
|
2014-10-11 08:38:27 +00:00
|
|
|
content::SSLHostStateDelegate* BrowserContext::GetSSLHostStateDelegate() {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-04-18 01:56:12 +00:00
|
|
|
content::BackgroundFetchDelegate* BrowserContext::GetBackgroundFetchDelegate() {
|
2017-12-18 01:40:29 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-03-23 22:47:30 +00:00
|
|
|
content::BackgroundSyncController*
|
|
|
|
BrowserContext::GetBackgroundSyncController() {
|
2016-03-08 14:28:28 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-08-23 21:35:34 +00:00
|
|
|
content::BrowsingDataRemoverDelegate*
|
|
|
|
BrowserContext::GetBrowsingDataRemoverDelegate() {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-05-23 03:28:41 +00:00
|
|
|
net::URLRequestContextGetter*
|
|
|
|
BrowserContext::CreateRequestContextForStoragePartition(
|
|
|
|
const base::FilePath& partition_path,
|
|
|
|
bool in_memory,
|
|
|
|
content::ProtocolHandlerMap* protocol_handlers,
|
|
|
|
content::URLRequestInterceptorScopedVector request_interceptors) {
|
2018-08-14 21:07:53 +00:00
|
|
|
NOTREACHED();
|
2016-05-23 03:28:41 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-04-18 01:56:12 +00:00
|
|
|
net::URLRequestContextGetter* BrowserContext::CreateMediaRequestContext() {
|
2018-08-14 21:07:53 +00:00
|
|
|
return io_handle_->GetMainRequestContextGetter().get();
|
2016-07-04 06:06:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
net::URLRequestContextGetter*
|
|
|
|
BrowserContext::CreateMediaRequestContextForStoragePartition(
|
|
|
|
const base::FilePath& partition_path,
|
|
|
|
bool in_memory) {
|
2018-08-14 21:07:53 +00:00
|
|
|
NOTREACHED();
|
2016-07-04 06:06:05 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-11-17 22:42:56 +00:00
|
|
|
} // namespace brightray
|