blob: 91f87bb1801dfcf93fe7a088c7b3202ec30fa9ca [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"
avib7348942015-12-25 20:57:1027#include "build/build_config.h"
dmurph7ac019a2016-05-13 00:13:1728#include "content/browser/blob_storage/chrome_blob_storage_context.h"
msrameke169ccb2017-04-26 05:21:4129#include "content/browser/browsing_data/browsing_data_remover_impl.h"
[email protected]b441a8492012-06-06 14:55:5730#include "content/browser/download/download_manager_impl.h"
[email protected]c4d281662013-03-31 00:35:0831#include "content/browser/indexed_db/indexed_db_context_impl.h"
[email protected]678c0362012-12-05 08:02:4432#include "content/browser/loader/resource_dispatcher_host_impl.h"
mvanouwerkerk17205ea2014-11-07 17:30:1533#include "content/browser/push_messaging/push_messaging_router.h"
Ken Rockotc7a279c2017-05-04 23:51:4534#include "content/browser/service_manager/common_browser_interfaces.h"
[email protected]4c3a23582012-08-18 08:54:3435#include "content/browser/storage_partition_impl_map.h"
[email protected]d7c7c98a2012-07-12 21:27:4436#include "content/common/child_process_host_impl.h"
[email protected]393b6cb2014-05-15 00:55:1237#include "content/public/browser/blob_handle.h"
[email protected]55eb70e762012-02-20 17:38:3938#include "content/public/browser/browser_thread.h"
[email protected]b441a8492012-06-06 14:55:5739#include "content/public/browser/content_browser_client.h"
falken04a6912a2016-09-23 21:06:2940#include "content/public/browser/render_process_host.h"
[email protected]536fd0b2013-03-14 17:41:5741#include "content/public/browser/site_instance.h"
Elad Alon12ecdf02018-03-14 12:07:3142#include "content/public/browser/webrtc_event_logger.h"
rockot963ad3e82016-04-13 16:33:2343#include "content/public/common/content_switches.h"
bend32292b2016-10-07 00:21:5844#include "content/public/common/service_manager_connection.h"
ben51bb6c62016-11-17 20:15:5745#include "content/public/common/service_names.mojom.h"
Chris Cunningham9e669472017-11-15 21:03:1146#include "media/capabilities/video_decode_stats_db_impl.h"
Chris Cunninghama5b38012017-10-28 07:27:1847#include "media/mojo/services/video_decode_perf_history.h"
[email protected]4d7c4ef2012-03-16 01:47:1248#include "net/cookies/cookie_store.h"
[email protected]6b8a3c742014-07-25 00:25:3549#include "net/ssl/channel_id_service.h"
50#include "net/ssl/channel_id_store.h"
[email protected]6e2d3d22012-02-24 18:10:3651#include "net/url_request/url_request_context.h"
[email protected]6939075a2012-08-28 08:35:5352#include "net/url_request/url_request_context_getter.h"
ben768c8dc2016-08-12 00:26:5053#include "services/file/file_service.h"
Ken Rockot21142de2018-02-10 01:45:3054#include "services/file/public/mojom/constants.mojom.h"
ben768c8dc2016-08-12 00:26:5055#include "services/file/user_id_map.h"
rockot734fb662016-10-15 16:41:3056#include "services/service_manager/public/cpp/connector.h"
Ken Rockot543f5e32018-02-04 02:13:5057#include "services/service_manager/public/mojom/service.mojom.h"
Xing Liu89860472018-02-09 20:07:0258#include "storage/browser/blob/blob_storage_context.h"
pilgrime92c5fcd2014-09-10 23:31:2359#include "storage/browser/database/database_tracker.h"
60#include "storage/browser/fileapi/external_mount_points.h"
[email protected]55eb70e762012-02-20 17:38:3961
Elad Alon03ad23b2018-02-07 16:54:3162
[email protected]314c3e22012-02-21 03:57:4263using base::UserDataAdapter;
[email protected]55eb70e762012-02-20 17:38:3964
[email protected]55eb70e762012-02-20 17:38:3965namespace content {
66
[email protected]735e20c2012-03-20 01:16:5967namespace {
68
scottmg5e65e3a2017-03-08 08:48:4669base::LazyInstance<std::map<std::string, BrowserContext*>>::DestructorAtExit
ben6c85c4492016-06-16 20:40:5170 g_user_id_to_context = LAZY_INSTANCE_INITIALIZER;
71
bend32292b2016-10-07 00:21:5872class ServiceUserIdHolder : public base::SupportsUserData::Data {
ben6c85c4492016-06-16 20:40:5173 public:
bend32292b2016-10-07 00:21:5874 explicit ServiceUserIdHolder(const std::string& user_id)
75 : user_id_(user_id) {}
76 ~ServiceUserIdHolder() override {}
ben6c85c4492016-06-16 20:40:5177
78 const std::string& user_id() const { return user_id_; }
79
80 private:
81 std::string user_id_;
82
bend32292b2016-10-07 00:21:5883 DISALLOW_COPY_AND_ASSIGN(ServiceUserIdHolder);
ben6c85c4492016-06-16 20:40:5184};
erge69130f52016-03-02 00:13:2885
[email protected]e0ce8a1e2012-09-18 10:26:3686// Key names on BrowserContext.
msrameke169ccb2017-04-26 05:21:4187const char kBrowsingDataRemoverKey[] = "browsing-data-remover";
[email protected]6ef0c3912013-01-25 22:46:3488const char kDownloadManagerKeyName[] = "download_manager";
erge69130f52016-03-02 00:13:2889const char kMojoWasInitialized[] = "mojo-was-initialized";
bend32292b2016-10-07 00:21:5890const char kServiceManagerConnection[] = "service-manager-connection";
91const char kServiceUserId[] = "service-user-id";
rockot963ad3e82016-04-13 16:33:2392const char kStoragePartitionMapKeyName[] = "content_storage_partition_map";
Chris Cunninghama5b38012017-10-28 07:27:1893const char kVideoDecodePerfHistoryId[] = "video-decode-perf-history";
erge69130f52016-03-02 00:13:2894
[email protected]9afc14e22013-09-25 22:34:1495#if defined(OS_CHROMEOS)
96const char kMountPointsKey[] = "mount_points";
97#endif // defined(OS_CHROMEOS)
98
ben6c85c4492016-06-16 20:40:5199void RemoveBrowserContextFromUserIdMap(BrowserContext* browser_context) {
bend32292b2016-10-07 00:21:58100 ServiceUserIdHolder* holder = static_cast<ServiceUserIdHolder*>(
101 browser_context->GetUserData(kServiceUserId));
ben6c85c4492016-06-16 20:40:51102 if (holder) {
103 auto it = g_user_id_to_context.Get().find(holder->user_id());
104 if (it != g_user_id_to_context.Get().end())
105 g_user_id_to_context.Get().erase(it);
106 }
107}
108
[email protected]14acc642012-11-17 12:20:10109StoragePartitionImplMap* GetStoragePartitionMap(
110 BrowserContext* browser_context) {
[email protected]4c3a23582012-08-18 08:54:34111 StoragePartitionImplMap* partition_map =
112 static_cast<StoragePartitionImplMap*>(
a.cavalcantiffab73762015-08-15 02:55:48113 browser_context->GetUserData(kStoragePartitionMapKeyName));
[email protected]d7c7c98a2012-07-12 21:27:44114 if (!partition_map) {
avi1ed4a4372017-04-25 05:39:41115 auto partition_map_owned =
Jeremy Roman04f27c372017-10-27 15:20:55116 std::make_unique<StoragePartitionImplMap>(browser_context);
avi1ed4a4372017-04-25 05:39:41117 partition_map = partition_map_owned.get();
118 browser_context->SetUserData(kStoragePartitionMapKeyName,
119 std::move(partition_map_owned));
[email protected]d7c7c98a2012-07-12 21:27:44120 }
[email protected]14acc642012-11-17 12:20:10121 return partition_map;
122}
123
124StoragePartition* GetStoragePartitionFromConfig(
125 BrowserContext* browser_context,
126 const std::string& partition_domain,
127 const std::string& partition_name,
Dan Elphick6c0d8492017-09-12 09:35:38128 bool in_memory,
129 bool can_create) {
[email protected]14acc642012-11-17 12:20:10130 StoragePartitionImplMap* partition_map =
131 GetStoragePartitionMap(browser_context);
[email protected]d7c7c98a2012-07-12 21:27:44132
[email protected]1bc28312012-11-08 08:31:53133 if (browser_context->IsOffTheRecord())
134 in_memory = true;
135
Dan Elphick6c0d8492017-09-12 09:35:38136 return partition_map->Get(partition_domain, partition_name, in_memory,
137 can_create);
[email protected]d1198fd2012-08-13 22:50:19138}
139
[email protected]6939075a2012-08-28 08:35:53140void SaveSessionStateOnIOThread(
141 const scoped_refptr<net::URLRequestContextGetter>& context_getter,
[email protected]98d6d4562014-06-25 20:57:55142 AppCacheServiceImpl* appcache_service) {
[email protected]6939075a2012-08-28 08:35:53143 net::URLRequestContext* context = context_getter->GetURLRequestContext();
mmenkeded79da2016-02-06 08:28:51144 context->cookie_store()->SetForceKeepSessionState();
[email protected]6b8a3c742014-07-25 00:25:35145 context->channel_id_service()->GetChannelIDStore()->
[email protected]6939075a2012-08-28 08:35:53146 SetForceKeepSessionState();
147 appcache_service->set_force_keep_session_state();
[email protected]6e2d3d22012-02-24 18:10:36148}
149
[email protected]89acda82013-06-25 20:52:50150void SaveSessionStateOnIndexedDBThread(
[email protected]6e2d3d22012-02-24 18:10:36151 scoped_refptr<IndexedDBContextImpl> indexed_db_context) {
[email protected]bf510ed2012-06-05 08:31:43152 indexed_db_context->SetForceKeepSessionState();
[email protected]6e2d3d22012-02-24 18:10:36153}
154
falken41f4175162014-10-29 07:03:41155void ShutdownServiceWorkerContext(StoragePartition* partition) {
156 ServiceWorkerContextWrapper* wrapper =
157 static_cast<ServiceWorkerContextWrapper*>(
158 partition->GetServiceWorkerContext());
159 wrapper->process_manager()->Shutdown();
160}
161
avi1ed4a4372017-04-25 05:39:41162void SetDownloadManager(
163 BrowserContext* context,
164 std::unique_ptr<content::DownloadManager> download_manager) {
ttr31481dc54b2015-08-06 20:11:26165 DCHECK_CURRENTLY_ON(BrowserThread::UI);
166 DCHECK(download_manager);
avi1ed4a4372017-04-25 05:39:41167 context->SetUserData(kDownloadManagerKeyName, std::move(download_manager));
ttr31481dc54b2015-08-06 20:11:26168}
169
bend32292b2016-10-07 00:21:58170class BrowserContextServiceManagerConnectionHolder
rockot963ad3e82016-04-13 16:33:23171 : public base::SupportsUserData::Data {
172 public:
benaad37ecd2017-04-12 22:08:20173 explicit BrowserContextServiceManagerConnectionHolder(
rockot400ea35b2016-10-15 19:15:32174 service_manager::mojom::ServiceRequest request)
benaad37ecd2017-04-12 22:08:20175 : service_manager_connection_(ServiceManagerConnection::Create(
rockotcef38272016-07-15 22:47:47176 std::move(request),
177 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO))) {}
bend32292b2016-10-07 00:21:58178 ~BrowserContextServiceManagerConnectionHolder() override {}
rockot963ad3e82016-04-13 16:33:23179
bend32292b2016-10-07 00:21:58180 ServiceManagerConnection* service_manager_connection() {
181 return service_manager_connection_.get();
182 }
rockot963ad3e82016-04-13 16:33:23183
184 private:
bend32292b2016-10-07 00:21:58185 std::unique_ptr<ServiceManagerConnection> service_manager_connection_;
rockot963ad3e82016-04-13 16:33:23186
bend32292b2016-10-07 00:21:58187 DISALLOW_COPY_AND_ASSIGN(BrowserContextServiceManagerConnectionHolder);
rockot963ad3e82016-04-13 16:33:23188};
189
Jens Widell7ca8fb42018-02-21 14:11:54190base::WeakPtr<storage::BlobStorageContext> BlobStorageContextGetterForBrowser(
Xing Liu89860472018-02-09 20:07:02191 scoped_refptr<ChromeBlobStorageContext> blob_context) {
192 DCHECK_CURRENTLY_ON(BrowserThread::IO);
193 return blob_context->context()->AsWeakPtr();
194}
195
[email protected]735e20c2012-03-20 01:16:59196} // namespace
197
[email protected]14acc642012-11-17 12:20:10198// static
199void BrowserContext::AsyncObliterateStoragePartition(
200 BrowserContext* browser_context,
[email protected]399583b2012-12-11 09:33:42201 const GURL& site,
202 const base::Closure& on_gc_required) {
203 GetStoragePartitionMap(browser_context)->AsyncObliterate(site,
204 on_gc_required);
205}
206
207// static
208void BrowserContext::GarbageCollectStoragePartitions(
dcheng59716272016-04-09 05:19:08209 BrowserContext* browser_context,
210 std::unique_ptr<base::hash_set<base::FilePath>> active_paths,
211 const base::Closure& done) {
dcheng36b6aec92015-12-26 06:16:36212 GetStoragePartitionMap(browser_context)
213 ->GarbageCollect(std::move(active_paths), done);
[email protected]14acc642012-11-17 12:20:10214}
215
[email protected]b441a8492012-06-06 14:55:57216DownloadManager* BrowserContext::GetDownloadManager(
217 BrowserContext* context) {
mostynbfbcdc27a2015-03-13 17:58:52218 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]b441a8492012-06-06 14:55:57219 if (!context->GetUserData(kDownloadManagerKeyName)) {
Min Qinddb40ce2017-10-28 03:38:26220 DownloadManager* download_manager = new DownloadManagerImpl(context);
[email protected]d25fda12012-06-12 17:05:03221
avi1ed4a4372017-04-25 05:39:41222 SetDownloadManager(context, base::WrapUnique(download_manager));
[email protected]b441a8492012-06-06 14:55:57223 download_manager->SetDelegate(context->GetDownloadManagerDelegate());
[email protected]b441a8492012-06-06 14:55:57224 }
225
[email protected]eba4a4d2013-05-29 02:18:06226 return static_cast<DownloadManager*>(
227 context->GetUserData(kDownloadManagerKeyName));
[email protected]b441a8492012-06-06 14:55:57228}
229
[email protected]6ef0c3912013-01-25 22:46:34230// static
[email protected]cd501a72014-08-22 19:58:31231storage::ExternalMountPoints* BrowserContext::GetMountPoints(
[email protected]6ef0c3912013-01-25 22:46:34232 BrowserContext* context) {
233 // Ensure that these methods are called on the UI thread, except for
234 // unittests where a UI thread might not have been created.
235 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) ||
236 !BrowserThread::IsMessageLoopValid(BrowserThread::UI));
237
238#if defined(OS_CHROMEOS)
239 if (!context->GetUserData(kMountPointsKey)) {
[email protected]cd501a72014-08-22 19:58:31240 scoped_refptr<storage::ExternalMountPoints> mount_points =
241 storage::ExternalMountPoints::CreateRefCounted();
[email protected]6ef0c3912013-01-25 22:46:34242 context->SetUserData(
243 kMountPointsKey,
Jeremy Roman04f27c372017-10-27 15:20:55244 std::make_unique<UserDataAdapter<storage::ExternalMountPoints>>(
avicb129c02017-05-03 06:49:29245 mount_points.get()));
[email protected]6ef0c3912013-01-25 22:46:34246 }
247
[email protected]cd501a72014-08-22 19:58:31248 return UserDataAdapter<storage::ExternalMountPoints>::Get(context,
249 kMountPointsKey);
[email protected]6ef0c3912013-01-25 22:46:34250#else
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28251 return nullptr;
[email protected]6ef0c3912013-01-25 22:46:34252#endif
253}
254
msrameke169ccb2017-04-26 05:21:41255// static
256content::BrowsingDataRemover* content::BrowserContext::GetBrowsingDataRemover(
257 BrowserContext* context) {
258 DCHECK_CURRENTLY_ON(BrowserThread::UI);
259
260 if (!context->GetUserData(kBrowsingDataRemoverKey)) {
261 std::unique_ptr<BrowsingDataRemoverImpl> remover =
Jeremy Roman04f27c372017-10-27 15:20:55262 std::make_unique<BrowsingDataRemoverImpl>(context);
msrameke169ccb2017-04-26 05:21:41263 remover->SetEmbedderDelegate(context->GetBrowsingDataRemoverDelegate());
264 context->SetUserData(kBrowsingDataRemoverKey, std::move(remover));
265 }
266
267 return static_cast<BrowsingDataRemoverImpl*>(
268 context->GetUserData(kBrowsingDataRemoverKey));
269}
270
[email protected]4c3a23582012-08-18 08:54:34271StoragePartition* BrowserContext::GetStoragePartition(
272 BrowserContext* browser_context,
Dan Elphick6c0d8492017-09-12 09:35:38273 SiteInstance* site_instance,
274 bool can_create) {
[email protected]1bc28312012-11-08 08:31:53275 std::string partition_domain;
276 std::string partition_name;
277 bool in_memory = false;
[email protected]4c3a23582012-08-18 08:54:34278
[email protected]4c3a23582012-08-18 08:54:34279 if (site_instance) {
[email protected]1bc28312012-11-08 08:31:53280 GetContentClient()->browser()->GetStoragePartitionConfigForSite(
[email protected]14acc642012-11-17 12:20:10281 browser_context, site_instance->GetSiteURL(), true,
[email protected]1bc28312012-11-08 08:31:53282 &partition_domain, &partition_name, &in_memory);
[email protected]4c3a23582012-08-18 08:54:34283 }
284
Dan Elphick6c0d8492017-09-12 09:35:38285 return GetStoragePartitionFromConfig(browser_context, partition_domain,
286 partition_name, in_memory, can_create);
[email protected]4c3a23582012-08-18 08:54:34287}
288
[email protected]e94bbcb2012-09-07 05:33:57289StoragePartition* BrowserContext::GetStoragePartitionForSite(
290 BrowserContext* browser_context,
Dan Elphick6c0d8492017-09-12 09:35:38291 const GURL& site,
292 bool can_create) {
[email protected]1bc28312012-11-08 08:31:53293 std::string partition_domain;
294 std::string partition_name;
295 bool in_memory;
[email protected]e94bbcb2012-09-07 05:33:57296
[email protected]1bc28312012-11-08 08:31:53297 GetContentClient()->browser()->GetStoragePartitionConfigForSite(
[email protected]14acc642012-11-17 12:20:10298 browser_context, site, true, &partition_domain, &partition_name,
299 &in_memory);
[email protected]1bc28312012-11-08 08:31:53300
Dan Elphick6c0d8492017-09-12 09:35:38301 return GetStoragePartitionFromConfig(browser_context, partition_domain,
302 partition_name, in_memory, can_create);
[email protected]e94bbcb2012-09-07 05:33:57303}
304
[email protected]4c3a23582012-08-18 08:54:34305void BrowserContext::ForEachStoragePartition(
306 BrowserContext* browser_context,
307 const StoragePartitionCallback& callback) {
308 StoragePartitionImplMap* partition_map =
309 static_cast<StoragePartitionImplMap*>(
a.cavalcantiffab73762015-08-15 02:55:48310 browser_context->GetUserData(kStoragePartitionMapKeyName));
[email protected]4c3a23582012-08-18 08:54:34311 if (!partition_map)
312 return;
313
314 partition_map->ForEach(callback);
315}
316
317StoragePartition* BrowserContext::GetDefaultStoragePartition(
318 BrowserContext* browser_context) {
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28319 return GetStoragePartition(browser_context, nullptr);
[email protected]55eb70e762012-02-20 17:38:39320}
321
tbarzicdb712682015-03-06 06:05:41322// static
[email protected]393b6cb2014-05-15 00:55:12323void BrowserContext::CreateMemoryBackedBlob(BrowserContext* browser_context,
Marijn Kruisselbrink604fd7e72017-10-26 16:31:05324 const char* data,
325 size_t length,
326 const std::string& content_type,
327 BlobCallback callback) {
mostynbfbcdc27a2015-03-13 17:58:52328 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]393b6cb2014-05-15 00:55:12329
330 ChromeBlobStorageContext* blob_context =
331 ChromeBlobStorageContext::GetFor(browser_context);
332 BrowserThread::PostTaskAndReplyWithResult(
333 BrowserThread::IO, FROM_HERE,
Marijn Kruisselbrink604fd7e72017-10-26 16:31:05334 base::BindOnce(&ChromeBlobStorageContext::CreateMemoryBackedBlob,
335 base::WrapRefCounted(blob_context), data, length,
336 content_type),
337 std::move(callback));
[email protected]393b6cb2014-05-15 00:55:12338}
339
[email protected]66e53d0282014-08-07 10:04:35340// static
tbarzicdb712682015-03-06 06:05:41341void BrowserContext::CreateFileBackedBlob(
342 BrowserContext* browser_context,
343 const base::FilePath& path,
344 int64_t offset,
345 int64_t size,
346 const base::Time& expected_modification_time,
Marijn Kruisselbrink604fd7e72017-10-26 16:31:05347 BlobCallback callback) {
mostynbfbcdc27a2015-03-13 17:58:52348 DCHECK_CURRENTLY_ON(BrowserThread::UI);
tbarzicdb712682015-03-06 06:05:41349
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::CreateFileBackedBlob,
355 base::WrapRefCounted(blob_context), path, offset, size,
356 expected_modification_time),
357 std::move(callback));
tbarzicdb712682015-03-06 06:05:41358}
359
360// 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
[email protected]66e53d0282014-08-07 10:04:35371void BrowserContext::DeliverPushMessage(
372 BrowserContext* browser_context,
373 const GURL& origin,
avib7348942015-12-25 20:57:10374 int64_t service_worker_registration_id,
harknessdd4d2b22016-01-27 19:26:43375 const PushEventPayload& payload,
Peter Beverloo7815db1e02017-07-12 19:03:21376 const base::Callback<void(mojom::PushDeliveryStatus)>& callback) {
mostynbfbcdc27a2015-03-13 17:58:52377 DCHECK_CURRENTLY_ON(BrowserThread::UI);
harknessdd4d2b22016-01-27 19:26:43378 PushMessagingRouter::DeliverMessage(browser_context, origin,
379 service_worker_registration_id, payload,
380 callback);
[email protected]66e53d0282014-08-07 10:04:35381}
382
falken41f4175162014-10-29 07:03:41383// static
384void BrowserContext::NotifyWillBeDestroyed(BrowserContext* browser_context) {
Lukasz Anforowicz58d0dac2018-03-23 15:48:10385 // Make sure NotifyWillBeDestroyed is idempotent. This helps facilitate the
386 // pattern where NotifyWillBeDestroyed is called from *both*
387 // ShellBrowserContext and its derived classes (e.g.
388 // LayoutTestBrowserContext).
389 if (browser_context->was_notify_will_be_destroyed_called_)
390 return;
391 browser_context->was_notify_will_be_destroyed_called_ = true;
392
falken41f4175162014-10-29 07:03:41393 // Service Workers must shutdown before the browser context is destroyed,
394 // since they keep render process hosts alive and the codebase assumes that
395 // render process hosts die before their profile (browser context) dies.
396 ForEachStoragePartition(browser_context,
397 base::Bind(ShutdownServiceWorkerContext));
falken04a6912a2016-09-23 21:06:29398
399 // Shared workers also keep render process hosts alive, and are expected to
Darin Fisherd3768b2f62017-10-07 01:00:16400 // return ref counts to 0 after documents close. However, to ensure that
401 // hosts are destructed now, forcibly release their ref counts here.
falken04a6912a2016-09-23 21:06:29402 for (RenderProcessHost::iterator host_iterator =
403 RenderProcessHost::AllHostsIterator();
404 !host_iterator.IsAtEnd(); host_iterator.Advance()) {
405 RenderProcessHost* host = host_iterator.GetCurrentValue();
Benoit Lizeab08362c2017-07-19 14:50:54406 if (host->GetBrowserContext() == browser_context) {
407 // This will also clean up spare RPH references.
Yutaka Hirano09a9afb92017-08-16 09:11:45408 host->DisableKeepAliveRefCount();
Benoit Lizeab08362c2017-07-19 14:50:54409 }
falken04a6912a2016-09-23 21:06:29410 }
falken41f4175162014-10-29 07:03:41411}
412
[email protected]314c3e22012-02-21 03:57:42413void BrowserContext::EnsureResourceContextInitialized(BrowserContext* context) {
[email protected]7e26ac92012-02-27 20:15:05414 // This will be enough to tickle initialization of BrowserContext if
415 // necessary, which initializes ResourceContext. The reason we don't call
[email protected]4c3a23582012-08-18 08:54:34416 // ResourceContext::InitializeResourceContext() directly here is that
417 // ResourceContext initialization may call back into BrowserContext
418 // and when that call returns it'll end rewriting its UserData map. It will
419 // end up rewriting the same value but this still causes a race condition.
420 //
421 // See http://crbug.com/115678.
422 GetDefaultStoragePartition(context);
[email protected]55eb70e762012-02-20 17:38:39423}
424
[email protected]6e2d3d22012-02-24 18:10:36425void BrowserContext::SaveSessionState(BrowserContext* browser_context) {
[email protected]b1b502e2012-09-16 07:31:43426 StoragePartition* storage_partition =
427 BrowserContext::GetDefaultStoragePartition(browser_context);
[email protected]6e2d3d22012-02-24 18:10:36428
Joshua Bell607cb142017-07-24 19:17:16429 storage::DatabaseTracker* database_tracker =
430 storage_partition->GetDatabaseTracker();
431 database_tracker->task_runner()->PostTask(
432 FROM_HERE,
433 base::BindOnce(&storage::DatabaseTracker::SetForceKeepSessionState,
kylecharda69d882017-10-04 05:49:52434 base::WrapRefCounted(database_tracker)));
Joshua Bell607cb142017-07-24 19:17:16435
[email protected]6e2d3d22012-02-24 18:10:36436 if (BrowserThread::IsMessageLoopValid(BrowserThread::IO)) {
437 BrowserThread::PostTask(
438 BrowserThread::IO, FROM_HERE,
tzik4fea24af2017-08-23 11:41:47439 base::BindOnce(
[email protected]6939075a2012-08-28 08:35:53440 &SaveSessionStateOnIOThread,
kylecharda69d882017-10-04 05:49:52441 base::WrapRefCounted(
tzik4fea24af2017-08-23 11:41:47442 BrowserContext::GetDefaultStoragePartition(browser_context)
443 ->GetURLRequestContext()),
[email protected]98d6d4562014-06-25 20:57:55444 static_cast<AppCacheServiceImpl*>(
[email protected]63ef85512014-06-05 14:21:26445 storage_partition->GetAppCacheService())));
[email protected]6e2d3d22012-02-24 18:10:36446 }
447
[email protected]5f2aa722013-08-07 16:59:41448 DOMStorageContextWrapper* dom_storage_context_proxy =
449 static_cast<DOMStorageContextWrapper*>(
[email protected]b1b502e2012-09-16 07:31:43450 storage_partition->GetDOMStorageContext());
[email protected]5f2aa722013-08-07 16:59:41451 dom_storage_context_proxy->SetForceKeepSessionState();
[email protected]735e20c2012-03-20 01:16:59452
[email protected]89acda82013-06-25 20:52:50453 IndexedDBContextImpl* indexed_db_context_impl =
454 static_cast<IndexedDBContextImpl*>(
[email protected]b1b502e2012-09-16 07:31:43455 storage_partition->GetIndexedDBContext());
[email protected]89acda82013-06-25 20:52:50456 // No task runner in unit tests.
457 if (indexed_db_context_impl->TaskRunner()) {
458 indexed_db_context_impl->TaskRunner()->PostTask(
kylecharda69d882017-10-04 05:49:52459 FROM_HERE,
460 base::BindOnce(&SaveSessionStateOnIndexedDBThread,
461 base::WrapRefCounted(indexed_db_context_impl)));
[email protected]6e2d3d22012-02-24 18:10:36462 }
463}
464
ttr31481dc54b2015-08-06 20:11:26465void BrowserContext::SetDownloadManagerForTesting(
466 BrowserContext* browser_context,
avi1ed4a4372017-04-25 05:39:41467 std::unique_ptr<content::DownloadManager> download_manager) {
468 SetDownloadManager(browser_context, std::move(download_manager));
ttr31481dc54b2015-08-06 20:11:26469}
470
rockot963ad3e82016-04-13 16:33:23471// static
erge69130f52016-03-02 00:13:28472void BrowserContext::Initialize(
473 BrowserContext* browser_context,
474 const base::FilePath& path) {
ben6c85c4492016-06-16 20:40:51475 std::string new_id;
476 if (GetContentClient() && GetContentClient()->browser()) {
bend32292b2016-10-07 00:21:58477 new_id = GetContentClient()->browser()->GetServiceUserIdForBrowserContext(
ben6c85c4492016-06-16 20:40:51478 browser_context);
479 } else {
480 // Some test scenarios initialize a BrowserContext without a content client.
bencccfe2a2016-03-05 16:54:14481 new_id = base::GenerateGUID();
ben6c85c4492016-06-16 20:40:51482 }
erge69130f52016-03-02 00:13:28483
bend32292b2016-10-07 00:21:58484 ServiceUserIdHolder* holder = static_cast<ServiceUserIdHolder*>(
485 browser_context->GetUserData(kServiceUserId));
ben6c85c4492016-06-16 20:40:51486 if (holder)
bend32292b2016-10-07 00:21:58487 file::ForgetServiceUserIdUserDirAssociation(holder->user_id());
488 file::AssociateServiceUserIdWithUserDir(new_id, path);
ben6c85c4492016-06-16 20:40:51489 RemoveBrowserContextFromUserIdMap(browser_context);
490 g_user_id_to_context.Get()[new_id] = browser_context;
bend32292b2016-10-07 00:21:58491 browser_context->SetUserData(kServiceUserId,
Jeremy Roman04f27c372017-10-27 15:20:55492 std::make_unique<ServiceUserIdHolder>(new_id));
erge69130f52016-03-02 00:13:28493
avi1ed4a4372017-04-25 05:39:41494 browser_context->SetUserData(
Jeremy Roman04f27c372017-10-27 15:20:55495 kMojoWasInitialized, std::make_unique<base::SupportsUserData::Data>());
rockot963ad3e82016-04-13 16:33:23496
bend32292b2016-10-07 00:21:58497 ServiceManagerConnection* service_manager_connection =
498 ServiceManagerConnection::GetForProcess();
fdoray4f155f02016-10-12 11:28:50499 if (service_manager_connection && base::ThreadTaskRunnerHandle::IsSet()) {
rockot963ad3e82016-04-13 16:33:23500 // NOTE: Many unit tests create a TestBrowserContext without initializing
bend32292b2016-10-07 00:21:58501 // Mojo or the global service manager connection.
rockot963ad3e82016-04-13 16:33:23502
rockot400ea35b2016-10-15 19:15:32503 service_manager::mojom::ServicePtr service;
Ken Rockotf4d8a942017-05-13 00:10:37504 auto service_request = mojo::MakeRequest(&service);
rockot963ad3e82016-04-13 16:33:23505
rockot400ea35b2016-10-15 19:15:32506 service_manager::mojom::PIDReceiverPtr pid_receiver;
bene6a9f012017-01-07 00:43:43507 service_manager::Identity identity(mojom::kBrowserServiceName, new_id);
benbd3c2482017-01-07 05:48:21508 service_manager_connection->GetConnector()->StartService(
bene6a9f012017-01-07 00:43:43509 identity, std::move(service), mojo::MakeRequest(&pid_receiver));
rockot963ad3e82016-04-13 16:33:23510 pid_receiver->SetPID(base::GetCurrentProcId());
511
benaad37ecd2017-04-12 22:08:20512 service_manager_connection->GetConnector()->StartService(identity);
bend32292b2016-10-07 00:21:58513 BrowserContextServiceManagerConnectionHolder* connection_holder =
514 new BrowserContextServiceManagerConnectionHolder(
bend32292b2016-10-07 00:21:58515 std::move(service_request));
avi1ed4a4372017-04-25 05:39:41516 browser_context->SetUserData(kServiceManagerConnection,
517 base::WrapUnique(connection_holder));
rockot963ad3e82016-04-13 16:33:23518
bend32292b2016-10-07 00:21:58519 ServiceManagerConnection* connection =
520 connection_holder->service_manager_connection();
rockot963ad3e82016-04-13 16:33:23521
ben146248de2016-06-14 15:24:59522 // New embedded service factories should be added to |connection| here.
rockot963ad3e82016-04-13 16:33:23523
Marijn Kruisselbrink3e68abf2018-02-12 18:52:19524 service_manager::EmbeddedServiceInfo info;
525 info.factory = base::BindRepeating(&file::CreateFileService);
526 connection->AddEmbeddedService(file::mojom::kServiceName, info);
tibellab3d36b2017-03-10 02:57:25527
528 ContentBrowserClient::StaticServiceMap services;
529 browser_context->RegisterInProcessServices(&services);
530 for (const auto& entry : services) {
531 connection->AddEmbeddedService(entry.first, entry.second);
532 }
Ken Rockotc7a279c2017-05-04 23:51:45533
534 RegisterCommonBrowserInterfaces(connection);
sammc030f5012017-03-13 02:28:45535 connection->Start();
rockot963ad3e82016-04-13 16:33:23536 }
Elad Alon03ad23b2018-02-07 16:54:31537
538#if BUILDFLAG(ENABLE_WEBRTC)
539 if (!browser_context->IsOffTheRecord()) {
Elad Alon12ecdf02018-03-14 12:07:31540 WebRtcEventLogger* const logger = WebRtcEventLogger::Get();
541 if (logger) {
542 logger->EnableForBrowserContext(browser_context);
Elad Alon03ad23b2018-02-07 16:54:31543 }
544 }
545#endif
erge69130f52016-03-02 00:13:28546}
547
rockot963ad3e82016-04-13 16:33:23548// static
bend32292b2016-10-07 00:21:58549const std::string& BrowserContext::GetServiceUserIdFor(
bencccfe2a2016-03-05 16:54:14550 BrowserContext* browser_context) {
erge69130f52016-03-02 00:13:28551 CHECK(browser_context->GetUserData(kMojoWasInitialized))
552 << "Attempting to get the mojo user id for a BrowserContext that was "
553 << "never Initialize()ed.";
554
bend32292b2016-10-07 00:21:58555 ServiceUserIdHolder* holder = static_cast<ServiceUserIdHolder*>(
556 browser_context->GetUserData(kServiceUserId));
ben6c85c4492016-06-16 20:40:51557 return holder->user_id();
erge69130f52016-03-02 00:13:28558}
559
rockot963ad3e82016-04-13 16:33:23560// static
bend32292b2016-10-07 00:21:58561BrowserContext* BrowserContext::GetBrowserContextForServiceUserId(
ben6c85c4492016-06-16 20:40:51562 const std::string& user_id) {
563 auto it = g_user_id_to_context.Get().find(user_id);
564 return it != g_user_id_to_context.Get().end() ? it->second : nullptr;
565}
566
567// static
rockot400ea35b2016-10-15 19:15:32568service_manager::Connector* BrowserContext::GetConnectorFor(
rockot963ad3e82016-04-13 16:33:23569 BrowserContext* browser_context) {
bend32292b2016-10-07 00:21:58570 ServiceManagerConnection* connection =
571 GetServiceManagerConnectionFor(browser_context);
ben5be0b9132016-08-03 00:17:18572 return connection ? connection->GetConnector() : nullptr;
573}
574
575// static
bend32292b2016-10-07 00:21:58576ServiceManagerConnection* BrowserContext::GetServiceManagerConnectionFor(
ben5be0b9132016-08-03 00:17:18577 BrowserContext* browser_context) {
bend32292b2016-10-07 00:21:58578 BrowserContextServiceManagerConnectionHolder* connection_holder =
579 static_cast<BrowserContextServiceManagerConnectionHolder*>(
580 browser_context->GetUserData(kServiceManagerConnection));
581 return connection_holder ? connection_holder->service_manager_connection()
582 : nullptr;
rockot963ad3e82016-04-13 16:33:23583}
584
mmenkec0b2b8b12017-04-21 16:27:52585BrowserContext::BrowserContext()
586 : media_device_id_salt_(CreateRandomMediaDeviceIDSalt()) {}
587
[email protected]55eb70e762012-02-20 17:38:39588BrowserContext::~BrowserContext() {
erge69130f52016-03-02 00:13:28589 CHECK(GetUserData(kMojoWasInitialized))
590 << "Attempting to destroy a BrowserContext that never called "
591 << "Initialize()";
592
kinukof6ed359c2016-07-26 13:27:21593 DCHECK(!GetUserData(kStoragePartitionMapKeyName))
594 << "StoragePartitionMap is not shut down properly";
595
Lukasz Anforowicz58d0dac2018-03-23 15:48:10596 DCHECK(was_notify_will_be_destroyed_called_);
597
Elad Alon03ad23b2018-02-07 16:54:31598#if BUILDFLAG(ENABLE_WEBRTC)
Elad Alon12ecdf02018-03-14 12:07:31599 WebRtcEventLogger* const logger = WebRtcEventLogger::Get();
600 if (logger) {
601 logger->DisableForBrowserContext(this);
Elad Alon03ad23b2018-02-07 16:54:31602 }
603#endif
604
ben6c85c4492016-06-16 20:40:51605 RemoveBrowserContextFromUserIdMap(this);
606
[email protected]b441a8492012-06-06 14:55:57607 if (GetUserData(kDownloadManagerKeyName))
608 GetDownloadManager(this)->Shutdown();
[email protected]55eb70e762012-02-20 17:38:39609}
610
kinukof6ed359c2016-07-26 13:27:21611void BrowserContext::ShutdownStoragePartitions() {
612 if (GetUserData(kStoragePartitionMapKeyName))
613 RemoveUserData(kStoragePartitionMapKeyName);
614}
615
mmenkec0b2b8b12017-04-21 16:27:52616std::string BrowserContext::GetMediaDeviceIDSalt() {
617 return media_device_id_salt_;
618}
619
620// static
621std::string BrowserContext::CreateRandomMediaDeviceIDSalt() {
622 std::string salt;
623 base::Base64Encode(base::RandBytesAsString(16), &salt);
624 DCHECK(!salt.empty());
625 return salt;
626}
627
Chris Cunninghama5b38012017-10-28 07:27:18628media::VideoDecodePerfHistory* BrowserContext::GetVideoDecodePerfHistory() {
629 media::VideoDecodePerfHistory* decode_history =
630 static_cast<media::VideoDecodePerfHistory*>(
631 GetUserData(kVideoDecodePerfHistoryId));
632
633 // Lazily created. Note, this does not trigger loading the DB from disk. That
634 // occurs later upon first VideoDecodePerfHistory API request that requires DB
635 // access. DB operations will not block the UI thread.
636 if (!decode_history) {
637 auto db_factory = std::make_unique<media::VideoDecodeStatsDBImplFactory>(
638 GetPath().Append(FILE_PATH_LITERAL("VideoDecodeStats")));
639 decode_history = new media::VideoDecodePerfHistory(std::move(db_factory));
640 SetUserData(kVideoDecodePerfHistoryId, base::WrapUnique(decode_history));
641 }
642
643 return decode_history;
644}
645
[email protected]55eb70e762012-02-20 17:38:39646} // namespace content