blob: 79355bc3089bde7dda9c7c2caf68d33980c302ff [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]55eb70e762012-02-20 17:38:398#include "content/browser/appcache/chrome_appcache_service.h"
[email protected]1ea3c792012-04-17 01:25:049#include "content/browser/dom_storage/dom_storage_context_impl.h"
[email protected]b441a8492012-06-06 14:55:5710#include "content/browser/download/download_manager_impl.h"
[email protected]6e2d3d22012-02-24 18:10:3611#include "content/browser/in_process_webkit/indexed_db_context_impl.h"
[email protected]d25fda12012-06-12 17:05:0312#include "content/browser/renderer_host/resource_dispatcher_host_impl.h"
[email protected]e94bbcb2012-09-07 05:33:5713#include "content/public/browser/site_instance.h"
[email protected]4c3a23582012-08-18 08:54:3414#include "content/browser/storage_partition_impl.h"
15#include "content/browser/storage_partition_impl_map.h"
[email protected]d7c7c98a2012-07-12 21:27:4416#include "content/common/child_process_host_impl.h"
[email protected]55eb70e762012-02-20 17:38:3917#include "content/public/browser/browser_thread.h"
[email protected]b441a8492012-06-06 14:55:5718#include "content/public/browser/content_browser_client.h"
[email protected]bf510ed2012-06-05 08:31:4319#include "net/base/server_bound_cert_service.h"
20#include "net/base/server_bound_cert_store.h"
[email protected]4d7c4ef2012-03-16 01:47:1221#include "net/cookies/cookie_monster.h"
22#include "net/cookies/cookie_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]53ac00e82012-10-18 20:59:2025#include "webkit/database/database_tracker.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.
37const char* kDownloadManagerKeyName = "download_manager";
38const char* kStorageParitionMapKeyName = "content_storage_partition_map";
39
[email protected]14acc642012-11-17 12:20:1040StoragePartitionImplMap* GetStoragePartitionMap(
41 BrowserContext* browser_context) {
[email protected]4c3a23582012-08-18 08:54:3442 StoragePartitionImplMap* partition_map =
43 static_cast<StoragePartitionImplMap*>(
44 browser_context->GetUserData(kStorageParitionMapKeyName));
[email protected]d7c7c98a2012-07-12 21:27:4445 if (!partition_map) {
[email protected]4c3a23582012-08-18 08:54:3446 partition_map = new StoragePartitionImplMap(browser_context);
[email protected]d7c7c98a2012-07-12 21:27:4447 browser_context->SetUserData(kStorageParitionMapKeyName, partition_map);
48 }
[email protected]14acc642012-11-17 12:20:1049 return partition_map;
50}
51
52StoragePartition* GetStoragePartitionFromConfig(
53 BrowserContext* browser_context,
54 const std::string& partition_domain,
55 const std::string& partition_name,
56 bool in_memory) {
57 StoragePartitionImplMap* partition_map =
58 GetStoragePartitionMap(browser_context);
[email protected]d7c7c98a2012-07-12 21:27:4459
[email protected]1bc28312012-11-08 08:31:5360 if (browser_context->IsOffTheRecord())
61 in_memory = true;
62
[email protected]1bc28312012-11-08 08:31:5363 return partition_map->Get(partition_domain, partition_name, in_memory);
[email protected]d1198fd2012-08-13 22:50:1964}
65
[email protected]d7c7c98a2012-07-12 21:27:4466// Run |callback| on each DOMStorageContextImpl in |browser_context|.
[email protected]1bc28312012-11-08 08:31:5367void PurgeDOMStorageContextInPartition(StoragePartition* storage_partition) {
[email protected]4c3a23582012-08-18 08:54:3468 static_cast<StoragePartitionImpl*>(storage_partition)->
69 GetDOMStorageContext()->PurgeMemory();
[email protected]55eb70e762012-02-20 17:38:3970}
71
[email protected]6939075a2012-08-28 08:35:5372void SaveSessionStateOnIOThread(
73 const scoped_refptr<net::URLRequestContextGetter>& context_getter,
74 appcache::AppCacheService* appcache_service) {
75 net::URLRequestContext* context = context_getter->GetURLRequestContext();
76 context->cookie_store()->GetCookieMonster()->
[email protected]bf510ed2012-06-05 08:31:4377 SetForceKeepSessionState();
[email protected]6939075a2012-08-28 08:35:5378 context->server_bound_cert_service()->GetCertStore()->
79 SetForceKeepSessionState();
80 appcache_service->set_force_keep_session_state();
[email protected]6e2d3d22012-02-24 18:10:3681}
82
83void SaveSessionStateOnWebkitThread(
[email protected]6e2d3d22012-02-24 18:10:3684 scoped_refptr<IndexedDBContextImpl> indexed_db_context) {
[email protected]bf510ed2012-06-05 08:31:4385 indexed_db_context->SetForceKeepSessionState();
[email protected]6e2d3d22012-02-24 18:10:3686}
87
[email protected]6939075a2012-08-28 08:35:5388void PurgeMemoryOnIOThread(appcache::AppCacheService* appcache_service) {
89 appcache_service->PurgeMemory();
[email protected]6e2d3d22012-02-24 18:10:3690}
91
[email protected]735e20c2012-03-20 01:16:5992} // namespace
93
[email protected]14acc642012-11-17 12:20:1094// static
95void BrowserContext::AsyncObliterateStoragePartition(
96 BrowserContext* browser_context,
97 const GURL& site) {
98 GetStoragePartitionMap(browser_context)->AsyncObliterate(site);
99}
100
[email protected]b441a8492012-06-06 14:55:57101DownloadManager* BrowserContext::GetDownloadManager(
102 BrowserContext* context) {
[email protected]ecd3ad22012-07-10 20:02:40103 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]b441a8492012-06-06 14:55:57104 if (!context->GetUserData(kDownloadManagerKeyName)) {
[email protected]d25fda12012-06-12 17:05:03105 ResourceDispatcherHostImpl* rdh = ResourceDispatcherHostImpl::Get();
106 DCHECK(rdh);
[email protected]d25fda12012-06-12 17:05:03107 scoped_refptr<DownloadManager> download_manager =
108 new DownloadManagerImpl(
[email protected]d25fda12012-06-12 17:05:03109 GetContentClient()->browser()->GetNetLog());
110
[email protected]b441a8492012-06-06 14:55:57111 context->SetUserData(
112 kDownloadManagerKeyName,
113 new UserDataAdapter<DownloadManager>(download_manager));
114 download_manager->SetDelegate(context->GetDownloadManagerDelegate());
115 download_manager->Init(context);
116 }
117
118 return UserDataAdapter<DownloadManager>::Get(
119 context, kDownloadManagerKeyName);
120}
121
[email protected]4c3a23582012-08-18 08:54:34122StoragePartition* BrowserContext::GetStoragePartition(
123 BrowserContext* browser_context,
124 SiteInstance* site_instance) {
[email protected]1bc28312012-11-08 08:31:53125 std::string partition_domain;
126 std::string partition_name;
127 bool in_memory = false;
[email protected]4c3a23582012-08-18 08:54:34128
129 // TODO(ajwong): After GetDefaultStoragePartition() is removed, get rid of
130 // this conditional and require that |site_instance| is non-NULL.
131 if (site_instance) {
[email protected]1bc28312012-11-08 08:31:53132 GetContentClient()->browser()->GetStoragePartitionConfigForSite(
[email protected]14acc642012-11-17 12:20:10133 browser_context, site_instance->GetSiteURL(), true,
[email protected]1bc28312012-11-08 08:31:53134 &partition_domain, &partition_name, &in_memory);
[email protected]4c3a23582012-08-18 08:54:34135 }
136
[email protected]1bc28312012-11-08 08:31:53137 return GetStoragePartitionFromConfig(
138 browser_context, partition_domain, partition_name, in_memory);
[email protected]4c3a23582012-08-18 08:54:34139}
140
[email protected]e94bbcb2012-09-07 05:33:57141StoragePartition* BrowserContext::GetStoragePartitionForSite(
142 BrowserContext* browser_context,
143 const GURL& site) {
[email protected]1bc28312012-11-08 08:31:53144 std::string partition_domain;
145 std::string partition_name;
146 bool in_memory;
[email protected]e94bbcb2012-09-07 05:33:57147
[email protected]1bc28312012-11-08 08:31:53148 GetContentClient()->browser()->GetStoragePartitionConfigForSite(
[email protected]14acc642012-11-17 12:20:10149 browser_context, site, true, &partition_domain, &partition_name,
150 &in_memory);
[email protected]1bc28312012-11-08 08:31:53151
152 return GetStoragePartitionFromConfig(
153 browser_context, partition_domain, partition_name, in_memory);
[email protected]e94bbcb2012-09-07 05:33:57154}
155
[email protected]4c3a23582012-08-18 08:54:34156void BrowserContext::ForEachStoragePartition(
157 BrowserContext* browser_context,
158 const StoragePartitionCallback& callback) {
159 StoragePartitionImplMap* partition_map =
160 static_cast<StoragePartitionImplMap*>(
161 browser_context->GetUserData(kStorageParitionMapKeyName));
162 if (!partition_map)
163 return;
164
165 partition_map->ForEach(callback);
166}
167
168StoragePartition* BrowserContext::GetDefaultStoragePartition(
169 BrowserContext* browser_context) {
170 return GetStoragePartition(browser_context, NULL);
[email protected]55eb70e762012-02-20 17:38:39171}
172
[email protected]314c3e22012-02-21 03:57:42173void BrowserContext::EnsureResourceContextInitialized(BrowserContext* context) {
[email protected]7e26ac92012-02-27 20:15:05174 // This will be enough to tickle initialization of BrowserContext if
175 // necessary, which initializes ResourceContext. The reason we don't call
[email protected]4c3a23582012-08-18 08:54:34176 // ResourceContext::InitializeResourceContext() directly here is that
177 // ResourceContext initialization may call back into BrowserContext
178 // and when that call returns it'll end rewriting its UserData map. It will
179 // end up rewriting the same value but this still causes a race condition.
180 //
181 // See http://crbug.com/115678.
182 GetDefaultStoragePartition(context);
[email protected]55eb70e762012-02-20 17:38:39183}
184
[email protected]6e2d3d22012-02-24 18:10:36185void BrowserContext::SaveSessionState(BrowserContext* browser_context) {
[email protected]5c8e67c2012-08-29 00:48:52186 GetDefaultStoragePartition(browser_context)->GetDatabaseTracker()->
187 SetForceKeepSessionState();
[email protected]b1b502e2012-09-16 07:31:43188 StoragePartition* storage_partition =
189 BrowserContext::GetDefaultStoragePartition(browser_context);
[email protected]6e2d3d22012-02-24 18:10:36190
191 if (BrowserThread::IsMessageLoopValid(BrowserThread::IO)) {
192 BrowserThread::PostTask(
193 BrowserThread::IO, FROM_HERE,
[email protected]6939075a2012-08-28 08:35:53194 base::Bind(
195 &SaveSessionStateOnIOThread,
196 make_scoped_refptr(browser_context->GetRequestContext()),
[email protected]b1b502e2012-09-16 07:31:43197 storage_partition->GetAppCacheService()));
[email protected]6e2d3d22012-02-24 18:10:36198 }
199
[email protected]4c3a23582012-08-18 08:54:34200 DOMStorageContextImpl* dom_storage_context_impl =
201 static_cast<DOMStorageContextImpl*>(
[email protected]b1b502e2012-09-16 07:31:43202 storage_partition->GetDOMStorageContext());
[email protected]4c3a23582012-08-18 08:54:34203 dom_storage_context_impl->SetForceKeepSessionState();
[email protected]735e20c2012-03-20 01:16:59204
[email protected]6e2d3d22012-02-24 18:10:36205 if (BrowserThread::IsMessageLoopValid(BrowserThread::WEBKIT_DEPRECATED)) {
[email protected]6e2d3d22012-02-24 18:10:36206 IndexedDBContextImpl* indexed_db = static_cast<IndexedDBContextImpl*>(
[email protected]b1b502e2012-09-16 07:31:43207 storage_partition->GetIndexedDBContext());
[email protected]6e2d3d22012-02-24 18:10:36208 BrowserThread::PostTask(
209 BrowserThread::WEBKIT_DEPRECATED, FROM_HERE,
210 base::Bind(&SaveSessionStateOnWebkitThread,
[email protected]6e2d3d22012-02-24 18:10:36211 make_scoped_refptr(indexed_db)));
212 }
213}
214
[email protected]6e2d3d22012-02-24 18:10:36215void BrowserContext::PurgeMemory(BrowserContext* browser_context) {
216 if (BrowserThread::IsMessageLoopValid(BrowserThread::IO)) {
217 BrowserThread::PostTask(
218 BrowserThread::IO, FROM_HERE,
[email protected]6939075a2012-08-28 08:35:53219 base::Bind(
220 &PurgeMemoryOnIOThread,
221 BrowserContext::GetDefaultStoragePartition(browser_context)->
222 GetAppCacheService()));
[email protected]6e2d3d22012-02-24 18:10:36223 }
224
[email protected]4c3a23582012-08-18 08:54:34225 ForEachStoragePartition(browser_context,
226 base::Bind(&PurgeDOMStorageContextInPartition));
[email protected]6e2d3d22012-02-24 18:10:36227}
[email protected]e0ce8a1e2012-09-18 10:26:36228#endif // !OS_IOS
[email protected]6e2d3d22012-02-24 18:10:36229
[email protected]55eb70e762012-02-20 17:38:39230BrowserContext::~BrowserContext() {
[email protected]e0ce8a1e2012-09-18 10:26:36231#if !defined(OS_IOS)
[email protected]b441a8492012-06-06 14:55:57232 if (GetUserData(kDownloadManagerKeyName))
233 GetDownloadManager(this)->Shutdown();
[email protected]e0ce8a1e2012-09-18 10:26:36234#endif
[email protected]55eb70e762012-02-20 17:38:39235}
236
237} // namespace content