blob: d730da2ecbabf6af26b303fad4b666b598efb021 [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]6ef0c3912013-01-25 22:46:348#include "base/path_service.h"
[email protected]55eb70e762012-02-20 17:38:399#include "content/browser/appcache/chrome_appcache_service.h"
[email protected]1ea3c792012-04-17 01:25:0410#include "content/browser/dom_storage/dom_storage_context_impl.h"
[email protected]b441a8492012-06-06 14:55:5711#include "content/browser/download/download_manager_impl.h"
[email protected]c4d281662013-03-31 00:35:0812#include "content/browser/indexed_db/indexed_db_context_impl.h"
[email protected]678c0362012-12-05 08:02:4413#include "content/browser/loader/resource_dispatcher_host_impl.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]536fd0b2013-03-14 17:41:5719#include "content/public/browser/site_instance.h"
[email protected]4d7c4ef2012-03-16 01:47:1220#include "net/cookies/cookie_monster.h"
21#include "net/cookies/cookie_store.h"
[email protected]536fd0b2013-03-14 17:41:5722#include "net/ssl/server_bound_cert_service.h"
23#include "net/ssl/server_bound_cert_store.h"
[email protected]6e2d3d22012-02-24 18:10:3624#include "net/url_request/url_request_context.h"
[email protected]6939075a2012-08-28 08:35:5325#include "net/url_request/url_request_context_getter.h"
[email protected]7061be92013-02-18 15:44:0226#include "ui/base/clipboard/clipboard.h"
[email protected]08b1f75f2013-05-22 22:02:3827#include "webkit/browser/database/database_tracker.h"
[email protected]f25e1132013-05-24 13:58:0428#include "webkit/browser/fileapi/external_mount_points.h"
[email protected]e0ce8a1e2012-09-18 10:26:3629#endif // !OS_IOS
[email protected]55eb70e762012-02-20 17:38:3930
[email protected]314c3e22012-02-21 03:57:4231using base::UserDataAdapter;
[email protected]55eb70e762012-02-20 17:38:3932
[email protected]55eb70e762012-02-20 17:38:3933namespace content {
34
[email protected]e0ce8a1e2012-09-18 10:26:3635// Only ~BrowserContext() is needed on iOS.
36#if !defined(OS_IOS)
[email protected]735e20c2012-03-20 01:16:5937namespace {
38
[email protected]e0ce8a1e2012-09-18 10:26:3639// Key names on BrowserContext.
[email protected]7061be92013-02-18 15:44:0240const char kClipboardDestroyerKey[] = "clipboard_destroyer";
[email protected]6ef0c3912013-01-25 22:46:3441const char kDownloadManagerKeyName[] = "download_manager";
42const char kMountPointsKey[] = "mount_points";
43const char kStorageParitionMapKeyName[] = "content_storage_partition_map";
[email protected]e0ce8a1e2012-09-18 10:26:3644
[email protected]14acc642012-11-17 12:20:1045StoragePartitionImplMap* GetStoragePartitionMap(
46 BrowserContext* browser_context) {
[email protected]4c3a23582012-08-18 08:54:3447 StoragePartitionImplMap* partition_map =
48 static_cast<StoragePartitionImplMap*>(
49 browser_context->GetUserData(kStorageParitionMapKeyName));
[email protected]d7c7c98a2012-07-12 21:27:4450 if (!partition_map) {
[email protected]4c3a23582012-08-18 08:54:3451 partition_map = new StoragePartitionImplMap(browser_context);
[email protected]d7c7c98a2012-07-12 21:27:4452 browser_context->SetUserData(kStorageParitionMapKeyName, partition_map);
53 }
[email protected]14acc642012-11-17 12:20:1054 return partition_map;
55}
56
57StoragePartition* GetStoragePartitionFromConfig(
58 BrowserContext* browser_context,
59 const std::string& partition_domain,
60 const std::string& partition_name,
61 bool in_memory) {
62 StoragePartitionImplMap* partition_map =
63 GetStoragePartitionMap(browser_context);
[email protected]d7c7c98a2012-07-12 21:27:4464
[email protected]1bc28312012-11-08 08:31:5365 if (browser_context->IsOffTheRecord())
66 in_memory = true;
67
[email protected]1bc28312012-11-08 08:31:5368 return partition_map->Get(partition_domain, partition_name, in_memory);
[email protected]d1198fd2012-08-13 22:50:1969}
70
[email protected]d7c7c98a2012-07-12 21:27:4471// Run |callback| on each DOMStorageContextImpl in |browser_context|.
[email protected]1bc28312012-11-08 08:31:5372void PurgeDOMStorageContextInPartition(StoragePartition* storage_partition) {
[email protected]4c3a23582012-08-18 08:54:3473 static_cast<StoragePartitionImpl*>(storage_partition)->
74 GetDOMStorageContext()->PurgeMemory();
[email protected]55eb70e762012-02-20 17:38:3975}
76
[email protected]6939075a2012-08-28 08:35:5377void SaveSessionStateOnIOThread(
78 const scoped_refptr<net::URLRequestContextGetter>& context_getter,
79 appcache::AppCacheService* appcache_service) {
80 net::URLRequestContext* context = context_getter->GetURLRequestContext();
81 context->cookie_store()->GetCookieMonster()->
[email protected]bf510ed2012-06-05 08:31:4382 SetForceKeepSessionState();
[email protected]6939075a2012-08-28 08:35:5383 context->server_bound_cert_service()->GetCertStore()->
84 SetForceKeepSessionState();
85 appcache_service->set_force_keep_session_state();
[email protected]6e2d3d22012-02-24 18:10:3686}
87
88void SaveSessionStateOnWebkitThread(
[email protected]6e2d3d22012-02-24 18:10:3689 scoped_refptr<IndexedDBContextImpl> indexed_db_context) {
[email protected]bf510ed2012-06-05 08:31:4390 indexed_db_context->SetForceKeepSessionState();
[email protected]6e2d3d22012-02-24 18:10:3691}
92
[email protected]6939075a2012-08-28 08:35:5393void PurgeMemoryOnIOThread(appcache::AppCacheService* appcache_service) {
94 appcache_service->PurgeMemory();
[email protected]6e2d3d22012-02-24 18:10:3695}
96
[email protected]7061be92013-02-18 15:44:0297// OffTheRecordClipboardDestroyer is supposed to clear the clipboard in
98// destructor if current clipboard content came from corresponding OffTheRecord
99// browser context.
100class OffTheRecordClipboardDestroyer : public base::SupportsUserData::Data {
101 public:
102 virtual ~OffTheRecordClipboardDestroyer() {
103 ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
104 ExamineClipboard(clipboard, ui::Clipboard::BUFFER_STANDARD);
105 if (ui::Clipboard::IsValidBuffer(ui::Clipboard::BUFFER_SELECTION))
106 ExamineClipboard(clipboard, ui::Clipboard::BUFFER_SELECTION);
107 }
108
[email protected]02fb6a402013-04-26 07:20:57109 ui::SourceTag GetAsSourceTag() {
110 return ui::SourceTag(this);
[email protected]7061be92013-02-18 15:44:02111 }
112
113 private:
114 void ExamineClipboard(ui::Clipboard* clipboard,
115 ui::Clipboard::Buffer buffer) {
[email protected]02fb6a402013-04-26 07:20:57116 ui::SourceTag source_tag = clipboard->ReadSourceTag(buffer);
117 if (source_tag == ui::SourceTag(this))
[email protected]45c4fe63c2013-04-02 17:36:30118 clipboard->Clear(buffer);
[email protected]7061be92013-02-18 15:44:02119 }
120};
121
122// Returns existing OffTheRecordClipboardDestroyer or creates one.
123OffTheRecordClipboardDestroyer* GetClipboardDestroyerForBrowserContext(
124 BrowserContext* context) {
125 if (base::SupportsUserData::Data* data = context->GetUserData(
126 kClipboardDestroyerKey))
127 return static_cast<OffTheRecordClipboardDestroyer*>(data);
128 OffTheRecordClipboardDestroyer* data = new OffTheRecordClipboardDestroyer;
129 context->SetUserData(kClipboardDestroyerKey, data);
130 return data;
131}
132
[email protected]735e20c2012-03-20 01:16:59133} // namespace
134
[email protected]14acc642012-11-17 12:20:10135// static
136void BrowserContext::AsyncObliterateStoragePartition(
137 BrowserContext* browser_context,
[email protected]399583b2012-12-11 09:33:42138 const GURL& site,
139 const base::Closure& on_gc_required) {
140 GetStoragePartitionMap(browser_context)->AsyncObliterate(site,
141 on_gc_required);
142}
143
144// static
145void BrowserContext::GarbageCollectStoragePartitions(
146 BrowserContext* browser_context,
[email protected]2dec8ec2013-02-07 19:20:34147 scoped_ptr<base::hash_set<base::FilePath> > active_paths,
[email protected]399583b2012-12-11 09:33:42148 const base::Closure& done) {
149 GetStoragePartitionMap(browser_context)->GarbageCollect(
150 active_paths.Pass(), done);
[email protected]14acc642012-11-17 12:20:10151}
152
[email protected]b441a8492012-06-06 14:55:57153DownloadManager* BrowserContext::GetDownloadManager(
154 BrowserContext* context) {
[email protected]ecd3ad22012-07-10 20:02:40155 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]b441a8492012-06-06 14:55:57156 if (!context->GetUserData(kDownloadManagerKeyName)) {
[email protected]d25fda12012-06-12 17:05:03157 ResourceDispatcherHostImpl* rdh = ResourceDispatcherHostImpl::Get();
158 DCHECK(rdh);
[email protected]d25fda12012-06-12 17:05:03159 scoped_refptr<DownloadManager> download_manager =
160 new DownloadManagerImpl(
[email protected]16798692013-04-23 18:08:38161 GetContentClient()->browser()->GetNetLog(), context);
[email protected]d25fda12012-06-12 17:05:03162
[email protected]b441a8492012-06-06 14:55:57163 context->SetUserData(
164 kDownloadManagerKeyName,
165 new UserDataAdapter<DownloadManager>(download_manager));
166 download_manager->SetDelegate(context->GetDownloadManagerDelegate());
[email protected]b441a8492012-06-06 14:55:57167 }
168
169 return UserDataAdapter<DownloadManager>::Get(
170 context, kDownloadManagerKeyName);
171}
172
[email protected]6ef0c3912013-01-25 22:46:34173// static
174fileapi::ExternalMountPoints* BrowserContext::GetMountPoints(
175 BrowserContext* context) {
176 // Ensure that these methods are called on the UI thread, except for
177 // unittests where a UI thread might not have been created.
178 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) ||
179 !BrowserThread::IsMessageLoopValid(BrowserThread::UI));
180
181#if defined(OS_CHROMEOS)
182 if (!context->GetUserData(kMountPointsKey)) {
183 scoped_refptr<fileapi::ExternalMountPoints> mount_points =
184 fileapi::ExternalMountPoints::CreateRefCounted();
185 context->SetUserData(
186 kMountPointsKey,
187 new UserDataAdapter<fileapi::ExternalMountPoints>(
188 mount_points));
189
190 // Add Downloads mount point.
[email protected]2dec8ec2013-02-07 19:20:34191 base::FilePath home_path;
[email protected]6ef0c3912013-01-25 22:46:34192 if (PathService::Get(base::DIR_HOME, &home_path)) {
193 mount_points->RegisterFileSystem(
194 "Downloads",
195 fileapi::kFileSystemTypeNativeLocal,
196 home_path.AppendASCII("Downloads"));
197 }
198 }
199
200 return UserDataAdapter<fileapi::ExternalMountPoints>::Get(
201 context, kMountPointsKey);
202#else
203 return NULL;
204#endif
205}
206
[email protected]4c3a23582012-08-18 08:54:34207StoragePartition* BrowserContext::GetStoragePartition(
208 BrowserContext* browser_context,
209 SiteInstance* site_instance) {
[email protected]1bc28312012-11-08 08:31:53210 std::string partition_domain;
211 std::string partition_name;
212 bool in_memory = false;
[email protected]4c3a23582012-08-18 08:54:34213
214 // TODO(ajwong): After GetDefaultStoragePartition() is removed, get rid of
215 // this conditional and require that |site_instance| is non-NULL.
216 if (site_instance) {
[email protected]1bc28312012-11-08 08:31:53217 GetContentClient()->browser()->GetStoragePartitionConfigForSite(
[email protected]14acc642012-11-17 12:20:10218 browser_context, site_instance->GetSiteURL(), true,
[email protected]1bc28312012-11-08 08:31:53219 &partition_domain, &partition_name, &in_memory);
[email protected]4c3a23582012-08-18 08:54:34220 }
221
[email protected]1bc28312012-11-08 08:31:53222 return GetStoragePartitionFromConfig(
223 browser_context, partition_domain, partition_name, in_memory);
[email protected]4c3a23582012-08-18 08:54:34224}
225
[email protected]e94bbcb2012-09-07 05:33:57226StoragePartition* BrowserContext::GetStoragePartitionForSite(
227 BrowserContext* browser_context,
228 const GURL& site) {
[email protected]1bc28312012-11-08 08:31:53229 std::string partition_domain;
230 std::string partition_name;
231 bool in_memory;
[email protected]e94bbcb2012-09-07 05:33:57232
[email protected]1bc28312012-11-08 08:31:53233 GetContentClient()->browser()->GetStoragePartitionConfigForSite(
[email protected]14acc642012-11-17 12:20:10234 browser_context, site, true, &partition_domain, &partition_name,
235 &in_memory);
[email protected]1bc28312012-11-08 08:31:53236
237 return GetStoragePartitionFromConfig(
238 browser_context, partition_domain, partition_name, in_memory);
[email protected]e94bbcb2012-09-07 05:33:57239}
240
[email protected]4c3a23582012-08-18 08:54:34241void BrowserContext::ForEachStoragePartition(
242 BrowserContext* browser_context,
243 const StoragePartitionCallback& callback) {
244 StoragePartitionImplMap* partition_map =
245 static_cast<StoragePartitionImplMap*>(
246 browser_context->GetUserData(kStorageParitionMapKeyName));
247 if (!partition_map)
248 return;
249
250 partition_map->ForEach(callback);
251}
252
253StoragePartition* BrowserContext::GetDefaultStoragePartition(
254 BrowserContext* browser_context) {
255 return GetStoragePartition(browser_context, NULL);
[email protected]55eb70e762012-02-20 17:38:39256}
257
[email protected]314c3e22012-02-21 03:57:42258void BrowserContext::EnsureResourceContextInitialized(BrowserContext* context) {
[email protected]7e26ac92012-02-27 20:15:05259 // This will be enough to tickle initialization of BrowserContext if
260 // necessary, which initializes ResourceContext. The reason we don't call
[email protected]4c3a23582012-08-18 08:54:34261 // ResourceContext::InitializeResourceContext() directly here is that
262 // ResourceContext initialization may call back into BrowserContext
263 // and when that call returns it'll end rewriting its UserData map. It will
264 // end up rewriting the same value but this still causes a race condition.
265 //
266 // See http://crbug.com/115678.
267 GetDefaultStoragePartition(context);
[email protected]55eb70e762012-02-20 17:38:39268}
269
[email protected]6e2d3d22012-02-24 18:10:36270void BrowserContext::SaveSessionState(BrowserContext* browser_context) {
[email protected]5c8e67c2012-08-29 00:48:52271 GetDefaultStoragePartition(browser_context)->GetDatabaseTracker()->
272 SetForceKeepSessionState();
[email protected]b1b502e2012-09-16 07:31:43273 StoragePartition* storage_partition =
274 BrowserContext::GetDefaultStoragePartition(browser_context);
[email protected]6e2d3d22012-02-24 18:10:36275
276 if (BrowserThread::IsMessageLoopValid(BrowserThread::IO)) {
277 BrowserThread::PostTask(
278 BrowserThread::IO, FROM_HERE,
[email protected]6939075a2012-08-28 08:35:53279 base::Bind(
280 &SaveSessionStateOnIOThread,
281 make_scoped_refptr(browser_context->GetRequestContext()),
[email protected]b1b502e2012-09-16 07:31:43282 storage_partition->GetAppCacheService()));
[email protected]6e2d3d22012-02-24 18:10:36283 }
284
[email protected]4c3a23582012-08-18 08:54:34285 DOMStorageContextImpl* dom_storage_context_impl =
286 static_cast<DOMStorageContextImpl*>(
[email protected]b1b502e2012-09-16 07:31:43287 storage_partition->GetDOMStorageContext());
[email protected]4c3a23582012-08-18 08:54:34288 dom_storage_context_impl->SetForceKeepSessionState();
[email protected]735e20c2012-03-20 01:16:59289
[email protected]6e2d3d22012-02-24 18:10:36290 if (BrowserThread::IsMessageLoopValid(BrowserThread::WEBKIT_DEPRECATED)) {
[email protected]6e2d3d22012-02-24 18:10:36291 IndexedDBContextImpl* indexed_db = static_cast<IndexedDBContextImpl*>(
[email protected]b1b502e2012-09-16 07:31:43292 storage_partition->GetIndexedDBContext());
[email protected]6e2d3d22012-02-24 18:10:36293 BrowserThread::PostTask(
294 BrowserThread::WEBKIT_DEPRECATED, FROM_HERE,
295 base::Bind(&SaveSessionStateOnWebkitThread,
[email protected]6e2d3d22012-02-24 18:10:36296 make_scoped_refptr(indexed_db)));
297 }
298}
299
[email protected]6e2d3d22012-02-24 18:10:36300void BrowserContext::PurgeMemory(BrowserContext* browser_context) {
301 if (BrowserThread::IsMessageLoopValid(BrowserThread::IO)) {
302 BrowserThread::PostTask(
303 BrowserThread::IO, FROM_HERE,
[email protected]6939075a2012-08-28 08:35:53304 base::Bind(
305 &PurgeMemoryOnIOThread,
306 BrowserContext::GetDefaultStoragePartition(browser_context)->
307 GetAppCacheService()));
[email protected]6e2d3d22012-02-24 18:10:36308 }
309
[email protected]4c3a23582012-08-18 08:54:34310 ForEachStoragePartition(browser_context,
311 base::Bind(&PurgeDOMStorageContextInPartition));
[email protected]6e2d3d22012-02-24 18:10:36312}
[email protected]7061be92013-02-18 15:44:02313
[email protected]02fb6a402013-04-26 07:20:57314ui::SourceTag BrowserContext::GetMarkerForOffTheRecordContext(
[email protected]7061be92013-02-18 15:44:02315 BrowserContext* context) {
316 if (context && context->IsOffTheRecord()) {
317 OffTheRecordClipboardDestroyer* clipboard_destroyer =
318 GetClipboardDestroyerForBrowserContext(context);
319
320 return clipboard_destroyer->GetAsSourceTag();
321 }
[email protected]02fb6a402013-04-26 07:20:57322 return ui::SourceTag();
[email protected]7061be92013-02-18 15:44:02323}
[email protected]e0ce8a1e2012-09-18 10:26:36324#endif // !OS_IOS
[email protected]6e2d3d22012-02-24 18:10:36325
[email protected]55eb70e762012-02-20 17:38:39326BrowserContext::~BrowserContext() {
[email protected]e0ce8a1e2012-09-18 10:26:36327#if !defined(OS_IOS)
[email protected]b441a8492012-06-06 14:55:57328 if (GetUserData(kDownloadManagerKeyName))
329 GetDownloadManager(this)->Shutdown();
[email protected]e0ce8a1e2012-09-18 10:26:36330#endif
[email protected]55eb70e762012-02-20 17:38:39331}
332
333} // namespace content