blob: 5c185b3690ee526750ef11746af0df9666d82c2f [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"
rockot963ad3e82016-04-13 16:33:2342#include "content/public/common/content_switches.h"
bend32292b2016-10-07 00:21:5843#include "content/public/common/service_manager_connection.h"
ben51bb6c62016-11-17 20:15:5744#include "content/public/common/service_names.mojom.h"
Chris Cunninghama5b38012017-10-28 07:27:1845#include "media/mojo/services/video_decode_perf_history.h"
46#include "media/mojo/services/video_decode_stats_db_impl.h"
[email protected]4d7c4ef2012-03-16 01:47:1247#include "net/cookies/cookie_store.h"
[email protected]6b8a3c742014-07-25 00:25:3548#include "net/ssl/channel_id_service.h"
49#include "net/ssl/channel_id_store.h"
[email protected]6e2d3d22012-02-24 18:10:3650#include "net/url_request/url_request_context.h"
[email protected]6939075a2012-08-28 08:35:5351#include "net/url_request/url_request_context_getter.h"
ben768c8dc2016-08-12 00:26:5052#include "services/file/file_service.h"
benaabad0b2016-11-16 23:54:1753#include "services/file/public/interfaces/constants.mojom.h"
ben768c8dc2016-08-12 00:26:5054#include "services/file/user_id_map.h"
rockot734fb662016-10-15 16:41:3055#include "services/service_manager/public/cpp/connector.h"
56#include "services/service_manager/public/interfaces/service.mojom.h"
pilgrime92c5fcd2014-09-10 23:31:2357#include "storage/browser/database/database_tracker.h"
58#include "storage/browser/fileapi/external_mount_points.h"
[email protected]55eb70e762012-02-20 17:38:3959
[email protected]314c3e22012-02-21 03:57:4260using base::UserDataAdapter;
[email protected]55eb70e762012-02-20 17:38:3961
[email protected]55eb70e762012-02-20 17:38:3962namespace content {
63
[email protected]735e20c2012-03-20 01:16:5964namespace {
65
scottmg5e65e3a2017-03-08 08:48:4666base::LazyInstance<std::map<std::string, BrowserContext*>>::DestructorAtExit
ben6c85c4492016-06-16 20:40:5167 g_user_id_to_context = LAZY_INSTANCE_INITIALIZER;
68
bend32292b2016-10-07 00:21:5869class ServiceUserIdHolder : public base::SupportsUserData::Data {
ben6c85c4492016-06-16 20:40:5170 public:
bend32292b2016-10-07 00:21:5871 explicit ServiceUserIdHolder(const std::string& user_id)
72 : user_id_(user_id) {}
73 ~ServiceUserIdHolder() override {}
ben6c85c4492016-06-16 20:40:5174
75 const std::string& user_id() const { return user_id_; }
76
77 private:
78 std::string user_id_;
79
bend32292b2016-10-07 00:21:5880 DISALLOW_COPY_AND_ASSIGN(ServiceUserIdHolder);
ben6c85c4492016-06-16 20:40:5181};
erge69130f52016-03-02 00:13:2882
[email protected]e0ce8a1e2012-09-18 10:26:3683// Key names on BrowserContext.
msrameke169ccb2017-04-26 05:21:4184const char kBrowsingDataRemoverKey[] = "browsing-data-remover";
[email protected]6ef0c3912013-01-25 22:46:3485const char kDownloadManagerKeyName[] = "download_manager";
erge69130f52016-03-02 00:13:2886const char kMojoWasInitialized[] = "mojo-was-initialized";
bend32292b2016-10-07 00:21:5887const char kServiceManagerConnection[] = "service-manager-connection";
88const char kServiceUserId[] = "service-user-id";
rockot963ad3e82016-04-13 16:33:2389const char kStoragePartitionMapKeyName[] = "content_storage_partition_map";
Chris Cunninghama5b38012017-10-28 07:27:1890const char kVideoDecodePerfHistoryId[] = "video-decode-perf-history";
erge69130f52016-03-02 00:13:2891
[email protected]9afc14e22013-09-25 22:34:1492#if defined(OS_CHROMEOS)
93const char kMountPointsKey[] = "mount_points";
94#endif // defined(OS_CHROMEOS)
95
ben6c85c4492016-06-16 20:40:5196void RemoveBrowserContextFromUserIdMap(BrowserContext* browser_context) {
bend32292b2016-10-07 00:21:5897 ServiceUserIdHolder* holder = static_cast<ServiceUserIdHolder*>(
98 browser_context->GetUserData(kServiceUserId));
ben6c85c4492016-06-16 20:40:5199 if (holder) {
100 auto it = g_user_id_to_context.Get().find(holder->user_id());
101 if (it != g_user_id_to_context.Get().end())
102 g_user_id_to_context.Get().erase(it);
103 }
104}
105
[email protected]14acc642012-11-17 12:20:10106StoragePartitionImplMap* GetStoragePartitionMap(
107 BrowserContext* browser_context) {
[email protected]4c3a23582012-08-18 08:54:34108 StoragePartitionImplMap* partition_map =
109 static_cast<StoragePartitionImplMap*>(
a.cavalcantiffab73762015-08-15 02:55:48110 browser_context->GetUserData(kStoragePartitionMapKeyName));
[email protected]d7c7c98a2012-07-12 21:27:44111 if (!partition_map) {
avi1ed4a4372017-04-25 05:39:41112 auto partition_map_owned =
Jeremy Roman04f27c372017-10-27 15:20:55113 std::make_unique<StoragePartitionImplMap>(browser_context);
avi1ed4a4372017-04-25 05:39:41114 partition_map = partition_map_owned.get();
115 browser_context->SetUserData(kStoragePartitionMapKeyName,
116 std::move(partition_map_owned));
[email protected]d7c7c98a2012-07-12 21:27:44117 }
[email protected]14acc642012-11-17 12:20:10118 return partition_map;
119}
120
121StoragePartition* GetStoragePartitionFromConfig(
122 BrowserContext* browser_context,
123 const std::string& partition_domain,
124 const std::string& partition_name,
Dan Elphick6c0d8492017-09-12 09:35:38125 bool in_memory,
126 bool can_create) {
[email protected]14acc642012-11-17 12:20:10127 StoragePartitionImplMap* partition_map =
128 GetStoragePartitionMap(browser_context);
[email protected]d7c7c98a2012-07-12 21:27:44129
[email protected]1bc28312012-11-08 08:31:53130 if (browser_context->IsOffTheRecord())
131 in_memory = true;
132
Dan Elphick6c0d8492017-09-12 09:35:38133 return partition_map->Get(partition_domain, partition_name, in_memory,
134 can_create);
[email protected]d1198fd2012-08-13 22:50:19135}
136
[email protected]6939075a2012-08-28 08:35:53137void SaveSessionStateOnIOThread(
138 const scoped_refptr<net::URLRequestContextGetter>& context_getter,
[email protected]98d6d4562014-06-25 20:57:55139 AppCacheServiceImpl* appcache_service) {
[email protected]6939075a2012-08-28 08:35:53140 net::URLRequestContext* context = context_getter->GetURLRequestContext();
mmenkeded79da2016-02-06 08:28:51141 context->cookie_store()->SetForceKeepSessionState();
[email protected]6b8a3c742014-07-25 00:25:35142 context->channel_id_service()->GetChannelIDStore()->
[email protected]6939075a2012-08-28 08:35:53143 SetForceKeepSessionState();
144 appcache_service->set_force_keep_session_state();
[email protected]6e2d3d22012-02-24 18:10:36145}
146
[email protected]89acda82013-06-25 20:52:50147void SaveSessionStateOnIndexedDBThread(
[email protected]6e2d3d22012-02-24 18:10:36148 scoped_refptr<IndexedDBContextImpl> indexed_db_context) {
[email protected]bf510ed2012-06-05 08:31:43149 indexed_db_context->SetForceKeepSessionState();
[email protected]6e2d3d22012-02-24 18:10:36150}
151
falken41f4175162014-10-29 07:03:41152void ShutdownServiceWorkerContext(StoragePartition* partition) {
153 ServiceWorkerContextWrapper* wrapper =
154 static_cast<ServiceWorkerContextWrapper*>(
155 partition->GetServiceWorkerContext());
156 wrapper->process_manager()->Shutdown();
157}
158
avi1ed4a4372017-04-25 05:39:41159void SetDownloadManager(
160 BrowserContext* context,
161 std::unique_ptr<content::DownloadManager> download_manager) {
ttr31481dc54b2015-08-06 20:11:26162 DCHECK_CURRENTLY_ON(BrowserThread::UI);
163 DCHECK(download_manager);
avi1ed4a4372017-04-25 05:39:41164 context->SetUserData(kDownloadManagerKeyName, std::move(download_manager));
ttr31481dc54b2015-08-06 20:11:26165}
166
bend32292b2016-10-07 00:21:58167class BrowserContextServiceManagerConnectionHolder
rockot963ad3e82016-04-13 16:33:23168 : public base::SupportsUserData::Data {
169 public:
benaad37ecd2017-04-12 22:08:20170 explicit BrowserContextServiceManagerConnectionHolder(
rockot400ea35b2016-10-15 19:15:32171 service_manager::mojom::ServiceRequest request)
benaad37ecd2017-04-12 22:08:20172 : service_manager_connection_(ServiceManagerConnection::Create(
rockotcef38272016-07-15 22:47:47173 std::move(request),
174 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO))) {}
bend32292b2016-10-07 00:21:58175 ~BrowserContextServiceManagerConnectionHolder() override {}
rockot963ad3e82016-04-13 16:33:23176
bend32292b2016-10-07 00:21:58177 ServiceManagerConnection* service_manager_connection() {
178 return service_manager_connection_.get();
179 }
rockot963ad3e82016-04-13 16:33:23180
181 private:
bend32292b2016-10-07 00:21:58182 std::unique_ptr<ServiceManagerConnection> service_manager_connection_;
rockot963ad3e82016-04-13 16:33:23183
bend32292b2016-10-07 00:21:58184 DISALLOW_COPY_AND_ASSIGN(BrowserContextServiceManagerConnectionHolder);
rockot963ad3e82016-04-13 16:33:23185};
186
[email protected]735e20c2012-03-20 01:16:59187} // namespace
188
[email protected]14acc642012-11-17 12:20:10189// static
190void BrowserContext::AsyncObliterateStoragePartition(
191 BrowserContext* browser_context,
[email protected]399583b2012-12-11 09:33:42192 const GURL& site,
193 const base::Closure& on_gc_required) {
194 GetStoragePartitionMap(browser_context)->AsyncObliterate(site,
195 on_gc_required);
196}
197
198// static
199void BrowserContext::GarbageCollectStoragePartitions(
dcheng59716272016-04-09 05:19:08200 BrowserContext* browser_context,
201 std::unique_ptr<base::hash_set<base::FilePath>> active_paths,
202 const base::Closure& done) {
dcheng36b6aec92015-12-26 06:16:36203 GetStoragePartitionMap(browser_context)
204 ->GarbageCollect(std::move(active_paths), done);
[email protected]14acc642012-11-17 12:20:10205}
206
[email protected]b441a8492012-06-06 14:55:57207DownloadManager* BrowserContext::GetDownloadManager(
208 BrowserContext* context) {
mostynbfbcdc27a2015-03-13 17:58:52209 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]b441a8492012-06-06 14:55:57210 if (!context->GetUserData(kDownloadManagerKeyName)) {
Min Qinddb40ce2017-10-28 03:38:26211 DownloadManager* download_manager = new DownloadManagerImpl(context);
[email protected]d25fda12012-06-12 17:05:03212
avi1ed4a4372017-04-25 05:39:41213 SetDownloadManager(context, base::WrapUnique(download_manager));
[email protected]b441a8492012-06-06 14:55:57214 download_manager->SetDelegate(context->GetDownloadManagerDelegate());
[email protected]b441a8492012-06-06 14:55:57215 }
216
[email protected]eba4a4d2013-05-29 02:18:06217 return static_cast<DownloadManager*>(
218 context->GetUserData(kDownloadManagerKeyName));
[email protected]b441a8492012-06-06 14:55:57219}
220
[email protected]6ef0c3912013-01-25 22:46:34221// static
[email protected]cd501a72014-08-22 19:58:31222storage::ExternalMountPoints* BrowserContext::GetMountPoints(
[email protected]6ef0c3912013-01-25 22:46:34223 BrowserContext* context) {
224 // Ensure that these methods are called on the UI thread, except for
225 // unittests where a UI thread might not have been created.
226 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) ||
227 !BrowserThread::IsMessageLoopValid(BrowserThread::UI));
228
229#if defined(OS_CHROMEOS)
230 if (!context->GetUserData(kMountPointsKey)) {
[email protected]cd501a72014-08-22 19:58:31231 scoped_refptr<storage::ExternalMountPoints> mount_points =
232 storage::ExternalMountPoints::CreateRefCounted();
[email protected]6ef0c3912013-01-25 22:46:34233 context->SetUserData(
234 kMountPointsKey,
Jeremy Roman04f27c372017-10-27 15:20:55235 std::make_unique<UserDataAdapter<storage::ExternalMountPoints>>(
avicb129c02017-05-03 06:49:29236 mount_points.get()));
[email protected]6ef0c3912013-01-25 22:46:34237 }
238
[email protected]cd501a72014-08-22 19:58:31239 return UserDataAdapter<storage::ExternalMountPoints>::Get(context,
240 kMountPointsKey);
[email protected]6ef0c3912013-01-25 22:46:34241#else
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28242 return nullptr;
[email protected]6ef0c3912013-01-25 22:46:34243#endif
244}
245
msrameke169ccb2017-04-26 05:21:41246// static
247content::BrowsingDataRemover* content::BrowserContext::GetBrowsingDataRemover(
248 BrowserContext* context) {
249 DCHECK_CURRENTLY_ON(BrowserThread::UI);
250
251 if (!context->GetUserData(kBrowsingDataRemoverKey)) {
252 std::unique_ptr<BrowsingDataRemoverImpl> remover =
Jeremy Roman04f27c372017-10-27 15:20:55253 std::make_unique<BrowsingDataRemoverImpl>(context);
msrameke169ccb2017-04-26 05:21:41254 remover->SetEmbedderDelegate(context->GetBrowsingDataRemoverDelegate());
255 context->SetUserData(kBrowsingDataRemoverKey, std::move(remover));
256 }
257
258 return static_cast<BrowsingDataRemoverImpl*>(
259 context->GetUserData(kBrowsingDataRemoverKey));
260}
261
[email protected]4c3a23582012-08-18 08:54:34262StoragePartition* BrowserContext::GetStoragePartition(
263 BrowserContext* browser_context,
Dan Elphick6c0d8492017-09-12 09:35:38264 SiteInstance* site_instance,
265 bool can_create) {
[email protected]1bc28312012-11-08 08:31:53266 std::string partition_domain;
267 std::string partition_name;
268 bool in_memory = false;
[email protected]4c3a23582012-08-18 08:54:34269
[email protected]4c3a23582012-08-18 08:54:34270 if (site_instance) {
[email protected]1bc28312012-11-08 08:31:53271 GetContentClient()->browser()->GetStoragePartitionConfigForSite(
[email protected]14acc642012-11-17 12:20:10272 browser_context, site_instance->GetSiteURL(), true,
[email protected]1bc28312012-11-08 08:31:53273 &partition_domain, &partition_name, &in_memory);
[email protected]4c3a23582012-08-18 08:54:34274 }
275
Dan Elphick6c0d8492017-09-12 09:35:38276 return GetStoragePartitionFromConfig(browser_context, partition_domain,
277 partition_name, in_memory, can_create);
[email protected]4c3a23582012-08-18 08:54:34278}
279
[email protected]e94bbcb2012-09-07 05:33:57280StoragePartition* BrowserContext::GetStoragePartitionForSite(
281 BrowserContext* browser_context,
Dan Elphick6c0d8492017-09-12 09:35:38282 const GURL& site,
283 bool can_create) {
[email protected]1bc28312012-11-08 08:31:53284 std::string partition_domain;
285 std::string partition_name;
286 bool in_memory;
[email protected]e94bbcb2012-09-07 05:33:57287
[email protected]1bc28312012-11-08 08:31:53288 GetContentClient()->browser()->GetStoragePartitionConfigForSite(
[email protected]14acc642012-11-17 12:20:10289 browser_context, site, true, &partition_domain, &partition_name,
290 &in_memory);
[email protected]1bc28312012-11-08 08:31:53291
Dan Elphick6c0d8492017-09-12 09:35:38292 return GetStoragePartitionFromConfig(browser_context, partition_domain,
293 partition_name, in_memory, can_create);
[email protected]e94bbcb2012-09-07 05:33:57294}
295
[email protected]4c3a23582012-08-18 08:54:34296void BrowserContext::ForEachStoragePartition(
297 BrowserContext* browser_context,
298 const StoragePartitionCallback& callback) {
299 StoragePartitionImplMap* partition_map =
300 static_cast<StoragePartitionImplMap*>(
a.cavalcantiffab73762015-08-15 02:55:48301 browser_context->GetUserData(kStoragePartitionMapKeyName));
[email protected]4c3a23582012-08-18 08:54:34302 if (!partition_map)
303 return;
304
305 partition_map->ForEach(callback);
306}
307
308StoragePartition* BrowserContext::GetDefaultStoragePartition(
309 BrowserContext* browser_context) {
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28310 return GetStoragePartition(browser_context, nullptr);
[email protected]55eb70e762012-02-20 17:38:39311}
312
tbarzicdb712682015-03-06 06:05:41313// static
[email protected]393b6cb2014-05-15 00:55:12314void BrowserContext::CreateMemoryBackedBlob(BrowserContext* browser_context,
Marijn Kruisselbrink604fd7e72017-10-26 16:31:05315 const char* data,
316 size_t length,
317 const std::string& content_type,
318 BlobCallback callback) {
mostynbfbcdc27a2015-03-13 17:58:52319 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]393b6cb2014-05-15 00:55:12320
321 ChromeBlobStorageContext* blob_context =
322 ChromeBlobStorageContext::GetFor(browser_context);
323 BrowserThread::PostTaskAndReplyWithResult(
324 BrowserThread::IO, FROM_HERE,
Marijn Kruisselbrink604fd7e72017-10-26 16:31:05325 base::BindOnce(&ChromeBlobStorageContext::CreateMemoryBackedBlob,
326 base::WrapRefCounted(blob_context), data, length,
327 content_type),
328 std::move(callback));
[email protected]393b6cb2014-05-15 00:55:12329}
330
[email protected]66e53d0282014-08-07 10:04:35331// static
tbarzicdb712682015-03-06 06:05:41332void BrowserContext::CreateFileBackedBlob(
333 BrowserContext* browser_context,
334 const base::FilePath& path,
335 int64_t offset,
336 int64_t size,
337 const base::Time& expected_modification_time,
Marijn Kruisselbrink604fd7e72017-10-26 16:31:05338 BlobCallback callback) {
mostynbfbcdc27a2015-03-13 17:58:52339 DCHECK_CURRENTLY_ON(BrowserThread::UI);
tbarzicdb712682015-03-06 06:05:41340
341 ChromeBlobStorageContext* blob_context =
342 ChromeBlobStorageContext::GetFor(browser_context);
343 BrowserThread::PostTaskAndReplyWithResult(
344 BrowserThread::IO, FROM_HERE,
Marijn Kruisselbrink604fd7e72017-10-26 16:31:05345 base::BindOnce(&ChromeBlobStorageContext::CreateFileBackedBlob,
346 base::WrapRefCounted(blob_context), path, offset, size,
347 expected_modification_time),
348 std::move(callback));
tbarzicdb712682015-03-06 06:05:41349}
350
351// static
[email protected]66e53d0282014-08-07 10:04:35352void BrowserContext::DeliverPushMessage(
353 BrowserContext* browser_context,
354 const GURL& origin,
avib7348942015-12-25 20:57:10355 int64_t service_worker_registration_id,
harknessdd4d2b22016-01-27 19:26:43356 const PushEventPayload& payload,
Peter Beverloo7815db1e02017-07-12 19:03:21357 const base::Callback<void(mojom::PushDeliveryStatus)>& callback) {
mostynbfbcdc27a2015-03-13 17:58:52358 DCHECK_CURRENTLY_ON(BrowserThread::UI);
harknessdd4d2b22016-01-27 19:26:43359 PushMessagingRouter::DeliverMessage(browser_context, origin,
360 service_worker_registration_id, payload,
361 callback);
[email protected]66e53d0282014-08-07 10:04:35362}
363
falken41f4175162014-10-29 07:03:41364// static
365void BrowserContext::NotifyWillBeDestroyed(BrowserContext* browser_context) {
366 // Service Workers must shutdown before the browser context is destroyed,
367 // since they keep render process hosts alive and the codebase assumes that
368 // render process hosts die before their profile (browser context) dies.
369 ForEachStoragePartition(browser_context,
370 base::Bind(ShutdownServiceWorkerContext));
falken04a6912a2016-09-23 21:06:29371
372 // Shared workers also keep render process hosts alive, and are expected to
Darin Fisherd3768b2f62017-10-07 01:00:16373 // return ref counts to 0 after documents close. However, to ensure that
374 // hosts are destructed now, forcibly release their ref counts here.
falken04a6912a2016-09-23 21:06:29375 for (RenderProcessHost::iterator host_iterator =
376 RenderProcessHost::AllHostsIterator();
377 !host_iterator.IsAtEnd(); host_iterator.Advance()) {
378 RenderProcessHost* host = host_iterator.GetCurrentValue();
Benoit Lizeab08362c2017-07-19 14:50:54379 if (host->GetBrowserContext() == browser_context) {
380 // This will also clean up spare RPH references.
Yutaka Hirano09a9afb92017-08-16 09:11:45381 host->DisableKeepAliveRefCount();
Benoit Lizeab08362c2017-07-19 14:50:54382 }
falken04a6912a2016-09-23 21:06:29383 }
falken41f4175162014-10-29 07:03:41384}
385
[email protected]314c3e22012-02-21 03:57:42386void BrowserContext::EnsureResourceContextInitialized(BrowserContext* context) {
[email protected]7e26ac92012-02-27 20:15:05387 // This will be enough to tickle initialization of BrowserContext if
388 // necessary, which initializes ResourceContext. The reason we don't call
[email protected]4c3a23582012-08-18 08:54:34389 // ResourceContext::InitializeResourceContext() directly here is that
390 // ResourceContext initialization may call back into BrowserContext
391 // and when that call returns it'll end rewriting its UserData map. It will
392 // end up rewriting the same value but this still causes a race condition.
393 //
394 // See http://crbug.com/115678.
395 GetDefaultStoragePartition(context);
[email protected]55eb70e762012-02-20 17:38:39396}
397
[email protected]6e2d3d22012-02-24 18:10:36398void BrowserContext::SaveSessionState(BrowserContext* browser_context) {
[email protected]b1b502e2012-09-16 07:31:43399 StoragePartition* storage_partition =
400 BrowserContext::GetDefaultStoragePartition(browser_context);
[email protected]6e2d3d22012-02-24 18:10:36401
Joshua Bell607cb142017-07-24 19:17:16402 storage::DatabaseTracker* database_tracker =
403 storage_partition->GetDatabaseTracker();
404 database_tracker->task_runner()->PostTask(
405 FROM_HERE,
406 base::BindOnce(&storage::DatabaseTracker::SetForceKeepSessionState,
kylecharda69d882017-10-04 05:49:52407 base::WrapRefCounted(database_tracker)));
Joshua Bell607cb142017-07-24 19:17:16408
[email protected]6e2d3d22012-02-24 18:10:36409 if (BrowserThread::IsMessageLoopValid(BrowserThread::IO)) {
410 BrowserThread::PostTask(
411 BrowserThread::IO, FROM_HERE,
tzik4fea24af2017-08-23 11:41:47412 base::BindOnce(
[email protected]6939075a2012-08-28 08:35:53413 &SaveSessionStateOnIOThread,
kylecharda69d882017-10-04 05:49:52414 base::WrapRefCounted(
tzik4fea24af2017-08-23 11:41:47415 BrowserContext::GetDefaultStoragePartition(browser_context)
416 ->GetURLRequestContext()),
[email protected]98d6d4562014-06-25 20:57:55417 static_cast<AppCacheServiceImpl*>(
[email protected]63ef85512014-06-05 14:21:26418 storage_partition->GetAppCacheService())));
[email protected]6e2d3d22012-02-24 18:10:36419 }
420
[email protected]5f2aa722013-08-07 16:59:41421 DOMStorageContextWrapper* dom_storage_context_proxy =
422 static_cast<DOMStorageContextWrapper*>(
[email protected]b1b502e2012-09-16 07:31:43423 storage_partition->GetDOMStorageContext());
[email protected]5f2aa722013-08-07 16:59:41424 dom_storage_context_proxy->SetForceKeepSessionState();
[email protected]735e20c2012-03-20 01:16:59425
[email protected]89acda82013-06-25 20:52:50426 IndexedDBContextImpl* indexed_db_context_impl =
427 static_cast<IndexedDBContextImpl*>(
[email protected]b1b502e2012-09-16 07:31:43428 storage_partition->GetIndexedDBContext());
[email protected]89acda82013-06-25 20:52:50429 // No task runner in unit tests.
430 if (indexed_db_context_impl->TaskRunner()) {
431 indexed_db_context_impl->TaskRunner()->PostTask(
kylecharda69d882017-10-04 05:49:52432 FROM_HERE,
433 base::BindOnce(&SaveSessionStateOnIndexedDBThread,
434 base::WrapRefCounted(indexed_db_context_impl)));
[email protected]6e2d3d22012-02-24 18:10:36435 }
436}
437
ttr31481dc54b2015-08-06 20:11:26438void BrowserContext::SetDownloadManagerForTesting(
439 BrowserContext* browser_context,
avi1ed4a4372017-04-25 05:39:41440 std::unique_ptr<content::DownloadManager> download_manager) {
441 SetDownloadManager(browser_context, std::move(download_manager));
ttr31481dc54b2015-08-06 20:11:26442}
443
rockot963ad3e82016-04-13 16:33:23444// static
erge69130f52016-03-02 00:13:28445void BrowserContext::Initialize(
446 BrowserContext* browser_context,
447 const base::FilePath& path) {
ben6c85c4492016-06-16 20:40:51448
449 std::string new_id;
450 if (GetContentClient() && GetContentClient()->browser()) {
bend32292b2016-10-07 00:21:58451 new_id = GetContentClient()->browser()->GetServiceUserIdForBrowserContext(
ben6c85c4492016-06-16 20:40:51452 browser_context);
453 } else {
454 // Some test scenarios initialize a BrowserContext without a content client.
bencccfe2a2016-03-05 16:54:14455 new_id = base::GenerateGUID();
ben6c85c4492016-06-16 20:40:51456 }
erge69130f52016-03-02 00:13:28457
bend32292b2016-10-07 00:21:58458 ServiceUserIdHolder* holder = static_cast<ServiceUserIdHolder*>(
459 browser_context->GetUserData(kServiceUserId));
ben6c85c4492016-06-16 20:40:51460 if (holder)
bend32292b2016-10-07 00:21:58461 file::ForgetServiceUserIdUserDirAssociation(holder->user_id());
462 file::AssociateServiceUserIdWithUserDir(new_id, path);
ben6c85c4492016-06-16 20:40:51463 RemoveBrowserContextFromUserIdMap(browser_context);
464 g_user_id_to_context.Get()[new_id] = browser_context;
bend32292b2016-10-07 00:21:58465 browser_context->SetUserData(kServiceUserId,
Jeremy Roman04f27c372017-10-27 15:20:55466 std::make_unique<ServiceUserIdHolder>(new_id));
erge69130f52016-03-02 00:13:28467
avi1ed4a4372017-04-25 05:39:41468 browser_context->SetUserData(
Jeremy Roman04f27c372017-10-27 15:20:55469 kMojoWasInitialized, std::make_unique<base::SupportsUserData::Data>());
rockot963ad3e82016-04-13 16:33:23470
bend32292b2016-10-07 00:21:58471 ServiceManagerConnection* service_manager_connection =
472 ServiceManagerConnection::GetForProcess();
fdoray4f155f02016-10-12 11:28:50473 if (service_manager_connection && base::ThreadTaskRunnerHandle::IsSet()) {
rockot963ad3e82016-04-13 16:33:23474 // NOTE: Many unit tests create a TestBrowserContext without initializing
bend32292b2016-10-07 00:21:58475 // Mojo or the global service manager connection.
rockot963ad3e82016-04-13 16:33:23476
rockot400ea35b2016-10-15 19:15:32477 service_manager::mojom::ServicePtr service;
Ken Rockotf4d8a942017-05-13 00:10:37478 auto service_request = mojo::MakeRequest(&service);
rockot963ad3e82016-04-13 16:33:23479
rockot400ea35b2016-10-15 19:15:32480 service_manager::mojom::PIDReceiverPtr pid_receiver;
bene6a9f012017-01-07 00:43:43481 service_manager::Identity identity(mojom::kBrowserServiceName, new_id);
benbd3c2482017-01-07 05:48:21482 service_manager_connection->GetConnector()->StartService(
bene6a9f012017-01-07 00:43:43483 identity, std::move(service), mojo::MakeRequest(&pid_receiver));
rockot963ad3e82016-04-13 16:33:23484 pid_receiver->SetPID(base::GetCurrentProcId());
485
benaad37ecd2017-04-12 22:08:20486 service_manager_connection->GetConnector()->StartService(identity);
bend32292b2016-10-07 00:21:58487 BrowserContextServiceManagerConnectionHolder* connection_holder =
488 new BrowserContextServiceManagerConnectionHolder(
bend32292b2016-10-07 00:21:58489 std::move(service_request));
avi1ed4a4372017-04-25 05:39:41490 browser_context->SetUserData(kServiceManagerConnection,
491 base::WrapUnique(connection_holder));
rockot963ad3e82016-04-13 16:33:23492
bend32292b2016-10-07 00:21:58493 ServiceManagerConnection* connection =
494 connection_holder->service_manager_connection();
rockot963ad3e82016-04-13 16:33:23495
ben146248de2016-06-14 15:24:59496 // New embedded service factories should be added to |connection| here.
rockot963ad3e82016-04-13 16:33:23497
mekc8e2b272017-06-06 23:18:28498 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
499 switches::kDisableMojoLocalStorage)) {
Colin Blundell812ec6802017-06-27 15:50:49500 service_manager::EmbeddedServiceInfo info;
Marijn Kruisselbrinkcbc3df82017-07-06 19:25:59501 info.factory = base::Bind(&file::CreateFileService);
benaabad0b2016-11-16 23:54:17502 connection->AddEmbeddedService(file::mojom::kServiceName, info);
rockot963ad3e82016-04-13 16:33:23503 }
tibellab3d36b2017-03-10 02:57:25504
505 ContentBrowserClient::StaticServiceMap services;
506 browser_context->RegisterInProcessServices(&services);
507 for (const auto& entry : services) {
508 connection->AddEmbeddedService(entry.first, entry.second);
509 }
Ken Rockotc7a279c2017-05-04 23:51:45510
511 RegisterCommonBrowserInterfaces(connection);
sammc030f5012017-03-13 02:28:45512 connection->Start();
rockot963ad3e82016-04-13 16:33:23513 }
erge69130f52016-03-02 00:13:28514}
515
rockot963ad3e82016-04-13 16:33:23516// static
bend32292b2016-10-07 00:21:58517const std::string& BrowserContext::GetServiceUserIdFor(
bencccfe2a2016-03-05 16:54:14518 BrowserContext* browser_context) {
erge69130f52016-03-02 00:13:28519 CHECK(browser_context->GetUserData(kMojoWasInitialized))
520 << "Attempting to get the mojo user id for a BrowserContext that was "
521 << "never Initialize()ed.";
522
bend32292b2016-10-07 00:21:58523 ServiceUserIdHolder* holder = static_cast<ServiceUserIdHolder*>(
524 browser_context->GetUserData(kServiceUserId));
ben6c85c4492016-06-16 20:40:51525 return holder->user_id();
erge69130f52016-03-02 00:13:28526}
527
rockot963ad3e82016-04-13 16:33:23528// static
bend32292b2016-10-07 00:21:58529BrowserContext* BrowserContext::GetBrowserContextForServiceUserId(
ben6c85c4492016-06-16 20:40:51530 const std::string& user_id) {
531 auto it = g_user_id_to_context.Get().find(user_id);
532 return it != g_user_id_to_context.Get().end() ? it->second : nullptr;
533}
534
535// static
rockot400ea35b2016-10-15 19:15:32536service_manager::Connector* BrowserContext::GetConnectorFor(
rockot963ad3e82016-04-13 16:33:23537 BrowserContext* browser_context) {
bend32292b2016-10-07 00:21:58538 ServiceManagerConnection* connection =
539 GetServiceManagerConnectionFor(browser_context);
ben5be0b9132016-08-03 00:17:18540 return connection ? connection->GetConnector() : nullptr;
541}
542
543// static
bend32292b2016-10-07 00:21:58544ServiceManagerConnection* BrowserContext::GetServiceManagerConnectionFor(
ben5be0b9132016-08-03 00:17:18545 BrowserContext* browser_context) {
bend32292b2016-10-07 00:21:58546 BrowserContextServiceManagerConnectionHolder* connection_holder =
547 static_cast<BrowserContextServiceManagerConnectionHolder*>(
548 browser_context->GetUserData(kServiceManagerConnection));
549 return connection_holder ? connection_holder->service_manager_connection()
550 : nullptr;
rockot963ad3e82016-04-13 16:33:23551}
552
mmenkec0b2b8b12017-04-21 16:27:52553BrowserContext::BrowserContext()
554 : media_device_id_salt_(CreateRandomMediaDeviceIDSalt()) {}
555
[email protected]55eb70e762012-02-20 17:38:39556BrowserContext::~BrowserContext() {
erge69130f52016-03-02 00:13:28557 CHECK(GetUserData(kMojoWasInitialized))
558 << "Attempting to destroy a BrowserContext that never called "
559 << "Initialize()";
560
kinukof6ed359c2016-07-26 13:27:21561 DCHECK(!GetUserData(kStoragePartitionMapKeyName))
562 << "StoragePartitionMap is not shut down properly";
563
ben6c85c4492016-06-16 20:40:51564 RemoveBrowserContextFromUserIdMap(this);
565
[email protected]b441a8492012-06-06 14:55:57566 if (GetUserData(kDownloadManagerKeyName))
567 GetDownloadManager(this)->Shutdown();
[email protected]55eb70e762012-02-20 17:38:39568}
569
kinukof6ed359c2016-07-26 13:27:21570void BrowserContext::ShutdownStoragePartitions() {
571 if (GetUserData(kStoragePartitionMapKeyName))
572 RemoveUserData(kStoragePartitionMapKeyName);
573}
574
mmenkec0b2b8b12017-04-21 16:27:52575std::string BrowserContext::GetMediaDeviceIDSalt() {
576 return media_device_id_salt_;
577}
578
579// static
580std::string BrowserContext::CreateRandomMediaDeviceIDSalt() {
581 std::string salt;
582 base::Base64Encode(base::RandBytesAsString(16), &salt);
583 DCHECK(!salt.empty());
584 return salt;
585}
586
Chris Cunninghama5b38012017-10-28 07:27:18587media::VideoDecodePerfHistory* BrowserContext::GetVideoDecodePerfHistory() {
588 media::VideoDecodePerfHistory* decode_history =
589 static_cast<media::VideoDecodePerfHistory*>(
590 GetUserData(kVideoDecodePerfHistoryId));
591
592 // Lazily created. Note, this does not trigger loading the DB from disk. That
593 // occurs later upon first VideoDecodePerfHistory API request that requires DB
594 // access. DB operations will not block the UI thread.
595 if (!decode_history) {
596 auto db_factory = std::make_unique<media::VideoDecodeStatsDBImplFactory>(
597 GetPath().Append(FILE_PATH_LITERAL("VideoDecodeStats")));
598 decode_history = new media::VideoDecodePerfHistory(std::move(db_factory));
599 SetUserData(kVideoDecodePerfHistoryId, base::WrapUnique(decode_history));
600 }
601
602 return decode_history;
603}
604
[email protected]55eb70e762012-02-20 17:38:39605} // namespace content