blob: cdb38c8437e09b1bf007d06aadf7a4aea93f2e0c [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"
mek17399e672017-05-09 03:51:2025#include "base/task_scheduler/post_task.h"
Ken Rockot84f58fd2018-06-22 21:54:1426#include "base/threading/sequenced_task_runner_handle.h"
ben21a34c252016-06-29 22:24:3727#include "base/threading/thread_task_runner_handle.h"
Andrey Lushnikovd39d9062018-04-24 17:33:3528#include "base/unguessable_token.h"
avib7348942015-12-25 20:57:1029#include "build/build_config.h"
dmurph7ac019a2016-05-13 00:13:1730#include "content/browser/blob_storage/chrome_blob_storage_context.h"
msrameke169ccb2017-04-26 05:21:4131#include "content/browser/browsing_data/browsing_data_remover_impl.h"
Ken Rockot84f58fd2018-06-22 21:54:1432#include "content/browser/content_service_delegate_impl.h"
[email protected]b441a8492012-06-06 14:55:5733#include "content/browser/download/download_manager_impl.h"
[email protected]c4d281662013-03-31 00:35:0834#include "content/browser/indexed_db/indexed_db_context_impl.h"
[email protected]678c0362012-12-05 08:02:4435#include "content/browser/loader/resource_dispatcher_host_impl.h"
mvanouwerkerk17205ea2014-11-07 17:30:1536#include "content/browser/push_messaging/push_messaging_router.h"
Ken Rockotc7a279c2017-05-04 23:51:4537#include "content/browser/service_manager/common_browser_interfaces.h"
[email protected]4c3a23582012-08-18 08:54:3438#include "content/browser/storage_partition_impl_map.h"
[email protected]d7c7c98a2012-07-12 21:27:4439#include "content/common/child_process_host_impl.h"
[email protected]393b6cb2014-05-15 00:55:1240#include "content/public/browser/blob_handle.h"
[email protected]55eb70e762012-02-20 17:38:3941#include "content/public/browser/browser_thread.h"
[email protected]b441a8492012-06-06 14:55:5742#include "content/public/browser/content_browser_client.h"
falken04a6912a2016-09-23 21:06:2943#include "content/public/browser/render_process_host.h"
[email protected]536fd0b2013-03-14 17:41:5744#include "content/public/browser/site_instance.h"
Elad Alon12ecdf02018-03-14 12:07:3145#include "content/public/browser/webrtc_event_logger.h"
rockot963ad3e82016-04-13 16:33:2346#include "content/public/common/content_switches.h"
bend32292b2016-10-07 00:21:5847#include "content/public/common/service_manager_connection.h"
ben51bb6c62016-11-17 20:15:5748#include "content/public/common/service_names.mojom.h"
Chris Cunningham9e669472017-11-15 21:03:1149#include "media/capabilities/video_decode_stats_db_impl.h"
Chris Cunninghama5b38012017-10-28 07:27:1850#include "media/mojo/services/video_decode_perf_history.h"
[email protected]4d7c4ef2012-03-16 01:47:1251#include "net/cookies/cookie_store.h"
[email protected]6b8a3c742014-07-25 00:25:3552#include "net/ssl/channel_id_service.h"
53#include "net/ssl/channel_id_store.h"
[email protected]6e2d3d22012-02-24 18:10:3654#include "net/url_request/url_request_context.h"
[email protected]6939075a2012-08-28 08:35:5355#include "net/url_request/url_request_context_getter.h"
Ken Rockot84f58fd2018-06-22 21:54:1456#include "services/content/public/mojom/constants.mojom.h"
57#include "services/content/service.h"
ben768c8dc2016-08-12 00:26:5058#include "services/file/file_service.h"
Ken Rockot21142de2018-02-10 01:45:3059#include "services/file/public/mojom/constants.mojom.h"
ben768c8dc2016-08-12 00:26:5060#include "services/file/user_id_map.h"
rockot734fb662016-10-15 16:41:3061#include "services/service_manager/public/cpp/connector.h"
Ken Rockot543f5e32018-02-04 02:13:5062#include "services/service_manager/public/mojom/service.mojom.h"
Xing Liu89860472018-02-09 20:07:0263#include "storage/browser/blob/blob_storage_context.h"
pilgrime92c5fcd2014-09-10 23:31:2364#include "storage/browser/database/database_tracker.h"
65#include "storage/browser/fileapi/external_mount_points.h"
[email protected]55eb70e762012-02-20 17:38:3966
Elad Alon03ad23b2018-02-07 16:54:3167
[email protected]314c3e22012-02-21 03:57:4268using base::UserDataAdapter;
[email protected]55eb70e762012-02-20 17:38:3969
[email protected]55eb70e762012-02-20 17:38:3970namespace content {
71
[email protected]735e20c2012-03-20 01:16:5972namespace {
73
scottmg5e65e3a2017-03-08 08:48:4674base::LazyInstance<std::map<std::string, BrowserContext*>>::DestructorAtExit
ben6c85c4492016-06-16 20:40:5175 g_user_id_to_context = LAZY_INSTANCE_INITIALIZER;
76
bend32292b2016-10-07 00:21:5877class ServiceUserIdHolder : public base::SupportsUserData::Data {
ben6c85c4492016-06-16 20:40:5178 public:
bend32292b2016-10-07 00:21:5879 explicit ServiceUserIdHolder(const std::string& user_id)
80 : user_id_(user_id) {}
81 ~ServiceUserIdHolder() override {}
ben6c85c4492016-06-16 20:40:5182
83 const std::string& user_id() const { return user_id_; }
84
85 private:
86 std::string user_id_;
87
bend32292b2016-10-07 00:21:5888 DISALLOW_COPY_AND_ASSIGN(ServiceUserIdHolder);
ben6c85c4492016-06-16 20:40:5189};
erge69130f52016-03-02 00:13:2890
Ken Rockot84f58fd2018-06-22 21:54:1491class ContentServiceDelegateHolder : public base::SupportsUserData::Data {
92 public:
93 explicit ContentServiceDelegateHolder(BrowserContext* browser_context)
94 : delegate_(browser_context) {}
95 ~ContentServiceDelegateHolder() override = default;
96
97 ContentServiceDelegateImpl* delegate() { return &delegate_; }
98
99 private:
100 ContentServiceDelegateImpl delegate_;
101
102 DISALLOW_COPY_AND_ASSIGN(ContentServiceDelegateHolder);
103};
104
[email protected]e0ce8a1e2012-09-18 10:26:36105// Key names on BrowserContext.
msrameke169ccb2017-04-26 05:21:41106const char kBrowsingDataRemoverKey[] = "browsing-data-remover";
Ken Rockot84f58fd2018-06-22 21:54:14107const char kContentServiceDelegateKey[] = "content-service-delegate";
[email protected]6ef0c3912013-01-25 22:46:34108const char kDownloadManagerKeyName[] = "download_manager";
erge69130f52016-03-02 00:13:28109const char kMojoWasInitialized[] = "mojo-was-initialized";
bend32292b2016-10-07 00:21:58110const char kServiceManagerConnection[] = "service-manager-connection";
111const char kServiceUserId[] = "service-user-id";
rockot963ad3e82016-04-13 16:33:23112const char kStoragePartitionMapKeyName[] = "content_storage_partition_map";
Chris Cunninghama5b38012017-10-28 07:27:18113const char kVideoDecodePerfHistoryId[] = "video-decode-perf-history";
erge69130f52016-03-02 00:13:28114
[email protected]9afc14e22013-09-25 22:34:14115#if defined(OS_CHROMEOS)
116const char kMountPointsKey[] = "mount_points";
117#endif // defined(OS_CHROMEOS)
118
ben6c85c4492016-06-16 20:40:51119void RemoveBrowserContextFromUserIdMap(BrowserContext* browser_context) {
bend32292b2016-10-07 00:21:58120 ServiceUserIdHolder* holder = static_cast<ServiceUserIdHolder*>(
121 browser_context->GetUserData(kServiceUserId));
ben6c85c4492016-06-16 20:40:51122 if (holder) {
123 auto it = g_user_id_to_context.Get().find(holder->user_id());
124 if (it != g_user_id_to_context.Get().end())
125 g_user_id_to_context.Get().erase(it);
126 }
127}
128
[email protected]14acc642012-11-17 12:20:10129StoragePartitionImplMap* GetStoragePartitionMap(
130 BrowserContext* browser_context) {
[email protected]4c3a23582012-08-18 08:54:34131 StoragePartitionImplMap* partition_map =
132 static_cast<StoragePartitionImplMap*>(
a.cavalcantiffab73762015-08-15 02:55:48133 browser_context->GetUserData(kStoragePartitionMapKeyName));
[email protected]d7c7c98a2012-07-12 21:27:44134 if (!partition_map) {
avi1ed4a4372017-04-25 05:39:41135 auto partition_map_owned =
Jeremy Roman04f27c372017-10-27 15:20:55136 std::make_unique<StoragePartitionImplMap>(browser_context);
avi1ed4a4372017-04-25 05:39:41137 partition_map = partition_map_owned.get();
138 browser_context->SetUserData(kStoragePartitionMapKeyName,
139 std::move(partition_map_owned));
[email protected]d7c7c98a2012-07-12 21:27:44140 }
[email protected]14acc642012-11-17 12:20:10141 return partition_map;
142}
143
144StoragePartition* GetStoragePartitionFromConfig(
145 BrowserContext* browser_context,
146 const std::string& partition_domain,
147 const std::string& partition_name,
Dan Elphick6c0d8492017-09-12 09:35:38148 bool in_memory,
149 bool can_create) {
[email protected]14acc642012-11-17 12:20:10150 StoragePartitionImplMap* partition_map =
151 GetStoragePartitionMap(browser_context);
[email protected]d7c7c98a2012-07-12 21:27:44152
[email protected]1bc28312012-11-08 08:31:53153 if (browser_context->IsOffTheRecord())
154 in_memory = true;
155
Dan Elphick6c0d8492017-09-12 09:35:38156 return partition_map->Get(partition_domain, partition_name, in_memory,
157 can_create);
[email protected]d1198fd2012-08-13 22:50:19158}
159
[email protected]6939075a2012-08-28 08:35:53160void SaveSessionStateOnIOThread(
161 const scoped_refptr<net::URLRequestContextGetter>& context_getter,
[email protected]98d6d4562014-06-25 20:57:55162 AppCacheServiceImpl* appcache_service) {
[email protected]6939075a2012-08-28 08:35:53163 net::URLRequestContext* context = context_getter->GetURLRequestContext();
mmenkeded79da2016-02-06 08:28:51164 context->cookie_store()->SetForceKeepSessionState();
Reid Klecknerc1148882018-05-11 00:04:07165 context->channel_id_service()->GetChannelIDStore()->
166 SetForceKeepSessionState();
[email protected]6939075a2012-08-28 08:35:53167 appcache_service->set_force_keep_session_state();
[email protected]6e2d3d22012-02-24 18:10:36168}
169
[email protected]89acda82013-06-25 20:52:50170void SaveSessionStateOnIndexedDBThread(
[email protected]6e2d3d22012-02-24 18:10:36171 scoped_refptr<IndexedDBContextImpl> indexed_db_context) {
[email protected]bf510ed2012-06-05 08:31:43172 indexed_db_context->SetForceKeepSessionState();
[email protected]6e2d3d22012-02-24 18:10:36173}
174
falken41f4175162014-10-29 07:03:41175void ShutdownServiceWorkerContext(StoragePartition* partition) {
176 ServiceWorkerContextWrapper* wrapper =
177 static_cast<ServiceWorkerContextWrapper*>(
178 partition->GetServiceWorkerContext());
179 wrapper->process_manager()->Shutdown();
180}
181
avi1ed4a4372017-04-25 05:39:41182void SetDownloadManager(
183 BrowserContext* context,
184 std::unique_ptr<content::DownloadManager> download_manager) {
ttr31481dc54b2015-08-06 20:11:26185 DCHECK_CURRENTLY_ON(BrowserThread::UI);
186 DCHECK(download_manager);
avi1ed4a4372017-04-25 05:39:41187 context->SetUserData(kDownloadManagerKeyName, std::move(download_manager));
ttr31481dc54b2015-08-06 20:11:26188}
189
bend32292b2016-10-07 00:21:58190class BrowserContextServiceManagerConnectionHolder
rockot963ad3e82016-04-13 16:33:23191 : public base::SupportsUserData::Data {
192 public:
benaad37ecd2017-04-12 22:08:20193 explicit BrowserContextServiceManagerConnectionHolder(
rockot400ea35b2016-10-15 19:15:32194 service_manager::mojom::ServiceRequest request)
benaad37ecd2017-04-12 22:08:20195 : service_manager_connection_(ServiceManagerConnection::Create(
rockotcef38272016-07-15 22:47:47196 std::move(request),
197 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO))) {}
bend32292b2016-10-07 00:21:58198 ~BrowserContextServiceManagerConnectionHolder() override {}
rockot963ad3e82016-04-13 16:33:23199
bend32292b2016-10-07 00:21:58200 ServiceManagerConnection* service_manager_connection() {
201 return service_manager_connection_.get();
202 }
rockot963ad3e82016-04-13 16:33:23203
204 private:
bend32292b2016-10-07 00:21:58205 std::unique_ptr<ServiceManagerConnection> service_manager_connection_;
rockot963ad3e82016-04-13 16:33:23206
bend32292b2016-10-07 00:21:58207 DISALLOW_COPY_AND_ASSIGN(BrowserContextServiceManagerConnectionHolder);
rockot963ad3e82016-04-13 16:33:23208};
209
Jens Widell7ca8fb42018-02-21 14:11:54210base::WeakPtr<storage::BlobStorageContext> BlobStorageContextGetterForBrowser(
Xing Liu89860472018-02-09 20:07:02211 scoped_refptr<ChromeBlobStorageContext> blob_context) {
212 DCHECK_CURRENTLY_ON(BrowserThread::IO);
213 return blob_context->context()->AsWeakPtr();
214}
215
[email protected]735e20c2012-03-20 01:16:59216} // namespace
217
[email protected]14acc642012-11-17 12:20:10218// static
219void BrowserContext::AsyncObliterateStoragePartition(
220 BrowserContext* browser_context,
[email protected]399583b2012-12-11 09:33:42221 const GURL& site,
222 const base::Closure& on_gc_required) {
223 GetStoragePartitionMap(browser_context)->AsyncObliterate(site,
224 on_gc_required);
225}
226
227// static
228void BrowserContext::GarbageCollectStoragePartitions(
dcheng59716272016-04-09 05:19:08229 BrowserContext* browser_context,
230 std::unique_ptr<base::hash_set<base::FilePath>> active_paths,
231 const base::Closure& done) {
dcheng36b6aec92015-12-26 06:16:36232 GetStoragePartitionMap(browser_context)
233 ->GarbageCollect(std::move(active_paths), done);
[email protected]14acc642012-11-17 12:20:10234}
235
[email protected]b441a8492012-06-06 14:55:57236DownloadManager* BrowserContext::GetDownloadManager(
237 BrowserContext* context) {
mostynbfbcdc27a2015-03-13 17:58:52238 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]b441a8492012-06-06 14:55:57239 if (!context->GetUserData(kDownloadManagerKeyName)) {
Min Qinddb40ce2017-10-28 03:38:26240 DownloadManager* download_manager = new DownloadManagerImpl(context);
[email protected]d25fda12012-06-12 17:05:03241
avi1ed4a4372017-04-25 05:39:41242 SetDownloadManager(context, base::WrapUnique(download_manager));
[email protected]b441a8492012-06-06 14:55:57243 download_manager->SetDelegate(context->GetDownloadManagerDelegate());
[email protected]b441a8492012-06-06 14:55:57244 }
245
[email protected]eba4a4d2013-05-29 02:18:06246 return static_cast<DownloadManager*>(
247 context->GetUserData(kDownloadManagerKeyName));
[email protected]b441a8492012-06-06 14:55:57248}
249
[email protected]6ef0c3912013-01-25 22:46:34250// static
[email protected]cd501a72014-08-22 19:58:31251storage::ExternalMountPoints* BrowserContext::GetMountPoints(
[email protected]6ef0c3912013-01-25 22:46:34252 BrowserContext* context) {
253 // Ensure that these methods are called on the UI thread, except for
254 // unittests where a UI thread might not have been created.
255 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) ||
Gabriel Charette2983181c2018-03-28 17:01:09256 !BrowserThread::IsThreadInitialized(BrowserThread::UI));
[email protected]6ef0c3912013-01-25 22:46:34257
258#if defined(OS_CHROMEOS)
259 if (!context->GetUserData(kMountPointsKey)) {
[email protected]cd501a72014-08-22 19:58:31260 scoped_refptr<storage::ExternalMountPoints> mount_points =
261 storage::ExternalMountPoints::CreateRefCounted();
[email protected]6ef0c3912013-01-25 22:46:34262 context->SetUserData(
263 kMountPointsKey,
Jeremy Roman04f27c372017-10-27 15:20:55264 std::make_unique<UserDataAdapter<storage::ExternalMountPoints>>(
avicb129c02017-05-03 06:49:29265 mount_points.get()));
[email protected]6ef0c3912013-01-25 22:46:34266 }
267
[email protected]cd501a72014-08-22 19:58:31268 return UserDataAdapter<storage::ExternalMountPoints>::Get(context,
269 kMountPointsKey);
[email protected]6ef0c3912013-01-25 22:46:34270#else
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28271 return nullptr;
[email protected]6ef0c3912013-01-25 22:46:34272#endif
273}
274
msrameke169ccb2017-04-26 05:21:41275// static
276content::BrowsingDataRemover* content::BrowserContext::GetBrowsingDataRemover(
277 BrowserContext* context) {
278 DCHECK_CURRENTLY_ON(BrowserThread::UI);
279
280 if (!context->GetUserData(kBrowsingDataRemoverKey)) {
281 std::unique_ptr<BrowsingDataRemoverImpl> remover =
Jeremy Roman04f27c372017-10-27 15:20:55282 std::make_unique<BrowsingDataRemoverImpl>(context);
msrameke169ccb2017-04-26 05:21:41283 remover->SetEmbedderDelegate(context->GetBrowsingDataRemoverDelegate());
284 context->SetUserData(kBrowsingDataRemoverKey, std::move(remover));
285 }
286
287 return static_cast<BrowsingDataRemoverImpl*>(
288 context->GetUserData(kBrowsingDataRemoverKey));
289}
290
[email protected]4c3a23582012-08-18 08:54:34291StoragePartition* BrowserContext::GetStoragePartition(
292 BrowserContext* browser_context,
Dan Elphick6c0d8492017-09-12 09:35:38293 SiteInstance* site_instance,
294 bool can_create) {
[email protected]1bc28312012-11-08 08:31:53295 std::string partition_domain;
296 std::string partition_name;
297 bool in_memory = false;
[email protected]4c3a23582012-08-18 08:54:34298
[email protected]4c3a23582012-08-18 08:54:34299 if (site_instance) {
[email protected]1bc28312012-11-08 08:31:53300 GetContentClient()->browser()->GetStoragePartitionConfigForSite(
[email protected]14acc642012-11-17 12:20:10301 browser_context, site_instance->GetSiteURL(), true,
[email protected]1bc28312012-11-08 08:31:53302 &partition_domain, &partition_name, &in_memory);
[email protected]4c3a23582012-08-18 08:54:34303 }
304
Dan Elphick6c0d8492017-09-12 09:35:38305 return GetStoragePartitionFromConfig(browser_context, partition_domain,
306 partition_name, in_memory, can_create);
[email protected]4c3a23582012-08-18 08:54:34307}
308
[email protected]e94bbcb2012-09-07 05:33:57309StoragePartition* BrowserContext::GetStoragePartitionForSite(
310 BrowserContext* browser_context,
Dan Elphick6c0d8492017-09-12 09:35:38311 const GURL& site,
312 bool can_create) {
[email protected]1bc28312012-11-08 08:31:53313 std::string partition_domain;
314 std::string partition_name;
315 bool in_memory;
[email protected]e94bbcb2012-09-07 05:33:57316
[email protected]1bc28312012-11-08 08:31:53317 GetContentClient()->browser()->GetStoragePartitionConfigForSite(
[email protected]14acc642012-11-17 12:20:10318 browser_context, site, true, &partition_domain, &partition_name,
319 &in_memory);
[email protected]1bc28312012-11-08 08:31:53320
Dan Elphick6c0d8492017-09-12 09:35:38321 return GetStoragePartitionFromConfig(browser_context, partition_domain,
322 partition_name, in_memory, can_create);
[email protected]e94bbcb2012-09-07 05:33:57323}
324
[email protected]4c3a23582012-08-18 08:54:34325void BrowserContext::ForEachStoragePartition(
326 BrowserContext* browser_context,
327 const StoragePartitionCallback& callback) {
328 StoragePartitionImplMap* partition_map =
329 static_cast<StoragePartitionImplMap*>(
a.cavalcantiffab73762015-08-15 02:55:48330 browser_context->GetUserData(kStoragePartitionMapKeyName));
[email protected]4c3a23582012-08-18 08:54:34331 if (!partition_map)
332 return;
333
334 partition_map->ForEach(callback);
335}
336
337StoragePartition* BrowserContext::GetDefaultStoragePartition(
338 BrowserContext* browser_context) {
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28339 return GetStoragePartition(browser_context, nullptr);
[email protected]55eb70e762012-02-20 17:38:39340}
341
tbarzicdb712682015-03-06 06:05:41342// static
[email protected]393b6cb2014-05-15 00:55:12343void BrowserContext::CreateMemoryBackedBlob(BrowserContext* browser_context,
Marijn Kruisselbrink604fd7e72017-10-26 16:31:05344 const char* data,
345 size_t length,
346 const std::string& content_type,
347 BlobCallback callback) {
mostynbfbcdc27a2015-03-13 17:58:52348 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]393b6cb2014-05-15 00:55:12349
350 ChromeBlobStorageContext* blob_context =
351 ChromeBlobStorageContext::GetFor(browser_context);
352 BrowserThread::PostTaskAndReplyWithResult(
353 BrowserThread::IO, FROM_HERE,
Marijn Kruisselbrink604fd7e72017-10-26 16:31:05354 base::BindOnce(&ChromeBlobStorageContext::CreateMemoryBackedBlob,
355 base::WrapRefCounted(blob_context), data, length,
356 content_type),
357 std::move(callback));
[email protected]393b6cb2014-05-15 00:55:12358}
359
[email protected]66e53d0282014-08-07 10:04:35360// static
Xing Liu89860472018-02-09 20:07:02361BrowserContext::BlobContextGetter BrowserContext::GetBlobStorageContext(
362 BrowserContext* browser_context) {
363 DCHECK_CURRENTLY_ON(BrowserThread::UI);
364 scoped_refptr<ChromeBlobStorageContext> chrome_blob_context =
365 ChromeBlobStorageContext::GetFor(browser_context);
Jens Widell7ca8fb42018-02-21 14:11:54366 return base::BindRepeating(&BlobStorageContextGetterForBrowser,
367 chrome_blob_context);
Xing Liu89860472018-02-09 20:07:02368}
369
370// static
Marijn Kruisselbrink9e073a82018-06-18 17:48:58371blink::mojom::BlobPtr BrowserContext::GetBlobPtr(
372 BrowserContext* browser_context,
373 const std::string& uuid) {
374 DCHECK_CURRENTLY_ON(BrowserThread::UI);
375 return ChromeBlobStorageContext::GetBlobPtr(browser_context, uuid);
376}
377
378// static
[email protected]66e53d0282014-08-07 10:04:35379void BrowserContext::DeliverPushMessage(
380 BrowserContext* browser_context,
381 const GURL& origin,
avib7348942015-12-25 20:57:10382 int64_t service_worker_registration_id,
harknessdd4d2b22016-01-27 19:26:43383 const PushEventPayload& payload,
Peter Beverloo7815db1e02017-07-12 19:03:21384 const base::Callback<void(mojom::PushDeliveryStatus)>& callback) {
mostynbfbcdc27a2015-03-13 17:58:52385 DCHECK_CURRENTLY_ON(BrowserThread::UI);
harknessdd4d2b22016-01-27 19:26:43386 PushMessagingRouter::DeliverMessage(browser_context, origin,
387 service_worker_registration_id, payload,
388 callback);
[email protected]66e53d0282014-08-07 10:04:35389}
390
falken41f4175162014-10-29 07:03:41391// static
392void BrowserContext::NotifyWillBeDestroyed(BrowserContext* browser_context) {
Lukasz Anforowicz58d0dac2018-03-23 15:48:10393 // Make sure NotifyWillBeDestroyed is idempotent. This helps facilitate the
394 // pattern where NotifyWillBeDestroyed is called from *both*
395 // ShellBrowserContext and its derived classes (e.g.
396 // LayoutTestBrowserContext).
397 if (browser_context->was_notify_will_be_destroyed_called_)
398 return;
399 browser_context->was_notify_will_be_destroyed_called_ = true;
400
falken41f4175162014-10-29 07:03:41401 // Service Workers must shutdown before the browser context is destroyed,
402 // since they keep render process hosts alive and the codebase assumes that
403 // render process hosts die before their profile (browser context) dies.
404 ForEachStoragePartition(browser_context,
405 base::Bind(ShutdownServiceWorkerContext));
falken04a6912a2016-09-23 21:06:29406
407 // Shared workers also keep render process hosts alive, and are expected to
Darin Fisherd3768b2f62017-10-07 01:00:16408 // return ref counts to 0 after documents close. However, to ensure that
409 // hosts are destructed now, forcibly release their ref counts here.
falken04a6912a2016-09-23 21:06:29410 for (RenderProcessHost::iterator host_iterator =
411 RenderProcessHost::AllHostsIterator();
412 !host_iterator.IsAtEnd(); host_iterator.Advance()) {
413 RenderProcessHost* host = host_iterator.GetCurrentValue();
Benoit Lizeab08362c2017-07-19 14:50:54414 if (host->GetBrowserContext() == browser_context) {
415 // This will also clean up spare RPH references.
Yutaka Hirano09a9afb92017-08-16 09:11:45416 host->DisableKeepAliveRefCount();
Benoit Lizeab08362c2017-07-19 14:50:54417 }
falken04a6912a2016-09-23 21:06:29418 }
falken41f4175162014-10-29 07:03:41419}
420
[email protected]314c3e22012-02-21 03:57:42421void BrowserContext::EnsureResourceContextInitialized(BrowserContext* context) {
[email protected]7e26ac92012-02-27 20:15:05422 // This will be enough to tickle initialization of BrowserContext if
423 // necessary, which initializes ResourceContext. The reason we don't call
[email protected]4c3a23582012-08-18 08:54:34424 // ResourceContext::InitializeResourceContext() directly here is that
425 // ResourceContext initialization may call back into BrowserContext
426 // and when that call returns it'll end rewriting its UserData map. It will
427 // end up rewriting the same value but this still causes a race condition.
428 //
429 // See http://crbug.com/115678.
430 GetDefaultStoragePartition(context);
[email protected]55eb70e762012-02-20 17:38:39431}
432
[email protected]6e2d3d22012-02-24 18:10:36433void BrowserContext::SaveSessionState(BrowserContext* browser_context) {
[email protected]b1b502e2012-09-16 07:31:43434 StoragePartition* storage_partition =
435 BrowserContext::GetDefaultStoragePartition(browser_context);
[email protected]6e2d3d22012-02-24 18:10:36436
Joshua Bell607cb142017-07-24 19:17:16437 storage::DatabaseTracker* database_tracker =
438 storage_partition->GetDatabaseTracker();
439 database_tracker->task_runner()->PostTask(
440 FROM_HERE,
441 base::BindOnce(&storage::DatabaseTracker::SetForceKeepSessionState,
kylecharda69d882017-10-04 05:49:52442 base::WrapRefCounted(database_tracker)));
Joshua Bell607cb142017-07-24 19:17:16443
Gabriel Charette2983181c2018-03-28 17:01:09444 if (BrowserThread::IsThreadInitialized(BrowserThread::IO)) {
[email protected]6e2d3d22012-02-24 18:10:36445 BrowserThread::PostTask(
446 BrowserThread::IO, FROM_HERE,
tzik4fea24af2017-08-23 11:41:47447 base::BindOnce(
[email protected]6939075a2012-08-28 08:35:53448 &SaveSessionStateOnIOThread,
Clark DuVall385b5a52018-06-14 21:33:32449 base::WrapRefCounted(storage_partition->GetURLRequestContext()),
[email protected]98d6d4562014-06-25 20:57:55450 static_cast<AppCacheServiceImpl*>(
[email protected]63ef85512014-06-05 14:21:26451 storage_partition->GetAppCacheService())));
[email protected]6e2d3d22012-02-24 18:10:36452 }
453
Clark DuVall385b5a52018-06-14 21:33:32454 storage_partition->GetCookieManagerForBrowserProcess()
455 ->SetForceKeepSessionState();
456
[email protected]5f2aa722013-08-07 16:59:41457 DOMStorageContextWrapper* dom_storage_context_proxy =
458 static_cast<DOMStorageContextWrapper*>(
[email protected]b1b502e2012-09-16 07:31:43459 storage_partition->GetDOMStorageContext());
[email protected]5f2aa722013-08-07 16:59:41460 dom_storage_context_proxy->SetForceKeepSessionState();
[email protected]735e20c2012-03-20 01:16:59461
[email protected]89acda82013-06-25 20:52:50462 IndexedDBContextImpl* indexed_db_context_impl =
463 static_cast<IndexedDBContextImpl*>(
[email protected]b1b502e2012-09-16 07:31:43464 storage_partition->GetIndexedDBContext());
[email protected]89acda82013-06-25 20:52:50465 // No task runner in unit tests.
466 if (indexed_db_context_impl->TaskRunner()) {
467 indexed_db_context_impl->TaskRunner()->PostTask(
kylecharda69d882017-10-04 05:49:52468 FROM_HERE,
469 base::BindOnce(&SaveSessionStateOnIndexedDBThread,
470 base::WrapRefCounted(indexed_db_context_impl)));
[email protected]6e2d3d22012-02-24 18:10:36471 }
472}
473
ttr31481dc54b2015-08-06 20:11:26474void BrowserContext::SetDownloadManagerForTesting(
475 BrowserContext* browser_context,
avi1ed4a4372017-04-25 05:39:41476 std::unique_ptr<content::DownloadManager> download_manager) {
477 SetDownloadManager(browser_context, std::move(download_manager));
ttr31481dc54b2015-08-06 20:11:26478}
479
rockot963ad3e82016-04-13 16:33:23480// static
erge69130f52016-03-02 00:13:28481void BrowserContext::Initialize(
482 BrowserContext* browser_context,
483 const base::FilePath& path) {
ben6c85c4492016-06-16 20:40:51484 std::string new_id;
485 if (GetContentClient() && GetContentClient()->browser()) {
bend32292b2016-10-07 00:21:58486 new_id = GetContentClient()->browser()->GetServiceUserIdForBrowserContext(
ben6c85c4492016-06-16 20:40:51487 browser_context);
488 } else {
489 // Some test scenarios initialize a BrowserContext without a content client.
bencccfe2a2016-03-05 16:54:14490 new_id = base::GenerateGUID();
ben6c85c4492016-06-16 20:40:51491 }
erge69130f52016-03-02 00:13:28492
bend32292b2016-10-07 00:21:58493 ServiceUserIdHolder* holder = static_cast<ServiceUserIdHolder*>(
494 browser_context->GetUserData(kServiceUserId));
ben6c85c4492016-06-16 20:40:51495 if (holder)
bend32292b2016-10-07 00:21:58496 file::ForgetServiceUserIdUserDirAssociation(holder->user_id());
497 file::AssociateServiceUserIdWithUserDir(new_id, path);
ben6c85c4492016-06-16 20:40:51498 RemoveBrowserContextFromUserIdMap(browser_context);
499 g_user_id_to_context.Get()[new_id] = browser_context;
bend32292b2016-10-07 00:21:58500 browser_context->SetUserData(kServiceUserId,
Jeremy Roman04f27c372017-10-27 15:20:55501 std::make_unique<ServiceUserIdHolder>(new_id));
erge69130f52016-03-02 00:13:28502
avi1ed4a4372017-04-25 05:39:41503 browser_context->SetUserData(
Jeremy Roman04f27c372017-10-27 15:20:55504 kMojoWasInitialized, std::make_unique<base::SupportsUserData::Data>());
rockot963ad3e82016-04-13 16:33:23505
bend32292b2016-10-07 00:21:58506 ServiceManagerConnection* service_manager_connection =
507 ServiceManagerConnection::GetForProcess();
fdoray4f155f02016-10-12 11:28:50508 if (service_manager_connection && base::ThreadTaskRunnerHandle::IsSet()) {
rockot963ad3e82016-04-13 16:33:23509 // NOTE: Many unit tests create a TestBrowserContext without initializing
bend32292b2016-10-07 00:21:58510 // Mojo or the global service manager connection.
rockot963ad3e82016-04-13 16:33:23511
rockot400ea35b2016-10-15 19:15:32512 service_manager::mojom::ServicePtr service;
Ken Rockotf4d8a942017-05-13 00:10:37513 auto service_request = mojo::MakeRequest(&service);
rockot963ad3e82016-04-13 16:33:23514
rockot400ea35b2016-10-15 19:15:32515 service_manager::mojom::PIDReceiverPtr pid_receiver;
bene6a9f012017-01-07 00:43:43516 service_manager::Identity identity(mojom::kBrowserServiceName, new_id);
benbd3c2482017-01-07 05:48:21517 service_manager_connection->GetConnector()->StartService(
bene6a9f012017-01-07 00:43:43518 identity, std::move(service), mojo::MakeRequest(&pid_receiver));
rockot963ad3e82016-04-13 16:33:23519 pid_receiver->SetPID(base::GetCurrentProcId());
520
benaad37ecd2017-04-12 22:08:20521 service_manager_connection->GetConnector()->StartService(identity);
bend32292b2016-10-07 00:21:58522 BrowserContextServiceManagerConnectionHolder* connection_holder =
523 new BrowserContextServiceManagerConnectionHolder(
bend32292b2016-10-07 00:21:58524 std::move(service_request));
avi1ed4a4372017-04-25 05:39:41525 browser_context->SetUserData(kServiceManagerConnection,
526 base::WrapUnique(connection_holder));
rockot963ad3e82016-04-13 16:33:23527
bend32292b2016-10-07 00:21:58528 ServiceManagerConnection* connection =
529 connection_holder->service_manager_connection();
rockot963ad3e82016-04-13 16:33:23530
ben146248de2016-06-14 15:24:59531 // New embedded service factories should be added to |connection| here.
rockot963ad3e82016-04-13 16:33:23532
Ken Rockot84f58fd2018-06-22 21:54:14533 {
534 service_manager::EmbeddedServiceInfo info;
535 info.factory = base::BindRepeating(&file::CreateFileService);
536 connection->AddEmbeddedService(file::mojom::kServiceName, info);
537 }
538
539 browser_context->SetUserData(
540 kContentServiceDelegateKey,
541 std::make_unique<ContentServiceDelegateHolder>(browser_context));
542
543 {
544 service_manager::EmbeddedServiceInfo info;
545 info.task_runner = base::SequencedTaskRunnerHandle::Get();
546 info.factory = base::BindRepeating(
547 [](BrowserContext* context)
548 -> std::unique_ptr<service_manager::Service> {
549 auto* holder = static_cast<ContentServiceDelegateHolder*>(
550 context->GetUserData(kContentServiceDelegateKey));
551 auto* delegate = holder->delegate();
552 auto service = std::make_unique<content::Service>(delegate);
553 delegate->AddService(service.get());
554 return service;
555 },
556 browser_context);
557 connection->AddEmbeddedService(content::mojom::kServiceName, info);
558 }
tibellab3d36b2017-03-10 02:57:25559
560 ContentBrowserClient::StaticServiceMap services;
561 browser_context->RegisterInProcessServices(&services);
562 for (const auto& entry : services) {
563 connection->AddEmbeddedService(entry.first, entry.second);
564 }
Ken Rockotc7a279c2017-05-04 23:51:45565
566 RegisterCommonBrowserInterfaces(connection);
sammc030f5012017-03-13 02:28:45567 connection->Start();
rockot963ad3e82016-04-13 16:33:23568 }
Elad Alon03ad23b2018-02-07 16:54:31569
Elad Alon03ad23b2018-02-07 16:54:31570 if (!browser_context->IsOffTheRecord()) {
Elad Alon12ecdf02018-03-14 12:07:31571 WebRtcEventLogger* const logger = WebRtcEventLogger::Get();
572 if (logger) {
573 logger->EnableForBrowserContext(browser_context);
Elad Alon03ad23b2018-02-07 16:54:31574 }
575 }
erge69130f52016-03-02 00:13:28576}
577
rockot963ad3e82016-04-13 16:33:23578// static
bend32292b2016-10-07 00:21:58579const std::string& BrowserContext::GetServiceUserIdFor(
bencccfe2a2016-03-05 16:54:14580 BrowserContext* browser_context) {
erge69130f52016-03-02 00:13:28581 CHECK(browser_context->GetUserData(kMojoWasInitialized))
582 << "Attempting to get the mojo user id for a BrowserContext that was "
583 << "never Initialize()ed.";
584
bend32292b2016-10-07 00:21:58585 ServiceUserIdHolder* holder = static_cast<ServiceUserIdHolder*>(
586 browser_context->GetUserData(kServiceUserId));
ben6c85c4492016-06-16 20:40:51587 return holder->user_id();
erge69130f52016-03-02 00:13:28588}
589
rockot963ad3e82016-04-13 16:33:23590// static
bend32292b2016-10-07 00:21:58591BrowserContext* BrowserContext::GetBrowserContextForServiceUserId(
ben6c85c4492016-06-16 20:40:51592 const std::string& user_id) {
593 auto it = g_user_id_to_context.Get().find(user_id);
594 return it != g_user_id_to_context.Get().end() ? it->second : nullptr;
595}
596
597// static
rockot400ea35b2016-10-15 19:15:32598service_manager::Connector* BrowserContext::GetConnectorFor(
rockot963ad3e82016-04-13 16:33:23599 BrowserContext* browser_context) {
bend32292b2016-10-07 00:21:58600 ServiceManagerConnection* connection =
601 GetServiceManagerConnectionFor(browser_context);
ben5be0b9132016-08-03 00:17:18602 return connection ? connection->GetConnector() : nullptr;
603}
604
605// static
bend32292b2016-10-07 00:21:58606ServiceManagerConnection* BrowserContext::GetServiceManagerConnectionFor(
ben5be0b9132016-08-03 00:17:18607 BrowserContext* browser_context) {
bend32292b2016-10-07 00:21:58608 BrowserContextServiceManagerConnectionHolder* connection_holder =
609 static_cast<BrowserContextServiceManagerConnectionHolder*>(
610 browser_context->GetUserData(kServiceManagerConnection));
611 return connection_holder ? connection_holder->service_manager_connection()
612 : nullptr;
rockot963ad3e82016-04-13 16:33:23613}
614
mmenkec0b2b8b12017-04-21 16:27:52615BrowserContext::BrowserContext()
Andrey Lushnikovd39d9062018-04-24 17:33:35616 : unique_id_(base::UnguessableToken::Create().ToString()) {}
mmenkec0b2b8b12017-04-21 16:27:52617
[email protected]55eb70e762012-02-20 17:38:39618BrowserContext::~BrowserContext() {
erge69130f52016-03-02 00:13:28619 CHECK(GetUserData(kMojoWasInitialized))
620 << "Attempting to destroy a BrowserContext that never called "
621 << "Initialize()";
622
kinukof6ed359c2016-07-26 13:27:21623 DCHECK(!GetUserData(kStoragePartitionMapKeyName))
624 << "StoragePartitionMap is not shut down properly";
625
Lukasz Anforowicz58d0dac2018-03-23 15:48:10626 DCHECK(was_notify_will_be_destroyed_called_);
627
Elad Alon12ecdf02018-03-14 12:07:31628 WebRtcEventLogger* const logger = WebRtcEventLogger::Get();
629 if (logger) {
630 logger->DisableForBrowserContext(this);
Elad Alon03ad23b2018-02-07 16:54:31631 }
Elad Alon03ad23b2018-02-07 16:54:31632
ben6c85c4492016-06-16 20:40:51633 RemoveBrowserContextFromUserIdMap(this);
634
[email protected]b441a8492012-06-06 14:55:57635 if (GetUserData(kDownloadManagerKeyName))
636 GetDownloadManager(this)->Shutdown();
[email protected]55eb70e762012-02-20 17:38:39637}
638
kinukof6ed359c2016-07-26 13:27:21639void BrowserContext::ShutdownStoragePartitions() {
640 if (GetUserData(kStoragePartitionMapKeyName))
641 RemoveUserData(kStoragePartitionMapKeyName);
642}
643
mmenkec0b2b8b12017-04-21 16:27:52644std::string BrowserContext::GetMediaDeviceIDSalt() {
Andrey Lushnikovd39d9062018-04-24 17:33:35645 return unique_id_;
mmenkec0b2b8b12017-04-21 16:27:52646}
647
648// static
649std::string BrowserContext::CreateRandomMediaDeviceIDSalt() {
Andrey Lushnikovd39d9062018-04-24 17:33:35650 return base::UnguessableToken::Create().ToString();
651}
652
653const std::string& BrowserContext::UniqueId() const {
654 return unique_id_;
mmenkec0b2b8b12017-04-21 16:27:52655}
656
Chris Cunninghama5b38012017-10-28 07:27:18657media::VideoDecodePerfHistory* BrowserContext::GetVideoDecodePerfHistory() {
658 media::VideoDecodePerfHistory* decode_history =
659 static_cast<media::VideoDecodePerfHistory*>(
660 GetUserData(kVideoDecodePerfHistoryId));
661
662 // Lazily created. Note, this does not trigger loading the DB from disk. That
663 // occurs later upon first VideoDecodePerfHistory API request that requires DB
664 // access. DB operations will not block the UI thread.
665 if (!decode_history) {
666 auto db_factory = std::make_unique<media::VideoDecodeStatsDBImplFactory>(
667 GetPath().Append(FILE_PATH_LITERAL("VideoDecodeStats")));
668 decode_history = new media::VideoDecodePerfHistory(std::move(db_factory));
669 SetUserData(kVideoDecodePerfHistoryId, base::WrapUnique(decode_history));
670 }
671
672 return decode_history;
673}
674
[email protected]55eb70e762012-02-20 17:38:39675} // namespace content