blob: ec49e03720cd285295d4b6e4fd6f79cf2f6fa1c4 [file] [log] [blame]
Avi Drissman4e1b7bc32022-09-15 14:03:501// Copyright 2013 The Chromium Authors
[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
Peter Boströmdd7e40ec2021-04-05 20:40:107#include <memory>
dchengf63a1252015-12-26 20:43:138#include <utility>
9
[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"
Avi Drissmanadac21992023-01-11 23:46:3913#include "base/functional/bind.h"
[email protected]b0f146f2011-09-15 22:14:2514#include "base/logging.h"
15#include "base/path_service.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"
Peter Birk Pakkenbergd927c942022-11-17 14:30:4223#include "components/origin_trials/browser/leveldb_persistence_provider.h"
24#include "components/origin_trials/browser/origin_trials.h"
25#include "components/origin_trials/common/features.h"
Eric Seckler8652dcd52018-09-20 10:42:2826#include "content/public/browser/browser_task_traits.h"
[email protected]c38831a12011-10-28 12:44:4927#include "content/public/browser/browser_thread.h"
Peter Birk Pakkenbergd927c942022-11-17 14:30:4228#include "content/public/browser/origin_trials_controller_delegate.h"
[email protected]6bd30072013-02-08 18:17:1129#include "content/public/browser/storage_partition.h"
30#include "content/public/common/content_switches.h"
Victor Tan10d93aca2022-08-12 16:46:2831#include "content/shell/browser/shell_content_browser_client.h"
danakjdd5bb4ef2020-06-26 23:29:2232#include "content/shell/browser/shell_content_index_provider.h"
[email protected]de7d61ff2013-08-20 11:30:4133#include "content/shell/browser/shell_download_manager_delegate.h"
Christian Biesingerdcca1722022-03-18 15:02:4834#include "content/shell/browser/shell_federated_permission_context.h"
John Abd-El-Malek22763052021-06-15 03:35:5335#include "content/shell/browser/shell_paths.h"
mlamouri4e372022015-03-29 14:51:0636#include "content/shell/browser/shell_permission_manager.h"
[email protected]b7c504c2013-05-07 14:42:1237#include "content/shell/common/shell_switches.h"
nsatragnodb7b65c642016-02-16 20:14:0238#include "content/test/mock_background_sync_controller.h"
Victor Tan10d93aca2022-08-12 16:46:2839#include "content/test/mock_reduce_accept_language_controller_delegate.h"
Peter Birk Pakkenbergd927c942022-11-17 14:30:4240#include "third_party/blink/public/common/origin_trials/trial_token_validator.h"
[email protected]b0f146f2011-09-15 22:14:2541
[email protected]810ddc52012-01-24 01:00:3542namespace content {
43
John Abd-El-Malek8104d672018-11-28 20:27:0344ShellBrowserContext::ShellResourceContext::ShellResourceContext() {}
[email protected]6bd30072013-02-08 18:17:1145
hanxi3328d992014-10-09 13:27:1146ShellBrowserContext::ShellResourceContext::~ShellResourceContext() {
47}
[email protected]6bd30072013-02-08 18:17:1148
[email protected]d5869bf2013-07-03 16:21:4749ShellBrowserContext::ShellBrowserContext(bool off_the_record,
Michael Giuffridac55719602018-02-09 17:10:3550 bool delay_services_creation)
Kevin McNee6f0513b2020-09-29 19:37:0151 : resource_context_(std::make_unique<ShellResourceContext>()),
52 off_the_record_(off_the_record) {
[email protected]11a65b692012-03-30 11:29:1653 InitWhileIOAllowed();
Will Harris7ddd8502023-01-10 23:51:3454#if BUILDFLAG(IS_WIN)
55 base::SetExtraNoExecuteAllowedPath(SHELL_DIR_USER_DATA);
56#endif // BUILDFLAG(IS_WIN)
57
Michael Giuffridac55719602018-02-09 17:10:3558 if (!delay_services_creation) {
59 BrowserContextDependencyManager::GetInstance()
60 ->CreateBrowserContextServices(this);
61 }
[email protected]b0f146f2011-09-15 22:14:2562}
63
64ShellBrowserContext::~ShellBrowserContext() {
Lukasz Anforowiczeda637d22021-05-20 00:32:2365 NotifyWillBeDestroyed();
Lukasz Anforowicz58d0dac2018-03-23 15:48:1066
Henrique Nakashimadd1cdd12019-04-25 15:47:0467 // The SimpleDependencyManager should always be passed after the
68 // BrowserContextDependencyManager. This is because the KeyedService instances
69 // in the BrowserContextDependencyManager's dependency graph can depend on the
70 // ones in the SimpleDependencyManager's graph.
71 DependencyManager::PerformInterlockedTwoPhaseShutdown(
72 BrowserContextDependencyManager::GetInstance(), this,
73 SimpleDependencyManager::GetInstance(), key_.get());
Henrique Nakashima7bb9d522019-04-16 20:57:3674
75 SimpleKeyMap::GetInstance()->Dissociate(this);
76
jam21e62ae2017-03-21 19:57:4877 // Need to destruct the ResourceContext before posting tasks which may delete
78 // the URLRequestContext because ResourceContext's destructor will remove any
79 // outstanding request while URLRequestContext's destructor ensures that there
80 // are no more outstanding requests.
[email protected]59383c782013-04-17 16:43:2781 if (resource_context_) {
Gabriel Charettee7cdc5cd2020-05-27 23:35:0582 GetIOThreadTaskRunner({})->DeleteSoon(FROM_HERE,
83 resource_context_.release());
[email protected]e99ca5112011-09-26 17:22:5484 }
jam21e62ae2017-03-21 19:57:4885 ShutdownStoragePartitions();
[email protected]b0f146f2011-09-15 22:14:2586}
87
[email protected]11a65b692012-03-30 11:29:1688void ShellBrowserContext::InitWhileIOAllowed() {
avi83883c82014-12-23 00:08:4989 base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess();
mkwstd993d06a2014-10-10 11:44:3890 if (cmd_line->HasSwitch(switches::kIgnoreCertificateErrors))
[email protected]c2dad292012-09-07 21:27:3591 ignore_certificate_errors_ = true;
[email protected]e1cf85a22012-08-17 22:39:2392 if (cmd_line->HasSwitch(switches::kContentShellDataPath)) {
93 path_ = cmd_line->GetSwitchValuePath(switches::kContentShellDataPath);
michaelpg49a54b592016-10-04 02:07:2194 if (base::DirectoryExists(path_) || base::CreateDirectory(path_)) {
95 // BrowserContext needs an absolute path, which we would normally get via
96 // PathService. In this case, manually ensure the path is absolute.
97 if (!path_.IsAbsolute())
98 path_ = base::MakeAbsoluteFilePath(path_);
99 if (!path_.empty()) {
Henrique Nakashima7bb9d522019-04-16 20:57:36100 FinishInitWhileIOAllowed();
Will Harrisd5478a02022-08-05 22:21:10101 base::PathService::OverrideAndCreateIfNeeded(
102 SHELL_DIR_USER_DATA, path_, /*is_absolute=*/true, /*create=*/false);
michaelpg49a54b592016-10-04 02:07:21103 return;
104 }
105 } else {
106 LOG(WARNING) << "Unable to create data-path directory: " << path_.value();
107 }
[email protected]e1cf85a22012-08-17 22:39:23108 }
michaelpg49a54b592016-10-04 02:07:21109
John Abd-El-Malek22763052021-06-15 03:35:53110 CHECK(base::PathService::Get(SHELL_DIR_USER_DATA, &path_));
Henrique Nakashima7bb9d522019-04-16 20:57:36111
112 FinishInitWhileIOAllowed();
113}
114
115void ShellBrowserContext::FinishInitWhileIOAllowed() {
Xi Han720a2a22019-05-21 17:39:59116 key_ = std::make_unique<SimpleFactoryKey>(path_, off_the_record_);
Henrique Nakashima7bb9d522019-04-16 20:57:36117 SimpleKeyMap::GetInstance()->Associate(this, key_.get());
[email protected]11a65b692012-03-30 11:29:16118}
[email protected]b0f146f2011-09-15 22:14:25119
dcheng6003e0b2016-04-09 18:42:34120std::unique_ptr<ZoomLevelDelegate> ShellBrowserContext::CreateZoomLevelDelegate(
wjmacleancaa7d6d2014-11-12 16:42:11121 const base::FilePath&) {
Lei Zhangdf291f62021-04-14 17:23:44122 return nullptr;
wjmacleancaa7d6d2014-11-12 16:42:11123}
124
Lucas Furukawa Gadanif45223862019-06-21 23:11:14125base::FilePath ShellBrowserContext::GetPath() {
[email protected]e99ca5112011-09-26 17:22:54126 return path_;
[email protected]b0f146f2011-09-15 22:14:25127}
128
Lucas Furukawa Gadanif45223862019-06-21 23:11:14129bool ShellBrowserContext::IsOffTheRecord() {
[email protected]71d504f2012-07-25 17:15:28130 return off_the_record_;
[email protected]b0f146f2011-09-15 22:14:25131}
132
[email protected]b441a8492012-06-06 14:55:57133DownloadManagerDelegate* ShellBrowserContext::GetDownloadManagerDelegate() {
[email protected]fc72bb12013-06-02 21:13:46134 if (!download_manager_delegate_.get()) {
Peter Boströmdd7e40ec2021-04-05 20:40:10135 download_manager_delegate_ =
136 std::make_unique<ShellDownloadManagerDelegate>();
Lukasz Anforowicz48d83452021-05-12 02:58:20137 download_manager_delegate_->SetDownloadManager(GetDownloadManager());
[email protected]59002092012-08-27 19:42:56138 }
[email protected]08c92ac2012-08-21 23:41:40139
[email protected]b441a8492012-06-06 14:55:57140 return download_manager_delegate_.get();
[email protected]b0f146f2011-09-15 22:14:25141}
142
[email protected]df02aca2012-02-09 21:03:20143ResourceContext* ShellBrowserContext::GetResourceContext() {
[email protected]df02aca2012-02-09 21:03:20144 return resource_context_.get();
[email protected]b0f146f2011-09-15 22:14:25145}
146
[email protected]139355f2014-05-11 14:21:28147BrowserPluginGuestManager* ShellBrowserContext::GetGuestManager() {
Kevin McNee6f0513b2020-09-29 19:37:01148 return nullptr;
[email protected]24569262014-05-06 03:31:30149}
150
[email protected]cd501a72014-08-22 19:58:31151storage::SpecialStoragePolicy* ShellBrowserContext::GetSpecialStoragePolicy() {
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28152 return nullptr;
[email protected]b0f146f2011-09-15 22:14:25153}
154
Richard Knoll77b8b2b2021-09-08 09:29:01155PlatformNotificationService*
156ShellBrowserContext::GetPlatformNotificationService() {
157 return nullptr;
158}
159
[email protected]c5c89d042014-06-13 14:43:37160PushMessagingService* ShellBrowserContext::GetPushMessagingService() {
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28161 return nullptr;
[email protected]c5c89d042014-06-13 14:43:37162}
163
Jarryd20733d32019-10-02 23:27:17164StorageNotificationService*
165ShellBrowserContext::GetStorageNotificationService() {
166 return nullptr;
167}
168
[email protected]c5bbe0e2014-08-01 23:23:30169SSLHostStateDelegate* ShellBrowserContext::GetSSLHostStateDelegate() {
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28170 return nullptr;
[email protected]c5bbe0e2014-08-01 23:23:30171}
172
Andrey Lushnikovf3500102018-07-16 19:55:22173PermissionControllerDelegate*
174ShellBrowserContext::GetPermissionControllerDelegate() {
mlamouri4e372022015-03-29 14:51:06175 if (!permission_manager_.get())
Peter Boströmdd7e40ec2021-04-05 20:40:10176 permission_manager_ = std::make_unique<ShellPermissionManager>();
mlamouri4e372022015-03-29 14:51:06177 return permission_manager_.get();
178}
179
Tarun Bansal1ff665c2019-01-09 16:35:52180ClientHintsControllerDelegate*
181ShellBrowserContext::GetClientHintsControllerDelegate() {
Scott Violet3cd8d362020-04-30 22:18:24182 return client_hints_controller_delegate_;
Tarun Bansal1ff665c2019-01-09 16:35:52183}
184
Dan Elphick975213182017-09-22 10:37:47185BackgroundFetchDelegate* ShellBrowserContext::GetBackgroundFetchDelegate() {
186 return nullptr;
187}
188
jkarlin6f5078ed2015-10-06 15:13:35189BackgroundSyncController* ShellBrowserContext::GetBackgroundSyncController() {
Peter Boströmdd7e40ec2021-04-05 20:40:10190 if (!background_sync_controller_) {
191 background_sync_controller_ =
192 std::make_unique<MockBackgroundSyncController>();
193 }
jkarlin428a4a32016-01-08 16:13:24194 return background_sync_controller_.get();
jkarlin6f5078ed2015-10-06 15:13:35195}
196
msramek86b6bc72017-05-31 12:21:10197BrowsingDataRemoverDelegate*
198ShellBrowserContext::GetBrowsingDataRemoverDelegate() {
199 return nullptr;
200}
201
Rayan Kanso4f753a672019-08-12 17:30:07202ContentIndexProvider* ShellBrowserContext::GetContentIndexProvider() {
203 if (!content_index_provider_)
danakjdd5bb4ef2020-06-26 23:29:22204 content_index_provider_ = std::make_unique<ShellContentIndexProvider>();
Rayan Kanso4f753a672019-08-12 17:30:07205 return content_index_provider_.get();
206}
207
Peter Kotwicz153898a2022-05-05 18:48:39208FederatedIdentityApiPermissionContextDelegate*
209ShellBrowserContext::GetFederatedIdentityApiPermissionContext() {
210 if (!federated_permission_context_)
211 federated_permission_context_ =
212 std::make_unique<ShellFederatedPermissionContext>();
213 return federated_permission_context_.get();
214}
215
Zachary Tan3d1c32c2023-01-31 16:50:54216FederatedIdentityAutoSigninPermissionContextDelegate*
217ShellBrowserContext::GetFederatedIdentityAutoSigninPermissionContext() {
218 if (!federated_permission_context_) {
219 federated_permission_context_ =
220 std::make_unique<ShellFederatedPermissionContext>();
221 }
222 return federated_permission_context_.get();
223}
224
Peter Kotwiczc18ea672022-11-30 23:58:27225FederatedIdentityPermissionContextDelegate*
226ShellBrowserContext::GetFederatedIdentityPermissionContext() {
Christian Biesingerdcca1722022-03-18 15:02:48227 if (!federated_permission_context_)
228 federated_permission_context_ =
229 std::make_unique<ShellFederatedPermissionContext>();
230 return federated_permission_context_.get();
231}
232
Victor Tan10d93aca2022-08-12 16:46:28233ReduceAcceptLanguageControllerDelegate*
234ShellBrowserContext::GetReduceAcceptLanguageControllerDelegate() {
235 if (!reduce_accept_lang_controller_delegate_) {
236 reduce_accept_lang_controller_delegate_ =
237 std::make_unique<MockReduceAcceptLanguageControllerDelegate>(
238 GetShellLanguage());
239 }
240 return reduce_accept_lang_controller_delegate_.get();
241}
242
Peter Birk Pakkenbergd927c942022-11-17 14:30:42243OriginTrialsControllerDelegate*
244ShellBrowserContext::GetOriginTrialsControllerDelegate() {
245 if (!origin_trials::features::IsPersistentOriginTrialsEnabled())
246 return nullptr;
247
248 if (!origin_trials_controller_delegate_) {
249 origin_trials_controller_delegate_ =
250 std::make_unique<origin_trials::OriginTrials>(
251 std::make_unique<origin_trials::LevelDbPersistenceProvider>(
252 GetPath(),
253 GetDefaultStoragePartition()->GetProtoDatabaseProvider()),
254 std::make_unique<blink::TrialTokenValidator>());
255 }
256 return origin_trials_controller_delegate_.get();
257}
258
[email protected]b0f146f2011-09-15 22:14:25259} // namespace content