Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors |
[email protected] | b0f146f | 2011-09-15 22:14:25 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | de7d61ff | 2013-08-20 11:30:41 | [diff] [blame] | 5 | #include "content/shell/browser/shell_browser_context.h" |
[email protected] | b0f146f | 2011-09-15 22:14:25 | [diff] [blame] | 6 | |
Peter Boström | dd7e40ec | 2021-04-05 20:40:10 | [diff] [blame] | 7 | #include <memory> |
dcheng | f63a125 | 2015-12-26 20:43:13 | [diff] [blame] | 8 | #include <utility> |
| 9 | |
[email protected] | 2f2b951 | 2012-06-08 17:31:00 | [diff] [blame] | 10 | #include "base/command_line.h" |
[email protected] | 5cba3bf | 2012-01-14 02:40:35 | [diff] [blame] | 11 | #include "base/environment.h" |
thestig | b7aad54f | 2014-09-05 18:25:39 | [diff] [blame] | 12 | #include "base/files/file_util.h" |
Avi Drissman | adac2199 | 2023-01-11 23:46:39 | [diff] [blame] | 13 | #include "base/functional/bind.h" |
[email protected] | b0f146f | 2011-09-15 22:14:25 | [diff] [blame] | 14 | #include "base/logging.h" |
| 15 | #include "base/path_service.h" |
[email protected] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 16 | #include "base/threading/thread.h" |
avi | 66a0772 | 2015-12-25 23:38:12 | [diff] [blame] | 17 | #include "build/build_config.h" |
juliatuttle | ca57a11b | 2016-11-15 19:07:53 | [diff] [blame] | 18 | #include "components/keyed_service/content/browser_context_dependency_manager.h" |
Henrique Nakashima | dd1cdd1 | 2019-04-25 15:47:04 | [diff] [blame] | 19 | #include "components/keyed_service/core/simple_dependency_manager.h" |
Xi Han | 720a2a2 | 2019-05-21 17:39:59 | [diff] [blame] | 20 | #include "components/keyed_service/core/simple_factory_key.h" |
Henrique Nakashima | 7bb9d52 | 2019-04-16 20:57:36 | [diff] [blame] | 21 | #include "components/keyed_service/core/simple_key_map.h" |
mmenke | 68a5021 | 2017-06-12 20:20:31 | [diff] [blame] | 22 | #include "components/network_session_configurator/common/network_switches.h" |
Peter Birk Pakkenberg | d927c94 | 2022-11-17 14:30:42 | [diff] [blame] | 23 | #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 Seckler | 8652dcd5 | 2018-09-20 10:42:28 | [diff] [blame] | 26 | #include "content/public/browser/browser_task_traits.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 27 | #include "content/public/browser/browser_thread.h" |
Peter Birk Pakkenberg | d927c94 | 2022-11-17 14:30:42 | [diff] [blame] | 28 | #include "content/public/browser/origin_trials_controller_delegate.h" |
[email protected] | 6bd3007 | 2013-02-08 18:17:11 | [diff] [blame] | 29 | #include "content/public/browser/storage_partition.h" |
| 30 | #include "content/public/common/content_switches.h" |
Victor Tan | 10d93aca | 2022-08-12 16:46:28 | [diff] [blame] | 31 | #include "content/shell/browser/shell_content_browser_client.h" |
danakj | dd5bb4ef | 2020-06-26 23:29:22 | [diff] [blame] | 32 | #include "content/shell/browser/shell_content_index_provider.h" |
[email protected] | de7d61ff | 2013-08-20 11:30:41 | [diff] [blame] | 33 | #include "content/shell/browser/shell_download_manager_delegate.h" |
Christian Biesinger | dcca172 | 2022-03-18 15:02:48 | [diff] [blame] | 34 | #include "content/shell/browser/shell_federated_permission_context.h" |
John Abd-El-Malek | 2276305 | 2021-06-15 03:35:53 | [diff] [blame] | 35 | #include "content/shell/browser/shell_paths.h" |
mlamouri | 4e37202 | 2015-03-29 14:51:06 | [diff] [blame] | 36 | #include "content/shell/browser/shell_permission_manager.h" |
[email protected] | b7c504c | 2013-05-07 14:42:12 | [diff] [blame] | 37 | #include "content/shell/common/shell_switches.h" |
nsatragno | db7b65c64 | 2016-02-16 20:14:02 | [diff] [blame] | 38 | #include "content/test/mock_background_sync_controller.h" |
Victor Tan | 10d93aca | 2022-08-12 16:46:28 | [diff] [blame] | 39 | #include "content/test/mock_reduce_accept_language_controller_delegate.h" |
Peter Birk Pakkenberg | d927c94 | 2022-11-17 14:30:42 | [diff] [blame] | 40 | #include "third_party/blink/public/common/origin_trials/trial_token_validator.h" |
[email protected] | b0f146f | 2011-09-15 22:14:25 | [diff] [blame] | 41 | |
[email protected] | 810ddc5 | 2012-01-24 01:00:35 | [diff] [blame] | 42 | namespace content { |
| 43 | |
[email protected] | d5869bf | 2013-07-03 16:21:47 | [diff] [blame] | 44 | ShellBrowserContext::ShellBrowserContext(bool off_the_record, |
Michael Giuffrida | c5571960 | 2018-02-09 17:10:35 | [diff] [blame] | 45 | bool delay_services_creation) |
Arthur Sonzogni | ff83758 | 2023-12-22 10:05:45 | [diff] [blame^] | 46 | : off_the_record_(off_the_record) { |
[email protected] | 11a65b69 | 2012-03-30 11:29:16 | [diff] [blame] | 47 | InitWhileIOAllowed(); |
Will Harris | 7ddd850 | 2023-01-10 23:51:34 | [diff] [blame] | 48 | #if BUILDFLAG(IS_WIN) |
| 49 | base::SetExtraNoExecuteAllowedPath(SHELL_DIR_USER_DATA); |
| 50 | #endif // BUILDFLAG(IS_WIN) |
| 51 | |
Michael Giuffrida | c5571960 | 2018-02-09 17:10:35 | [diff] [blame] | 52 | if (!delay_services_creation) { |
| 53 | BrowserContextDependencyManager::GetInstance() |
| 54 | ->CreateBrowserContextServices(this); |
| 55 | } |
[email protected] | b0f146f | 2011-09-15 22:14:25 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | ShellBrowserContext::~ShellBrowserContext() { |
Lukasz Anforowicz | eda637d2 | 2021-05-20 00:32:23 | [diff] [blame] | 59 | NotifyWillBeDestroyed(); |
Lukasz Anforowicz | 58d0dac | 2018-03-23 15:48:10 | [diff] [blame] | 60 | |
Henrique Nakashima | dd1cdd1 | 2019-04-25 15:47:04 | [diff] [blame] | 61 | // The SimpleDependencyManager should always be passed after the |
| 62 | // BrowserContextDependencyManager. This is because the KeyedService instances |
| 63 | // in the BrowserContextDependencyManager's dependency graph can depend on the |
| 64 | // ones in the SimpleDependencyManager's graph. |
| 65 | DependencyManager::PerformInterlockedTwoPhaseShutdown( |
| 66 | BrowserContextDependencyManager::GetInstance(), this, |
| 67 | SimpleDependencyManager::GetInstance(), key_.get()); |
Henrique Nakashima | 7bb9d52 | 2019-04-16 20:57:36 | [diff] [blame] | 68 | |
| 69 | SimpleKeyMap::GetInstance()->Dissociate(this); |
| 70 | |
jam | 21e62ae | 2017-03-21 19:57:48 | [diff] [blame] | 71 | ShutdownStoragePartitions(); |
[email protected] | b0f146f | 2011-09-15 22:14:25 | [diff] [blame] | 72 | } |
| 73 | |
[email protected] | 11a65b69 | 2012-03-30 11:29:16 | [diff] [blame] | 74 | void ShellBrowserContext::InitWhileIOAllowed() { |
avi | 83883c8 | 2014-12-23 00:08:49 | [diff] [blame] | 75 | base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess(); |
mkwst | d993d06a | 2014-10-10 11:44:38 | [diff] [blame] | 76 | if (cmd_line->HasSwitch(switches::kIgnoreCertificateErrors)) |
[email protected] | c2dad29 | 2012-09-07 21:27:35 | [diff] [blame] | 77 | ignore_certificate_errors_ = true; |
[email protected] | e1cf85a2 | 2012-08-17 22:39:23 | [diff] [blame] | 78 | if (cmd_line->HasSwitch(switches::kContentShellDataPath)) { |
| 79 | path_ = cmd_line->GetSwitchValuePath(switches::kContentShellDataPath); |
michaelpg | 49a54b59 | 2016-10-04 02:07:21 | [diff] [blame] | 80 | if (base::DirectoryExists(path_) || base::CreateDirectory(path_)) { |
| 81 | // BrowserContext needs an absolute path, which we would normally get via |
| 82 | // PathService. In this case, manually ensure the path is absolute. |
| 83 | if (!path_.IsAbsolute()) |
| 84 | path_ = base::MakeAbsoluteFilePath(path_); |
| 85 | if (!path_.empty()) { |
Henrique Nakashima | 7bb9d52 | 2019-04-16 20:57:36 | [diff] [blame] | 86 | FinishInitWhileIOAllowed(); |
Will Harris | d5478a0 | 2022-08-05 22:21:10 | [diff] [blame] | 87 | base::PathService::OverrideAndCreateIfNeeded( |
| 88 | SHELL_DIR_USER_DATA, path_, /*is_absolute=*/true, /*create=*/false); |
michaelpg | 49a54b59 | 2016-10-04 02:07:21 | [diff] [blame] | 89 | return; |
| 90 | } |
| 91 | } else { |
| 92 | LOG(WARNING) << "Unable to create data-path directory: " << path_.value(); |
| 93 | } |
[email protected] | e1cf85a2 | 2012-08-17 22:39:23 | [diff] [blame] | 94 | } |
michaelpg | 49a54b59 | 2016-10-04 02:07:21 | [diff] [blame] | 95 | |
John Abd-El-Malek | 2276305 | 2021-06-15 03:35:53 | [diff] [blame] | 96 | CHECK(base::PathService::Get(SHELL_DIR_USER_DATA, &path_)); |
Henrique Nakashima | 7bb9d52 | 2019-04-16 20:57:36 | [diff] [blame] | 97 | |
| 98 | FinishInitWhileIOAllowed(); |
| 99 | } |
| 100 | |
| 101 | void ShellBrowserContext::FinishInitWhileIOAllowed() { |
Xi Han | 720a2a2 | 2019-05-21 17:39:59 | [diff] [blame] | 102 | key_ = std::make_unique<SimpleFactoryKey>(path_, off_the_record_); |
Henrique Nakashima | 7bb9d52 | 2019-04-16 20:57:36 | [diff] [blame] | 103 | SimpleKeyMap::GetInstance()->Associate(this, key_.get()); |
[email protected] | 11a65b69 | 2012-03-30 11:29:16 | [diff] [blame] | 104 | } |
[email protected] | b0f146f | 2011-09-15 22:14:25 | [diff] [blame] | 105 | |
dcheng | 6003e0b | 2016-04-09 18:42:34 | [diff] [blame] | 106 | std::unique_ptr<ZoomLevelDelegate> ShellBrowserContext::CreateZoomLevelDelegate( |
wjmaclean | caa7d6d | 2014-11-12 16:42:11 | [diff] [blame] | 107 | const base::FilePath&) { |
Lei Zhang | df291f6 | 2021-04-14 17:23:44 | [diff] [blame] | 108 | return nullptr; |
wjmaclean | caa7d6d | 2014-11-12 16:42:11 | [diff] [blame] | 109 | } |
| 110 | |
Lucas Furukawa Gadani | f4522386 | 2019-06-21 23:11:14 | [diff] [blame] | 111 | base::FilePath ShellBrowserContext::GetPath() { |
[email protected] | e99ca511 | 2011-09-26 17:22:54 | [diff] [blame] | 112 | return path_; |
[email protected] | b0f146f | 2011-09-15 22:14:25 | [diff] [blame] | 113 | } |
| 114 | |
Lucas Furukawa Gadani | f4522386 | 2019-06-21 23:11:14 | [diff] [blame] | 115 | bool ShellBrowserContext::IsOffTheRecord() { |
[email protected] | 71d504f | 2012-07-25 17:15:28 | [diff] [blame] | 116 | return off_the_record_; |
[email protected] | b0f146f | 2011-09-15 22:14:25 | [diff] [blame] | 117 | } |
| 118 | |
[email protected] | b441a849 | 2012-06-06 14:55:57 | [diff] [blame] | 119 | DownloadManagerDelegate* ShellBrowserContext::GetDownloadManagerDelegate() { |
[email protected] | fc72bb1 | 2013-06-02 21:13:46 | [diff] [blame] | 120 | if (!download_manager_delegate_.get()) { |
Peter Boström | dd7e40ec | 2021-04-05 20:40:10 | [diff] [blame] | 121 | download_manager_delegate_ = |
| 122 | std::make_unique<ShellDownloadManagerDelegate>(); |
Lukasz Anforowicz | 48d8345 | 2021-05-12 02:58:20 | [diff] [blame] | 123 | download_manager_delegate_->SetDownloadManager(GetDownloadManager()); |
[email protected] | 5900209 | 2012-08-27 19:42:56 | [diff] [blame] | 124 | } |
[email protected] | 08c92ac | 2012-08-21 23:41:40 | [diff] [blame] | 125 | |
[email protected] | b441a849 | 2012-06-06 14:55:57 | [diff] [blame] | 126 | return download_manager_delegate_.get(); |
[email protected] | b0f146f | 2011-09-15 22:14:25 | [diff] [blame] | 127 | } |
| 128 | |
[email protected] | 139355f | 2014-05-11 14:21:28 | [diff] [blame] | 129 | BrowserPluginGuestManager* ShellBrowserContext::GetGuestManager() { |
Kevin McNee | 6f0513b | 2020-09-29 19:37:01 | [diff] [blame] | 130 | return nullptr; |
[email protected] | 2456926 | 2014-05-06 03:31:30 | [diff] [blame] | 131 | } |
| 132 | |
[email protected] | cd501a7 | 2014-08-22 19:58:31 | [diff] [blame] | 133 | storage::SpecialStoragePolicy* ShellBrowserContext::GetSpecialStoragePolicy() { |
Ivan Kotenkov | 2c0d2bb3 | 2017-11-01 15:41:28 | [diff] [blame] | 134 | return nullptr; |
[email protected] | b0f146f | 2011-09-15 22:14:25 | [diff] [blame] | 135 | } |
| 136 | |
Richard Knoll | 77b8b2b | 2021-09-08 09:29:01 | [diff] [blame] | 137 | PlatformNotificationService* |
| 138 | ShellBrowserContext::GetPlatformNotificationService() { |
| 139 | return nullptr; |
| 140 | } |
| 141 | |
[email protected] | c5c89d04 | 2014-06-13 14:43:37 | [diff] [blame] | 142 | PushMessagingService* ShellBrowserContext::GetPushMessagingService() { |
Ivan Kotenkov | 2c0d2bb3 | 2017-11-01 15:41:28 | [diff] [blame] | 143 | return nullptr; |
[email protected] | c5c89d04 | 2014-06-13 14:43:37 | [diff] [blame] | 144 | } |
| 145 | |
Jarryd | 20733d3 | 2019-10-02 23:27:17 | [diff] [blame] | 146 | StorageNotificationService* |
| 147 | ShellBrowserContext::GetStorageNotificationService() { |
| 148 | return nullptr; |
| 149 | } |
| 150 | |
[email protected] | c5bbe0e | 2014-08-01 23:23:30 | [diff] [blame] | 151 | SSLHostStateDelegate* ShellBrowserContext::GetSSLHostStateDelegate() { |
Ivan Kotenkov | 2c0d2bb3 | 2017-11-01 15:41:28 | [diff] [blame] | 152 | return nullptr; |
[email protected] | c5bbe0e | 2014-08-01 23:23:30 | [diff] [blame] | 153 | } |
| 154 | |
Andrey Lushnikov | f350010 | 2018-07-16 19:55:22 | [diff] [blame] | 155 | PermissionControllerDelegate* |
| 156 | ShellBrowserContext::GetPermissionControllerDelegate() { |
mlamouri | 4e37202 | 2015-03-29 14:51:06 | [diff] [blame] | 157 | if (!permission_manager_.get()) |
Peter Boström | dd7e40ec | 2021-04-05 20:40:10 | [diff] [blame] | 158 | permission_manager_ = std::make_unique<ShellPermissionManager>(); |
mlamouri | 4e37202 | 2015-03-29 14:51:06 | [diff] [blame] | 159 | return permission_manager_.get(); |
| 160 | } |
| 161 | |
Tarun Bansal | 1ff665c | 2019-01-09 16:35:52 | [diff] [blame] | 162 | ClientHintsControllerDelegate* |
| 163 | ShellBrowserContext::GetClientHintsControllerDelegate() { |
Scott Violet | 3cd8d36 | 2020-04-30 22:18:24 | [diff] [blame] | 164 | return client_hints_controller_delegate_; |
Tarun Bansal | 1ff665c | 2019-01-09 16:35:52 | [diff] [blame] | 165 | } |
| 166 | |
Dan Elphick | 97521318 | 2017-09-22 10:37:47 | [diff] [blame] | 167 | BackgroundFetchDelegate* ShellBrowserContext::GetBackgroundFetchDelegate() { |
| 168 | return nullptr; |
| 169 | } |
| 170 | |
jkarlin | 6f5078ed | 2015-10-06 15:13:35 | [diff] [blame] | 171 | BackgroundSyncController* ShellBrowserContext::GetBackgroundSyncController() { |
Peter Boström | dd7e40ec | 2021-04-05 20:40:10 | [diff] [blame] | 172 | if (!background_sync_controller_) { |
| 173 | background_sync_controller_ = |
| 174 | std::make_unique<MockBackgroundSyncController>(); |
| 175 | } |
jkarlin | 428a4a3 | 2016-01-08 16:13:24 | [diff] [blame] | 176 | return background_sync_controller_.get(); |
jkarlin | 6f5078ed | 2015-10-06 15:13:35 | [diff] [blame] | 177 | } |
| 178 | |
msramek | 86b6bc7 | 2017-05-31 12:21:10 | [diff] [blame] | 179 | BrowsingDataRemoverDelegate* |
| 180 | ShellBrowserContext::GetBrowsingDataRemoverDelegate() { |
| 181 | return nullptr; |
| 182 | } |
| 183 | |
Rayan Kanso | 4f753a67 | 2019-08-12 17:30:07 | [diff] [blame] | 184 | ContentIndexProvider* ShellBrowserContext::GetContentIndexProvider() { |
| 185 | if (!content_index_provider_) |
danakj | dd5bb4ef | 2020-06-26 23:29:22 | [diff] [blame] | 186 | content_index_provider_ = std::make_unique<ShellContentIndexProvider>(); |
Rayan Kanso | 4f753a67 | 2019-08-12 17:30:07 | [diff] [blame] | 187 | return content_index_provider_.get(); |
| 188 | } |
| 189 | |
Peter Kotwicz | 153898a | 2022-05-05 18:48:39 | [diff] [blame] | 190 | FederatedIdentityApiPermissionContextDelegate* |
| 191 | ShellBrowserContext::GetFederatedIdentityApiPermissionContext() { |
Christian Biesinger | 4b36fa1 | 2023-08-18 01:43:07 | [diff] [blame] | 192 | return GetShellFederatedPermissionContext(); |
Peter Kotwicz | 153898a | 2022-05-05 18:48:39 | [diff] [blame] | 193 | } |
| 194 | |
Zachary Tan | cef87242 | 2023-02-17 14:45:49 | [diff] [blame] | 195 | FederatedIdentityAutoReauthnPermissionContextDelegate* |
| 196 | ShellBrowserContext::GetFederatedIdentityAutoReauthnPermissionContext() { |
Christian Biesinger | 4b36fa1 | 2023-08-18 01:43:07 | [diff] [blame] | 197 | return GetShellFederatedPermissionContext(); |
Zachary Tan | 3d1c32c | 2023-01-31 16:50:54 | [diff] [blame] | 198 | } |
| 199 | |
Peter Kotwicz | c18ea67 | 2022-11-30 23:58:27 | [diff] [blame] | 200 | FederatedIdentityPermissionContextDelegate* |
| 201 | ShellBrowserContext::GetFederatedIdentityPermissionContext() { |
Christian Biesinger | 4b36fa1 | 2023-08-18 01:43:07 | [diff] [blame] | 202 | return GetShellFederatedPermissionContext(); |
| 203 | } |
| 204 | |
| 205 | ShellFederatedPermissionContext* |
| 206 | ShellBrowserContext::GetShellFederatedPermissionContext() { |
Christian Biesinger | dcca172 | 2022-03-18 15:02:48 | [diff] [blame] | 207 | if (!federated_permission_context_) |
| 208 | federated_permission_context_ = |
| 209 | std::make_unique<ShellFederatedPermissionContext>(); |
| 210 | return federated_permission_context_.get(); |
| 211 | } |
| 212 | |
Victor Tan | 10d93aca | 2022-08-12 16:46:28 | [diff] [blame] | 213 | ReduceAcceptLanguageControllerDelegate* |
| 214 | ShellBrowserContext::GetReduceAcceptLanguageControllerDelegate() { |
| 215 | if (!reduce_accept_lang_controller_delegate_) { |
| 216 | reduce_accept_lang_controller_delegate_ = |
| 217 | std::make_unique<MockReduceAcceptLanguageControllerDelegate>( |
| 218 | GetShellLanguage()); |
| 219 | } |
| 220 | return reduce_accept_lang_controller_delegate_.get(); |
| 221 | } |
| 222 | |
Peter Birk Pakkenberg | d927c94 | 2022-11-17 14:30:42 | [diff] [blame] | 223 | OriginTrialsControllerDelegate* |
| 224 | ShellBrowserContext::GetOriginTrialsControllerDelegate() { |
| 225 | if (!origin_trials::features::IsPersistentOriginTrialsEnabled()) |
| 226 | return nullptr; |
| 227 | |
| 228 | if (!origin_trials_controller_delegate_) { |
| 229 | origin_trials_controller_delegate_ = |
| 230 | std::make_unique<origin_trials::OriginTrials>( |
| 231 | std::make_unique<origin_trials::LevelDbPersistenceProvider>( |
| 232 | GetPath(), |
| 233 | GetDefaultStoragePartition()->GetProtoDatabaseProvider()), |
| 234 | std::make_unique<blink::TrialTokenValidator>()); |
| 235 | } |
| 236 | return origin_trials_controller_delegate_.get(); |
| 237 | } |
| 238 | |
[email protected] | b0f146f | 2011-09-15 22:14:25 | [diff] [blame] | 239 | } // namespace content |