blob: 2e76dc00ad5182f4e5979ba511cd0ed625b3cd34 [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]5f2aa722013-08-07 16:59:419#include "content/browser/dom_storage/dom_storage_context_wrapper.h"
[email protected]b441a8492012-06-06 14:55:5710#include "content/browser/download/download_manager_impl.h"
[email protected]c4d281662013-03-31 00:35:0811#include "content/browser/indexed_db/indexed_db_context_impl.h"
[email protected]678c0362012-12-05 08:02:4412#include "content/browser/loader/resource_dispatcher_host_impl.h"
[email protected]4c3a23582012-08-18 08:54:3413#include "content/browser/storage_partition_impl.h"
14#include "content/browser/storage_partition_impl_map.h"
[email protected]d7c7c98a2012-07-12 21:27:4415#include "content/common/child_process_host_impl.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]536fd0b2013-03-14 17:41:5721#include "net/ssl/server_bound_cert_service.h"
22#include "net/ssl/server_bound_cert_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";
39const char kMountPointsKey[] = "mount_points";
40const char kStorageParitionMapKeyName[] = "content_storage_partition_map";
[email protected]e0ce8a1e2012-09-18 10:26:3641
[email protected]14acc642012-11-17 12:20:1042StoragePartitionImplMap* GetStoragePartitionMap(
43 BrowserContext* browser_context) {
[email protected]4c3a23582012-08-18 08:54:3444 StoragePartitionImplMap* partition_map =
45 static_cast<StoragePartitionImplMap*>(
46 browser_context->GetUserData(kStorageParitionMapKeyName));
[email protected]d7c7c98a2012-07-12 21:27:4447 if (!partition_map) {
[email protected]4c3a23582012-08-18 08:54:3448 partition_map = new StoragePartitionImplMap(browser_context);
[email protected]d7c7c98a2012-07-12 21:27:4449 browser_context->SetUserData(kStorageParitionMapKeyName, partition_map);
50 }
[email protected]14acc642012-11-17 12:20:1051 return partition_map;
52}
53
54StoragePartition* GetStoragePartitionFromConfig(
55 BrowserContext* browser_context,
56 const std::string& partition_domain,
57 const std::string& partition_name,
58 bool in_memory) {
59 StoragePartitionImplMap* partition_map =
60 GetStoragePartitionMap(browser_context);
[email protected]d7c7c98a2012-07-12 21:27:4461
[email protected]1bc28312012-11-08 08:31:5362 if (browser_context->IsOffTheRecord())
63 in_memory = true;
64
[email protected]1bc28312012-11-08 08:31:5365 return partition_map->Get(partition_domain, partition_name, in_memory);
[email protected]d1198fd2012-08-13 22:50:1966}
67
[email protected]5f2aa722013-08-07 16:59:4168// Run |callback| on each DOMStorageContextWrapper in |browser_context|.
[email protected]1bc28312012-11-08 08:31:5369void PurgeDOMStorageContextInPartition(StoragePartition* storage_partition) {
[email protected]4c3a23582012-08-18 08:54:3470 static_cast<StoragePartitionImpl*>(storage_partition)->
71 GetDOMStorageContext()->PurgeMemory();
[email protected]55eb70e762012-02-20 17:38:3972}
73
[email protected]6939075a2012-08-28 08:35:5374void SaveSessionStateOnIOThread(
75 const scoped_refptr<net::URLRequestContextGetter>& context_getter,
76 appcache::AppCacheService* appcache_service) {
77 net::URLRequestContext* context = context_getter->GetURLRequestContext();
78 context->cookie_store()->GetCookieMonster()->
[email protected]bf510ed2012-06-05 08:31:4379 SetForceKeepSessionState();
[email protected]6939075a2012-08-28 08:35:5380 context->server_bound_cert_service()->GetCertStore()->
81 SetForceKeepSessionState();
82 appcache_service->set_force_keep_session_state();
[email protected]6e2d3d22012-02-24 18:10:3683}
84
[email protected]89acda82013-06-25 20:52:5085void SaveSessionStateOnIndexedDBThread(
[email protected]6e2d3d22012-02-24 18:10:3686 scoped_refptr<IndexedDBContextImpl> indexed_db_context) {
[email protected]bf510ed2012-06-05 08:31:4387 indexed_db_context->SetForceKeepSessionState();
[email protected]6e2d3d22012-02-24 18:10:3688}
89
[email protected]6939075a2012-08-28 08:35:5390void PurgeMemoryOnIOThread(appcache::AppCacheService* appcache_service) {
91 appcache_service->PurgeMemory();
[email protected]6e2d3d22012-02-24 18:10:3692}
93
[email protected]735e20c2012-03-20 01:16:5994} // namespace
95
[email protected]14acc642012-11-17 12:20:1096// static
97void BrowserContext::AsyncObliterateStoragePartition(
98 BrowserContext* browser_context,
[email protected]399583b2012-12-11 09:33:4299 const GURL& site,
100 const base::Closure& on_gc_required) {
101 GetStoragePartitionMap(browser_context)->AsyncObliterate(site,
102 on_gc_required);
103}
104
105// static
106void BrowserContext::GarbageCollectStoragePartitions(
107 BrowserContext* browser_context,
[email protected]2dec8ec2013-02-07 19:20:34108 scoped_ptr<base::hash_set<base::FilePath> > active_paths,
[email protected]399583b2012-12-11 09:33:42109 const base::Closure& done) {
110 GetStoragePartitionMap(browser_context)->GarbageCollect(
111 active_paths.Pass(), done);
[email protected]14acc642012-11-17 12:20:10112}
113
[email protected]b441a8492012-06-06 14:55:57114DownloadManager* BrowserContext::GetDownloadManager(
115 BrowserContext* context) {
[email protected]ecd3ad22012-07-10 20:02:40116 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]b441a8492012-06-06 14:55:57117 if (!context->GetUserData(kDownloadManagerKeyName)) {
[email protected]d25fda12012-06-12 17:05:03118 ResourceDispatcherHostImpl* rdh = ResourceDispatcherHostImpl::Get();
119 DCHECK(rdh);
[email protected]eba4a4d2013-05-29 02:18:06120 DownloadManager* download_manager =
[email protected]d25fda12012-06-12 17:05:03121 new DownloadManagerImpl(
[email protected]16798692013-04-23 18:08:38122 GetContentClient()->browser()->GetNetLog(), context);
[email protected]d25fda12012-06-12 17:05:03123
[email protected]b441a8492012-06-06 14:55:57124 context->SetUserData(
125 kDownloadManagerKeyName,
[email protected]eba4a4d2013-05-29 02:18:06126 download_manager);
[email protected]b441a8492012-06-06 14:55:57127 download_manager->SetDelegate(context->GetDownloadManagerDelegate());
[email protected]b441a8492012-06-06 14:55:57128 }
129
[email protected]eba4a4d2013-05-29 02:18:06130 return static_cast<DownloadManager*>(
131 context->GetUserData(kDownloadManagerKeyName));
[email protected]b441a8492012-06-06 14:55:57132}
133
[email protected]6ef0c3912013-01-25 22:46:34134// static
135fileapi::ExternalMountPoints* BrowserContext::GetMountPoints(
136 BrowserContext* context) {
137 // Ensure that these methods are called on the UI thread, except for
138 // unittests where a UI thread might not have been created.
139 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) ||
140 !BrowserThread::IsMessageLoopValid(BrowserThread::UI));
141
142#if defined(OS_CHROMEOS)
143 if (!context->GetUserData(kMountPointsKey)) {
144 scoped_refptr<fileapi::ExternalMountPoints> mount_points =
145 fileapi::ExternalMountPoints::CreateRefCounted();
146 context->SetUserData(
147 kMountPointsKey,
[email protected]144b6c42013-06-14 07:30:38148 new UserDataAdapter<fileapi::ExternalMountPoints>(mount_points.get()));
[email protected]6ef0c3912013-01-25 22:46:34149 }
150
151 return UserDataAdapter<fileapi::ExternalMountPoints>::Get(
152 context, kMountPointsKey);
153#else
154 return NULL;
155#endif
156}
157
[email protected]4c3a23582012-08-18 08:54:34158StoragePartition* BrowserContext::GetStoragePartition(
159 BrowserContext* browser_context,
160 SiteInstance* site_instance) {
[email protected]1bc28312012-11-08 08:31:53161 std::string partition_domain;
162 std::string partition_name;
163 bool in_memory = false;
[email protected]4c3a23582012-08-18 08:54:34164
165 // TODO(ajwong): After GetDefaultStoragePartition() is removed, get rid of
166 // this conditional and require that |site_instance| is non-NULL.
167 if (site_instance) {
[email protected]1bc28312012-11-08 08:31:53168 GetContentClient()->browser()->GetStoragePartitionConfigForSite(
[email protected]14acc642012-11-17 12:20:10169 browser_context, site_instance->GetSiteURL(), true,
[email protected]1bc28312012-11-08 08:31:53170 &partition_domain, &partition_name, &in_memory);
[email protected]4c3a23582012-08-18 08:54:34171 }
172
[email protected]1bc28312012-11-08 08:31:53173 return GetStoragePartitionFromConfig(
174 browser_context, partition_domain, partition_name, in_memory);
[email protected]4c3a23582012-08-18 08:54:34175}
176
[email protected]e94bbcb2012-09-07 05:33:57177StoragePartition* BrowserContext::GetStoragePartitionForSite(
178 BrowserContext* browser_context,
179 const GURL& site) {
[email protected]1bc28312012-11-08 08:31:53180 std::string partition_domain;
181 std::string partition_name;
182 bool in_memory;
[email protected]e94bbcb2012-09-07 05:33:57183
[email protected]1bc28312012-11-08 08:31:53184 GetContentClient()->browser()->GetStoragePartitionConfigForSite(
[email protected]14acc642012-11-17 12:20:10185 browser_context, site, true, &partition_domain, &partition_name,
186 &in_memory);
[email protected]1bc28312012-11-08 08:31:53187
188 return GetStoragePartitionFromConfig(
189 browser_context, partition_domain, partition_name, in_memory);
[email protected]e94bbcb2012-09-07 05:33:57190}
191
[email protected]4c3a23582012-08-18 08:54:34192void BrowserContext::ForEachStoragePartition(
193 BrowserContext* browser_context,
194 const StoragePartitionCallback& callback) {
195 StoragePartitionImplMap* partition_map =
196 static_cast<StoragePartitionImplMap*>(
197 browser_context->GetUserData(kStorageParitionMapKeyName));
198 if (!partition_map)
199 return;
200
201 partition_map->ForEach(callback);
202}
203
204StoragePartition* BrowserContext::GetDefaultStoragePartition(
205 BrowserContext* browser_context) {
206 return GetStoragePartition(browser_context, NULL);
[email protected]55eb70e762012-02-20 17:38:39207}
208
[email protected]314c3e22012-02-21 03:57:42209void BrowserContext::EnsureResourceContextInitialized(BrowserContext* context) {
[email protected]7e26ac92012-02-27 20:15:05210 // This will be enough to tickle initialization of BrowserContext if
211 // necessary, which initializes ResourceContext. The reason we don't call
[email protected]4c3a23582012-08-18 08:54:34212 // ResourceContext::InitializeResourceContext() directly here is that
213 // ResourceContext initialization may call back into BrowserContext
214 // and when that call returns it'll end rewriting its UserData map. It will
215 // end up rewriting the same value but this still causes a race condition.
216 //
217 // See http://crbug.com/115678.
218 GetDefaultStoragePartition(context);
[email protected]55eb70e762012-02-20 17:38:39219}
220
[email protected]6e2d3d22012-02-24 18:10:36221void BrowserContext::SaveSessionState(BrowserContext* browser_context) {
[email protected]5c8e67c2012-08-29 00:48:52222 GetDefaultStoragePartition(browser_context)->GetDatabaseTracker()->
223 SetForceKeepSessionState();
[email protected]b1b502e2012-09-16 07:31:43224 StoragePartition* storage_partition =
225 BrowserContext::GetDefaultStoragePartition(browser_context);
[email protected]6e2d3d22012-02-24 18:10:36226
227 if (BrowserThread::IsMessageLoopValid(BrowserThread::IO)) {
228 BrowserThread::PostTask(
229 BrowserThread::IO, FROM_HERE,
[email protected]6939075a2012-08-28 08:35:53230 base::Bind(
231 &SaveSessionStateOnIOThread,
232 make_scoped_refptr(browser_context->GetRequestContext()),
[email protected]b1b502e2012-09-16 07:31:43233 storage_partition->GetAppCacheService()));
[email protected]6e2d3d22012-02-24 18:10:36234 }
235
[email protected]5f2aa722013-08-07 16:59:41236 DOMStorageContextWrapper* dom_storage_context_proxy =
237 static_cast<DOMStorageContextWrapper*>(
[email protected]b1b502e2012-09-16 07:31:43238 storage_partition->GetDOMStorageContext());
[email protected]5f2aa722013-08-07 16:59:41239 dom_storage_context_proxy->SetForceKeepSessionState();
[email protected]735e20c2012-03-20 01:16:59240
[email protected]89acda82013-06-25 20:52:50241 IndexedDBContextImpl* indexed_db_context_impl =
242 static_cast<IndexedDBContextImpl*>(
[email protected]b1b502e2012-09-16 07:31:43243 storage_partition->GetIndexedDBContext());
[email protected]89acda82013-06-25 20:52:50244 // No task runner in unit tests.
245 if (indexed_db_context_impl->TaskRunner()) {
246 indexed_db_context_impl->TaskRunner()->PostTask(
247 FROM_HERE,
248 base::Bind(&SaveSessionStateOnIndexedDBThread,
249 make_scoped_refptr(indexed_db_context_impl)));
[email protected]6e2d3d22012-02-24 18:10:36250 }
251}
252
[email protected]6e2d3d22012-02-24 18:10:36253void BrowserContext::PurgeMemory(BrowserContext* browser_context) {
254 if (BrowserThread::IsMessageLoopValid(BrowserThread::IO)) {
255 BrowserThread::PostTask(
256 BrowserThread::IO, FROM_HERE,
[email protected]6939075a2012-08-28 08:35:53257 base::Bind(
258 &PurgeMemoryOnIOThread,
259 BrowserContext::GetDefaultStoragePartition(browser_context)->
260 GetAppCacheService()));
[email protected]6e2d3d22012-02-24 18:10:36261 }
262
[email protected]4c3a23582012-08-18 08:54:34263 ForEachStoragePartition(browser_context,
264 base::Bind(&PurgeDOMStorageContextInPartition));
[email protected]6e2d3d22012-02-24 18:10:36265}
[email protected]7061be92013-02-18 15:44:02266
[email protected]e0ce8a1e2012-09-18 10:26:36267#endif // !OS_IOS
[email protected]6e2d3d22012-02-24 18:10:36268
[email protected]55eb70e762012-02-20 17:38:39269BrowserContext::~BrowserContext() {
[email protected]e0ce8a1e2012-09-18 10:26:36270#if !defined(OS_IOS)
[email protected]b441a8492012-06-06 14:55:57271 if (GetUserData(kDownloadManagerKeyName))
272 GetDownloadManager(this)->Shutdown();
[email protected]e0ce8a1e2012-09-18 10:26:36273#endif
[email protected]55eb70e762012-02-20 17:38:39274}
275
276} // namespace content