blob: ff199b31c9727ab47a5ab8b31ae5f28c603ed212 [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"
ben21a34c252016-06-29 22:24:3726#include "base/threading/thread_task_runner_handle.h"
Andrey Lushnikovd39d9062018-04-24 17:33:3527#include "base/unguessable_token.h"
avib7348942015-12-25 20:57:1028#include "build/build_config.h"
dmurph7ac019a2016-05-13 00:13:1729#include "content/browser/blob_storage/chrome_blob_storage_context.h"
msrameke169ccb2017-04-26 05:21:4130#include "content/browser/browsing_data/browsing_data_remover_impl.h"
[email protected]b441a8492012-06-06 14:55:5731#include "content/browser/download/download_manager_impl.h"
[email protected]c4d281662013-03-31 00:35:0832#include "content/browser/indexed_db/indexed_db_context_impl.h"
[email protected]678c0362012-12-05 08:02:4433#include "content/browser/loader/resource_dispatcher_host_impl.h"
mvanouwerkerk17205ea2014-11-07 17:30:1534#include "content/browser/push_messaging/push_messaging_router.h"
Ken Rockotc7a279c2017-05-04 23:51:4535#include "content/browser/service_manager/common_browser_interfaces.h"
[email protected]4c3a23582012-08-18 08:54:3436#include "content/browser/storage_partition_impl_map.h"
[email protected]d7c7c98a2012-07-12 21:27:4437#include "content/common/child_process_host_impl.h"
[email protected]393b6cb2014-05-15 00:55:1238#include "content/public/browser/blob_handle.h"
[email protected]55eb70e762012-02-20 17:38:3939#include "content/public/browser/browser_thread.h"
[email protected]b441a8492012-06-06 14:55:5740#include "content/public/browser/content_browser_client.h"
falken04a6912a2016-09-23 21:06:2941#include "content/public/browser/render_process_host.h"
[email protected]536fd0b2013-03-14 17:41:5742#include "content/public/browser/site_instance.h"
Elad Alon12ecdf02018-03-14 12:07:3143#include "content/public/browser/webrtc_event_logger.h"
rockot963ad3e82016-04-13 16:33:2344#include "content/public/common/content_switches.h"
bend32292b2016-10-07 00:21:5845#include "content/public/common/service_manager_connection.h"
ben51bb6c62016-11-17 20:15:5746#include "content/public/common/service_names.mojom.h"
Chris Cunningham9e669472017-11-15 21:03:1147#include "media/capabilities/video_decode_stats_db_impl.h"
Chris Cunninghama5b38012017-10-28 07:27:1848#include "media/mojo/services/video_decode_perf_history.h"
[email protected]4d7c4ef2012-03-16 01:47:1249#include "net/cookies/cookie_store.h"
[email protected]6b8a3c742014-07-25 00:25:3550#include "net/ssl/channel_id_service.h"
51#include "net/ssl/channel_id_store.h"
[email protected]6e2d3d22012-02-24 18:10:3652#include "net/url_request/url_request_context.h"
[email protected]6939075a2012-08-28 08:35:5353#include "net/url_request/url_request_context_getter.h"
ben768c8dc2016-08-12 00:26:5054#include "services/file/file_service.h"
Ken Rockot21142de2018-02-10 01:45:3055#include "services/file/public/mojom/constants.mojom.h"
ben768c8dc2016-08-12 00:26:5056#include "services/file/user_id_map.h"
rockot734fb662016-10-15 16:41:3057#include "services/service_manager/public/cpp/connector.h"
Ken Rockot543f5e32018-02-04 02:13:5058#include "services/service_manager/public/mojom/service.mojom.h"
Xing Liu89860472018-02-09 20:07:0259#include "storage/browser/blob/blob_storage_context.h"
pilgrime92c5fcd2014-09-10 23:31:2360#include "storage/browser/database/database_tracker.h"
61#include "storage/browser/fileapi/external_mount_points.h"
[email protected]55eb70e762012-02-20 17:38:3962
Elad Alon03ad23b2018-02-07 16:54:3163
[email protected]314c3e22012-02-21 03:57:4264using base::UserDataAdapter;
[email protected]55eb70e762012-02-20 17:38:3965
[email protected]55eb70e762012-02-20 17:38:3966namespace content {
67
[email protected]735e20c2012-03-20 01:16:5968namespace {
69
scottmg5e65e3a2017-03-08 08:48:4670base::LazyInstance<std::map<std::string, BrowserContext*>>::DestructorAtExit
ben6c85c4492016-06-16 20:40:5171 g_user_id_to_context = LAZY_INSTANCE_INITIALIZER;
72
bend32292b2016-10-07 00:21:5873class ServiceUserIdHolder : public base::SupportsUserData::Data {
ben6c85c4492016-06-16 20:40:5174 public:
bend32292b2016-10-07 00:21:5875 explicit ServiceUserIdHolder(const std::string& user_id)
76 : user_id_(user_id) {}
77 ~ServiceUserIdHolder() override {}
ben6c85c4492016-06-16 20:40:5178
79 const std::string& user_id() const { return user_id_; }
80
81 private:
82 std::string user_id_;
83
bend32292b2016-10-07 00:21:5884 DISALLOW_COPY_AND_ASSIGN(ServiceUserIdHolder);
ben6c85c4492016-06-16 20:40:5185};
erge69130f52016-03-02 00:13:2886
[email protected]e0ce8a1e2012-09-18 10:26:3687// Key names on BrowserContext.
msrameke169ccb2017-04-26 05:21:4188const char kBrowsingDataRemoverKey[] = "browsing-data-remover";
[email protected]6ef0c3912013-01-25 22:46:3489const char kDownloadManagerKeyName[] = "download_manager";
erge69130f52016-03-02 00:13:2890const char kMojoWasInitialized[] = "mojo-was-initialized";
bend32292b2016-10-07 00:21:5891const char kServiceManagerConnection[] = "service-manager-connection";
92const char kServiceUserId[] = "service-user-id";
rockot963ad3e82016-04-13 16:33:2393const char kStoragePartitionMapKeyName[] = "content_storage_partition_map";
Chris Cunninghama5b38012017-10-28 07:27:1894const char kVideoDecodePerfHistoryId[] = "video-decode-perf-history";
erge69130f52016-03-02 00:13:2895
[email protected]9afc14e22013-09-25 22:34:1496#if defined(OS_CHROMEOS)
97const char kMountPointsKey[] = "mount_points";
98#endif // defined(OS_CHROMEOS)
99
ben6c85c4492016-06-16 20:40:51100void RemoveBrowserContextFromUserIdMap(BrowserContext* browser_context) {
bend32292b2016-10-07 00:21:58101 ServiceUserIdHolder* holder = static_cast<ServiceUserIdHolder*>(
102 browser_context->GetUserData(kServiceUserId));
ben6c85c4492016-06-16 20:40:51103 if (holder) {
104 auto it = g_user_id_to_context.Get().find(holder->user_id());
105 if (it != g_user_id_to_context.Get().end())
106 g_user_id_to_context.Get().erase(it);
107 }
108}
109
[email protected]14acc642012-11-17 12:20:10110StoragePartitionImplMap* GetStoragePartitionMap(
111 BrowserContext* browser_context) {
[email protected]4c3a23582012-08-18 08:54:34112 StoragePartitionImplMap* partition_map =
113 static_cast<StoragePartitionImplMap*>(
a.cavalcantiffab73762015-08-15 02:55:48114 browser_context->GetUserData(kStoragePartitionMapKeyName));
[email protected]d7c7c98a2012-07-12 21:27:44115 if (!partition_map) {
avi1ed4a4372017-04-25 05:39:41116 auto partition_map_owned =
Jeremy Roman04f27c372017-10-27 15:20:55117 std::make_unique<StoragePartitionImplMap>(browser_context);
avi1ed4a4372017-04-25 05:39:41118 partition_map = partition_map_owned.get();
119 browser_context->SetUserData(kStoragePartitionMapKeyName,
120 std::move(partition_map_owned));
[email protected]d7c7c98a2012-07-12 21:27:44121 }
[email protected]14acc642012-11-17 12:20:10122 return partition_map;
123}
124
125StoragePartition* GetStoragePartitionFromConfig(
126 BrowserContext* browser_context,
127 const std::string& partition_domain,
128 const std::string& partition_name,
Dan Elphick6c0d8492017-09-12 09:35:38129 bool in_memory,
130 bool can_create) {
[email protected]14acc642012-11-17 12:20:10131 StoragePartitionImplMap* partition_map =
132 GetStoragePartitionMap(browser_context);
[email protected]d7c7c98a2012-07-12 21:27:44133
[email protected]1bc28312012-11-08 08:31:53134 if (browser_context->IsOffTheRecord())
135 in_memory = true;
136
Dan Elphick6c0d8492017-09-12 09:35:38137 return partition_map->Get(partition_domain, partition_name, in_memory,
138 can_create);
[email protected]d1198fd2012-08-13 22:50:19139}
140
[email protected]6939075a2012-08-28 08:35:53141void SaveSessionStateOnIOThread(
142 const scoped_refptr<net::URLRequestContextGetter>& context_getter,
[email protected]98d6d4562014-06-25 20:57:55143 AppCacheServiceImpl* appcache_service) {
[email protected]6939075a2012-08-28 08:35:53144 net::URLRequestContext* context = context_getter->GetURLRequestContext();
mmenkeded79da2016-02-06 08:28:51145 context->cookie_store()->SetForceKeepSessionState();
Reid Klecknerc1148882018-05-11 00:04:07146 context->channel_id_service()->GetChannelIDStore()->
147 SetForceKeepSessionState();
[email protected]6939075a2012-08-28 08:35:53148 appcache_service->set_force_keep_session_state();
[email protected]6e2d3d22012-02-24 18:10:36149}
150
[email protected]89acda82013-06-25 20:52:50151void SaveSessionStateOnIndexedDBThread(
[email protected]6e2d3d22012-02-24 18:10:36152 scoped_refptr<IndexedDBContextImpl> indexed_db_context) {
[email protected]bf510ed2012-06-05 08:31:43153 indexed_db_context->SetForceKeepSessionState();
[email protected]6e2d3d22012-02-24 18:10:36154}
155
falken41f4175162014-10-29 07:03:41156void ShutdownServiceWorkerContext(StoragePartition* partition) {
157 ServiceWorkerContextWrapper* wrapper =
158 static_cast<ServiceWorkerContextWrapper*>(
159 partition->GetServiceWorkerContext());
160 wrapper->process_manager()->Shutdown();
161}
162
avi1ed4a4372017-04-25 05:39:41163void SetDownloadManager(
164 BrowserContext* context,
165 std::unique_ptr<content::DownloadManager> download_manager) {
ttr31481dc54b2015-08-06 20:11:26166 DCHECK_CURRENTLY_ON(BrowserThread::UI);
167 DCHECK(download_manager);
avi1ed4a4372017-04-25 05:39:41168 context->SetUserData(kDownloadManagerKeyName, std::move(download_manager));
ttr31481dc54b2015-08-06 20:11:26169}
170
bend32292b2016-10-07 00:21:58171class BrowserContextServiceManagerConnectionHolder
rockot963ad3e82016-04-13 16:33:23172 : public base::SupportsUserData::Data {
173 public:
benaad37ecd2017-04-12 22:08:20174 explicit BrowserContextServiceManagerConnectionHolder(
rockot400ea35b2016-10-15 19:15:32175 service_manager::mojom::ServiceRequest request)
benaad37ecd2017-04-12 22:08:20176 : service_manager_connection_(ServiceManagerConnection::Create(
rockotcef38272016-07-15 22:47:47177 std::move(request),
178 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO))) {}
bend32292b2016-10-07 00:21:58179 ~BrowserContextServiceManagerConnectionHolder() override {}
rockot963ad3e82016-04-13 16:33:23180
bend32292b2016-10-07 00:21:58181 ServiceManagerConnection* service_manager_connection() {
182 return service_manager_connection_.get();
183 }
rockot963ad3e82016-04-13 16:33:23184
185 private:
bend32292b2016-10-07 00:21:58186 std::unique_ptr<ServiceManagerConnection> service_manager_connection_;
rockot963ad3e82016-04-13 16:33:23187
bend32292b2016-10-07 00:21:58188 DISALLOW_COPY_AND_ASSIGN(BrowserContextServiceManagerConnectionHolder);
rockot963ad3e82016-04-13 16:33:23189};
190
Jens Widell7ca8fb42018-02-21 14:11:54191base::WeakPtr<storage::BlobStorageContext> BlobStorageContextGetterForBrowser(
Xing Liu89860472018-02-09 20:07:02192 scoped_refptr<ChromeBlobStorageContext> blob_context) {
193 DCHECK_CURRENTLY_ON(BrowserThread::IO);
194 return blob_context->context()->AsWeakPtr();
195}
196
[email protected]735e20c2012-03-20 01:16:59197} // namespace
198
[email protected]14acc642012-11-17 12:20:10199// static
200void BrowserContext::AsyncObliterateStoragePartition(
201 BrowserContext* browser_context,
[email protected]399583b2012-12-11 09:33:42202 const GURL& site,
203 const base::Closure& on_gc_required) {
204 GetStoragePartitionMap(browser_context)->AsyncObliterate(site,
205 on_gc_required);
206}
207
208// static
209void BrowserContext::GarbageCollectStoragePartitions(
dcheng59716272016-04-09 05:19:08210 BrowserContext* browser_context,
211 std::unique_ptr<base::hash_set<base::FilePath>> active_paths,
212 const base::Closure& done) {
dcheng36b6aec92015-12-26 06:16:36213 GetStoragePartitionMap(browser_context)
214 ->GarbageCollect(std::move(active_paths), done);
[email protected]14acc642012-11-17 12:20:10215}
216
[email protected]b441a8492012-06-06 14:55:57217DownloadManager* BrowserContext::GetDownloadManager(
218 BrowserContext* context) {
mostynbfbcdc27a2015-03-13 17:58:52219 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]b441a8492012-06-06 14:55:57220 if (!context->GetUserData(kDownloadManagerKeyName)) {
Min Qinddb40ce2017-10-28 03:38:26221 DownloadManager* download_manager = new DownloadManagerImpl(context);
[email protected]d25fda12012-06-12 17:05:03222
avi1ed4a4372017-04-25 05:39:41223 SetDownloadManager(context, base::WrapUnique(download_manager));
[email protected]b441a8492012-06-06 14:55:57224 download_manager->SetDelegate(context->GetDownloadManagerDelegate());
[email protected]b441a8492012-06-06 14:55:57225 }
226
[email protected]eba4a4d2013-05-29 02:18:06227 return static_cast<DownloadManager*>(
228 context->GetUserData(kDownloadManagerKeyName));
[email protected]b441a8492012-06-06 14:55:57229}
230
[email protected]6ef0c3912013-01-25 22:46:34231// static
[email protected]cd501a72014-08-22 19:58:31232storage::ExternalMountPoints* BrowserContext::GetMountPoints(
[email protected]6ef0c3912013-01-25 22:46:34233 BrowserContext* context) {
234 // Ensure that these methods are called on the UI thread, except for
235 // unittests where a UI thread might not have been created.
236 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) ||
Gabriel Charette2983181c2018-03-28 17:01:09237 !BrowserThread::IsThreadInitialized(BrowserThread::UI));
[email protected]6ef0c3912013-01-25 22:46:34238
239#if defined(OS_CHROMEOS)
240 if (!context->GetUserData(kMountPointsKey)) {
[email protected]cd501a72014-08-22 19:58:31241 scoped_refptr<storage::ExternalMountPoints> mount_points =
242 storage::ExternalMountPoints::CreateRefCounted();
[email protected]6ef0c3912013-01-25 22:46:34243 context->SetUserData(
244 kMountPointsKey,
Jeremy Roman04f27c372017-10-27 15:20:55245 std::make_unique<UserDataAdapter<storage::ExternalMountPoints>>(
avicb129c02017-05-03 06:49:29246 mount_points.get()));
[email protected]6ef0c3912013-01-25 22:46:34247 }
248
[email protected]cd501a72014-08-22 19:58:31249 return UserDataAdapter<storage::ExternalMountPoints>::Get(context,
250 kMountPointsKey);
[email protected]6ef0c3912013-01-25 22:46:34251#else
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28252 return nullptr;
[email protected]6ef0c3912013-01-25 22:46:34253#endif
254}
255
msrameke169ccb2017-04-26 05:21:41256// static
257content::BrowsingDataRemover* content::BrowserContext::GetBrowsingDataRemover(
258 BrowserContext* context) {
259 DCHECK_CURRENTLY_ON(BrowserThread::UI);
260
261 if (!context->GetUserData(kBrowsingDataRemoverKey)) {
262 std::unique_ptr<BrowsingDataRemoverImpl> remover =
Jeremy Roman04f27c372017-10-27 15:20:55263 std::make_unique<BrowsingDataRemoverImpl>(context);
msrameke169ccb2017-04-26 05:21:41264 remover->SetEmbedderDelegate(context->GetBrowsingDataRemoverDelegate());
265 context->SetUserData(kBrowsingDataRemoverKey, std::move(remover));
266 }
267
268 return static_cast<BrowsingDataRemoverImpl*>(
269 context->GetUserData(kBrowsingDataRemoverKey));
270}
271
[email protected]4c3a23582012-08-18 08:54:34272StoragePartition* BrowserContext::GetStoragePartition(
273 BrowserContext* browser_context,
Dan Elphick6c0d8492017-09-12 09:35:38274 SiteInstance* site_instance,
275 bool can_create) {
[email protected]1bc28312012-11-08 08:31:53276 std::string partition_domain;
277 std::string partition_name;
278 bool in_memory = false;
[email protected]4c3a23582012-08-18 08:54:34279
[email protected]4c3a23582012-08-18 08:54:34280 if (site_instance) {
[email protected]1bc28312012-11-08 08:31:53281 GetContentClient()->browser()->GetStoragePartitionConfigForSite(
[email protected]14acc642012-11-17 12:20:10282 browser_context, site_instance->GetSiteURL(), true,
[email protected]1bc28312012-11-08 08:31:53283 &partition_domain, &partition_name, &in_memory);
[email protected]4c3a23582012-08-18 08:54:34284 }
285
Dan Elphick6c0d8492017-09-12 09:35:38286 return GetStoragePartitionFromConfig(browser_context, partition_domain,
287 partition_name, in_memory, can_create);
[email protected]4c3a23582012-08-18 08:54:34288}
289
[email protected]e94bbcb2012-09-07 05:33:57290StoragePartition* BrowserContext::GetStoragePartitionForSite(
291 BrowserContext* browser_context,
Dan Elphick6c0d8492017-09-12 09:35:38292 const GURL& site,
293 bool can_create) {
[email protected]1bc28312012-11-08 08:31:53294 std::string partition_domain;
295 std::string partition_name;
296 bool in_memory;
[email protected]e94bbcb2012-09-07 05:33:57297
[email protected]1bc28312012-11-08 08:31:53298 GetContentClient()->browser()->GetStoragePartitionConfigForSite(
[email protected]14acc642012-11-17 12:20:10299 browser_context, site, true, &partition_domain, &partition_name,
300 &in_memory);
[email protected]1bc28312012-11-08 08:31:53301
Dan Elphick6c0d8492017-09-12 09:35:38302 return GetStoragePartitionFromConfig(browser_context, partition_domain,
303 partition_name, in_memory, can_create);
[email protected]e94bbcb2012-09-07 05:33:57304}
305
[email protected]4c3a23582012-08-18 08:54:34306void BrowserContext::ForEachStoragePartition(
307 BrowserContext* browser_context,
308 const StoragePartitionCallback& callback) {
309 StoragePartitionImplMap* partition_map =
310 static_cast<StoragePartitionImplMap*>(
a.cavalcantiffab73762015-08-15 02:55:48311 browser_context->GetUserData(kStoragePartitionMapKeyName));
[email protected]4c3a23582012-08-18 08:54:34312 if (!partition_map)
313 return;
314
315 partition_map->ForEach(callback);
316}
317
318StoragePartition* BrowserContext::GetDefaultStoragePartition(
319 BrowserContext* browser_context) {
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28320 return GetStoragePartition(browser_context, nullptr);
[email protected]55eb70e762012-02-20 17:38:39321}
322
tbarzicdb712682015-03-06 06:05:41323// static
[email protected]393b6cb2014-05-15 00:55:12324void BrowserContext::CreateMemoryBackedBlob(BrowserContext* browser_context,
Marijn Kruisselbrink604fd7e72017-10-26 16:31:05325 const char* data,
326 size_t length,
327 const std::string& content_type,
328 BlobCallback callback) {
mostynbfbcdc27a2015-03-13 17:58:52329 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]393b6cb2014-05-15 00:55:12330
331 ChromeBlobStorageContext* blob_context =
332 ChromeBlobStorageContext::GetFor(browser_context);
333 BrowserThread::PostTaskAndReplyWithResult(
334 BrowserThread::IO, FROM_HERE,
Marijn Kruisselbrink604fd7e72017-10-26 16:31:05335 base::BindOnce(&ChromeBlobStorageContext::CreateMemoryBackedBlob,
336 base::WrapRefCounted(blob_context), data, length,
337 content_type),
338 std::move(callback));
[email protected]393b6cb2014-05-15 00:55:12339}
340
[email protected]66e53d0282014-08-07 10:04:35341// static
Xing Liu89860472018-02-09 20:07:02342BrowserContext::BlobContextGetter BrowserContext::GetBlobStorageContext(
343 BrowserContext* browser_context) {
344 DCHECK_CURRENTLY_ON(BrowserThread::UI);
345 scoped_refptr<ChromeBlobStorageContext> chrome_blob_context =
346 ChromeBlobStorageContext::GetFor(browser_context);
Jens Widell7ca8fb42018-02-21 14:11:54347 return base::BindRepeating(&BlobStorageContextGetterForBrowser,
348 chrome_blob_context);
Xing Liu89860472018-02-09 20:07:02349}
350
351// static
Marijn Kruisselbrink9e073a82018-06-18 17:48:58352blink::mojom::BlobPtr BrowserContext::GetBlobPtr(
353 BrowserContext* browser_context,
354 const std::string& uuid) {
355 DCHECK_CURRENTLY_ON(BrowserThread::UI);
356 return ChromeBlobStorageContext::GetBlobPtr(browser_context, uuid);
357}
358
359// static
[email protected]66e53d0282014-08-07 10:04:35360void BrowserContext::DeliverPushMessage(
361 BrowserContext* browser_context,
362 const GURL& origin,
avib7348942015-12-25 20:57:10363 int64_t service_worker_registration_id,
harknessdd4d2b22016-01-27 19:26:43364 const PushEventPayload& payload,
Peter Beverloo7815db1e02017-07-12 19:03:21365 const base::Callback<void(mojom::PushDeliveryStatus)>& callback) {
mostynbfbcdc27a2015-03-13 17:58:52366 DCHECK_CURRENTLY_ON(BrowserThread::UI);
harknessdd4d2b22016-01-27 19:26:43367 PushMessagingRouter::DeliverMessage(browser_context, origin,
368 service_worker_registration_id, payload,
369 callback);
[email protected]66e53d0282014-08-07 10:04:35370}
371
falken41f4175162014-10-29 07:03:41372// static
373void BrowserContext::NotifyWillBeDestroyed(BrowserContext* browser_context) {
Lukasz Anforowicz58d0dac2018-03-23 15:48:10374 // Make sure NotifyWillBeDestroyed is idempotent. This helps facilitate the
375 // pattern where NotifyWillBeDestroyed is called from *both*
376 // ShellBrowserContext and its derived classes (e.g.
377 // LayoutTestBrowserContext).
378 if (browser_context->was_notify_will_be_destroyed_called_)
379 return;
380 browser_context->was_notify_will_be_destroyed_called_ = true;
381
falken41f4175162014-10-29 07:03:41382 // Service Workers must shutdown before the browser context is destroyed,
383 // since they keep render process hosts alive and the codebase assumes that
384 // render process hosts die before their profile (browser context) dies.
385 ForEachStoragePartition(browser_context,
386 base::Bind(ShutdownServiceWorkerContext));
falken04a6912a2016-09-23 21:06:29387
388 // Shared workers also keep render process hosts alive, and are expected to
Darin Fisherd3768b2f62017-10-07 01:00:16389 // return ref counts to 0 after documents close. However, to ensure that
390 // hosts are destructed now, forcibly release their ref counts here.
falken04a6912a2016-09-23 21:06:29391 for (RenderProcessHost::iterator host_iterator =
392 RenderProcessHost::AllHostsIterator();
393 !host_iterator.IsAtEnd(); host_iterator.Advance()) {
394 RenderProcessHost* host = host_iterator.GetCurrentValue();
Benoit Lizeab08362c2017-07-19 14:50:54395 if (host->GetBrowserContext() == browser_context) {
396 // This will also clean up spare RPH references.
Yutaka Hirano09a9afb92017-08-16 09:11:45397 host->DisableKeepAliveRefCount();
Benoit Lizeab08362c2017-07-19 14:50:54398 }
falken04a6912a2016-09-23 21:06:29399 }
falken41f4175162014-10-29 07:03:41400}
401
[email protected]314c3e22012-02-21 03:57:42402void BrowserContext::EnsureResourceContextInitialized(BrowserContext* context) {
[email protected]7e26ac92012-02-27 20:15:05403 // This will be enough to tickle initialization of BrowserContext if
404 // necessary, which initializes ResourceContext. The reason we don't call
[email protected]4c3a23582012-08-18 08:54:34405 // ResourceContext::InitializeResourceContext() directly here is that
406 // ResourceContext initialization may call back into BrowserContext
407 // and when that call returns it'll end rewriting its UserData map. It will
408 // end up rewriting the same value but this still causes a race condition.
409 //
410 // See http://crbug.com/115678.
411 GetDefaultStoragePartition(context);
[email protected]55eb70e762012-02-20 17:38:39412}
413
[email protected]6e2d3d22012-02-24 18:10:36414void BrowserContext::SaveSessionState(BrowserContext* browser_context) {
[email protected]b1b502e2012-09-16 07:31:43415 StoragePartition* storage_partition =
416 BrowserContext::GetDefaultStoragePartition(browser_context);
[email protected]6e2d3d22012-02-24 18:10:36417
Joshua Bell607cb142017-07-24 19:17:16418 storage::DatabaseTracker* database_tracker =
419 storage_partition->GetDatabaseTracker();
420 database_tracker->task_runner()->PostTask(
421 FROM_HERE,
422 base::BindOnce(&storage::DatabaseTracker::SetForceKeepSessionState,
kylecharda69d882017-10-04 05:49:52423 base::WrapRefCounted(database_tracker)));
Joshua Bell607cb142017-07-24 19:17:16424
Gabriel Charette2983181c2018-03-28 17:01:09425 if (BrowserThread::IsThreadInitialized(BrowserThread::IO)) {
[email protected]6e2d3d22012-02-24 18:10:36426 BrowserThread::PostTask(
427 BrowserThread::IO, FROM_HERE,
tzik4fea24af2017-08-23 11:41:47428 base::BindOnce(
[email protected]6939075a2012-08-28 08:35:53429 &SaveSessionStateOnIOThread,
Clark DuVall385b5a52018-06-14 21:33:32430 base::WrapRefCounted(storage_partition->GetURLRequestContext()),
[email protected]98d6d4562014-06-25 20:57:55431 static_cast<AppCacheServiceImpl*>(
[email protected]63ef85512014-06-05 14:21:26432 storage_partition->GetAppCacheService())));
[email protected]6e2d3d22012-02-24 18:10:36433 }
434
Clark DuVall385b5a52018-06-14 21:33:32435 storage_partition->GetCookieManagerForBrowserProcess()
436 ->SetForceKeepSessionState();
437
[email protected]5f2aa722013-08-07 16:59:41438 DOMStorageContextWrapper* dom_storage_context_proxy =
439 static_cast<DOMStorageContextWrapper*>(
[email protected]b1b502e2012-09-16 07:31:43440 storage_partition->GetDOMStorageContext());
[email protected]5f2aa722013-08-07 16:59:41441 dom_storage_context_proxy->SetForceKeepSessionState();
[email protected]735e20c2012-03-20 01:16:59442
[email protected]89acda82013-06-25 20:52:50443 IndexedDBContextImpl* indexed_db_context_impl =
444 static_cast<IndexedDBContextImpl*>(
[email protected]b1b502e2012-09-16 07:31:43445 storage_partition->GetIndexedDBContext());
[email protected]89acda82013-06-25 20:52:50446 // No task runner in unit tests.
447 if (indexed_db_context_impl->TaskRunner()) {
448 indexed_db_context_impl->TaskRunner()->PostTask(
kylecharda69d882017-10-04 05:49:52449 FROM_HERE,
450 base::BindOnce(&SaveSessionStateOnIndexedDBThread,
451 base::WrapRefCounted(indexed_db_context_impl)));
[email protected]6e2d3d22012-02-24 18:10:36452 }
453}
454
ttr31481dc54b2015-08-06 20:11:26455void BrowserContext::SetDownloadManagerForTesting(
456 BrowserContext* browser_context,
avi1ed4a4372017-04-25 05:39:41457 std::unique_ptr<content::DownloadManager> download_manager) {
458 SetDownloadManager(browser_context, std::move(download_manager));
ttr31481dc54b2015-08-06 20:11:26459}
460
rockot963ad3e82016-04-13 16:33:23461// static
erge69130f52016-03-02 00:13:28462void BrowserContext::Initialize(
463 BrowserContext* browser_context,
464 const base::FilePath& path) {
ben6c85c4492016-06-16 20:40:51465 std::string new_id;
466 if (GetContentClient() && GetContentClient()->browser()) {
bend32292b2016-10-07 00:21:58467 new_id = GetContentClient()->browser()->GetServiceUserIdForBrowserContext(
ben6c85c4492016-06-16 20:40:51468 browser_context);
469 } else {
470 // Some test scenarios initialize a BrowserContext without a content client.
bencccfe2a2016-03-05 16:54:14471 new_id = base::GenerateGUID();
ben6c85c4492016-06-16 20:40:51472 }
erge69130f52016-03-02 00:13:28473
bend32292b2016-10-07 00:21:58474 ServiceUserIdHolder* holder = static_cast<ServiceUserIdHolder*>(
475 browser_context->GetUserData(kServiceUserId));
ben6c85c4492016-06-16 20:40:51476 if (holder)
bend32292b2016-10-07 00:21:58477 file::ForgetServiceUserIdUserDirAssociation(holder->user_id());
478 file::AssociateServiceUserIdWithUserDir(new_id, path);
ben6c85c4492016-06-16 20:40:51479 RemoveBrowserContextFromUserIdMap(browser_context);
480 g_user_id_to_context.Get()[new_id] = browser_context;
bend32292b2016-10-07 00:21:58481 browser_context->SetUserData(kServiceUserId,
Jeremy Roman04f27c372017-10-27 15:20:55482 std::make_unique<ServiceUserIdHolder>(new_id));
erge69130f52016-03-02 00:13:28483
avi1ed4a4372017-04-25 05:39:41484 browser_context->SetUserData(
Jeremy Roman04f27c372017-10-27 15:20:55485 kMojoWasInitialized, std::make_unique<base::SupportsUserData::Data>());
rockot963ad3e82016-04-13 16:33:23486
bend32292b2016-10-07 00:21:58487 ServiceManagerConnection* service_manager_connection =
488 ServiceManagerConnection::GetForProcess();
fdoray4f155f02016-10-12 11:28:50489 if (service_manager_connection && base::ThreadTaskRunnerHandle::IsSet()) {
rockot963ad3e82016-04-13 16:33:23490 // NOTE: Many unit tests create a TestBrowserContext without initializing
bend32292b2016-10-07 00:21:58491 // Mojo or the global service manager connection.
rockot963ad3e82016-04-13 16:33:23492
rockot400ea35b2016-10-15 19:15:32493 service_manager::mojom::ServicePtr service;
Ken Rockotf4d8a942017-05-13 00:10:37494 auto service_request = mojo::MakeRequest(&service);
rockot963ad3e82016-04-13 16:33:23495
rockot400ea35b2016-10-15 19:15:32496 service_manager::mojom::PIDReceiverPtr pid_receiver;
bene6a9f012017-01-07 00:43:43497 service_manager::Identity identity(mojom::kBrowserServiceName, new_id);
benbd3c2482017-01-07 05:48:21498 service_manager_connection->GetConnector()->StartService(
bene6a9f012017-01-07 00:43:43499 identity, std::move(service), mojo::MakeRequest(&pid_receiver));
rockot963ad3e82016-04-13 16:33:23500 pid_receiver->SetPID(base::GetCurrentProcId());
501
benaad37ecd2017-04-12 22:08:20502 service_manager_connection->GetConnector()->StartService(identity);
bend32292b2016-10-07 00:21:58503 BrowserContextServiceManagerConnectionHolder* connection_holder =
504 new BrowserContextServiceManagerConnectionHolder(
bend32292b2016-10-07 00:21:58505 std::move(service_request));
avi1ed4a4372017-04-25 05:39:41506 browser_context->SetUserData(kServiceManagerConnection,
507 base::WrapUnique(connection_holder));
rockot963ad3e82016-04-13 16:33:23508
bend32292b2016-10-07 00:21:58509 ServiceManagerConnection* connection =
510 connection_holder->service_manager_connection();
rockot963ad3e82016-04-13 16:33:23511
ben146248de2016-06-14 15:24:59512 // New embedded service factories should be added to |connection| here.
rockot963ad3e82016-04-13 16:33:23513
Marijn Kruisselbrink3e68abf2018-02-12 18:52:19514 service_manager::EmbeddedServiceInfo info;
515 info.factory = base::BindRepeating(&file::CreateFileService);
516 connection->AddEmbeddedService(file::mojom::kServiceName, info);
tibellab3d36b2017-03-10 02:57:25517
518 ContentBrowserClient::StaticServiceMap services;
519 browser_context->RegisterInProcessServices(&services);
520 for (const auto& entry : services) {
521 connection->AddEmbeddedService(entry.first, entry.second);
522 }
Ken Rockotc7a279c2017-05-04 23:51:45523
524 RegisterCommonBrowserInterfaces(connection);
sammc030f5012017-03-13 02:28:45525 connection->Start();
rockot963ad3e82016-04-13 16:33:23526 }
Elad Alon03ad23b2018-02-07 16:54:31527
Elad Alon03ad23b2018-02-07 16:54:31528 if (!browser_context->IsOffTheRecord()) {
Elad Alon12ecdf02018-03-14 12:07:31529 WebRtcEventLogger* const logger = WebRtcEventLogger::Get();
530 if (logger) {
531 logger->EnableForBrowserContext(browser_context);
Elad Alon03ad23b2018-02-07 16:54:31532 }
533 }
erge69130f52016-03-02 00:13:28534}
535
rockot963ad3e82016-04-13 16:33:23536// static
bend32292b2016-10-07 00:21:58537const std::string& BrowserContext::GetServiceUserIdFor(
bencccfe2a2016-03-05 16:54:14538 BrowserContext* browser_context) {
erge69130f52016-03-02 00:13:28539 CHECK(browser_context->GetUserData(kMojoWasInitialized))
540 << "Attempting to get the mojo user id for a BrowserContext that was "
541 << "never Initialize()ed.";
542
bend32292b2016-10-07 00:21:58543 ServiceUserIdHolder* holder = static_cast<ServiceUserIdHolder*>(
544 browser_context->GetUserData(kServiceUserId));
ben6c85c4492016-06-16 20:40:51545 return holder->user_id();
erge69130f52016-03-02 00:13:28546}
547
rockot963ad3e82016-04-13 16:33:23548// static
bend32292b2016-10-07 00:21:58549BrowserContext* BrowserContext::GetBrowserContextForServiceUserId(
ben6c85c4492016-06-16 20:40:51550 const std::string& user_id) {
551 auto it = g_user_id_to_context.Get().find(user_id);
552 return it != g_user_id_to_context.Get().end() ? it->second : nullptr;
553}
554
555// static
rockot400ea35b2016-10-15 19:15:32556service_manager::Connector* BrowserContext::GetConnectorFor(
rockot963ad3e82016-04-13 16:33:23557 BrowserContext* browser_context) {
bend32292b2016-10-07 00:21:58558 ServiceManagerConnection* connection =
559 GetServiceManagerConnectionFor(browser_context);
ben5be0b9132016-08-03 00:17:18560 return connection ? connection->GetConnector() : nullptr;
561}
562
563// static
bend32292b2016-10-07 00:21:58564ServiceManagerConnection* BrowserContext::GetServiceManagerConnectionFor(
ben5be0b9132016-08-03 00:17:18565 BrowserContext* browser_context) {
bend32292b2016-10-07 00:21:58566 BrowserContextServiceManagerConnectionHolder* connection_holder =
567 static_cast<BrowserContextServiceManagerConnectionHolder*>(
568 browser_context->GetUserData(kServiceManagerConnection));
569 return connection_holder ? connection_holder->service_manager_connection()
570 : nullptr;
rockot963ad3e82016-04-13 16:33:23571}
572
mmenkec0b2b8b12017-04-21 16:27:52573BrowserContext::BrowserContext()
Andrey Lushnikovd39d9062018-04-24 17:33:35574 : unique_id_(base::UnguessableToken::Create().ToString()) {}
mmenkec0b2b8b12017-04-21 16:27:52575
[email protected]55eb70e762012-02-20 17:38:39576BrowserContext::~BrowserContext() {
erge69130f52016-03-02 00:13:28577 CHECK(GetUserData(kMojoWasInitialized))
578 << "Attempting to destroy a BrowserContext that never called "
579 << "Initialize()";
580
kinukof6ed359c2016-07-26 13:27:21581 DCHECK(!GetUserData(kStoragePartitionMapKeyName))
582 << "StoragePartitionMap is not shut down properly";
583
Lukasz Anforowicz58d0dac2018-03-23 15:48:10584 DCHECK(was_notify_will_be_destroyed_called_);
585
Elad Alon12ecdf02018-03-14 12:07:31586 WebRtcEventLogger* const logger = WebRtcEventLogger::Get();
587 if (logger) {
588 logger->DisableForBrowserContext(this);
Elad Alon03ad23b2018-02-07 16:54:31589 }
Elad Alon03ad23b2018-02-07 16:54:31590
ben6c85c4492016-06-16 20:40:51591 RemoveBrowserContextFromUserIdMap(this);
592
[email protected]b441a8492012-06-06 14:55:57593 if (GetUserData(kDownloadManagerKeyName))
594 GetDownloadManager(this)->Shutdown();
[email protected]55eb70e762012-02-20 17:38:39595}
596
kinukof6ed359c2016-07-26 13:27:21597void BrowserContext::ShutdownStoragePartitions() {
598 if (GetUserData(kStoragePartitionMapKeyName))
599 RemoveUserData(kStoragePartitionMapKeyName);
600}
601
mmenkec0b2b8b12017-04-21 16:27:52602std::string BrowserContext::GetMediaDeviceIDSalt() {
Andrey Lushnikovd39d9062018-04-24 17:33:35603 return unique_id_;
mmenkec0b2b8b12017-04-21 16:27:52604}
605
606// static
607std::string BrowserContext::CreateRandomMediaDeviceIDSalt() {
Andrey Lushnikovd39d9062018-04-24 17:33:35608 return base::UnguessableToken::Create().ToString();
609}
610
611const std::string& BrowserContext::UniqueId() const {
612 return unique_id_;
mmenkec0b2b8b12017-04-21 16:27:52613}
614
Chris Cunninghama5b38012017-10-28 07:27:18615media::VideoDecodePerfHistory* BrowserContext::GetVideoDecodePerfHistory() {
616 media::VideoDecodePerfHistory* decode_history =
617 static_cast<media::VideoDecodePerfHistory*>(
618 GetUserData(kVideoDecodePerfHistoryId));
619
620 // Lazily created. Note, this does not trigger loading the DB from disk. That
621 // occurs later upon first VideoDecodePerfHistory API request that requires DB
622 // access. DB operations will not block the UI thread.
623 if (!decode_history) {
624 auto db_factory = std::make_unique<media::VideoDecodeStatsDBImplFactory>(
625 GetPath().Append(FILE_PATH_LITERAL("VideoDecodeStats")));
626 decode_history = new media::VideoDecodePerfHistory(std::move(db_factory));
627 SetUserData(kVideoDecodePerfHistoryId, base::WrapUnique(decode_history));
628 }
629
630 return decode_history;
631}
632
[email protected]55eb70e762012-02-20 17:38:39633} // namespace content