blob: fc3f644fba214656908804ba9084f8770210ac99 [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>
dcheng36b6aec92015-12-26 06:16:369#include <utility>
avib7348942015-12-25 20:57:1010
11#include "build/build_config.h"
12
[email protected]e0ce8a1e2012-09-18 10:26:3613#if !defined(OS_IOS)
[email protected]b441a8492012-06-06 14:55:5714#include "content/browser/download/download_manager_impl.h"
[email protected]393b6cb2014-05-15 00:55:1215#include "content/browser/fileapi/chrome_blob_storage_context.h"
[email protected]c4d281662013-03-31 00:35:0816#include "content/browser/indexed_db/indexed_db_context_impl.h"
[email protected]678c0362012-12-05 08:02:4417#include "content/browser/loader/resource_dispatcher_host_impl.h"
mvanouwerkerk17205ea2014-11-07 17:30:1518#include "content/browser/push_messaging/push_messaging_router.h"
[email protected]4c3a23582012-08-18 08:54:3419#include "content/browser/storage_partition_impl_map.h"
[email protected]d7c7c98a2012-07-12 21:27:4420#include "content/common/child_process_host_impl.h"
[email protected]393b6cb2014-05-15 00:55:1221#include "content/public/browser/blob_handle.h"
[email protected]55eb70e762012-02-20 17:38:3922#include "content/public/browser/browser_thread.h"
[email protected]b441a8492012-06-06 14:55:5723#include "content/public/browser/content_browser_client.h"
[email protected]536fd0b2013-03-14 17:41:5724#include "content/public/browser/site_instance.h"
[email protected]4d7c4ef2012-03-16 01:47:1225#include "net/cookies/cookie_monster.h"
26#include "net/cookies/cookie_store.h"
[email protected]6b8a3c742014-07-25 00:25:3527#include "net/ssl/channel_id_service.h"
28#include "net/ssl/channel_id_store.h"
[email protected]6e2d3d22012-02-24 18:10:3629#include "net/url_request/url_request_context.h"
[email protected]6939075a2012-08-28 08:35:5330#include "net/url_request/url_request_context_getter.h"
pilgrime92c5fcd2014-09-10 23:31:2331#include "storage/browser/database/database_tracker.h"
32#include "storage/browser/fileapi/external_mount_points.h"
[email protected]e0ce8a1e2012-09-18 10:26:3633#endif // !OS_IOS
[email protected]55eb70e762012-02-20 17:38:3934
[email protected]314c3e22012-02-21 03:57:4235using base::UserDataAdapter;
[email protected]55eb70e762012-02-20 17:38:3936
[email protected]55eb70e762012-02-20 17:38:3937namespace content {
38
[email protected]e0ce8a1e2012-09-18 10:26:3639// Only ~BrowserContext() is needed on iOS.
40#if !defined(OS_IOS)
[email protected]735e20c2012-03-20 01:16:5941namespace {
42
[email protected]e0ce8a1e2012-09-18 10:26:3643// Key names on BrowserContext.
[email protected]6ef0c3912013-01-25 22:46:3444const char kDownloadManagerKeyName[] = "download_manager";
a.cavalcantiffab73762015-08-15 02:55:4845const char kStoragePartitionMapKeyName[] = "content_storage_partition_map";
[email protected]e0ce8a1e2012-09-18 10:26:3646
[email protected]9afc14e22013-09-25 22:34:1447#if defined(OS_CHROMEOS)
48const char kMountPointsKey[] = "mount_points";
49#endif // defined(OS_CHROMEOS)
50
[email protected]14acc642012-11-17 12:20:1051StoragePartitionImplMap* GetStoragePartitionMap(
52 BrowserContext* browser_context) {
[email protected]4c3a23582012-08-18 08:54:3453 StoragePartitionImplMap* partition_map =
54 static_cast<StoragePartitionImplMap*>(
a.cavalcantiffab73762015-08-15 02:55:4855 browser_context->GetUserData(kStoragePartitionMapKeyName));
[email protected]d7c7c98a2012-07-12 21:27:4456 if (!partition_map) {
[email protected]4c3a23582012-08-18 08:54:3457 partition_map = new StoragePartitionImplMap(browser_context);
a.cavalcantiffab73762015-08-15 02:55:4858 browser_context->SetUserData(kStoragePartitionMapKeyName, partition_map);
[email protected]d7c7c98a2012-07-12 21:27:4459 }
[email protected]14acc642012-11-17 12:20:1060 return partition_map;
61}
62
63StoragePartition* GetStoragePartitionFromConfig(
64 BrowserContext* browser_context,
65 const std::string& partition_domain,
66 const std::string& partition_name,
67 bool in_memory) {
68 StoragePartitionImplMap* partition_map =
69 GetStoragePartitionMap(browser_context);
[email protected]d7c7c98a2012-07-12 21:27:4470
[email protected]1bc28312012-11-08 08:31:5371 if (browser_context->IsOffTheRecord())
72 in_memory = true;
73
[email protected]1bc28312012-11-08 08:31:5374 return partition_map->Get(partition_domain, partition_name, in_memory);
[email protected]d1198fd2012-08-13 22:50:1975}
76
[email protected]6939075a2012-08-28 08:35:5377void SaveSessionStateOnIOThread(
78 const scoped_refptr<net::URLRequestContextGetter>& context_getter,
[email protected]98d6d4562014-06-25 20:57:5579 AppCacheServiceImpl* appcache_service) {
[email protected]6939075a2012-08-28 08:35:5380 net::URLRequestContext* context = context_getter->GetURLRequestContext();
81 context->cookie_store()->GetCookieMonster()->
[email protected]bf510ed2012-06-05 08:31:4382 SetForceKeepSessionState();
[email protected]6b8a3c742014-07-25 00:25:3583 context->channel_id_service()->GetChannelIDStore()->
[email protected]6939075a2012-08-28 08:35:5384 SetForceKeepSessionState();
85 appcache_service->set_force_keep_session_state();
[email protected]6e2d3d22012-02-24 18:10:3686}
87
[email protected]89acda82013-06-25 20:52:5088void SaveSessionStateOnIndexedDBThread(
[email protected]6e2d3d22012-02-24 18:10:3689 scoped_refptr<IndexedDBContextImpl> indexed_db_context) {
[email protected]bf510ed2012-06-05 08:31:4390 indexed_db_context->SetForceKeepSessionState();
[email protected]6e2d3d22012-02-24 18:10:3691}
92
falken41f4175162014-10-29 07:03:4193void ShutdownServiceWorkerContext(StoragePartition* partition) {
94 ServiceWorkerContextWrapper* wrapper =
95 static_cast<ServiceWorkerContextWrapper*>(
96 partition->GetServiceWorkerContext());
97 wrapper->process_manager()->Shutdown();
98}
99
ttr31481dc54b2015-08-06 20:11:26100void SetDownloadManager(BrowserContext* context,
101 content::DownloadManager* download_manager) {
102 DCHECK_CURRENTLY_ON(BrowserThread::UI);
103 DCHECK(download_manager);
104 context->SetUserData(kDownloadManagerKeyName, download_manager);
105}
106
[email protected]735e20c2012-03-20 01:16:59107} // namespace
108
[email protected]14acc642012-11-17 12:20:10109// static
110void BrowserContext::AsyncObliterateStoragePartition(
111 BrowserContext* browser_context,
[email protected]399583b2012-12-11 09:33:42112 const GURL& site,
113 const base::Closure& on_gc_required) {
114 GetStoragePartitionMap(browser_context)->AsyncObliterate(site,
115 on_gc_required);
116}
117
118// static
119void BrowserContext::GarbageCollectStoragePartitions(
120 BrowserContext* browser_context,
[email protected]2dec8ec2013-02-07 19:20:34121 scoped_ptr<base::hash_set<base::FilePath> > active_paths,
[email protected]399583b2012-12-11 09:33:42122 const base::Closure& done) {
dcheng36b6aec92015-12-26 06:16:36123 GetStoragePartitionMap(browser_context)
124 ->GarbageCollect(std::move(active_paths), done);
[email protected]14acc642012-11-17 12:20:10125}
126
[email protected]b441a8492012-06-06 14:55:57127DownloadManager* BrowserContext::GetDownloadManager(
128 BrowserContext* context) {
mostynbfbcdc27a2015-03-13 17:58:52129 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]b441a8492012-06-06 14:55:57130 if (!context->GetUserData(kDownloadManagerKeyName)) {
[email protected]eba4a4d2013-05-29 02:18:06131 DownloadManager* download_manager =
[email protected]d25fda12012-06-12 17:05:03132 new DownloadManagerImpl(
[email protected]16798692013-04-23 18:08:38133 GetContentClient()->browser()->GetNetLog(), context);
[email protected]d25fda12012-06-12 17:05:03134
ttr31481dc54b2015-08-06 20:11:26135 SetDownloadManager(context, download_manager);
[email protected]b441a8492012-06-06 14:55:57136 download_manager->SetDelegate(context->GetDownloadManagerDelegate());
[email protected]b441a8492012-06-06 14:55:57137 }
138
[email protected]eba4a4d2013-05-29 02:18:06139 return static_cast<DownloadManager*>(
140 context->GetUserData(kDownloadManagerKeyName));
[email protected]b441a8492012-06-06 14:55:57141}
142
[email protected]6ef0c3912013-01-25 22:46:34143// static
[email protected]cd501a72014-08-22 19:58:31144storage::ExternalMountPoints* BrowserContext::GetMountPoints(
[email protected]6ef0c3912013-01-25 22:46:34145 BrowserContext* context) {
146 // Ensure that these methods are called on the UI thread, except for
147 // unittests where a UI thread might not have been created.
148 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) ||
149 !BrowserThread::IsMessageLoopValid(BrowserThread::UI));
150
151#if defined(OS_CHROMEOS)
152 if (!context->GetUserData(kMountPointsKey)) {
[email protected]cd501a72014-08-22 19:58:31153 scoped_refptr<storage::ExternalMountPoints> mount_points =
154 storage::ExternalMountPoints::CreateRefCounted();
[email protected]6ef0c3912013-01-25 22:46:34155 context->SetUserData(
156 kMountPointsKey,
[email protected]cd501a72014-08-22 19:58:31157 new UserDataAdapter<storage::ExternalMountPoints>(mount_points.get()));
[email protected]6ef0c3912013-01-25 22:46:34158 }
159
[email protected]cd501a72014-08-22 19:58:31160 return UserDataAdapter<storage::ExternalMountPoints>::Get(context,
161 kMountPointsKey);
[email protected]6ef0c3912013-01-25 22:46:34162#else
163 return NULL;
164#endif
165}
166
[email protected]4c3a23582012-08-18 08:54:34167StoragePartition* BrowserContext::GetStoragePartition(
168 BrowserContext* browser_context,
169 SiteInstance* site_instance) {
[email protected]1bc28312012-11-08 08:31:53170 std::string partition_domain;
171 std::string partition_name;
172 bool in_memory = false;
[email protected]4c3a23582012-08-18 08:54:34173
174 // TODO(ajwong): After GetDefaultStoragePartition() is removed, get rid of
175 // this conditional and require that |site_instance| is non-NULL.
176 if (site_instance) {
[email protected]1bc28312012-11-08 08:31:53177 GetContentClient()->browser()->GetStoragePartitionConfigForSite(
[email protected]14acc642012-11-17 12:20:10178 browser_context, site_instance->GetSiteURL(), true,
[email protected]1bc28312012-11-08 08:31:53179 &partition_domain, &partition_name, &in_memory);
[email protected]4c3a23582012-08-18 08:54:34180 }
181
[email protected]1bc28312012-11-08 08:31:53182 return GetStoragePartitionFromConfig(
183 browser_context, partition_domain, partition_name, in_memory);
[email protected]4c3a23582012-08-18 08:54:34184}
185
[email protected]e94bbcb2012-09-07 05:33:57186StoragePartition* BrowserContext::GetStoragePartitionForSite(
187 BrowserContext* browser_context,
188 const GURL& site) {
[email protected]1bc28312012-11-08 08:31:53189 std::string partition_domain;
190 std::string partition_name;
191 bool in_memory;
[email protected]e94bbcb2012-09-07 05:33:57192
[email protected]1bc28312012-11-08 08:31:53193 GetContentClient()->browser()->GetStoragePartitionConfigForSite(
[email protected]14acc642012-11-17 12:20:10194 browser_context, site, true, &partition_domain, &partition_name,
195 &in_memory);
[email protected]1bc28312012-11-08 08:31:53196
197 return GetStoragePartitionFromConfig(
198 browser_context, partition_domain, partition_name, in_memory);
[email protected]e94bbcb2012-09-07 05:33:57199}
200
[email protected]4c3a23582012-08-18 08:54:34201void BrowserContext::ForEachStoragePartition(
202 BrowserContext* browser_context,
203 const StoragePartitionCallback& callback) {
204 StoragePartitionImplMap* partition_map =
205 static_cast<StoragePartitionImplMap*>(
a.cavalcantiffab73762015-08-15 02:55:48206 browser_context->GetUserData(kStoragePartitionMapKeyName));
[email protected]4c3a23582012-08-18 08:54:34207 if (!partition_map)
208 return;
209
210 partition_map->ForEach(callback);
211}
212
213StoragePartition* BrowserContext::GetDefaultStoragePartition(
214 BrowserContext* browser_context) {
215 return GetStoragePartition(browser_context, NULL);
[email protected]55eb70e762012-02-20 17:38:39216}
217
tbarzicdb712682015-03-06 06:05:41218// static
[email protected]393b6cb2014-05-15 00:55:12219void BrowserContext::CreateMemoryBackedBlob(BrowserContext* browser_context,
220 const char* data, size_t length,
221 const BlobCallback& callback) {
mostynbfbcdc27a2015-03-13 17:58:52222 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]393b6cb2014-05-15 00:55:12223
224 ChromeBlobStorageContext* blob_context =
225 ChromeBlobStorageContext::GetFor(browser_context);
226 BrowserThread::PostTaskAndReplyWithResult(
227 BrowserThread::IO, FROM_HERE,
228 base::Bind(&ChromeBlobStorageContext::CreateMemoryBackedBlob,
229 make_scoped_refptr(blob_context), data, length),
230 callback);
231}
232
[email protected]66e53d0282014-08-07 10:04:35233// static
tbarzicdb712682015-03-06 06:05:41234void BrowserContext::CreateFileBackedBlob(
235 BrowserContext* browser_context,
236 const base::FilePath& path,
237 int64_t offset,
238 int64_t size,
239 const base::Time& expected_modification_time,
240 const BlobCallback& callback) {
mostynbfbcdc27a2015-03-13 17:58:52241 DCHECK_CURRENTLY_ON(BrowserThread::UI);
tbarzicdb712682015-03-06 06:05:41242
243 ChromeBlobStorageContext* blob_context =
244 ChromeBlobStorageContext::GetFor(browser_context);
245 BrowserThread::PostTaskAndReplyWithResult(
246 BrowserThread::IO, FROM_HERE,
247 base::Bind(&ChromeBlobStorageContext::CreateFileBackedBlob,
248 make_scoped_refptr(blob_context), path, offset, size,
249 expected_modification_time),
250 callback);
251}
252
253// static
[email protected]66e53d0282014-08-07 10:04:35254void BrowserContext::DeliverPushMessage(
255 BrowserContext* browser_context,
256 const GURL& origin,
avib7348942015-12-25 20:57:10257 int64_t service_worker_registration_id,
[email protected]66e53d0282014-08-07 10:04:35258 const std::string& data,
johnmea80c2552014-10-17 14:51:40259 const base::Callback<void(PushDeliveryStatus)>& callback) {
mostynbfbcdc27a2015-03-13 17:58:52260 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]66e53d0282014-08-07 10:04:35261 PushMessagingRouter::DeliverMessage(
262 browser_context, origin, service_worker_registration_id, data, callback);
263}
264
falken41f4175162014-10-29 07:03:41265// static
266void BrowserContext::NotifyWillBeDestroyed(BrowserContext* browser_context) {
267 // Service Workers must shutdown before the browser context is destroyed,
268 // since they keep render process hosts alive and the codebase assumes that
269 // render process hosts die before their profile (browser context) dies.
270 ForEachStoragePartition(browser_context,
271 base::Bind(ShutdownServiceWorkerContext));
272}
273
[email protected]314c3e22012-02-21 03:57:42274void BrowserContext::EnsureResourceContextInitialized(BrowserContext* context) {
[email protected]7e26ac92012-02-27 20:15:05275 // This will be enough to tickle initialization of BrowserContext if
276 // necessary, which initializes ResourceContext. The reason we don't call
[email protected]4c3a23582012-08-18 08:54:34277 // ResourceContext::InitializeResourceContext() directly here is that
278 // ResourceContext initialization may call back into BrowserContext
279 // and when that call returns it'll end rewriting its UserData map. It will
280 // end up rewriting the same value but this still causes a race condition.
281 //
282 // See http://crbug.com/115678.
283 GetDefaultStoragePartition(context);
[email protected]55eb70e762012-02-20 17:38:39284}
285
[email protected]6e2d3d22012-02-24 18:10:36286void BrowserContext::SaveSessionState(BrowserContext* browser_context) {
[email protected]5c8e67c2012-08-29 00:48:52287 GetDefaultStoragePartition(browser_context)->GetDatabaseTracker()->
288 SetForceKeepSessionState();
[email protected]b1b502e2012-09-16 07:31:43289 StoragePartition* storage_partition =
290 BrowserContext::GetDefaultStoragePartition(browser_context);
[email protected]6e2d3d22012-02-24 18:10:36291
292 if (BrowserThread::IsMessageLoopValid(BrowserThread::IO)) {
293 BrowserThread::PostTask(
294 BrowserThread::IO, FROM_HERE,
[email protected]6939075a2012-08-28 08:35:53295 base::Bind(
296 &SaveSessionStateOnIOThread,
297 make_scoped_refptr(browser_context->GetRequestContext()),
[email protected]98d6d4562014-06-25 20:57:55298 static_cast<AppCacheServiceImpl*>(
[email protected]63ef85512014-06-05 14:21:26299 storage_partition->GetAppCacheService())));
[email protected]6e2d3d22012-02-24 18:10:36300 }
301
[email protected]5f2aa722013-08-07 16:59:41302 DOMStorageContextWrapper* dom_storage_context_proxy =
303 static_cast<DOMStorageContextWrapper*>(
[email protected]b1b502e2012-09-16 07:31:43304 storage_partition->GetDOMStorageContext());
[email protected]5f2aa722013-08-07 16:59:41305 dom_storage_context_proxy->SetForceKeepSessionState();
[email protected]735e20c2012-03-20 01:16:59306
[email protected]89acda82013-06-25 20:52:50307 IndexedDBContextImpl* indexed_db_context_impl =
308 static_cast<IndexedDBContextImpl*>(
[email protected]b1b502e2012-09-16 07:31:43309 storage_partition->GetIndexedDBContext());
[email protected]89acda82013-06-25 20:52:50310 // No task runner in unit tests.
311 if (indexed_db_context_impl->TaskRunner()) {
312 indexed_db_context_impl->TaskRunner()->PostTask(
313 FROM_HERE,
314 base::Bind(&SaveSessionStateOnIndexedDBThread,
315 make_scoped_refptr(indexed_db_context_impl)));
[email protected]6e2d3d22012-02-24 18:10:36316 }
317}
318
ttr31481dc54b2015-08-06 20:11:26319void BrowserContext::SetDownloadManagerForTesting(
320 BrowserContext* browser_context,
321 DownloadManager* download_manager) {
322 SetDownloadManager(browser_context, download_manager);
323}
324
[email protected]e0ce8a1e2012-09-18 10:26:36325#endif // !OS_IOS
[email protected]6e2d3d22012-02-24 18:10:36326
[email protected]55eb70e762012-02-20 17:38:39327BrowserContext::~BrowserContext() {
[email protected]e0ce8a1e2012-09-18 10:26:36328#if !defined(OS_IOS)
[email protected]b441a8492012-06-06 14:55:57329 if (GetUserData(kDownloadManagerKeyName))
330 GetDownloadManager(this)->Shutdown();
[email protected]e0ce8a1e2012-09-18 10:26:36331#endif
[email protected]55eb70e762012-02-20 17:38:39332}
333
[email protected]55eb70e762012-02-20 17:38:39334} // namespace content