blob: 3a964c177955e17db26ef98f72d9f5793d42f828 [file] [log] [blame]
[email protected]de7d61ff2013-08-20 11:30:411// Copyright 2013 The Chromium Authors. All rights reserved.
[email protected]b0f146f2011-09-15 22:14:252// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]de7d61ff2013-08-20 11:30:415#include "content/shell/browser/shell_browser_context.h"
[email protected]b0f146f2011-09-15 22:14:256
dchengf63a1252015-12-26 20:43:137#include <utility>
8
[email protected]037edb52011-11-15 21:14:069#include "base/bind.h"
[email protected]2f2b9512012-06-08 17:31:0010#include "base/command_line.h"
[email protected]5cba3bf2012-01-14 02:40:3511#include "base/environment.h"
thestigb7aad54f2014-09-05 18:25:3912#include "base/files/file_util.h"
[email protected]b0f146f2011-09-15 22:14:2513#include "base/logging.h"
14#include "base/path_service.h"
Eric Seckler8652dcd52018-09-20 10:42:2815#include "base/task/post_task.h"
[email protected]2e5b60a22011-11-28 15:56:4116#include "base/threading/thread.h"
avi66a07722015-12-25 23:38:1217#include "build/build_config.h"
juliatuttleca57a11b2016-11-15 19:07:5318#include "components/keyed_service/content/browser_context_dependency_manager.h"
Henrique Nakashimadd1cdd12019-04-25 15:47:0419#include "components/keyed_service/core/simple_dependency_manager.h"
Xi Han720a2a22019-05-21 17:39:5920#include "components/keyed_service/core/simple_factory_key.h"
Henrique Nakashima7bb9d522019-04-16 20:57:3621#include "components/keyed_service/core/simple_key_map.h"
mmenke68a50212017-06-12 20:20:3122#include "components/network_session_configurator/common/network_switches.h"
Eric Seckler8652dcd52018-09-20 10:42:2823#include "content/public/browser/browser_task_traits.h"
[email protected]c38831a12011-10-28 12:44:4924#include "content/public/browser/browser_thread.h"
Sam Goto0230b712019-05-14 00:30:5125#include "content/public/browser/sms_service.h"
[email protected]6bd30072013-02-08 18:17:1126#include "content/public/browser/storage_partition.h"
27#include "content/public/common/content_switches.h"
[email protected]de7d61ff2013-08-20 11:30:4128#include "content/shell/browser/shell_download_manager_delegate.h"
mlamouri4e372022015-03-29 14:51:0629#include "content/shell/browser/shell_permission_manager.h"
[email protected]b7c504c2013-05-07 14:42:1230#include "content/shell/common/shell_switches.h"
nsatragnodb7b65c642016-02-16 20:14:0231#include "content/test/mock_background_sync_controller.h"
[email protected]b0f146f2011-09-15 22:14:2532
33#if defined(OS_WIN)
34#include "base/base_paths_win.h"
[email protected]5cba3bf2012-01-14 02:40:3535#elif defined(OS_LINUX)
36#include "base/nix/xdg_util.h"
[email protected]80ec0b72012-03-22 22:45:2737#elif defined(OS_MACOSX)
38#include "base/base_paths_mac.h"
Kevin Marshallec64321f2018-05-22 03:17:4139#elif defined(OS_FUCHSIA)
40#include "base/base_paths_fuchsia.h"
[email protected]b0f146f2011-09-15 22:14:2541#endif
42
[email protected]810ddc52012-01-24 01:00:3543namespace content {
44
John Abd-El-Malek8104d672018-11-28 20:27:0345ShellBrowserContext::ShellResourceContext::ShellResourceContext() {}
[email protected]6bd30072013-02-08 18:17:1146
hanxi3328d992014-10-09 13:27:1147ShellBrowserContext::ShellResourceContext::~ShellResourceContext() {
48}
[email protected]6bd30072013-02-08 18:17:1149
[email protected]d5869bf2013-07-03 16:21:4750ShellBrowserContext::ShellBrowserContext(bool off_the_record,
Michael Giuffridac55719602018-02-09 17:10:3551 net::NetLog* net_log,
52 bool delay_services_creation)
hanxi3328d992014-10-09 13:27:1153 : resource_context_(new ShellResourceContext),
mkwstd993d06a2014-10-10 11:44:3854 ignore_certificate_errors_(false),
hanxi3328d992014-10-09 13:27:1155 off_the_record_(off_the_record),
[email protected]d5869bf2013-07-03 16:21:4756 net_log_(net_log),
Ivan Kotenkov2c0d2bb32017-11-01 15:41:2857 guest_manager_(nullptr) {
[email protected]11a65b692012-03-30 11:29:1658 InitWhileIOAllowed();
Michael Giuffridac55719602018-02-09 17:10:3559 if (!delay_services_creation) {
60 BrowserContextDependencyManager::GetInstance()
61 ->CreateBrowserContextServices(this);
62 }
[email protected]b0f146f2011-09-15 22:14:2563}
64
65ShellBrowserContext::~ShellBrowserContext() {
Lukasz Anforowicz58d0dac2018-03-23 15:48:1066 NotifyWillBeDestroyed(this);
67
Henrique Nakashimadd1cdd12019-04-25 15:47:0468 // The SimpleDependencyManager should always be passed after the
69 // BrowserContextDependencyManager. This is because the KeyedService instances
70 // in the BrowserContextDependencyManager's dependency graph can depend on the
71 // ones in the SimpleDependencyManager's graph.
72 DependencyManager::PerformInterlockedTwoPhaseShutdown(
73 BrowserContextDependencyManager::GetInstance(), this,
74 SimpleDependencyManager::GetInstance(), key_.get());
Henrique Nakashima7bb9d522019-04-16 20:57:3675
76 SimpleKeyMap::GetInstance()->Dissociate(this);
77
jam21e62ae2017-03-21 19:57:4878 // Need to destruct the ResourceContext before posting tasks which may delete
79 // the URLRequestContext because ResourceContext's destructor will remove any
80 // outstanding request while URLRequestContext's destructor ensures that there
81 // are no more outstanding requests.
[email protected]59383c782013-04-17 16:43:2782 if (resource_context_) {
[email protected]e99ca5112011-09-26 17:22:5483 BrowserThread::DeleteSoon(
84 BrowserThread::IO, FROM_HERE, resource_context_.release());
85 }
jam21e62ae2017-03-21 19:57:4886 ShutdownStoragePartitions();
Maks Orlovich4a89010e2017-08-25 01:49:4487 if (url_request_getter_) {
Eric Seckler8652dcd52018-09-20 10:42:2888 base::PostTaskWithTraits(
89 FROM_HERE, {BrowserThread::IO},
Maks Orlovich4a89010e2017-08-25 01:49:4490 base::BindOnce(&ShellURLRequestContextGetter::NotifyContextShuttingDown,
91 url_request_getter_));
92 }
[email protected]b0f146f2011-09-15 22:14:2593}
94
[email protected]11a65b692012-03-30 11:29:1695void ShellBrowserContext::InitWhileIOAllowed() {
avi83883c82014-12-23 00:08:4996 base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess();
mkwstd993d06a2014-10-10 11:44:3897 if (cmd_line->HasSwitch(switches::kIgnoreCertificateErrors))
[email protected]c2dad292012-09-07 21:27:3598 ignore_certificate_errors_ = true;
[email protected]e1cf85a22012-08-17 22:39:2399 if (cmd_line->HasSwitch(switches::kContentShellDataPath)) {
100 path_ = cmd_line->GetSwitchValuePath(switches::kContentShellDataPath);
michaelpg49a54b592016-10-04 02:07:21101 if (base::DirectoryExists(path_) || base::CreateDirectory(path_)) {
102 // BrowserContext needs an absolute path, which we would normally get via
103 // PathService. In this case, manually ensure the path is absolute.
104 if (!path_.IsAbsolute())
105 path_ = base::MakeAbsoluteFilePath(path_);
106 if (!path_.empty()) {
Henrique Nakashima7bb9d522019-04-16 20:57:36107 FinishInitWhileIOAllowed();
michaelpg49a54b592016-10-04 02:07:21108 return;
109 }
110 } else {
111 LOG(WARNING) << "Unable to create data-path directory: " << path_.value();
112 }
[email protected]e1cf85a22012-08-17 22:39:23113 }
michaelpg49a54b592016-10-04 02:07:21114
[email protected]b0f146f2011-09-15 22:14:25115#if defined(OS_WIN)
Avi Drissman1cb5e9f2018-05-01 15:53:28116 CHECK(base::PathService::Get(base::DIR_LOCAL_APP_DATA, &path_));
[email protected]e99ca5112011-09-26 17:22:54117 path_ = path_.Append(std::wstring(L"content_shell"));
[email protected]5cba3bf2012-01-14 02:40:35118#elif defined(OS_LINUX)
dcheng6003e0b2016-04-09 18:42:34119 std::unique_ptr<base::Environment> env(base::Environment::Create());
[email protected]d30a36f2013-02-07 04:16:26120 base::FilePath config_dir(
[email protected]9528c9a2012-06-13 23:20:22121 base::nix::GetXDGDirectory(env.get(),
122 base::nix::kXdgConfigHomeEnvVar,
123 base::nix::kDotConfigDir));
[email protected]5cba3bf2012-01-14 02:40:35124 path_ = config_dir.Append("content_shell");
[email protected]80ec0b72012-03-22 22:45:27125#elif defined(OS_MACOSX)
Avi Drissman1cb5e9f2018-05-01 15:53:28126 CHECK(base::PathService::Get(base::DIR_APP_DATA, &path_));
[email protected]80ec0b72012-03-22 22:45:27127 path_ = path_.Append("Chromium Content Shell");
[email protected]70a7f1192012-06-08 01:31:34128#elif defined(OS_ANDROID)
Avi Drissman1cb5e9f2018-05-01 15:53:28129 CHECK(base::PathService::Get(base::DIR_ANDROID_APP_DATA, &path_));
[email protected]70a7f1192012-06-08 01:31:34130 path_ = path_.Append(FILE_PATH_LITERAL("content_shell"));
Kevin Marshallec64321f2018-05-22 03:17:41131#elif defined(OS_FUCHSIA)
132 CHECK(base::PathService::Get(base::DIR_APP_DATA, &path_));
133 path_ = path_.Append(FILE_PATH_LITERAL("content_shell"));
[email protected]b0f146f2011-09-15 22:14:25134#else
135 NOTIMPLEMENTED();
136#endif
137
[email protected]7567484142013-07-11 17:36:07138 if (!base::PathExists(path_))
[email protected]426d1c92013-12-03 20:08:54139 base::CreateDirectory(path_);
Henrique Nakashima7bb9d522019-04-16 20:57:36140
141 FinishInitWhileIOAllowed();
142}
143
144void ShellBrowserContext::FinishInitWhileIOAllowed() {
erge69130f52016-03-02 00:13:28145 BrowserContext::Initialize(this, path_);
Xi Han720a2a22019-05-21 17:39:59146 key_ = std::make_unique<SimpleFactoryKey>(path_, off_the_record_);
Henrique Nakashima7bb9d522019-04-16 20:57:36147 SimpleKeyMap::GetInstance()->Associate(this, key_.get());
[email protected]11a65b692012-03-30 11:29:16148}
[email protected]b0f146f2011-09-15 22:14:25149
mcnee336ea2c2017-05-23 22:50:59150#if !defined(OS_ANDROID)
dcheng6003e0b2016-04-09 18:42:34151std::unique_ptr<ZoomLevelDelegate> ShellBrowserContext::CreateZoomLevelDelegate(
wjmacleancaa7d6d2014-11-12 16:42:11152 const base::FilePath&) {
dcheng6003e0b2016-04-09 18:42:34153 return std::unique_ptr<ZoomLevelDelegate>();
wjmacleancaa7d6d2014-11-12 16:42:11154}
mcnee336ea2c2017-05-23 22:50:59155#endif // !defined(OS_ANDROID)
wjmacleancaa7d6d2014-11-12 16:42:11156
[email protected]4251165a2013-07-17 04:33:40157base::FilePath ShellBrowserContext::GetPath() const {
[email protected]e99ca5112011-09-26 17:22:54158 return path_;
[email protected]b0f146f2011-09-15 22:14:25159}
160
[email protected]27d6e852012-03-02 21:31:32161bool ShellBrowserContext::IsOffTheRecord() const {
[email protected]71d504f2012-07-25 17:15:28162 return off_the_record_;
[email protected]b0f146f2011-09-15 22:14:25163}
164
[email protected]b441a8492012-06-06 14:55:57165DownloadManagerDelegate* ShellBrowserContext::GetDownloadManagerDelegate() {
[email protected]fc72bb12013-06-02 21:13:46166 if (!download_manager_delegate_.get()) {
[email protected]7cef82172013-12-17 06:58:37167 download_manager_delegate_.reset(new ShellDownloadManagerDelegate());
mkwstd993d06a2014-10-10 11:44:38168 download_manager_delegate_->SetDownloadManager(
169 BrowserContext::GetDownloadManager(this));
[email protected]59002092012-08-27 19:42:56170 }
[email protected]08c92ac2012-08-21 23:41:40171
[email protected]b441a8492012-06-06 14:55:57172 return download_manager_delegate_.get();
[email protected]b0f146f2011-09-15 22:14:25173}
174
mkwst94388f792014-10-16 07:36:52175ShellURLRequestContextGetter*
176ShellBrowserContext::CreateURLRequestContextGetter(
[email protected]7571263c2014-03-10 22:57:09177 ProtocolHandlerMap* protocol_handlers,
[email protected]3b90aab2014-05-30 17:56:15178 URLRequestInterceptorScopedVector request_interceptors) {
mkwst94388f792014-10-16 07:36:52179 return new ShellURLRequestContextGetter(
Maks Orlovich05651bf2017-07-24 15:20:26180 ignore_certificate_errors_, off_the_record_, GetPath(),
Eric Seckler8652dcd52018-09-20 10:42:28181 base::CreateSingleThreadTaskRunnerWithTraits({BrowserThread::IO}),
dchengf63a1252015-12-26 20:43:13182 protocol_handlers, std::move(request_interceptors), net_log_);
mkwst94388f792014-10-16 07:36:52183}
184
185net::URLRequestContextGetter* ShellBrowserContext::CreateRequestContext(
186 ProtocolHandlerMap* protocol_handlers,
187 URLRequestInterceptorScopedVector request_interceptors) {
188 DCHECK(!url_request_getter_.get());
189 url_request_getter_ = CreateURLRequestContextGetter(
dchengf63a1252015-12-26 20:43:13190 protocol_handlers, std::move(request_interceptors));
[email protected]6bd30072013-02-08 18:17:11191 return url_request_getter_.get();
[email protected]b0f146f2011-09-15 22:14:25192}
193
194net::URLRequestContextGetter*
[email protected]7571263c2014-03-10 22:57:09195ShellBrowserContext::CreateRequestContextForStoragePartition(
196 const base::FilePath& partition_path,
197 bool in_memory,
198 ProtocolHandlerMap* protocol_handlers,
[email protected]3b90aab2014-05-30 17:56:15199 URLRequestInterceptorScopedVector request_interceptors) {
Lukasz Anforowicz5d7e145f2018-04-11 01:37:05200 scoped_refptr<ShellURLRequestContextGetter>& context_getter =
201 isolated_url_request_getters_[partition_path];
202 if (!context_getter) {
203 context_getter = CreateURLRequestContextGetter(
204 protocol_handlers, std::move(request_interceptors));
205 }
206 return context_getter.get();
jam2503f932016-04-15 20:08:30207}
208
209net::URLRequestContextGetter*
210 ShellBrowserContext::CreateMediaRequestContext() {
211 DCHECK(url_request_getter_.get());
212 return url_request_getter_.get();
213}
214
215net::URLRequestContextGetter*
216 ShellBrowserContext::CreateMediaRequestContextForStoragePartition(
217 const base::FilePath& partition_path,
218 bool in_memory) {
219 return nullptr;
[email protected]55c0eca2012-09-15 05:12:34220}
221
[email protected]df02aca2012-02-09 21:03:20222ResourceContext* ShellBrowserContext::GetResourceContext() {
[email protected]df02aca2012-02-09 21:03:20223 return resource_context_.get();
[email protected]b0f146f2011-09-15 22:14:25224}
225
[email protected]139355f2014-05-11 14:21:28226BrowserPluginGuestManager* ShellBrowserContext::GetGuestManager() {
227 return guest_manager_;
[email protected]24569262014-05-06 03:31:30228}
229
[email protected]cd501a72014-08-22 19:58:31230storage::SpecialStoragePolicy* ShellBrowserContext::GetSpecialStoragePolicy() {
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28231 return nullptr;
[email protected]b0f146f2011-09-15 22:14:25232}
233
[email protected]c5c89d042014-06-13 14:43:37234PushMessagingService* ShellBrowserContext::GetPushMessagingService() {
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28235 return nullptr;
[email protected]c5c89d042014-06-13 14:43:37236}
237
[email protected]c5bbe0e2014-08-01 23:23:30238SSLHostStateDelegate* ShellBrowserContext::GetSSLHostStateDelegate() {
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28239 return nullptr;
[email protected]c5bbe0e2014-08-01 23:23:30240}
241
Andrey Lushnikovf3500102018-07-16 19:55:22242PermissionControllerDelegate*
243ShellBrowserContext::GetPermissionControllerDelegate() {
mlamouri4e372022015-03-29 14:51:06244 if (!permission_manager_.get())
245 permission_manager_.reset(new ShellPermissionManager());
246 return permission_manager_.get();
247}
248
Tarun Bansal1ff665c2019-01-09 16:35:52249ClientHintsControllerDelegate*
250ShellBrowserContext::GetClientHintsControllerDelegate() {
251 return nullptr;
252}
253
Dan Elphick975213182017-09-22 10:37:47254BackgroundFetchDelegate* ShellBrowserContext::GetBackgroundFetchDelegate() {
255 return nullptr;
256}
257
jkarlin6f5078ed2015-10-06 15:13:35258BackgroundSyncController* ShellBrowserContext::GetBackgroundSyncController() {
jkarlin428a4a32016-01-08 16:13:24259 if (!background_sync_controller_)
nsatragnodb7b65c642016-02-16 20:14:02260 background_sync_controller_.reset(new MockBackgroundSyncController());
jkarlin428a4a32016-01-08 16:13:24261 return background_sync_controller_.get();
jkarlin6f5078ed2015-10-06 15:13:35262}
263
msramek86b6bc72017-05-31 12:21:10264BrowsingDataRemoverDelegate*
265ShellBrowserContext::GetBrowsingDataRemoverDelegate() {
266 return nullptr;
267}
268
Sam Goto0230b712019-05-14 00:30:51269SmsService* ShellBrowserContext::GetSmsService() {
270 if (!sms_service_)
271 sms_service_ = content::SmsService::Create();
272 return sms_service_.get();
273}
274
[email protected]b0f146f2011-09-15 22:14:25275} // namespace content