blob: 2868583738ed17e254a658d7c3fbd7b74a4a4713 [file] [log] [blame]
[email protected]55eb70e762012-02-20 17:38:391// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "content/public/browser/browser_context.h"
6
avib7348942015-12-25 20:57:107#include <stddef.h>
8#include <stdint.h>
avi1ed4a4372017-04-25 05:39:419
erge69130f52016-03-02 00:13:2810#include <algorithm>
11#include <limits>
rockot963ad3e82016-04-13 16:33:2312#include <memory>
dcheng36b6aec92015-12-26 06:16:3613#include <utility>
rockot963ad3e82016-04-13 16:33:2314#include <vector>
avib7348942015-12-25 20:57:1015
mmenkec0b2b8b12017-04-21 16:27:5216#include "base/base64.h"
rockot963ad3e82016-04-13 16:33:2317#include "base/command_line.h"
Chris Cunninghama5b38012017-10-28 07:27:1818#include "base/files/file_path.h"
bencccfe2a2016-03-05 16:54:1419#include "base/guid.h"
erge69130f52016-03-02 00:13:2820#include "base/lazy_instance.h"
mmenkec0b2b8b12017-04-21 16:27:5221#include "base/logging.h"
rockot963ad3e82016-04-13 16:33:2322#include "base/macros.h"
avi1ed4a4372017-04-25 05:39:4123#include "base/memory/ptr_util.h"
erge69130f52016-03-02 00:13:2824#include "base/rand_util.h"
Takashi Toyoshima621e2bf2018-09-18 08:05:1625#include "base/supports_user_data.h"
Gabriel Charette44db1422018-08-06 11:19:3326#include "base/task/post_task.h"
Ken Rockot84f58fd2018-06-22 21:54:1427#include "base/threading/sequenced_task_runner_handle.h"
ben21a34c252016-06-29 22:24:3728#include "base/threading/thread_task_runner_handle.h"
Andrey Lushnikovd39d9062018-04-24 17:33:3529#include "base/unguessable_token.h"
avib7348942015-12-25 20:57:1030#include "build/build_config.h"
dmurph7ac019a2016-05-13 00:13:1731#include "content/browser/blob_storage/chrome_blob_storage_context.h"
msrameke169ccb2017-04-26 05:21:4132#include "content/browser/browsing_data/browsing_data_remover_impl.h"
Ken Rockot84f58fd2018-06-22 21:54:1433#include "content/browser/content_service_delegate_impl.h"
[email protected]b441a8492012-06-06 14:55:5734#include "content/browser/download/download_manager_impl.h"
[email protected]c4d281662013-03-31 00:35:0835#include "content/browser/indexed_db/indexed_db_context_impl.h"
[email protected]678c0362012-12-05 08:02:4436#include "content/browser/loader/resource_dispatcher_host_impl.h"
Takashi Toyoshima621e2bf2018-09-18 08:05:1637#include "content/browser/loader/shared_cors_origin_access_list_impl.h"
Andrey Lushnikovebff0442018-07-12 20:02:5838#include "content/browser/permissions/permission_controller_impl.h"
mvanouwerkerk17205ea2014-11-07 17:30:1539#include "content/browser/push_messaging/push_messaging_router.h"
Ken Rockotc7a279c2017-05-04 23:51:4540#include "content/browser/service_manager/common_browser_interfaces.h"
[email protected]4c3a23582012-08-18 08:54:3441#include "content/browser/storage_partition_impl_map.h"
[email protected]d7c7c98a2012-07-12 21:27:4442#include "content/common/child_process_host_impl.h"
[email protected]393b6cb2014-05-15 00:55:1243#include "content/public/browser/blob_handle.h"
Eric Seckler8652dcd52018-09-20 10:42:2844#include "content/public/browser/browser_task_traits.h"
[email protected]55eb70e762012-02-20 17:38:3945#include "content/public/browser/browser_thread.h"
[email protected]b441a8492012-06-06 14:55:5746#include "content/public/browser/content_browser_client.h"
falken04a6912a2016-09-23 21:06:2947#include "content/public/browser/render_process_host.h"
[email protected]536fd0b2013-03-14 17:41:5748#include "content/public/browser/site_instance.h"
rockot963ad3e82016-04-13 16:33:2349#include "content/public/common/content_switches.h"
bend32292b2016-10-07 00:21:5850#include "content/public/common/service_manager_connection.h"
ben51bb6c62016-11-17 20:15:5751#include "content/public/common/service_names.mojom.h"
Chris Cunningham9e669472017-11-15 21:03:1152#include "media/capabilities/video_decode_stats_db_impl.h"
Chris Cunninghama5b38012017-10-28 07:27:1853#include "media/mojo/services/video_decode_perf_history.h"
[email protected]4d7c4ef2012-03-16 01:47:1254#include "net/cookies/cookie_store.h"
[email protected]6b8a3c742014-07-25 00:25:3555#include "net/ssl/channel_id_service.h"
56#include "net/ssl/channel_id_store.h"
[email protected]6e2d3d22012-02-24 18:10:3657#include "net/url_request/url_request_context.h"
[email protected]6939075a2012-08-28 08:35:5358#include "net/url_request/url_request_context_getter.h"
Ken Rockot84f58fd2018-06-22 21:54:1459#include "services/content/public/mojom/constants.mojom.h"
60#include "services/content/service.h"
ben768c8dc2016-08-12 00:26:5061#include "services/file/file_service.h"
Ken Rockot21142de2018-02-10 01:45:3062#include "services/file/public/mojom/constants.mojom.h"
ben768c8dc2016-08-12 00:26:5063#include "services/file/user_id_map.h"
rockot734fb662016-10-15 16:41:3064#include "services/service_manager/public/cpp/connector.h"
Ken Rockot543f5e32018-02-04 02:13:5065#include "services/service_manager/public/mojom/service.mojom.h"
Xing Liu89860472018-02-09 20:07:0266#include "storage/browser/blob/blob_storage_context.h"
pilgrime92c5fcd2014-09-10 23:31:2367#include "storage/browser/database/database_tracker.h"
68#include "storage/browser/fileapi/external_mount_points.h"
[email protected]55eb70e762012-02-20 17:38:3969
[email protected]314c3e22012-02-21 03:57:4270using base::UserDataAdapter;
[email protected]55eb70e762012-02-20 17:38:3971
[email protected]55eb70e762012-02-20 17:38:3972namespace content {
73
[email protected]735e20c2012-03-20 01:16:5974namespace {
75
scottmg5e65e3a2017-03-08 08:48:4676base::LazyInstance<std::map<std::string, BrowserContext*>>::DestructorAtExit
ben6c85c4492016-06-16 20:40:5177 g_user_id_to_context = LAZY_INSTANCE_INITIALIZER;
78
bend32292b2016-10-07 00:21:5879class ServiceUserIdHolder : public base::SupportsUserData::Data {
ben6c85c4492016-06-16 20:40:5180 public:
bend32292b2016-10-07 00:21:5881 explicit ServiceUserIdHolder(const std::string& user_id)
82 : user_id_(user_id) {}
83 ~ServiceUserIdHolder() override {}
ben6c85c4492016-06-16 20:40:5184
85 const std::string& user_id() const { return user_id_; }
86
87 private:
88 std::string user_id_;
89
bend32292b2016-10-07 00:21:5890 DISALLOW_COPY_AND_ASSIGN(ServiceUserIdHolder);
ben6c85c4492016-06-16 20:40:5191};
erge69130f52016-03-02 00:13:2892
Ken Rockot84f58fd2018-06-22 21:54:1493class ContentServiceDelegateHolder : public base::SupportsUserData::Data {
94 public:
95 explicit ContentServiceDelegateHolder(BrowserContext* browser_context)
96 : delegate_(browser_context) {}
97 ~ContentServiceDelegateHolder() override = default;
98
99 ContentServiceDelegateImpl* delegate() { return &delegate_; }
100
101 private:
102 ContentServiceDelegateImpl delegate_;
103
104 DISALLOW_COPY_AND_ASSIGN(ContentServiceDelegateHolder);
105};
106
[email protected]e0ce8a1e2012-09-18 10:26:36107// Key names on BrowserContext.
msrameke169ccb2017-04-26 05:21:41108const char kBrowsingDataRemoverKey[] = "browsing-data-remover";
Ken Rockot84f58fd2018-06-22 21:54:14109const char kContentServiceDelegateKey[] = "content-service-delegate";
[email protected]6ef0c3912013-01-25 22:46:34110const char kDownloadManagerKeyName[] = "download_manager";
erge69130f52016-03-02 00:13:28111const char kMojoWasInitialized[] = "mojo-was-initialized";
Takashi Toyoshima621e2bf2018-09-18 08:05:16112const char kPermissionControllerKey[] = "permission-controller";
bend32292b2016-10-07 00:21:58113const char kServiceManagerConnection[] = "service-manager-connection";
114const char kServiceUserId[] = "service-user-id";
Takashi Toyoshima621e2bf2018-09-18 08:05:16115const char kSharedCorsOriginAccessListKey[] = "shared-cors-origin-access-list";
rockot963ad3e82016-04-13 16:33:23116const char kStoragePartitionMapKeyName[] = "content_storage_partition_map";
Chris Cunninghama5b38012017-10-28 07:27:18117const char kVideoDecodePerfHistoryId[] = "video-decode-perf-history";
erge69130f52016-03-02 00:13:28118
[email protected]9afc14e22013-09-25 22:34:14119#if defined(OS_CHROMEOS)
120const char kMountPointsKey[] = "mount_points";
121#endif // defined(OS_CHROMEOS)
122
ben6c85c4492016-06-16 20:40:51123void RemoveBrowserContextFromUserIdMap(BrowserContext* browser_context) {
bend32292b2016-10-07 00:21:58124 ServiceUserIdHolder* holder = static_cast<ServiceUserIdHolder*>(
125 browser_context->GetUserData(kServiceUserId));
ben6c85c4492016-06-16 20:40:51126 if (holder) {
127 auto it = g_user_id_to_context.Get().find(holder->user_id());
128 if (it != g_user_id_to_context.Get().end())
129 g_user_id_to_context.Get().erase(it);
130 }
131}
132
[email protected]14acc642012-11-17 12:20:10133StoragePartitionImplMap* GetStoragePartitionMap(
134 BrowserContext* browser_context) {
[email protected]4c3a23582012-08-18 08:54:34135 StoragePartitionImplMap* partition_map =
136 static_cast<StoragePartitionImplMap*>(
a.cavalcantiffab73762015-08-15 02:55:48137 browser_context->GetUserData(kStoragePartitionMapKeyName));
[email protected]d7c7c98a2012-07-12 21:27:44138 if (!partition_map) {
avi1ed4a4372017-04-25 05:39:41139 auto partition_map_owned =
Jeremy Roman04f27c372017-10-27 15:20:55140 std::make_unique<StoragePartitionImplMap>(browser_context);
avi1ed4a4372017-04-25 05:39:41141 partition_map = partition_map_owned.get();
142 browser_context->SetUserData(kStoragePartitionMapKeyName,
143 std::move(partition_map_owned));
[email protected]d7c7c98a2012-07-12 21:27:44144 }
[email protected]14acc642012-11-17 12:20:10145 return partition_map;
146}
147
148StoragePartition* GetStoragePartitionFromConfig(
149 BrowserContext* browser_context,
150 const std::string& partition_domain,
151 const std::string& partition_name,
Dan Elphick6c0d8492017-09-12 09:35:38152 bool in_memory,
153 bool can_create) {
[email protected]14acc642012-11-17 12:20:10154 StoragePartitionImplMap* partition_map =
155 GetStoragePartitionMap(browser_context);
[email protected]d7c7c98a2012-07-12 21:27:44156
[email protected]1bc28312012-11-08 08:31:53157 if (browser_context->IsOffTheRecord())
158 in_memory = true;
159
Dan Elphick6c0d8492017-09-12 09:35:38160 return partition_map->Get(partition_domain, partition_name, in_memory,
161 can_create);
[email protected]d1198fd2012-08-13 22:50:19162}
163
[email protected]6939075a2012-08-28 08:35:53164void SaveSessionStateOnIOThread(
165 const scoped_refptr<net::URLRequestContextGetter>& context_getter,
[email protected]98d6d4562014-06-25 20:57:55166 AppCacheServiceImpl* appcache_service) {
[email protected]6939075a2012-08-28 08:35:53167 net::URLRequestContext* context = context_getter->GetURLRequestContext();
mmenkeded79da2016-02-06 08:28:51168 context->cookie_store()->SetForceKeepSessionState();
Reid Klecknerc1148882018-05-11 00:04:07169 context->channel_id_service()->GetChannelIDStore()->
170 SetForceKeepSessionState();
[email protected]6939075a2012-08-28 08:35:53171 appcache_service->set_force_keep_session_state();
[email protected]6e2d3d22012-02-24 18:10:36172}
173
[email protected]89acda82013-06-25 20:52:50174void SaveSessionStateOnIndexedDBThread(
[email protected]6e2d3d22012-02-24 18:10:36175 scoped_refptr<IndexedDBContextImpl> indexed_db_context) {
[email protected]bf510ed2012-06-05 08:31:43176 indexed_db_context->SetForceKeepSessionState();
[email protected]6e2d3d22012-02-24 18:10:36177}
178
falken41f4175162014-10-29 07:03:41179void ShutdownServiceWorkerContext(StoragePartition* partition) {
180 ServiceWorkerContextWrapper* wrapper =
181 static_cast<ServiceWorkerContextWrapper*>(
182 partition->GetServiceWorkerContext());
183 wrapper->process_manager()->Shutdown();
184}
185
avi1ed4a4372017-04-25 05:39:41186void SetDownloadManager(
187 BrowserContext* context,
188 std::unique_ptr<content::DownloadManager> download_manager) {
ttr31481dc54b2015-08-06 20:11:26189 DCHECK_CURRENTLY_ON(BrowserThread::UI);
190 DCHECK(download_manager);
avi1ed4a4372017-04-25 05:39:41191 context->SetUserData(kDownloadManagerKeyName, std::move(download_manager));
ttr31481dc54b2015-08-06 20:11:26192}
193
bend32292b2016-10-07 00:21:58194class BrowserContextServiceManagerConnectionHolder
rockot963ad3e82016-04-13 16:33:23195 : public base::SupportsUserData::Data {
196 public:
benaad37ecd2017-04-12 22:08:20197 explicit BrowserContextServiceManagerConnectionHolder(
rockot400ea35b2016-10-15 19:15:32198 service_manager::mojom::ServiceRequest request)
benaad37ecd2017-04-12 22:08:20199 : service_manager_connection_(ServiceManagerConnection::Create(
rockotcef38272016-07-15 22:47:47200 std::move(request),
Eric Seckler8652dcd52018-09-20 10:42:28201 base::CreateSingleThreadTaskRunnerWithTraits(
202 {BrowserThread::IO}))) {}
bend32292b2016-10-07 00:21:58203 ~BrowserContextServiceManagerConnectionHolder() override {}
rockot963ad3e82016-04-13 16:33:23204
bend32292b2016-10-07 00:21:58205 ServiceManagerConnection* service_manager_connection() {
206 return service_manager_connection_.get();
207 }
rockot963ad3e82016-04-13 16:33:23208
209 private:
bend32292b2016-10-07 00:21:58210 std::unique_ptr<ServiceManagerConnection> service_manager_connection_;
rockot963ad3e82016-04-13 16:33:23211
bend32292b2016-10-07 00:21:58212 DISALLOW_COPY_AND_ASSIGN(BrowserContextServiceManagerConnectionHolder);
rockot963ad3e82016-04-13 16:33:23213};
214
Jens Widell7ca8fb42018-02-21 14:11:54215base::WeakPtr<storage::BlobStorageContext> BlobStorageContextGetterForBrowser(
Xing Liu89860472018-02-09 20:07:02216 scoped_refptr<ChromeBlobStorageContext> blob_context) {
217 DCHECK_CURRENTLY_ON(BrowserThread::IO);
218 return blob_context->context()->AsWeakPtr();
219}
220
[email protected]735e20c2012-03-20 01:16:59221} // namespace
222
[email protected]14acc642012-11-17 12:20:10223// static
224void BrowserContext::AsyncObliterateStoragePartition(
225 BrowserContext* browser_context,
[email protected]399583b2012-12-11 09:33:42226 const GURL& site,
227 const base::Closure& on_gc_required) {
228 GetStoragePartitionMap(browser_context)->AsyncObliterate(site,
229 on_gc_required);
230}
231
232// static
233void BrowserContext::GarbageCollectStoragePartitions(
dcheng59716272016-04-09 05:19:08234 BrowserContext* browser_context,
235 std::unique_ptr<base::hash_set<base::FilePath>> active_paths,
236 const base::Closure& done) {
dcheng36b6aec92015-12-26 06:16:36237 GetStoragePartitionMap(browser_context)
238 ->GarbageCollect(std::move(active_paths), done);
[email protected]14acc642012-11-17 12:20:10239}
240
[email protected]b441a8492012-06-06 14:55:57241DownloadManager* BrowserContext::GetDownloadManager(
242 BrowserContext* context) {
mostynbfbcdc27a2015-03-13 17:58:52243 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]b441a8492012-06-06 14:55:57244 if (!context->GetUserData(kDownloadManagerKeyName)) {
Min Qinddb40ce2017-10-28 03:38:26245 DownloadManager* download_manager = new DownloadManagerImpl(context);
[email protected]d25fda12012-06-12 17:05:03246
avi1ed4a4372017-04-25 05:39:41247 SetDownloadManager(context, base::WrapUnique(download_manager));
[email protected]b441a8492012-06-06 14:55:57248 download_manager->SetDelegate(context->GetDownloadManagerDelegate());
[email protected]b441a8492012-06-06 14:55:57249 }
250
[email protected]eba4a4d2013-05-29 02:18:06251 return static_cast<DownloadManager*>(
252 context->GetUserData(kDownloadManagerKeyName));
[email protected]b441a8492012-06-06 14:55:57253}
254
[email protected]6ef0c3912013-01-25 22:46:34255// static
[email protected]cd501a72014-08-22 19:58:31256storage::ExternalMountPoints* BrowserContext::GetMountPoints(
[email protected]6ef0c3912013-01-25 22:46:34257 BrowserContext* context) {
258 // Ensure that these methods are called on the UI thread, except for
259 // unittests where a UI thread might not have been created.
260 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) ||
Gabriel Charette2983181c2018-03-28 17:01:09261 !BrowserThread::IsThreadInitialized(BrowserThread::UI));
[email protected]6ef0c3912013-01-25 22:46:34262
263#if defined(OS_CHROMEOS)
264 if (!context->GetUserData(kMountPointsKey)) {
[email protected]cd501a72014-08-22 19:58:31265 scoped_refptr<storage::ExternalMountPoints> mount_points =
266 storage::ExternalMountPoints::CreateRefCounted();
[email protected]6ef0c3912013-01-25 22:46:34267 context->SetUserData(
268 kMountPointsKey,
Jeremy Roman04f27c372017-10-27 15:20:55269 std::make_unique<UserDataAdapter<storage::ExternalMountPoints>>(
avicb129c02017-05-03 06:49:29270 mount_points.get()));
[email protected]6ef0c3912013-01-25 22:46:34271 }
272
[email protected]cd501a72014-08-22 19:58:31273 return UserDataAdapter<storage::ExternalMountPoints>::Get(context,
274 kMountPointsKey);
[email protected]6ef0c3912013-01-25 22:46:34275#else
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28276 return nullptr;
[email protected]6ef0c3912013-01-25 22:46:34277#endif
278}
279
msrameke169ccb2017-04-26 05:21:41280// static
281content::BrowsingDataRemover* content::BrowserContext::GetBrowsingDataRemover(
282 BrowserContext* context) {
283 DCHECK_CURRENTLY_ON(BrowserThread::UI);
284
285 if (!context->GetUserData(kBrowsingDataRemoverKey)) {
286 std::unique_ptr<BrowsingDataRemoverImpl> remover =
Jeremy Roman04f27c372017-10-27 15:20:55287 std::make_unique<BrowsingDataRemoverImpl>(context);
msrameke169ccb2017-04-26 05:21:41288 remover->SetEmbedderDelegate(context->GetBrowsingDataRemoverDelegate());
289 context->SetUserData(kBrowsingDataRemoverKey, std::move(remover));
290 }
291
292 return static_cast<BrowsingDataRemoverImpl*>(
293 context->GetUserData(kBrowsingDataRemoverKey));
294}
295
Andrey Lushnikovebff0442018-07-12 20:02:58296// static
297content::PermissionController* content::BrowserContext::GetPermissionController(
298 BrowserContext* context) {
299 DCHECK_CURRENTLY_ON(BrowserThread::UI);
300
301 if (!context->GetUserData(kPermissionControllerKey)) {
302 context->SetUserData(kPermissionControllerKey,
303 std::make_unique<PermissionControllerImpl>(context));
304 }
305
306 return static_cast<PermissionControllerImpl*>(
307 context->GetUserData(kPermissionControllerKey));
308}
309
[email protected]4c3a23582012-08-18 08:54:34310StoragePartition* BrowserContext::GetStoragePartition(
311 BrowserContext* browser_context,
Dan Elphick6c0d8492017-09-12 09:35:38312 SiteInstance* site_instance,
313 bool can_create) {
[email protected]1bc28312012-11-08 08:31:53314 std::string partition_domain;
315 std::string partition_name;
316 bool in_memory = false;
[email protected]4c3a23582012-08-18 08:54:34317
[email protected]4c3a23582012-08-18 08:54:34318 if (site_instance) {
[email protected]1bc28312012-11-08 08:31:53319 GetContentClient()->browser()->GetStoragePartitionConfigForSite(
[email protected]14acc642012-11-17 12:20:10320 browser_context, site_instance->GetSiteURL(), true,
[email protected]1bc28312012-11-08 08:31:53321 &partition_domain, &partition_name, &in_memory);
[email protected]4c3a23582012-08-18 08:54:34322 }
323
Dan Elphick6c0d8492017-09-12 09:35:38324 return GetStoragePartitionFromConfig(browser_context, partition_domain,
325 partition_name, in_memory, can_create);
[email protected]4c3a23582012-08-18 08:54:34326}
327
[email protected]e94bbcb2012-09-07 05:33:57328StoragePartition* BrowserContext::GetStoragePartitionForSite(
329 BrowserContext* browser_context,
Dan Elphick6c0d8492017-09-12 09:35:38330 const GURL& site,
331 bool can_create) {
[email protected]1bc28312012-11-08 08:31:53332 std::string partition_domain;
333 std::string partition_name;
334 bool in_memory;
[email protected]e94bbcb2012-09-07 05:33:57335
[email protected]1bc28312012-11-08 08:31:53336 GetContentClient()->browser()->GetStoragePartitionConfigForSite(
[email protected]14acc642012-11-17 12:20:10337 browser_context, site, true, &partition_domain, &partition_name,
338 &in_memory);
[email protected]1bc28312012-11-08 08:31:53339
Dan Elphick6c0d8492017-09-12 09:35:38340 return GetStoragePartitionFromConfig(browser_context, partition_domain,
341 partition_name, in_memory, can_create);
[email protected]e94bbcb2012-09-07 05:33:57342}
343
[email protected]4c3a23582012-08-18 08:54:34344void BrowserContext::ForEachStoragePartition(
345 BrowserContext* browser_context,
346 const StoragePartitionCallback& callback) {
347 StoragePartitionImplMap* partition_map =
348 static_cast<StoragePartitionImplMap*>(
a.cavalcantiffab73762015-08-15 02:55:48349 browser_context->GetUserData(kStoragePartitionMapKeyName));
[email protected]4c3a23582012-08-18 08:54:34350 if (!partition_map)
351 return;
352
353 partition_map->ForEach(callback);
354}
355
356StoragePartition* BrowserContext::GetDefaultStoragePartition(
357 BrowserContext* browser_context) {
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28358 return GetStoragePartition(browser_context, nullptr);
[email protected]55eb70e762012-02-20 17:38:39359}
360
tbarzicdb712682015-03-06 06:05:41361// static
[email protected]393b6cb2014-05-15 00:55:12362void BrowserContext::CreateMemoryBackedBlob(BrowserContext* browser_context,
Marijn Kruisselbrink604fd7e72017-10-26 16:31:05363 const char* data,
364 size_t length,
365 const std::string& content_type,
366 BlobCallback callback) {
mostynbfbcdc27a2015-03-13 17:58:52367 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]393b6cb2014-05-15 00:55:12368
369 ChromeBlobStorageContext* blob_context =
370 ChromeBlobStorageContext::GetFor(browser_context);
Eric Seckler8652dcd52018-09-20 10:42:28371 base::PostTaskWithTraitsAndReplyWithResult(
372 FROM_HERE, {BrowserThread::IO},
Marijn Kruisselbrink604fd7e72017-10-26 16:31:05373 base::BindOnce(&ChromeBlobStorageContext::CreateMemoryBackedBlob,
374 base::WrapRefCounted(blob_context), data, length,
375 content_type),
376 std::move(callback));
[email protected]393b6cb2014-05-15 00:55:12377}
378
[email protected]66e53d0282014-08-07 10:04:35379// static
Xing Liu89860472018-02-09 20:07:02380BrowserContext::BlobContextGetter BrowserContext::GetBlobStorageContext(
381 BrowserContext* browser_context) {
382 DCHECK_CURRENTLY_ON(BrowserThread::UI);
383 scoped_refptr<ChromeBlobStorageContext> chrome_blob_context =
384 ChromeBlobStorageContext::GetFor(browser_context);
Jens Widell7ca8fb42018-02-21 14:11:54385 return base::BindRepeating(&BlobStorageContextGetterForBrowser,
386 chrome_blob_context);
Xing Liu89860472018-02-09 20:07:02387}
388
389// static
Marijn Kruisselbrink9e073a82018-06-18 17:48:58390blink::mojom::BlobPtr BrowserContext::GetBlobPtr(
391 BrowserContext* browser_context,
392 const std::string& uuid) {
393 DCHECK_CURRENTLY_ON(BrowserThread::UI);
394 return ChromeBlobStorageContext::GetBlobPtr(browser_context, uuid);
395}
396
397// static
[email protected]66e53d0282014-08-07 10:04:35398void BrowserContext::DeliverPushMessage(
399 BrowserContext* browser_context,
400 const GURL& origin,
avib7348942015-12-25 20:57:10401 int64_t service_worker_registration_id,
Han Leonc1deace2018-08-03 03:52:53402 base::Optional<std::string> payload,
Peter Beverloo7815db1e02017-07-12 19:03:21403 const base::Callback<void(mojom::PushDeliveryStatus)>& callback) {
mostynbfbcdc27a2015-03-13 17:58:52404 DCHECK_CURRENTLY_ON(BrowserThread::UI);
harknessdd4d2b22016-01-27 19:26:43405 PushMessagingRouter::DeliverMessage(browser_context, origin,
Han Leonc1deace2018-08-03 03:52:53406 service_worker_registration_id,
407 std::move(payload), callback);
[email protected]66e53d0282014-08-07 10:04:35408}
409
falken41f4175162014-10-29 07:03:41410// static
411void BrowserContext::NotifyWillBeDestroyed(BrowserContext* browser_context) {
Lukasz Anforowicz58d0dac2018-03-23 15:48:10412 // Make sure NotifyWillBeDestroyed is idempotent. This helps facilitate the
413 // pattern where NotifyWillBeDestroyed is called from *both*
414 // ShellBrowserContext and its derived classes (e.g.
415 // LayoutTestBrowserContext).
416 if (browser_context->was_notify_will_be_destroyed_called_)
417 return;
418 browser_context->was_notify_will_be_destroyed_called_ = true;
419
Ken Rockot99c5bc742018-07-12 15:36:56420 // Subclasses of BrowserContext may expect there to be no more
421 // RenderProcessHosts using them by the time this function returns. We
422 // therefore explicitly tear down embedded Content Service instances now to
423 // ensure that all their WebContents (and therefore RPHs) are torn down too.
424 browser_context->RemoveUserData(kContentServiceDelegateKey);
425
falken41f4175162014-10-29 07:03:41426 // Service Workers must shutdown before the browser context is destroyed,
427 // since they keep render process hosts alive and the codebase assumes that
428 // render process hosts die before their profile (browser context) dies.
429 ForEachStoragePartition(browser_context,
430 base::Bind(ShutdownServiceWorkerContext));
falken04a6912a2016-09-23 21:06:29431
432 // Shared workers also keep render process hosts alive, and are expected to
Darin Fisherd3768b2f62017-10-07 01:00:16433 // return ref counts to 0 after documents close. However, to ensure that
434 // hosts are destructed now, forcibly release their ref counts here.
falken04a6912a2016-09-23 21:06:29435 for (RenderProcessHost::iterator host_iterator =
436 RenderProcessHost::AllHostsIterator();
437 !host_iterator.IsAtEnd(); host_iterator.Advance()) {
438 RenderProcessHost* host = host_iterator.GetCurrentValue();
Benoit Lizeab08362c2017-07-19 14:50:54439 if (host->GetBrowserContext() == browser_context) {
440 // This will also clean up spare RPH references.
Yutaka Hirano09a9afb92017-08-16 09:11:45441 host->DisableKeepAliveRefCount();
Benoit Lizeab08362c2017-07-19 14:50:54442 }
falken04a6912a2016-09-23 21:06:29443 }
falken41f4175162014-10-29 07:03:41444}
445
[email protected]314c3e22012-02-21 03:57:42446void BrowserContext::EnsureResourceContextInitialized(BrowserContext* context) {
[email protected]7e26ac92012-02-27 20:15:05447 // This will be enough to tickle initialization of BrowserContext if
448 // necessary, which initializes ResourceContext. The reason we don't call
[email protected]4c3a23582012-08-18 08:54:34449 // ResourceContext::InitializeResourceContext() directly here is that
450 // ResourceContext initialization may call back into BrowserContext
451 // and when that call returns it'll end rewriting its UserData map. It will
452 // end up rewriting the same value but this still causes a race condition.
453 //
454 // See http://crbug.com/115678.
455 GetDefaultStoragePartition(context);
[email protected]55eb70e762012-02-20 17:38:39456}
457
[email protected]6e2d3d22012-02-24 18:10:36458void BrowserContext::SaveSessionState(BrowserContext* browser_context) {
[email protected]b1b502e2012-09-16 07:31:43459 StoragePartition* storage_partition =
460 BrowserContext::GetDefaultStoragePartition(browser_context);
[email protected]6e2d3d22012-02-24 18:10:36461
Joshua Bell607cb142017-07-24 19:17:16462 storage::DatabaseTracker* database_tracker =
463 storage_partition->GetDatabaseTracker();
464 database_tracker->task_runner()->PostTask(
465 FROM_HERE,
466 base::BindOnce(&storage::DatabaseTracker::SetForceKeepSessionState,
kylecharda69d882017-10-04 05:49:52467 base::WrapRefCounted(database_tracker)));
Joshua Bell607cb142017-07-24 19:17:16468
Gabriel Charette2983181c2018-03-28 17:01:09469 if (BrowserThread::IsThreadInitialized(BrowserThread::IO)) {
Eric Seckler8652dcd52018-09-20 10:42:28470 base::PostTaskWithTraits(
471 FROM_HERE, {BrowserThread::IO},
tzik4fea24af2017-08-23 11:41:47472 base::BindOnce(
[email protected]6939075a2012-08-28 08:35:53473 &SaveSessionStateOnIOThread,
Clark DuVall385b5a52018-06-14 21:33:32474 base::WrapRefCounted(storage_partition->GetURLRequestContext()),
[email protected]98d6d4562014-06-25 20:57:55475 static_cast<AppCacheServiceImpl*>(
[email protected]63ef85512014-06-05 14:21:26476 storage_partition->GetAppCacheService())));
[email protected]6e2d3d22012-02-24 18:10:36477 }
478
Clark DuVall385b5a52018-06-14 21:33:32479 storage_partition->GetCookieManagerForBrowserProcess()
480 ->SetForceKeepSessionState();
481
[email protected]5f2aa722013-08-07 16:59:41482 DOMStorageContextWrapper* dom_storage_context_proxy =
483 static_cast<DOMStorageContextWrapper*>(
[email protected]b1b502e2012-09-16 07:31:43484 storage_partition->GetDOMStorageContext());
[email protected]5f2aa722013-08-07 16:59:41485 dom_storage_context_proxy->SetForceKeepSessionState();
[email protected]735e20c2012-03-20 01:16:59486
[email protected]89acda82013-06-25 20:52:50487 IndexedDBContextImpl* indexed_db_context_impl =
488 static_cast<IndexedDBContextImpl*>(
[email protected]b1b502e2012-09-16 07:31:43489 storage_partition->GetIndexedDBContext());
[email protected]89acda82013-06-25 20:52:50490 // No task runner in unit tests.
491 if (indexed_db_context_impl->TaskRunner()) {
492 indexed_db_context_impl->TaskRunner()->PostTask(
kylecharda69d882017-10-04 05:49:52493 FROM_HERE,
494 base::BindOnce(&SaveSessionStateOnIndexedDBThread,
495 base::WrapRefCounted(indexed_db_context_impl)));
[email protected]6e2d3d22012-02-24 18:10:36496 }
497}
498
ttr31481dc54b2015-08-06 20:11:26499void BrowserContext::SetDownloadManagerForTesting(
500 BrowserContext* browser_context,
avi1ed4a4372017-04-25 05:39:41501 std::unique_ptr<content::DownloadManager> download_manager) {
502 SetDownloadManager(browser_context, std::move(download_manager));
ttr31481dc54b2015-08-06 20:11:26503}
504
rockot963ad3e82016-04-13 16:33:23505// static
erge69130f52016-03-02 00:13:28506void BrowserContext::Initialize(
507 BrowserContext* browser_context,
508 const base::FilePath& path) {
ben6c85c4492016-06-16 20:40:51509 std::string new_id;
510 if (GetContentClient() && GetContentClient()->browser()) {
bend32292b2016-10-07 00:21:58511 new_id = GetContentClient()->browser()->GetServiceUserIdForBrowserContext(
ben6c85c4492016-06-16 20:40:51512 browser_context);
513 } else {
514 // Some test scenarios initialize a BrowserContext without a content client.
bencccfe2a2016-03-05 16:54:14515 new_id = base::GenerateGUID();
ben6c85c4492016-06-16 20:40:51516 }
erge69130f52016-03-02 00:13:28517
bend32292b2016-10-07 00:21:58518 ServiceUserIdHolder* holder = static_cast<ServiceUserIdHolder*>(
519 browser_context->GetUserData(kServiceUserId));
ben6c85c4492016-06-16 20:40:51520 if (holder)
bend32292b2016-10-07 00:21:58521 file::ForgetServiceUserIdUserDirAssociation(holder->user_id());
522 file::AssociateServiceUserIdWithUserDir(new_id, path);
ben6c85c4492016-06-16 20:40:51523 RemoveBrowserContextFromUserIdMap(browser_context);
524 g_user_id_to_context.Get()[new_id] = browser_context;
bend32292b2016-10-07 00:21:58525 browser_context->SetUserData(kServiceUserId,
Jeremy Roman04f27c372017-10-27 15:20:55526 std::make_unique<ServiceUserIdHolder>(new_id));
erge69130f52016-03-02 00:13:28527
avi1ed4a4372017-04-25 05:39:41528 browser_context->SetUserData(
Jeremy Roman04f27c372017-10-27 15:20:55529 kMojoWasInitialized, std::make_unique<base::SupportsUserData::Data>());
rockot963ad3e82016-04-13 16:33:23530
bend32292b2016-10-07 00:21:58531 ServiceManagerConnection* service_manager_connection =
532 ServiceManagerConnection::GetForProcess();
fdoray4f155f02016-10-12 11:28:50533 if (service_manager_connection && base::ThreadTaskRunnerHandle::IsSet()) {
rockot963ad3e82016-04-13 16:33:23534 // NOTE: Many unit tests create a TestBrowserContext without initializing
bend32292b2016-10-07 00:21:58535 // Mojo or the global service manager connection.
rockot963ad3e82016-04-13 16:33:23536
rockot400ea35b2016-10-15 19:15:32537 service_manager::mojom::ServicePtr service;
Ken Rockotf4d8a942017-05-13 00:10:37538 auto service_request = mojo::MakeRequest(&service);
rockot963ad3e82016-04-13 16:33:23539
rockot400ea35b2016-10-15 19:15:32540 service_manager::mojom::PIDReceiverPtr pid_receiver;
bene6a9f012017-01-07 00:43:43541 service_manager::Identity identity(mojom::kBrowserServiceName, new_id);
benbd3c2482017-01-07 05:48:21542 service_manager_connection->GetConnector()->StartService(
bene6a9f012017-01-07 00:43:43543 identity, std::move(service), mojo::MakeRequest(&pid_receiver));
rockot963ad3e82016-04-13 16:33:23544 pid_receiver->SetPID(base::GetCurrentProcId());
545
benaad37ecd2017-04-12 22:08:20546 service_manager_connection->GetConnector()->StartService(identity);
bend32292b2016-10-07 00:21:58547 BrowserContextServiceManagerConnectionHolder* connection_holder =
548 new BrowserContextServiceManagerConnectionHolder(
bend32292b2016-10-07 00:21:58549 std::move(service_request));
avi1ed4a4372017-04-25 05:39:41550 browser_context->SetUserData(kServiceManagerConnection,
551 base::WrapUnique(connection_holder));
rockot963ad3e82016-04-13 16:33:23552
bend32292b2016-10-07 00:21:58553 ServiceManagerConnection* connection =
554 connection_holder->service_manager_connection();
rockot963ad3e82016-04-13 16:33:23555
ben146248de2016-06-14 15:24:59556 // New embedded service factories should be added to |connection| here.
rockot963ad3e82016-04-13 16:33:23557
Ken Rockot84f58fd2018-06-22 21:54:14558 {
559 service_manager::EmbeddedServiceInfo info;
560 info.factory = base::BindRepeating(&file::CreateFileService);
561 connection->AddEmbeddedService(file::mojom::kServiceName, info);
562 }
563
564 browser_context->SetUserData(
565 kContentServiceDelegateKey,
566 std::make_unique<ContentServiceDelegateHolder>(browser_context));
567
568 {
569 service_manager::EmbeddedServiceInfo info;
570 info.task_runner = base::SequencedTaskRunnerHandle::Get();
571 info.factory = base::BindRepeating(
572 [](BrowserContext* context)
573 -> std::unique_ptr<service_manager::Service> {
574 auto* holder = static_cast<ContentServiceDelegateHolder*>(
575 context->GetUserData(kContentServiceDelegateKey));
576 auto* delegate = holder->delegate();
577 auto service = std::make_unique<content::Service>(delegate);
578 delegate->AddService(service.get());
579 return service;
580 },
581 browser_context);
582 connection->AddEmbeddedService(content::mojom::kServiceName, info);
583 }
tibellab3d36b2017-03-10 02:57:25584
585 ContentBrowserClient::StaticServiceMap services;
586 browser_context->RegisterInProcessServices(&services);
587 for (const auto& entry : services) {
588 connection->AddEmbeddedService(entry.first, entry.second);
589 }
Ken Rockotc7a279c2017-05-04 23:51:45590
591 RegisterCommonBrowserInterfaces(connection);
sammc030f5012017-03-13 02:28:45592 connection->Start();
rockot963ad3e82016-04-13 16:33:23593 }
erge69130f52016-03-02 00:13:28594}
595
rockot963ad3e82016-04-13 16:33:23596// static
bend32292b2016-10-07 00:21:58597const std::string& BrowserContext::GetServiceUserIdFor(
bencccfe2a2016-03-05 16:54:14598 BrowserContext* browser_context) {
erge69130f52016-03-02 00:13:28599 CHECK(browser_context->GetUserData(kMojoWasInitialized))
600 << "Attempting to get the mojo user id for a BrowserContext that was "
601 << "never Initialize()ed.";
602
bend32292b2016-10-07 00:21:58603 ServiceUserIdHolder* holder = static_cast<ServiceUserIdHolder*>(
604 browser_context->GetUserData(kServiceUserId));
ben6c85c4492016-06-16 20:40:51605 return holder->user_id();
erge69130f52016-03-02 00:13:28606}
607
rockot963ad3e82016-04-13 16:33:23608// static
bend32292b2016-10-07 00:21:58609BrowserContext* BrowserContext::GetBrowserContextForServiceUserId(
ben6c85c4492016-06-16 20:40:51610 const std::string& user_id) {
611 auto it = g_user_id_to_context.Get().find(user_id);
612 return it != g_user_id_to_context.Get().end() ? it->second : nullptr;
613}
614
615// static
rockot400ea35b2016-10-15 19:15:32616service_manager::Connector* BrowserContext::GetConnectorFor(
rockot963ad3e82016-04-13 16:33:23617 BrowserContext* browser_context) {
bend32292b2016-10-07 00:21:58618 ServiceManagerConnection* connection =
619 GetServiceManagerConnectionFor(browser_context);
ben5be0b9132016-08-03 00:17:18620 return connection ? connection->GetConnector() : nullptr;
621}
622
623// static
bend32292b2016-10-07 00:21:58624ServiceManagerConnection* BrowserContext::GetServiceManagerConnectionFor(
ben5be0b9132016-08-03 00:17:18625 BrowserContext* browser_context) {
bend32292b2016-10-07 00:21:58626 BrowserContextServiceManagerConnectionHolder* connection_holder =
627 static_cast<BrowserContextServiceManagerConnectionHolder*>(
628 browser_context->GetUserData(kServiceManagerConnection));
629 return connection_holder ? connection_holder->service_manager_connection()
630 : nullptr;
rockot963ad3e82016-04-13 16:33:23631}
632
Takashi Toyoshima621e2bf2018-09-18 08:05:16633// static
634SharedCorsOriginAccessList* BrowserContext::GetSharedCorsOriginAccessList(
635 BrowserContext* browser_context) {
636 return UserDataAdapter<SharedCorsOriginAccessList>::Get(
637 browser_context, kSharedCorsOriginAccessListKey);
638}
639
mmenkec0b2b8b12017-04-21 16:27:52640BrowserContext::BrowserContext()
Takashi Toyoshima621e2bf2018-09-18 08:05:16641 : unique_id_(base::UnguessableToken::Create().ToString()) {
642 SetUserData(kSharedCorsOriginAccessListKey,
643 std::make_unique<UserDataAdapter<SharedCorsOriginAccessList>>(
644 new SharedCorsOriginAccessListImpl()));
645}
mmenkec0b2b8b12017-04-21 16:27:52646
[email protected]55eb70e762012-02-20 17:38:39647BrowserContext::~BrowserContext() {
erge69130f52016-03-02 00:13:28648 CHECK(GetUserData(kMojoWasInitialized))
649 << "Attempting to destroy a BrowserContext that never called "
650 << "Initialize()";
651
kinukof6ed359c2016-07-26 13:27:21652 DCHECK(!GetUserData(kStoragePartitionMapKeyName))
653 << "StoragePartitionMap is not shut down properly";
654
Lukasz Anforowicz58d0dac2018-03-23 15:48:10655 DCHECK(was_notify_will_be_destroyed_called_);
656
ben6c85c4492016-06-16 20:40:51657 RemoveBrowserContextFromUserIdMap(this);
658
[email protected]b441a8492012-06-06 14:55:57659 if (GetUserData(kDownloadManagerKeyName))
660 GetDownloadManager(this)->Shutdown();
[email protected]55eb70e762012-02-20 17:38:39661}
662
kinukof6ed359c2016-07-26 13:27:21663void BrowserContext::ShutdownStoragePartitions() {
664 if (GetUserData(kStoragePartitionMapKeyName))
665 RemoveUserData(kStoragePartitionMapKeyName);
666}
667
mmenkec0b2b8b12017-04-21 16:27:52668std::string BrowserContext::GetMediaDeviceIDSalt() {
Andrey Lushnikovd39d9062018-04-24 17:33:35669 return unique_id_;
mmenkec0b2b8b12017-04-21 16:27:52670}
671
672// static
673std::string BrowserContext::CreateRandomMediaDeviceIDSalt() {
Andrey Lushnikovd39d9062018-04-24 17:33:35674 return base::UnguessableToken::Create().ToString();
675}
676
677const std::string& BrowserContext::UniqueId() const {
678 return unique_id_;
mmenkec0b2b8b12017-04-21 16:27:52679}
680
Chris Cunninghama5b38012017-10-28 07:27:18681media::VideoDecodePerfHistory* BrowserContext::GetVideoDecodePerfHistory() {
682 media::VideoDecodePerfHistory* decode_history =
683 static_cast<media::VideoDecodePerfHistory*>(
684 GetUserData(kVideoDecodePerfHistoryId));
685
686 // Lazily created. Note, this does not trigger loading the DB from disk. That
687 // occurs later upon first VideoDecodePerfHistory API request that requires DB
688 // access. DB operations will not block the UI thread.
689 if (!decode_history) {
chcunningham06b81092018-09-24 20:20:51690 std::unique_ptr<media::VideoDecodeStatsDBImpl> stats_db =
691 media::VideoDecodeStatsDBImpl::Create(
692 GetPath().Append(FILE_PATH_LITERAL("VideoDecodeStats")));
693 auto new_decode_history =
694 std::make_unique<media::VideoDecodePerfHistory>(std::move(stats_db));
695 decode_history = new_decode_history.get();
696
697 SetUserData(kVideoDecodePerfHistoryId, std::move(new_decode_history));
Chris Cunninghama5b38012017-10-28 07:27:18698 }
699
700 return decode_history;
701}
702
Min Qind3ff2ed62018-07-21 06:46:59703download::InProgressDownloadManager*
704BrowserContext::RetriveInProgressDownloadManager() {
705 return nullptr;
706}
707
[email protected]55eb70e762012-02-20 17:38:39708} // namespace content