blob: b0c63338d1eda582febf294000565e0ce187de01 [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]53ac00e82012-10-18 20:59:2027#include "webkit/database/database_tracker.h"
[email protected]6ef0c3912013-01-25 22:46:3428#include "webkit/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
109 ui::Clipboard::SourceTag GetAsSourceTag() {
110 return ui::Clipboard::SourceTag(this);
111 }
112
113 private:
114 void ExamineClipboard(ui::Clipboard* clipboard,
115 ui::Clipboard::Buffer buffer) {
116 ui::Clipboard::SourceTag source_tag = clipboard->ReadSourceTag(buffer);
[email protected]c978cb9b2013-02-27 11:06:23117 if (source_tag == ui::Clipboard::SourceTag(this)) {
118 if (buffer == ui::Clipboard::BUFFER_STANDARD) {
119 // We want to leave invalid SourceTag in the clipboard in order to
120 // collect statistics later.
121 clipboard->WriteObjects(buffer,
122 ui::Clipboard::ObjectMap(),
123 ui::Clipboard::kInvalidSourceTag);
124 } else {
125 clipboard->Clear(buffer);
126 }
127 }
[email protected]7061be92013-02-18 15:44:02128 }
129};
130
131// Returns existing OffTheRecordClipboardDestroyer or creates one.
132OffTheRecordClipboardDestroyer* GetClipboardDestroyerForBrowserContext(
133 BrowserContext* context) {
134 if (base::SupportsUserData::Data* data = context->GetUserData(
135 kClipboardDestroyerKey))
136 return static_cast<OffTheRecordClipboardDestroyer*>(data);
137 OffTheRecordClipboardDestroyer* data = new OffTheRecordClipboardDestroyer;
138 context->SetUserData(kClipboardDestroyerKey, data);
139 return data;
140}
141
[email protected]735e20c2012-03-20 01:16:59142} // namespace
143
[email protected]14acc642012-11-17 12:20:10144// static
145void BrowserContext::AsyncObliterateStoragePartition(
146 BrowserContext* browser_context,
[email protected]399583b2012-12-11 09:33:42147 const GURL& site,
148 const base::Closure& on_gc_required) {
149 GetStoragePartitionMap(browser_context)->AsyncObliterate(site,
150 on_gc_required);
151}
152
153// static
154void BrowserContext::GarbageCollectStoragePartitions(
155 BrowserContext* browser_context,
[email protected]2dec8ec2013-02-07 19:20:34156 scoped_ptr<base::hash_set<base::FilePath> > active_paths,
[email protected]399583b2012-12-11 09:33:42157 const base::Closure& done) {
158 GetStoragePartitionMap(browser_context)->GarbageCollect(
159 active_paths.Pass(), done);
[email protected]14acc642012-11-17 12:20:10160}
161
[email protected]b441a8492012-06-06 14:55:57162DownloadManager* BrowserContext::GetDownloadManager(
163 BrowserContext* context) {
[email protected]ecd3ad22012-07-10 20:02:40164 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]b441a8492012-06-06 14:55:57165 if (!context->GetUserData(kDownloadManagerKeyName)) {
[email protected]d25fda12012-06-12 17:05:03166 ResourceDispatcherHostImpl* rdh = ResourceDispatcherHostImpl::Get();
167 DCHECK(rdh);
[email protected]d25fda12012-06-12 17:05:03168 scoped_refptr<DownloadManager> download_manager =
169 new DownloadManagerImpl(
[email protected]d25fda12012-06-12 17:05:03170 GetContentClient()->browser()->GetNetLog());
171
[email protected]b441a8492012-06-06 14:55:57172 context->SetUserData(
173 kDownloadManagerKeyName,
174 new UserDataAdapter<DownloadManager>(download_manager));
175 download_manager->SetDelegate(context->GetDownloadManagerDelegate());
176 download_manager->Init(context);
177 }
178
179 return UserDataAdapter<DownloadManager>::Get(
180 context, kDownloadManagerKeyName);
181}
182
[email protected]6ef0c3912013-01-25 22:46:34183// static
184fileapi::ExternalMountPoints* BrowserContext::GetMountPoints(
185 BrowserContext* context) {
186 // Ensure that these methods are called on the UI thread, except for
187 // unittests where a UI thread might not have been created.
188 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) ||
189 !BrowserThread::IsMessageLoopValid(BrowserThread::UI));
190
191#if defined(OS_CHROMEOS)
192 if (!context->GetUserData(kMountPointsKey)) {
193 scoped_refptr<fileapi::ExternalMountPoints> mount_points =
194 fileapi::ExternalMountPoints::CreateRefCounted();
195 context->SetUserData(
196 kMountPointsKey,
197 new UserDataAdapter<fileapi::ExternalMountPoints>(
198 mount_points));
199
200 // Add Downloads mount point.
[email protected]2dec8ec2013-02-07 19:20:34201 base::FilePath home_path;
[email protected]6ef0c3912013-01-25 22:46:34202 if (PathService::Get(base::DIR_HOME, &home_path)) {
203 mount_points->RegisterFileSystem(
204 "Downloads",
205 fileapi::kFileSystemTypeNativeLocal,
206 home_path.AppendASCII("Downloads"));
207 }
208 }
209
210 return UserDataAdapter<fileapi::ExternalMountPoints>::Get(
211 context, kMountPointsKey);
212#else
213 return NULL;
214#endif
215}
216
[email protected]4c3a23582012-08-18 08:54:34217StoragePartition* BrowserContext::GetStoragePartition(
218 BrowserContext* browser_context,
219 SiteInstance* site_instance) {
[email protected]1bc28312012-11-08 08:31:53220 std::string partition_domain;
221 std::string partition_name;
222 bool in_memory = false;
[email protected]4c3a23582012-08-18 08:54:34223
224 // TODO(ajwong): After GetDefaultStoragePartition() is removed, get rid of
225 // this conditional and require that |site_instance| is non-NULL.
226 if (site_instance) {
[email protected]1bc28312012-11-08 08:31:53227 GetContentClient()->browser()->GetStoragePartitionConfigForSite(
[email protected]14acc642012-11-17 12:20:10228 browser_context, site_instance->GetSiteURL(), true,
[email protected]1bc28312012-11-08 08:31:53229 &partition_domain, &partition_name, &in_memory);
[email protected]4c3a23582012-08-18 08:54:34230 }
231
[email protected]1bc28312012-11-08 08:31:53232 return GetStoragePartitionFromConfig(
233 browser_context, partition_domain, partition_name, in_memory);
[email protected]4c3a23582012-08-18 08:54:34234}
235
[email protected]e94bbcb2012-09-07 05:33:57236StoragePartition* BrowserContext::GetStoragePartitionForSite(
237 BrowserContext* browser_context,
238 const GURL& site) {
[email protected]1bc28312012-11-08 08:31:53239 std::string partition_domain;
240 std::string partition_name;
241 bool in_memory;
[email protected]e94bbcb2012-09-07 05:33:57242
[email protected]1bc28312012-11-08 08:31:53243 GetContentClient()->browser()->GetStoragePartitionConfigForSite(
[email protected]14acc642012-11-17 12:20:10244 browser_context, site, true, &partition_domain, &partition_name,
245 &in_memory);
[email protected]1bc28312012-11-08 08:31:53246
247 return GetStoragePartitionFromConfig(
248 browser_context, partition_domain, partition_name, in_memory);
[email protected]e94bbcb2012-09-07 05:33:57249}
250
[email protected]4c3a23582012-08-18 08:54:34251void BrowserContext::ForEachStoragePartition(
252 BrowserContext* browser_context,
253 const StoragePartitionCallback& callback) {
254 StoragePartitionImplMap* partition_map =
255 static_cast<StoragePartitionImplMap*>(
256 browser_context->GetUserData(kStorageParitionMapKeyName));
257 if (!partition_map)
258 return;
259
260 partition_map->ForEach(callback);
261}
262
263StoragePartition* BrowserContext::GetDefaultStoragePartition(
264 BrowserContext* browser_context) {
265 return GetStoragePartition(browser_context, NULL);
[email protected]55eb70e762012-02-20 17:38:39266}
267
[email protected]314c3e22012-02-21 03:57:42268void BrowserContext::EnsureResourceContextInitialized(BrowserContext* context) {
[email protected]7e26ac92012-02-27 20:15:05269 // This will be enough to tickle initialization of BrowserContext if
270 // necessary, which initializes ResourceContext. The reason we don't call
[email protected]4c3a23582012-08-18 08:54:34271 // ResourceContext::InitializeResourceContext() directly here is that
272 // ResourceContext initialization may call back into BrowserContext
273 // and when that call returns it'll end rewriting its UserData map. It will
274 // end up rewriting the same value but this still causes a race condition.
275 //
276 // See http://crbug.com/115678.
277 GetDefaultStoragePartition(context);
[email protected]55eb70e762012-02-20 17:38:39278}
279
[email protected]6e2d3d22012-02-24 18:10:36280void BrowserContext::SaveSessionState(BrowserContext* browser_context) {
[email protected]5c8e67c2012-08-29 00:48:52281 GetDefaultStoragePartition(browser_context)->GetDatabaseTracker()->
282 SetForceKeepSessionState();
[email protected]b1b502e2012-09-16 07:31:43283 StoragePartition* storage_partition =
284 BrowserContext::GetDefaultStoragePartition(browser_context);
[email protected]6e2d3d22012-02-24 18:10:36285
286 if (BrowserThread::IsMessageLoopValid(BrowserThread::IO)) {
287 BrowserThread::PostTask(
288 BrowserThread::IO, FROM_HERE,
[email protected]6939075a2012-08-28 08:35:53289 base::Bind(
290 &SaveSessionStateOnIOThread,
291 make_scoped_refptr(browser_context->GetRequestContext()),
[email protected]b1b502e2012-09-16 07:31:43292 storage_partition->GetAppCacheService()));
[email protected]6e2d3d22012-02-24 18:10:36293 }
294
[email protected]4c3a23582012-08-18 08:54:34295 DOMStorageContextImpl* dom_storage_context_impl =
296 static_cast<DOMStorageContextImpl*>(
[email protected]b1b502e2012-09-16 07:31:43297 storage_partition->GetDOMStorageContext());
[email protected]4c3a23582012-08-18 08:54:34298 dom_storage_context_impl->SetForceKeepSessionState();
[email protected]735e20c2012-03-20 01:16:59299
[email protected]6e2d3d22012-02-24 18:10:36300 if (BrowserThread::IsMessageLoopValid(BrowserThread::WEBKIT_DEPRECATED)) {
[email protected]6e2d3d22012-02-24 18:10:36301 IndexedDBContextImpl* indexed_db = static_cast<IndexedDBContextImpl*>(
[email protected]b1b502e2012-09-16 07:31:43302 storage_partition->GetIndexedDBContext());
[email protected]6e2d3d22012-02-24 18:10:36303 BrowserThread::PostTask(
304 BrowserThread::WEBKIT_DEPRECATED, FROM_HERE,
305 base::Bind(&SaveSessionStateOnWebkitThread,
[email protected]6e2d3d22012-02-24 18:10:36306 make_scoped_refptr(indexed_db)));
307 }
308}
309
[email protected]6e2d3d22012-02-24 18:10:36310void BrowserContext::PurgeMemory(BrowserContext* browser_context) {
311 if (BrowserThread::IsMessageLoopValid(BrowserThread::IO)) {
312 BrowserThread::PostTask(
313 BrowserThread::IO, FROM_HERE,
[email protected]6939075a2012-08-28 08:35:53314 base::Bind(
315 &PurgeMemoryOnIOThread,
316 BrowserContext::GetDefaultStoragePartition(browser_context)->
317 GetAppCacheService()));
[email protected]6e2d3d22012-02-24 18:10:36318 }
319
[email protected]4c3a23582012-08-18 08:54:34320 ForEachStoragePartition(browser_context,
321 base::Bind(&PurgeDOMStorageContextInPartition));
[email protected]6e2d3d22012-02-24 18:10:36322}
[email protected]7061be92013-02-18 15:44:02323
324ui::Clipboard::SourceTag BrowserContext::GetMarkerForOffTheRecordContext(
325 BrowserContext* context) {
326 if (context && context->IsOffTheRecord()) {
327 OffTheRecordClipboardDestroyer* clipboard_destroyer =
328 GetClipboardDestroyerForBrowserContext(context);
329
330 return clipboard_destroyer->GetAsSourceTag();
331 }
332 return ui::Clipboard::SourceTag();
333}
[email protected]e0ce8a1e2012-09-18 10:26:36334#endif // !OS_IOS
[email protected]6e2d3d22012-02-24 18:10:36335
[email protected]55eb70e762012-02-20 17:38:39336BrowserContext::~BrowserContext() {
[email protected]e0ce8a1e2012-09-18 10:26:36337#if !defined(OS_IOS)
[email protected]b441a8492012-06-06 14:55:57338 if (GetUserData(kDownloadManagerKeyName))
339 GetDownloadManager(this)->Shutdown();
[email protected]e0ce8a1e2012-09-18 10:26:36340#endif
[email protected]55eb70e762012-02-20 17:38:39341}
342
343} // namespace content