blob: 99905f8b3ba5602d265610036f5a55c4065f7c65 [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]1bc28312012-11-08 08:31:5340StoragePartition* GetStoragePartitionFromConfig(
[email protected]d1198fd2012-08-13 22:50:1941 BrowserContext* browser_context,
[email protected]1bc28312012-11-08 08:31:5342 const std::string& partition_domain,
43 const std::string& partition_name,
44 bool in_memory) {
[email protected]4c3a23582012-08-18 08:54:3445 StoragePartitionImplMap* partition_map =
46 static_cast<StoragePartitionImplMap*>(
47 browser_context->GetUserData(kStorageParitionMapKeyName));
[email protected]d7c7c98a2012-07-12 21:27:4448 if (!partition_map) {
[email protected]4c3a23582012-08-18 08:54:3449 partition_map = new StoragePartitionImplMap(browser_context);
[email protected]d7c7c98a2012-07-12 21:27:4450 browser_context->SetUserData(kStorageParitionMapKeyName, partition_map);
51 }
52
[email protected]1bc28312012-11-08 08:31:5353 if (browser_context->IsOffTheRecord())
54 in_memory = true;
55
56 // TODO(nasko): Webview tags with named partitions will have both
57 // partition_domain and partition_name set. In this case, mark them in-memory
58 // until the on-disk storage code has landed. http://crbug.com/159464
59 if (!partition_domain.empty() && !partition_name.empty())
60 in_memory = true;
61
62 return partition_map->Get(partition_domain, partition_name, in_memory);
[email protected]d1198fd2012-08-13 22:50:1963}
64
[email protected]d7c7c98a2012-07-12 21:27:4465// Run |callback| on each DOMStorageContextImpl in |browser_context|.
[email protected]1bc28312012-11-08 08:31:5366void PurgeDOMStorageContextInPartition(StoragePartition* storage_partition) {
[email protected]4c3a23582012-08-18 08:54:3467 static_cast<StoragePartitionImpl*>(storage_partition)->
68 GetDOMStorageContext()->PurgeMemory();
[email protected]55eb70e762012-02-20 17:38:3969}
70
[email protected]6939075a2012-08-28 08:35:5371void SaveSessionStateOnIOThread(
72 const scoped_refptr<net::URLRequestContextGetter>& context_getter,
73 appcache::AppCacheService* appcache_service) {
74 net::URLRequestContext* context = context_getter->GetURLRequestContext();
75 context->cookie_store()->GetCookieMonster()->
[email protected]bf510ed2012-06-05 08:31:4376 SetForceKeepSessionState();
[email protected]6939075a2012-08-28 08:35:5377 context->server_bound_cert_service()->GetCertStore()->
78 SetForceKeepSessionState();
79 appcache_service->set_force_keep_session_state();
[email protected]6e2d3d22012-02-24 18:10:3680}
81
82void SaveSessionStateOnWebkitThread(
[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]6939075a2012-08-28 08:35:5387void PurgeMemoryOnIOThread(appcache::AppCacheService* appcache_service) {
88 appcache_service->PurgeMemory();
[email protected]6e2d3d22012-02-24 18:10:3689}
90
[email protected]735e20c2012-03-20 01:16:5991} // namespace
92
[email protected]b441a8492012-06-06 14:55:5793DownloadManager* BrowserContext::GetDownloadManager(
94 BrowserContext* context) {
[email protected]ecd3ad22012-07-10 20:02:4095 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]b441a8492012-06-06 14:55:5796 if (!context->GetUserData(kDownloadManagerKeyName)) {
[email protected]d25fda12012-06-12 17:05:0397 ResourceDispatcherHostImpl* rdh = ResourceDispatcherHostImpl::Get();
98 DCHECK(rdh);
[email protected]d25fda12012-06-12 17:05:0399 scoped_refptr<DownloadManager> download_manager =
100 new DownloadManagerImpl(
[email protected]d25fda12012-06-12 17:05:03101 GetContentClient()->browser()->GetNetLog());
102
[email protected]b441a8492012-06-06 14:55:57103 context->SetUserData(
104 kDownloadManagerKeyName,
105 new UserDataAdapter<DownloadManager>(download_manager));
106 download_manager->SetDelegate(context->GetDownloadManagerDelegate());
107 download_manager->Init(context);
108 }
109
110 return UserDataAdapter<DownloadManager>::Get(
111 context, kDownloadManagerKeyName);
112}
113
[email protected]4c3a23582012-08-18 08:54:34114StoragePartition* BrowserContext::GetStoragePartition(
115 BrowserContext* browser_context,
116 SiteInstance* site_instance) {
[email protected]1bc28312012-11-08 08:31:53117 std::string partition_domain;
118 std::string partition_name;
119 bool in_memory = false;
[email protected]4c3a23582012-08-18 08:54:34120
121 // TODO(ajwong): After GetDefaultStoragePartition() is removed, get rid of
122 // this conditional and require that |site_instance| is non-NULL.
123 if (site_instance) {
[email protected]1bc28312012-11-08 08:31:53124 GetContentClient()->browser()->GetStoragePartitionConfigForSite(
125 browser_context, site_instance->GetSiteURL(),
126 &partition_domain, &partition_name, &in_memory);
[email protected]4c3a23582012-08-18 08:54:34127 }
128
[email protected]1bc28312012-11-08 08:31:53129 return GetStoragePartitionFromConfig(
130 browser_context, partition_domain, partition_name, in_memory);
[email protected]4c3a23582012-08-18 08:54:34131}
132
[email protected]e94bbcb2012-09-07 05:33:57133StoragePartition* BrowserContext::GetStoragePartitionForSite(
134 BrowserContext* browser_context,
135 const GURL& site) {
[email protected]1bc28312012-11-08 08:31:53136 std::string partition_domain;
137 std::string partition_name;
138 bool in_memory;
[email protected]e94bbcb2012-09-07 05:33:57139
[email protected]1bc28312012-11-08 08:31:53140 GetContentClient()->browser()->GetStoragePartitionConfigForSite(
141 browser_context, site, &partition_domain, &partition_name, &in_memory);
142
143 return GetStoragePartitionFromConfig(
144 browser_context, partition_domain, partition_name, in_memory);
[email protected]e94bbcb2012-09-07 05:33:57145}
146
[email protected]4c3a23582012-08-18 08:54:34147void BrowserContext::ForEachStoragePartition(
148 BrowserContext* browser_context,
149 const StoragePartitionCallback& callback) {
150 StoragePartitionImplMap* partition_map =
151 static_cast<StoragePartitionImplMap*>(
152 browser_context->GetUserData(kStorageParitionMapKeyName));
153 if (!partition_map)
154 return;
155
156 partition_map->ForEach(callback);
157}
158
159StoragePartition* BrowserContext::GetDefaultStoragePartition(
160 BrowserContext* browser_context) {
161 return GetStoragePartition(browser_context, NULL);
[email protected]55eb70e762012-02-20 17:38:39162}
163
[email protected]314c3e22012-02-21 03:57:42164void BrowserContext::EnsureResourceContextInitialized(BrowserContext* context) {
[email protected]7e26ac92012-02-27 20:15:05165 // This will be enough to tickle initialization of BrowserContext if
166 // necessary, which initializes ResourceContext. The reason we don't call
[email protected]4c3a23582012-08-18 08:54:34167 // ResourceContext::InitializeResourceContext() directly here is that
168 // ResourceContext initialization may call back into BrowserContext
169 // and when that call returns it'll end rewriting its UserData map. It will
170 // end up rewriting the same value but this still causes a race condition.
171 //
172 // See http://crbug.com/115678.
173 GetDefaultStoragePartition(context);
[email protected]55eb70e762012-02-20 17:38:39174}
175
[email protected]6e2d3d22012-02-24 18:10:36176void BrowserContext::SaveSessionState(BrowserContext* browser_context) {
[email protected]5c8e67c2012-08-29 00:48:52177 GetDefaultStoragePartition(browser_context)->GetDatabaseTracker()->
178 SetForceKeepSessionState();
[email protected]b1b502e2012-09-16 07:31:43179 StoragePartition* storage_partition =
180 BrowserContext::GetDefaultStoragePartition(browser_context);
[email protected]6e2d3d22012-02-24 18:10:36181
182 if (BrowserThread::IsMessageLoopValid(BrowserThread::IO)) {
183 BrowserThread::PostTask(
184 BrowserThread::IO, FROM_HERE,
[email protected]6939075a2012-08-28 08:35:53185 base::Bind(
186 &SaveSessionStateOnIOThread,
187 make_scoped_refptr(browser_context->GetRequestContext()),
[email protected]b1b502e2012-09-16 07:31:43188 storage_partition->GetAppCacheService()));
[email protected]6e2d3d22012-02-24 18:10:36189 }
190
[email protected]4c3a23582012-08-18 08:54:34191 DOMStorageContextImpl* dom_storage_context_impl =
192 static_cast<DOMStorageContextImpl*>(
[email protected]b1b502e2012-09-16 07:31:43193 storage_partition->GetDOMStorageContext());
[email protected]4c3a23582012-08-18 08:54:34194 dom_storage_context_impl->SetForceKeepSessionState();
[email protected]735e20c2012-03-20 01:16:59195
[email protected]6e2d3d22012-02-24 18:10:36196 if (BrowserThread::IsMessageLoopValid(BrowserThread::WEBKIT_DEPRECATED)) {
[email protected]6e2d3d22012-02-24 18:10:36197 IndexedDBContextImpl* indexed_db = static_cast<IndexedDBContextImpl*>(
[email protected]b1b502e2012-09-16 07:31:43198 storage_partition->GetIndexedDBContext());
[email protected]6e2d3d22012-02-24 18:10:36199 BrowserThread::PostTask(
200 BrowserThread::WEBKIT_DEPRECATED, FROM_HERE,
201 base::Bind(&SaveSessionStateOnWebkitThread,
[email protected]6e2d3d22012-02-24 18:10:36202 make_scoped_refptr(indexed_db)));
203 }
204}
205
[email protected]6e2d3d22012-02-24 18:10:36206void BrowserContext::PurgeMemory(BrowserContext* browser_context) {
207 if (BrowserThread::IsMessageLoopValid(BrowserThread::IO)) {
208 BrowserThread::PostTask(
209 BrowserThread::IO, FROM_HERE,
[email protected]6939075a2012-08-28 08:35:53210 base::Bind(
211 &PurgeMemoryOnIOThread,
212 BrowserContext::GetDefaultStoragePartition(browser_context)->
213 GetAppCacheService()));
[email protected]6e2d3d22012-02-24 18:10:36214 }
215
[email protected]4c3a23582012-08-18 08:54:34216 ForEachStoragePartition(browser_context,
217 base::Bind(&PurgeDOMStorageContextInPartition));
[email protected]6e2d3d22012-02-24 18:10:36218}
[email protected]e0ce8a1e2012-09-18 10:26:36219#endif // !OS_IOS
[email protected]6e2d3d22012-02-24 18:10:36220
[email protected]55eb70e762012-02-20 17:38:39221BrowserContext::~BrowserContext() {
[email protected]e0ce8a1e2012-09-18 10:26:36222#if !defined(OS_IOS)
[email protected]b441a8492012-06-06 14:55:57223 if (GetUserData(kDownloadManagerKeyName))
224 GetDownloadManager(this)->Shutdown();
[email protected]e0ce8a1e2012-09-18 10:26:36225#endif
[email protected]55eb70e762012-02-20 17:38:39226}
227
228} // namespace content