blob: 785a05896cc576eccb23f4c5d88e8284f5ed832a [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]4c3a23582012-08-18 08:54:3412#include "content/browser/storage_partition_impl_map.h"
[email protected]d7c7c98a2012-07-12 21:27:4413#include "content/common/child_process_host_impl.h"
[email protected]393b6cb2014-05-15 00:55:1214#include "content/public/browser/blob_handle.h"
[email protected]55eb70e762012-02-20 17:38:3915#include "content/public/browser/browser_thread.h"
[email protected]b441a8492012-06-06 14:55:5716#include "content/public/browser/content_browser_client.h"
[email protected]536fd0b2013-03-14 17:41:5717#include "content/public/browser/site_instance.h"
[email protected]4d7c4ef2012-03-16 01:47:1218#include "net/cookies/cookie_monster.h"
19#include "net/cookies/cookie_store.h"
[email protected]6b8a3c742014-07-25 00:25:3520#include "net/ssl/channel_id_service.h"
21#include "net/ssl/channel_id_store.h"
[email protected]6e2d3d22012-02-24 18:10:3622#include "net/url_request/url_request_context.h"
[email protected]6939075a2012-08-28 08:35:5323#include "net/url_request/url_request_context_getter.h"
[email protected]08b1f75f2013-05-22 22:02:3824#include "webkit/browser/database/database_tracker.h"
[email protected]f25e1132013-05-24 13:58:0425#include "webkit/browser/fileapi/external_mount_points.h"
[email protected]e0ce8a1e2012-09-18 10:26:3626#endif // !OS_IOS
[email protected]55eb70e762012-02-20 17:38:3927
[email protected]314c3e22012-02-21 03:57:4228using base::UserDataAdapter;
[email protected]55eb70e762012-02-20 17:38:3929
[email protected]55eb70e762012-02-20 17:38:3930namespace content {
31
[email protected]e0ce8a1e2012-09-18 10:26:3632// Only ~BrowserContext() is needed on iOS.
33#if !defined(OS_IOS)
[email protected]735e20c2012-03-20 01:16:5934namespace {
35
[email protected]e0ce8a1e2012-09-18 10:26:3636// Key names on BrowserContext.
[email protected]6ef0c3912013-01-25 22:46:3437const char kDownloadManagerKeyName[] = "download_manager";
[email protected]6ef0c3912013-01-25 22:46:3438const char kStorageParitionMapKeyName[] = "content_storage_partition_map";
[email protected]e0ce8a1e2012-09-18 10:26:3639
[email protected]9afc14e22013-09-25 22:34:1440#if defined(OS_CHROMEOS)
41const char kMountPointsKey[] = "mount_points";
42#endif // defined(OS_CHROMEOS)
43
[email protected]14acc642012-11-17 12:20:1044StoragePartitionImplMap* GetStoragePartitionMap(
45 BrowserContext* browser_context) {
[email protected]4c3a23582012-08-18 08:54:3446 StoragePartitionImplMap* partition_map =
47 static_cast<StoragePartitionImplMap*>(
48 browser_context->GetUserData(kStorageParitionMapKeyName));
[email protected]d7c7c98a2012-07-12 21:27:4449 if (!partition_map) {
[email protected]4c3a23582012-08-18 08:54:3450 partition_map = new StoragePartitionImplMap(browser_context);
[email protected]d7c7c98a2012-07-12 21:27:4451 browser_context->SetUserData(kStorageParitionMapKeyName, partition_map);
52 }
[email protected]14acc642012-11-17 12:20:1053 return partition_map;
54}
55
56StoragePartition* GetStoragePartitionFromConfig(
57 BrowserContext* browser_context,
58 const std::string& partition_domain,
59 const std::string& partition_name,
60 bool in_memory) {
61 StoragePartitionImplMap* partition_map =
62 GetStoragePartitionMap(browser_context);
[email protected]d7c7c98a2012-07-12 21:27:4463
[email protected]1bc28312012-11-08 08:31:5364 if (browser_context->IsOffTheRecord())
65 in_memory = true;
66
[email protected]1bc28312012-11-08 08:31:5367 return partition_map->Get(partition_domain, partition_name, in_memory);
[email protected]d1198fd2012-08-13 22:50:1968}
69
[email protected]6939075a2012-08-28 08:35:5370void SaveSessionStateOnIOThread(
71 const scoped_refptr<net::URLRequestContextGetter>& context_getter,
[email protected]98d6d4562014-06-25 20:57:5572 AppCacheServiceImpl* appcache_service) {
[email protected]6939075a2012-08-28 08:35:5373 net::URLRequestContext* context = context_getter->GetURLRequestContext();
74 context->cookie_store()->GetCookieMonster()->
[email protected]bf510ed2012-06-05 08:31:4375 SetForceKeepSessionState();
[email protected]6b8a3c742014-07-25 00:25:3576 context->channel_id_service()->GetChannelIDStore()->
[email protected]6939075a2012-08-28 08:35:5377 SetForceKeepSessionState();
78 appcache_service->set_force_keep_session_state();
[email protected]6e2d3d22012-02-24 18:10:3679}
80
[email protected]89acda82013-06-25 20:52:5081void SaveSessionStateOnIndexedDBThread(
[email protected]6e2d3d22012-02-24 18:10:3682 scoped_refptr<IndexedDBContextImpl> indexed_db_context) {
[email protected]bf510ed2012-06-05 08:31:4383 indexed_db_context->SetForceKeepSessionState();
[email protected]6e2d3d22012-02-24 18:10:3684}
85
[email protected]735e20c2012-03-20 01:16:5986} // namespace
87
[email protected]14acc642012-11-17 12:20:1088// static
89void BrowserContext::AsyncObliterateStoragePartition(
90 BrowserContext* browser_context,
[email protected]399583b2012-12-11 09:33:4291 const GURL& site,
92 const base::Closure& on_gc_required) {
93 GetStoragePartitionMap(browser_context)->AsyncObliterate(site,
94 on_gc_required);
95}
96
97// static
98void BrowserContext::GarbageCollectStoragePartitions(
99 BrowserContext* browser_context,
[email protected]2dec8ec2013-02-07 19:20:34100 scoped_ptr<base::hash_set<base::FilePath> > active_paths,
[email protected]399583b2012-12-11 09:33:42101 const base::Closure& done) {
102 GetStoragePartitionMap(browser_context)->GarbageCollect(
103 active_paths.Pass(), done);
[email protected]14acc642012-11-17 12:20:10104}
105
[email protected]b441a8492012-06-06 14:55:57106DownloadManager* BrowserContext::GetDownloadManager(
107 BrowserContext* context) {
[email protected]ecd3ad22012-07-10 20:02:40108 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]b441a8492012-06-06 14:55:57109 if (!context->GetUserData(kDownloadManagerKeyName)) {
[email protected]d25fda12012-06-12 17:05:03110 ResourceDispatcherHostImpl* rdh = ResourceDispatcherHostImpl::Get();
111 DCHECK(rdh);
[email protected]eba4a4d2013-05-29 02:18:06112 DownloadManager* download_manager =
[email protected]d25fda12012-06-12 17:05:03113 new DownloadManagerImpl(
[email protected]16798692013-04-23 18:08:38114 GetContentClient()->browser()->GetNetLog(), context);
[email protected]d25fda12012-06-12 17:05:03115
[email protected]b441a8492012-06-06 14:55:57116 context->SetUserData(
117 kDownloadManagerKeyName,
[email protected]eba4a4d2013-05-29 02:18:06118 download_manager);
[email protected]b441a8492012-06-06 14:55:57119 download_manager->SetDelegate(context->GetDownloadManagerDelegate());
[email protected]b441a8492012-06-06 14:55:57120 }
121
[email protected]eba4a4d2013-05-29 02:18:06122 return static_cast<DownloadManager*>(
123 context->GetUserData(kDownloadManagerKeyName));
[email protected]b441a8492012-06-06 14:55:57124}
125
[email protected]6ef0c3912013-01-25 22:46:34126// static
127fileapi::ExternalMountPoints* BrowserContext::GetMountPoints(
128 BrowserContext* context) {
129 // Ensure that these methods are called on the UI thread, except for
130 // unittests where a UI thread might not have been created.
131 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) ||
132 !BrowserThread::IsMessageLoopValid(BrowserThread::UI));
133
134#if defined(OS_CHROMEOS)
135 if (!context->GetUserData(kMountPointsKey)) {
136 scoped_refptr<fileapi::ExternalMountPoints> mount_points =
137 fileapi::ExternalMountPoints::CreateRefCounted();
138 context->SetUserData(
139 kMountPointsKey,
[email protected]144b6c42013-06-14 07:30:38140 new UserDataAdapter<fileapi::ExternalMountPoints>(mount_points.get()));
[email protected]6ef0c3912013-01-25 22:46:34141 }
142
143 return UserDataAdapter<fileapi::ExternalMountPoints>::Get(
144 context, kMountPointsKey);
145#else
146 return NULL;
147#endif
148}
149
[email protected]4c3a23582012-08-18 08:54:34150StoragePartition* BrowserContext::GetStoragePartition(
151 BrowserContext* browser_context,
152 SiteInstance* site_instance) {
[email protected]1bc28312012-11-08 08:31:53153 std::string partition_domain;
154 std::string partition_name;
155 bool in_memory = false;
[email protected]4c3a23582012-08-18 08:54:34156
157 // TODO(ajwong): After GetDefaultStoragePartition() is removed, get rid of
158 // this conditional and require that |site_instance| is non-NULL.
159 if (site_instance) {
[email protected]1bc28312012-11-08 08:31:53160 GetContentClient()->browser()->GetStoragePartitionConfigForSite(
[email protected]14acc642012-11-17 12:20:10161 browser_context, site_instance->GetSiteURL(), true,
[email protected]1bc28312012-11-08 08:31:53162 &partition_domain, &partition_name, &in_memory);
[email protected]4c3a23582012-08-18 08:54:34163 }
164
[email protected]1bc28312012-11-08 08:31:53165 return GetStoragePartitionFromConfig(
166 browser_context, partition_domain, partition_name, in_memory);
[email protected]4c3a23582012-08-18 08:54:34167}
168
[email protected]e94bbcb2012-09-07 05:33:57169StoragePartition* BrowserContext::GetStoragePartitionForSite(
170 BrowserContext* browser_context,
171 const GURL& site) {
[email protected]1bc28312012-11-08 08:31:53172 std::string partition_domain;
173 std::string partition_name;
174 bool in_memory;
[email protected]e94bbcb2012-09-07 05:33:57175
[email protected]1bc28312012-11-08 08:31:53176 GetContentClient()->browser()->GetStoragePartitionConfigForSite(
[email protected]14acc642012-11-17 12:20:10177 browser_context, site, true, &partition_domain, &partition_name,
178 &in_memory);
[email protected]1bc28312012-11-08 08:31:53179
180 return GetStoragePartitionFromConfig(
181 browser_context, partition_domain, partition_name, in_memory);
[email protected]e94bbcb2012-09-07 05:33:57182}
183
[email protected]4c3a23582012-08-18 08:54:34184void BrowserContext::ForEachStoragePartition(
185 BrowserContext* browser_context,
186 const StoragePartitionCallback& callback) {
187 StoragePartitionImplMap* partition_map =
188 static_cast<StoragePartitionImplMap*>(
189 browser_context->GetUserData(kStorageParitionMapKeyName));
190 if (!partition_map)
191 return;
192
193 partition_map->ForEach(callback);
194}
195
196StoragePartition* BrowserContext::GetDefaultStoragePartition(
197 BrowserContext* browser_context) {
198 return GetStoragePartition(browser_context, NULL);
[email protected]55eb70e762012-02-20 17:38:39199}
200
[email protected]393b6cb2014-05-15 00:55:12201void BrowserContext::CreateMemoryBackedBlob(BrowserContext* browser_context,
202 const char* data, size_t length,
203 const BlobCallback& callback) {
204 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
205
206 ChromeBlobStorageContext* blob_context =
207 ChromeBlobStorageContext::GetFor(browser_context);
208 BrowserThread::PostTaskAndReplyWithResult(
209 BrowserThread::IO, FROM_HERE,
210 base::Bind(&ChromeBlobStorageContext::CreateMemoryBackedBlob,
211 make_scoped_refptr(blob_context), data, length),
212 callback);
213}
214
[email protected]314c3e22012-02-21 03:57:42215void BrowserContext::EnsureResourceContextInitialized(BrowserContext* context) {
[email protected]7e26ac92012-02-27 20:15:05216 // This will be enough to tickle initialization of BrowserContext if
217 // necessary, which initializes ResourceContext. The reason we don't call
[email protected]4c3a23582012-08-18 08:54:34218 // ResourceContext::InitializeResourceContext() directly here is that
219 // ResourceContext initialization may call back into BrowserContext
220 // and when that call returns it'll end rewriting its UserData map. It will
221 // end up rewriting the same value but this still causes a race condition.
222 //
223 // See http://crbug.com/115678.
224 GetDefaultStoragePartition(context);
[email protected]55eb70e762012-02-20 17:38:39225}
226
[email protected]6e2d3d22012-02-24 18:10:36227void BrowserContext::SaveSessionState(BrowserContext* browser_context) {
[email protected]5c8e67c2012-08-29 00:48:52228 GetDefaultStoragePartition(browser_context)->GetDatabaseTracker()->
229 SetForceKeepSessionState();
[email protected]b1b502e2012-09-16 07:31:43230 StoragePartition* storage_partition =
231 BrowserContext::GetDefaultStoragePartition(browser_context);
[email protected]6e2d3d22012-02-24 18:10:36232
233 if (BrowserThread::IsMessageLoopValid(BrowserThread::IO)) {
234 BrowserThread::PostTask(
235 BrowserThread::IO, FROM_HERE,
[email protected]6939075a2012-08-28 08:35:53236 base::Bind(
237 &SaveSessionStateOnIOThread,
238 make_scoped_refptr(browser_context->GetRequestContext()),
[email protected]98d6d4562014-06-25 20:57:55239 static_cast<AppCacheServiceImpl*>(
[email protected]63ef85512014-06-05 14:21:26240 storage_partition->GetAppCacheService())));
[email protected]6e2d3d22012-02-24 18:10:36241 }
242
[email protected]5f2aa722013-08-07 16:59:41243 DOMStorageContextWrapper* dom_storage_context_proxy =
244 static_cast<DOMStorageContextWrapper*>(
[email protected]b1b502e2012-09-16 07:31:43245 storage_partition->GetDOMStorageContext());
[email protected]5f2aa722013-08-07 16:59:41246 dom_storage_context_proxy->SetForceKeepSessionState();
[email protected]735e20c2012-03-20 01:16:59247
[email protected]89acda82013-06-25 20:52:50248 IndexedDBContextImpl* indexed_db_context_impl =
249 static_cast<IndexedDBContextImpl*>(
[email protected]b1b502e2012-09-16 07:31:43250 storage_partition->GetIndexedDBContext());
[email protected]89acda82013-06-25 20:52:50251 // No task runner in unit tests.
252 if (indexed_db_context_impl->TaskRunner()) {
253 indexed_db_context_impl->TaskRunner()->PostTask(
254 FROM_HERE,
255 base::Bind(&SaveSessionStateOnIndexedDBThread,
256 make_scoped_refptr(indexed_db_context_impl)));
[email protected]6e2d3d22012-02-24 18:10:36257 }
258}
259
[email protected]e0ce8a1e2012-09-18 10:26:36260#endif // !OS_IOS
[email protected]6e2d3d22012-02-24 18:10:36261
[email protected]55eb70e762012-02-20 17:38:39262BrowserContext::~BrowserContext() {
[email protected]e0ce8a1e2012-09-18 10:26:36263#if !defined(OS_IOS)
[email protected]b441a8492012-06-06 14:55:57264 if (GetUserData(kDownloadManagerKeyName))
265 GetDownloadManager(this)->Shutdown();
[email protected]e0ce8a1e2012-09-18 10:26:36266#endif
[email protected]55eb70e762012-02-20 17:38:39267}
268
[email protected]55eb70e762012-02-20 17:38:39269} // namespace content