blob: fbf5a00c46d7e8a32bbe79b1bba9d969cc317ebf [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
7#include "content/browser/appcache/chrome_appcache_service.h"
[email protected]d7c7c98a2012-07-12 21:27:448#include "webkit/database/database_tracker.h"
[email protected]1ea3c792012-04-17 01:25:049#include "content/browser/dom_storage/dom_storage_context_impl.h"
[email protected]d25fda12012-06-12 17:05:0310#include "content/browser/download/download_file_manager.h"
[email protected]b441a8492012-06-06 14:55:5711#include "content/browser/download/download_manager_impl.h"
[email protected]6e2d3d22012-02-24 18:10:3612#include "content/browser/in_process_webkit/indexed_db_context_impl.h"
[email protected]d25fda12012-06-12 17:05:0313#include "content/browser/renderer_host/resource_dispatcher_host_impl.h"
[email protected]d7c7c98a2012-07-12 21:27:4414#include "content/public/browser/resource_context.h"
15#include "content/browser/storage_partition.h"
16#include "content/browser/storage_partition_map.h"
17#include "content/common/child_process_host_impl.h"
[email protected]55eb70e762012-02-20 17:38:3918#include "content/public/browser/browser_thread.h"
[email protected]b441a8492012-06-06 14:55:5719#include "content/public/browser/content_browser_client.h"
[email protected]bf510ed2012-06-05 08:31:4320#include "net/base/server_bound_cert_service.h"
21#include "net/base/server_bound_cert_store.h"
[email protected]4d7c4ef2012-03-16 01:47:1222#include "net/cookies/cookie_monster.h"
23#include "net/cookies/cookie_store.h"
[email protected]6e2d3d22012-02-24 18:10:3624#include "net/url_request/url_request_context.h"
[email protected]55eb70e762012-02-20 17:38:3925
[email protected]314c3e22012-02-21 03:57:4226using base::UserDataAdapter;
[email protected]55eb70e762012-02-20 17:38:3927
[email protected]314c3e22012-02-21 03:57:4228// Key names on BrowserContext.
[email protected]b441a8492012-06-06 14:55:5729static const char* kDownloadManagerKeyName = "download_manager";
[email protected]d7c7c98a2012-07-12 21:27:4430static const char* kStorageParitionMapKeyName = "content_storage_partition_map";
[email protected]55eb70e762012-02-20 17:38:3931
32namespace content {
33
[email protected]735e20c2012-03-20 01:16:5934namespace {
35
[email protected]d7c7c98a2012-07-12 21:27:4436StoragePartition* GetStoragePartition(BrowserContext* browser_context,
37 int renderer_child_id) {
38 StoragePartitionMap* partition_map = static_cast<StoragePartitionMap*>(
39 browser_context->GetUserData(kStorageParitionMapKeyName));
40 if (!partition_map) {
41 partition_map = new StoragePartitionMap(browser_context);
42 browser_context->SetUserData(kStorageParitionMapKeyName, partition_map);
43 }
44
45 const std::string& partition_id =
46 GetContentClient()->browser()->GetStoragePartitionIdForChildProcess(
47 browser_context,
48 renderer_child_id);
49
50 return partition_map->Get(partition_id);
51}
52
53// Run |callback| on each storage partition in |browser_context|.
54void ForEachStoragePartition(
55 BrowserContext* browser_context,
56 const base::Callback<void(StoragePartition*)>& callback) {
57 StoragePartitionMap* partition_map = static_cast<StoragePartitionMap*>(
58 browser_context->GetUserData(kStorageParitionMapKeyName));
59 if (!partition_map) {
[email protected]55eb70e762012-02-20 17:38:3960 return;
61 }
62
[email protected]d7c7c98a2012-07-12 21:27:4463 partition_map->ForEach(callback);
64}
[email protected]55eb70e762012-02-20 17:38:3965
[email protected]d7c7c98a2012-07-12 21:27:4466// Used to convert a callback meant to take a DOMStorageContextImpl* into one
67// that can take a StoragePartition*.
68void ProcessDOMStorageContext(
69 const base::Callback<void(DOMStorageContextImpl*)>& callback,
70 StoragePartition* partition) {
71 callback.Run(partition->dom_storage_context());
72}
[email protected]55eb70e762012-02-20 17:38:3973
[email protected]d7c7c98a2012-07-12 21:27:4474// Run |callback| on each DOMStorageContextImpl in |browser_context|.
75void ForEachDOMStorageContext(
76 BrowserContext* browser_context,
77 const base::Callback<void(DOMStorageContextImpl*)>& callback) {
78 ForEachStoragePartition(browser_context,
79 base::Bind(&ProcessDOMStorageContext, callback));
[email protected]55eb70e762012-02-20 17:38:3980}
81
[email protected]6e2d3d22012-02-24 18:10:3682void SaveSessionStateOnIOThread(ResourceContext* resource_context) {
83 resource_context->GetRequestContext()->cookie_store()->GetCookieMonster()->
[email protected]bf510ed2012-06-05 08:31:4384 SetForceKeepSessionState();
85 resource_context->GetRequestContext()->server_bound_cert_service()->
86 GetCertStore()->SetForceKeepSessionState();
87 ResourceContext::GetAppCacheService(resource_context)->
88 set_force_keep_session_state();
[email protected]6e2d3d22012-02-24 18:10:3689}
90
91void SaveSessionStateOnWebkitThread(
[email protected]6e2d3d22012-02-24 18:10:3692 scoped_refptr<IndexedDBContextImpl> indexed_db_context) {
[email protected]bf510ed2012-06-05 08:31:4393 indexed_db_context->SetForceKeepSessionState();
[email protected]6e2d3d22012-02-24 18:10:3694}
95
96void PurgeMemoryOnIOThread(ResourceContext* resource_context) {
97 ResourceContext::GetAppCacheService(resource_context)->PurgeMemory();
98}
99
[email protected]d7c7c98a2012-07-12 21:27:44100DOMStorageContextImpl* GetDefaultDOMStorageContextImpl(
101 BrowserContext* context) {
[email protected]735e20c2012-03-20 01:16:59102 return static_cast<DOMStorageContextImpl*>(
[email protected]d7c7c98a2012-07-12 21:27:44103 BrowserContext::GetDefaultDOMStorageContext(context));
[email protected]6e2d3d22012-02-24 18:10:36104}
105
[email protected]735e20c2012-03-20 01:16:59106} // namespace
107
[email protected]b441a8492012-06-06 14:55:57108DownloadManager* BrowserContext::GetDownloadManager(
109 BrowserContext* context) {
[email protected]ecd3ad22012-07-10 20:02:40110 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]b441a8492012-06-06 14:55:57111 if (!context->GetUserData(kDownloadManagerKeyName)) {
[email protected]d25fda12012-06-12 17:05:03112 ResourceDispatcherHostImpl* rdh = ResourceDispatcherHostImpl::Get();
113 DCHECK(rdh);
114 DownloadFileManager* file_manager = rdh->download_file_manager();
115 DCHECK(file_manager);
116 scoped_refptr<DownloadManager> download_manager =
117 new DownloadManagerImpl(
118 file_manager,
119 scoped_ptr<DownloadItemFactory>(),
120 GetContentClient()->browser()->GetNetLog());
121
[email protected]b441a8492012-06-06 14:55:57122 context->SetUserData(
123 kDownloadManagerKeyName,
124 new UserDataAdapter<DownloadManager>(download_manager));
125 download_manager->SetDelegate(context->GetDownloadManagerDelegate());
126 download_manager->Init(context);
127 }
128
129 return UserDataAdapter<DownloadManager>::Get(
130 context, kDownloadManagerKeyName);
131}
132
[email protected]d7c7c98a2012-07-12 21:27:44133quota::QuotaManager* BrowserContext::GetQuotaManager(
134 BrowserContext* browser_context) {
135 // TODO(ajwong): Change this API to require a process id instead of using
136 // kInvalidChildProcessId.
137 StoragePartition* partition =
138 GetStoragePartition(browser_context,
139 ChildProcessHostImpl::kInvalidChildProcessId);
140 return partition->quota_manager();
141}
142
143DOMStorageContext* BrowserContext::GetDefaultDOMStorageContext(
144 BrowserContext* browser_context) {
145 // TODO(ajwong): Force all users to know which process id they are performing
146 // actions on behalf of, migrate them to GetDOMStorageContext(), and then
147 // delete this function.
148 return GetDOMStorageContext(browser_context,
149 ChildProcessHostImpl::kInvalidChildProcessId);
[email protected]55eb70e762012-02-20 17:38:39150}
151
[email protected]c1fff072012-02-24 23:38:12152DOMStorageContext* BrowserContext::GetDOMStorageContext(
[email protected]d7c7c98a2012-07-12 21:27:44153 BrowserContext* browser_context,
154 int render_child_id) {
155 StoragePartition* partition =
156 GetStoragePartition(browser_context, render_child_id);
157 return partition->dom_storage_context();
[email protected]c1fff072012-02-24 23:38:12158}
159
[email protected]d7c7c98a2012-07-12 21:27:44160IndexedDBContext* BrowserContext::GetIndexedDBContext(
[email protected]55eb70e762012-02-20 17:38:39161 BrowserContext* browser_context) {
[email protected]d7c7c98a2012-07-12 21:27:44162 // TODO(ajwong): Change this API to require a process id instead of using
163 // kInvalidChildProcessId.
164 StoragePartition* partition =
165 GetStoragePartition(browser_context,
166 ChildProcessHostImpl::kInvalidChildProcessId);
167 return partition->indexed_db_context();
[email protected]55eb70e762012-02-20 17:38:39168}
169
[email protected]d7c7c98a2012-07-12 21:27:44170webkit_database::DatabaseTracker* BrowserContext::GetDatabaseTracker(
[email protected]55eb70e762012-02-20 17:38:39171 BrowserContext* browser_context) {
[email protected]d7c7c98a2012-07-12 21:27:44172 // TODO(ajwong): Change this API to require a process id instead of using
173 // kInvalidChildProcessId.
174 StoragePartition* partition =
175 GetStoragePartition(browser_context,
176 ChildProcessHostImpl::kInvalidChildProcessId);
177 return partition->database_tracker();
178}
179
180appcache::AppCacheService* BrowserContext::GetAppCacheService(
181 BrowserContext* browser_context) {
182 // TODO(ajwong): Change this API to require a process id instead of using
183 // kInvalidChildProcessId.
184 StoragePartition* partition =
185 GetStoragePartition(browser_context,
186 ChildProcessHostImpl::kInvalidChildProcessId);
187 return partition->appcache_service();
188}
189
190fileapi::FileSystemContext* BrowserContext::GetFileSystemContext(
191 BrowserContext* browser_context) {
192 // TODO(ajwong): Change this API to require a process id instead of using
193 // kInvalidChildProcessId.
194 StoragePartition* partition =
195 GetStoragePartition(browser_context,
196 ChildProcessHostImpl::kInvalidChildProcessId);
197 return partition->filesystem_context();
[email protected]55eb70e762012-02-20 17:38:39198}
199
[email protected]314c3e22012-02-21 03:57:42200void BrowserContext::EnsureResourceContextInitialized(BrowserContext* context) {
[email protected]7e26ac92012-02-27 20:15:05201 // This will be enough to tickle initialization of BrowserContext if
202 // necessary, which initializes ResourceContext. The reason we don't call
203 // ResourceContext::InitializeResourceContext directly here is that if
204 // ResourceContext ends up initializing it will call back into BrowserContext
[email protected]d7c7c98a2012-07-12 21:27:44205 // and when that call returns it'll end rewriting its UserData map (with the
[email protected]7e26ac92012-02-27 20:15:05206 // same value) but this causes a race condition. See http://crbug.com/115678.
[email protected]d7c7c98a2012-07-12 21:27:44207 GetStoragePartition(context, ChildProcessHostImpl::kInvalidChildProcessId);
[email protected]55eb70e762012-02-20 17:38:39208}
209
[email protected]6e2d3d22012-02-24 18:10:36210void BrowserContext::SaveSessionState(BrowserContext* browser_context) {
[email protected]bf510ed2012-06-05 08:31:43211 GetDatabaseTracker(browser_context)->SetForceKeepSessionState();
[email protected]6e2d3d22012-02-24 18:10:36212
213 if (BrowserThread::IsMessageLoopValid(BrowserThread::IO)) {
214 BrowserThread::PostTask(
215 BrowserThread::IO, FROM_HERE,
216 base::Bind(&SaveSessionStateOnIOThread,
217 browser_context->GetResourceContext()));
218 }
219
[email protected]d7c7c98a2012-07-12 21:27:44220 // TODO(ajwong): This is the only usage of GetDefaultDOMStorageContextImpl().
221 // After we migrate this to support multiple DOMStorageContexts, don't forget
222 // to remove the GetDefaultDOMStorageContextImpl() function as well.
223 GetDefaultDOMStorageContextImpl(browser_context)->SetForceKeepSessionState();
[email protected]735e20c2012-03-20 01:16:59224
[email protected]6e2d3d22012-02-24 18:10:36225 if (BrowserThread::IsMessageLoopValid(BrowserThread::WEBKIT_DEPRECATED)) {
[email protected]6e2d3d22012-02-24 18:10:36226 IndexedDBContextImpl* indexed_db = static_cast<IndexedDBContextImpl*>(
[email protected]c1fff072012-02-24 23:38:12227 GetIndexedDBContext(browser_context));
[email protected]6e2d3d22012-02-24 18:10:36228 BrowserThread::PostTask(
229 BrowserThread::WEBKIT_DEPRECATED, FROM_HERE,
230 base::Bind(&SaveSessionStateOnWebkitThread,
[email protected]6e2d3d22012-02-24 18:10:36231 make_scoped_refptr(indexed_db)));
232 }
233}
234
[email protected]6e2d3d22012-02-24 18:10:36235void BrowserContext::PurgeMemory(BrowserContext* browser_context) {
236 if (BrowserThread::IsMessageLoopValid(BrowserThread::IO)) {
237 BrowserThread::PostTask(
238 BrowserThread::IO, FROM_HERE,
239 base::Bind(&PurgeMemoryOnIOThread,
240 browser_context->GetResourceContext()));
241 }
242
[email protected]d7c7c98a2012-07-12 21:27:44243 ForEachDOMStorageContext(browser_context,
244 base::Bind(&DOMStorageContextImpl::PurgeMemory));
[email protected]6e2d3d22012-02-24 18:10:36245}
246
[email protected]55eb70e762012-02-20 17:38:39247BrowserContext::~BrowserContext() {
[email protected]b441a8492012-06-06 14:55:57248 if (GetUserData(kDownloadManagerKeyName))
249 GetDownloadManager(this)->Shutdown();
[email protected]55eb70e762012-02-20 17:38:39250}
251
252} // namespace content