blob: 9bccfeb25527a78948b6fcd7272ce0f6da601ebc [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
[email protected]e0ce8a1e2012-09-18 10:26:367#if !defined(OS_IOS)
[email protected]b441a8492012-06-06 14:55:578#include "content/browser/download/download_manager_impl.h"
[email protected]393b6cb2014-05-15 00:55:129#include "content/browser/fileapi/chrome_blob_storage_context.h"
[email protected]c4d281662013-03-31 00:35:0810#include "content/browser/indexed_db/indexed_db_context_impl.h"
[email protected]678c0362012-12-05 08:02:4411#include "content/browser/loader/resource_dispatcher_host_impl.h"
[email protected]66e53d0282014-08-07 10:04:3512#include "content/browser/push_messaging_router.h"
[email protected]4c3a23582012-08-18 08:54:3413#include "content/browser/storage_partition_impl_map.h"
[email protected]d7c7c98a2012-07-12 21:27:4414#include "content/common/child_process_host_impl.h"
[email protected]393b6cb2014-05-15 00:55:1215#include "content/public/browser/blob_handle.h"
[email protected]55eb70e762012-02-20 17:38:3916#include "content/public/browser/browser_thread.h"
[email protected]b441a8492012-06-06 14:55:5717#include "content/public/browser/content_browser_client.h"
[email protected]536fd0b2013-03-14 17:41:5718#include "content/public/browser/site_instance.h"
[email protected]4d7c4ef2012-03-16 01:47:1219#include "net/cookies/cookie_monster.h"
20#include "net/cookies/cookie_store.h"
[email protected]6b8a3c742014-07-25 00:25:3521#include "net/ssl/channel_id_service.h"
22#include "net/ssl/channel_id_store.h"
[email protected]6e2d3d22012-02-24 18:10:3623#include "net/url_request/url_request_context.h"
[email protected]6939075a2012-08-28 08:35:5324#include "net/url_request/url_request_context_getter.h"
[email protected]08b1f75f2013-05-22 22:02:3825#include "webkit/browser/database/database_tracker.h"
[email protected]f25e1132013-05-24 13:58:0426#include "webkit/browser/fileapi/external_mount_points.h"
[email protected]e0ce8a1e2012-09-18 10:26:3627#endif // !OS_IOS
[email protected]55eb70e762012-02-20 17:38:3928
[email protected]314c3e22012-02-21 03:57:4229using base::UserDataAdapter;
[email protected]55eb70e762012-02-20 17:38:3930
[email protected]55eb70e762012-02-20 17:38:3931namespace content {
32
[email protected]e0ce8a1e2012-09-18 10:26:3633// Only ~BrowserContext() is needed on iOS.
34#if !defined(OS_IOS)
[email protected]735e20c2012-03-20 01:16:5935namespace {
36
[email protected]e0ce8a1e2012-09-18 10:26:3637// Key names on BrowserContext.
[email protected]6ef0c3912013-01-25 22:46:3438const char kDownloadManagerKeyName[] = "download_manager";
[email protected]6ef0c3912013-01-25 22:46:3439const char kStorageParitionMapKeyName[] = "content_storage_partition_map";
[email protected]e0ce8a1e2012-09-18 10:26:3640
[email protected]9afc14e22013-09-25 22:34:1441#if defined(OS_CHROMEOS)
42const char kMountPointsKey[] = "mount_points";
43#endif // defined(OS_CHROMEOS)
44
[email protected]14acc642012-11-17 12:20:1045StoragePartitionImplMap* GetStoragePartitionMap(
46 BrowserContext* browser_context) {
[email protected]4c3a23582012-08-18 08:54:3447 StoragePartitionImplMap* partition_map =
48 static_cast<StoragePartitionImplMap*>(
49 browser_context->GetUserData(kStorageParitionMapKeyName));
[email protected]d7c7c98a2012-07-12 21:27:4450 if (!partition_map) {
[email protected]4c3a23582012-08-18 08:54:3451 partition_map = new StoragePartitionImplMap(browser_context);
[email protected]d7c7c98a2012-07-12 21:27:4452 browser_context->SetUserData(kStorageParitionMapKeyName, partition_map);
53 }
[email protected]14acc642012-11-17 12:20:1054 return partition_map;
55}
56
57StoragePartition* GetStoragePartitionFromConfig(
58 BrowserContext* browser_context,
59 const std::string& partition_domain,
60 const std::string& partition_name,
61 bool in_memory) {
62 StoragePartitionImplMap* partition_map =
63 GetStoragePartitionMap(browser_context);
[email protected]d7c7c98a2012-07-12 21:27:4464
[email protected]1bc28312012-11-08 08:31:5365 if (browser_context->IsOffTheRecord())
66 in_memory = true;
67
[email protected]1bc28312012-11-08 08:31:5368 return partition_map->Get(partition_domain, partition_name, in_memory);
[email protected]d1198fd2012-08-13 22:50:1969}
70
[email protected]6939075a2012-08-28 08:35:5371void SaveSessionStateOnIOThread(
72 const scoped_refptr<net::URLRequestContextGetter>& context_getter,
[email protected]98d6d4562014-06-25 20:57:5573 AppCacheServiceImpl* appcache_service) {
[email protected]6939075a2012-08-28 08:35:5374 net::URLRequestContext* context = context_getter->GetURLRequestContext();
75 context->cookie_store()->GetCookieMonster()->
[email protected]bf510ed2012-06-05 08:31:4376 SetForceKeepSessionState();
[email protected]6b8a3c742014-07-25 00:25:3577 context->channel_id_service()->GetChannelIDStore()->
[email protected]6939075a2012-08-28 08:35:5378 SetForceKeepSessionState();
79 appcache_service->set_force_keep_session_state();
[email protected]6e2d3d22012-02-24 18:10:3680}
81
[email protected]89acda82013-06-25 20:52:5082void SaveSessionStateOnIndexedDBThread(
[email protected]6e2d3d22012-02-24 18:10:3683 scoped_refptr<IndexedDBContextImpl> indexed_db_context) {
[email protected]bf510ed2012-06-05 08:31:4384 indexed_db_context->SetForceKeepSessionState();
[email protected]6e2d3d22012-02-24 18:10:3685}
86
[email protected]735e20c2012-03-20 01:16:5987} // namespace
88
[email protected]14acc642012-11-17 12:20:1089// static
90void BrowserContext::AsyncObliterateStoragePartition(
91 BrowserContext* browser_context,
[email protected]399583b2012-12-11 09:33:4292 const GURL& site,
93 const base::Closure& on_gc_required) {
94 GetStoragePartitionMap(browser_context)->AsyncObliterate(site,
95 on_gc_required);
96}
97
98// static
99void BrowserContext::GarbageCollectStoragePartitions(
100 BrowserContext* browser_context,
[email protected]2dec8ec2013-02-07 19:20:34101 scoped_ptr<base::hash_set<base::FilePath> > active_paths,
[email protected]399583b2012-12-11 09:33:42102 const base::Closure& done) {
103 GetStoragePartitionMap(browser_context)->GarbageCollect(
104 active_paths.Pass(), done);
[email protected]14acc642012-11-17 12:20:10105}
106
[email protected]b441a8492012-06-06 14:55:57107DownloadManager* BrowserContext::GetDownloadManager(
108 BrowserContext* context) {
[email protected]ecd3ad22012-07-10 20:02:40109 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]b441a8492012-06-06 14:55:57110 if (!context->GetUserData(kDownloadManagerKeyName)) {
[email protected]d25fda12012-06-12 17:05:03111 ResourceDispatcherHostImpl* rdh = ResourceDispatcherHostImpl::Get();
112 DCHECK(rdh);
[email protected]eba4a4d2013-05-29 02:18:06113 DownloadManager* download_manager =
[email protected]d25fda12012-06-12 17:05:03114 new DownloadManagerImpl(
[email protected]16798692013-04-23 18:08:38115 GetContentClient()->browser()->GetNetLog(), context);
[email protected]d25fda12012-06-12 17:05:03116
[email protected]b441a8492012-06-06 14:55:57117 context->SetUserData(
118 kDownloadManagerKeyName,
[email protected]eba4a4d2013-05-29 02:18:06119 download_manager);
[email protected]b441a8492012-06-06 14:55:57120 download_manager->SetDelegate(context->GetDownloadManagerDelegate());
[email protected]b441a8492012-06-06 14:55:57121 }
122
[email protected]eba4a4d2013-05-29 02:18:06123 return static_cast<DownloadManager*>(
124 context->GetUserData(kDownloadManagerKeyName));
[email protected]b441a8492012-06-06 14:55:57125}
126
[email protected]6ef0c3912013-01-25 22:46:34127// static
[email protected]cd501a72014-08-22 19:58:31128storage::ExternalMountPoints* BrowserContext::GetMountPoints(
[email protected]6ef0c3912013-01-25 22:46:34129 BrowserContext* context) {
130 // Ensure that these methods are called on the UI thread, except for
131 // unittests where a UI thread might not have been created.
132 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) ||
133 !BrowserThread::IsMessageLoopValid(BrowserThread::UI));
134
135#if defined(OS_CHROMEOS)
136 if (!context->GetUserData(kMountPointsKey)) {
[email protected]cd501a72014-08-22 19:58:31137 scoped_refptr<storage::ExternalMountPoints> mount_points =
138 storage::ExternalMountPoints::CreateRefCounted();
[email protected]6ef0c3912013-01-25 22:46:34139 context->SetUserData(
140 kMountPointsKey,
[email protected]cd501a72014-08-22 19:58:31141 new UserDataAdapter<storage::ExternalMountPoints>(mount_points.get()));
[email protected]6ef0c3912013-01-25 22:46:34142 }
143
[email protected]cd501a72014-08-22 19:58:31144 return UserDataAdapter<storage::ExternalMountPoints>::Get(context,
145 kMountPointsKey);
[email protected]6ef0c3912013-01-25 22:46:34146#else
147 return NULL;
148#endif
149}
150
[email protected]4c3a23582012-08-18 08:54:34151StoragePartition* BrowserContext::GetStoragePartition(
152 BrowserContext* browser_context,
153 SiteInstance* site_instance) {
[email protected]1bc28312012-11-08 08:31:53154 std::string partition_domain;
155 std::string partition_name;
156 bool in_memory = false;
[email protected]4c3a23582012-08-18 08:54:34157
158 // TODO(ajwong): After GetDefaultStoragePartition() is removed, get rid of
159 // this conditional and require that |site_instance| is non-NULL.
160 if (site_instance) {
[email protected]1bc28312012-11-08 08:31:53161 GetContentClient()->browser()->GetStoragePartitionConfigForSite(
[email protected]14acc642012-11-17 12:20:10162 browser_context, site_instance->GetSiteURL(), true,
[email protected]1bc28312012-11-08 08:31:53163 &partition_domain, &partition_name, &in_memory);
[email protected]4c3a23582012-08-18 08:54:34164 }
165
[email protected]1bc28312012-11-08 08:31:53166 return GetStoragePartitionFromConfig(
167 browser_context, partition_domain, partition_name, in_memory);
[email protected]4c3a23582012-08-18 08:54:34168}
169
[email protected]e94bbcb2012-09-07 05:33:57170StoragePartition* BrowserContext::GetStoragePartitionForSite(
171 BrowserContext* browser_context,
172 const GURL& site) {
[email protected]1bc28312012-11-08 08:31:53173 std::string partition_domain;
174 std::string partition_name;
175 bool in_memory;
[email protected]e94bbcb2012-09-07 05:33:57176
[email protected]1bc28312012-11-08 08:31:53177 GetContentClient()->browser()->GetStoragePartitionConfigForSite(
[email protected]14acc642012-11-17 12:20:10178 browser_context, site, true, &partition_domain, &partition_name,
179 &in_memory);
[email protected]1bc28312012-11-08 08:31:53180
181 return GetStoragePartitionFromConfig(
182 browser_context, partition_domain, partition_name, in_memory);
[email protected]e94bbcb2012-09-07 05:33:57183}
184
[email protected]4c3a23582012-08-18 08:54:34185void BrowserContext::ForEachStoragePartition(
186 BrowserContext* browser_context,
187 const StoragePartitionCallback& callback) {
188 StoragePartitionImplMap* partition_map =
189 static_cast<StoragePartitionImplMap*>(
190 browser_context->GetUserData(kStorageParitionMapKeyName));
191 if (!partition_map)
192 return;
193
194 partition_map->ForEach(callback);
195}
196
197StoragePartition* BrowserContext::GetDefaultStoragePartition(
198 BrowserContext* browser_context) {
199 return GetStoragePartition(browser_context, NULL);
[email protected]55eb70e762012-02-20 17:38:39200}
201
[email protected]393b6cb2014-05-15 00:55:12202void BrowserContext::CreateMemoryBackedBlob(BrowserContext* browser_context,
203 const char* data, size_t length,
204 const BlobCallback& callback) {
205 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
206
207 ChromeBlobStorageContext* blob_context =
208 ChromeBlobStorageContext::GetFor(browser_context);
209 BrowserThread::PostTaskAndReplyWithResult(
210 BrowserThread::IO, FROM_HERE,
211 base::Bind(&ChromeBlobStorageContext::CreateMemoryBackedBlob,
212 make_scoped_refptr(blob_context), data, length),
213 callback);
214}
215
[email protected]66e53d0282014-08-07 10:04:35216// static
217void BrowserContext::DeliverPushMessage(
218 BrowserContext* browser_context,
219 const GURL& origin,
220 int64 service_worker_registration_id,
221 const std::string& data,
222 const base::Callback<void(PushMessagingStatus)>& callback) {
223 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
224 PushMessagingRouter::DeliverMessage(
225 browser_context, origin, service_worker_registration_id, data, callback);
226}
227
[email protected]314c3e22012-02-21 03:57:42228void BrowserContext::EnsureResourceContextInitialized(BrowserContext* context) {
[email protected]7e26ac92012-02-27 20:15:05229 // This will be enough to tickle initialization of BrowserContext if
230 // necessary, which initializes ResourceContext. The reason we don't call
[email protected]4c3a23582012-08-18 08:54:34231 // ResourceContext::InitializeResourceContext() directly here is that
232 // ResourceContext initialization may call back into BrowserContext
233 // and when that call returns it'll end rewriting its UserData map. It will
234 // end up rewriting the same value but this still causes a race condition.
235 //
236 // See http://crbug.com/115678.
237 GetDefaultStoragePartition(context);
[email protected]55eb70e762012-02-20 17:38:39238}
239
[email protected]6e2d3d22012-02-24 18:10:36240void BrowserContext::SaveSessionState(BrowserContext* browser_context) {
[email protected]5c8e67c2012-08-29 00:48:52241 GetDefaultStoragePartition(browser_context)->GetDatabaseTracker()->
242 SetForceKeepSessionState();
[email protected]b1b502e2012-09-16 07:31:43243 StoragePartition* storage_partition =
244 BrowserContext::GetDefaultStoragePartition(browser_context);
[email protected]6e2d3d22012-02-24 18:10:36245
246 if (BrowserThread::IsMessageLoopValid(BrowserThread::IO)) {
247 BrowserThread::PostTask(
248 BrowserThread::IO, FROM_HERE,
[email protected]6939075a2012-08-28 08:35:53249 base::Bind(
250 &SaveSessionStateOnIOThread,
251 make_scoped_refptr(browser_context->GetRequestContext()),
[email protected]98d6d4562014-06-25 20:57:55252 static_cast<AppCacheServiceImpl*>(
[email protected]63ef85512014-06-05 14:21:26253 storage_partition->GetAppCacheService())));
[email protected]6e2d3d22012-02-24 18:10:36254 }
255
[email protected]5f2aa722013-08-07 16:59:41256 DOMStorageContextWrapper* dom_storage_context_proxy =
257 static_cast<DOMStorageContextWrapper*>(
[email protected]b1b502e2012-09-16 07:31:43258 storage_partition->GetDOMStorageContext());
[email protected]5f2aa722013-08-07 16:59:41259 dom_storage_context_proxy->SetForceKeepSessionState();
[email protected]735e20c2012-03-20 01:16:59260
[email protected]89acda82013-06-25 20:52:50261 IndexedDBContextImpl* indexed_db_context_impl =
262 static_cast<IndexedDBContextImpl*>(
[email protected]b1b502e2012-09-16 07:31:43263 storage_partition->GetIndexedDBContext());
[email protected]89acda82013-06-25 20:52:50264 // No task runner in unit tests.
265 if (indexed_db_context_impl->TaskRunner()) {
266 indexed_db_context_impl->TaskRunner()->PostTask(
267 FROM_HERE,
268 base::Bind(&SaveSessionStateOnIndexedDBThread,
269 make_scoped_refptr(indexed_db_context_impl)));
[email protected]6e2d3d22012-02-24 18:10:36270 }
271}
272
[email protected]e0ce8a1e2012-09-18 10:26:36273#endif // !OS_IOS
[email protected]6e2d3d22012-02-24 18:10:36274
[email protected]55eb70e762012-02-20 17:38:39275BrowserContext::~BrowserContext() {
[email protected]e0ce8a1e2012-09-18 10:26:36276#if !defined(OS_IOS)
[email protected]b441a8492012-06-06 14:55:57277 if (GetUserData(kDownloadManagerKeyName))
278 GetDownloadManager(this)->Shutdown();
[email protected]e0ce8a1e2012-09-18 10:26:36279#endif
[email protected]55eb70e762012-02-20 17:38:39280}
281
[email protected]55eb70e762012-02-20 17:38:39282} // namespace content