blob: 5557719408c4745e4f880157c28e1a151e483f64 [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]b441a8492012-06-06 14:55:578#include "content/browser/download/download_manager_impl.h"
[email protected]393b6cb2014-05-15 00:55:129#include "content/browser/fileapi/chrome_blob_storage_context.h"
[email protected]c4d281662013-03-31 00:35:0810#include "content/browser/indexed_db/indexed_db_context_impl.h"
[email protected]678c0362012-12-05 08:02:4411#include "content/browser/loader/resource_dispatcher_host_impl.h"
[email protected]66e53d0282014-08-07 10:04:3512#include "content/browser/push_messaging_router.h"
[email protected]4c3a23582012-08-18 08:54:3413#include "content/browser/storage_partition_impl_map.h"
[email protected]d7c7c98a2012-07-12 21:27:4414#include "content/common/child_process_host_impl.h"
[email protected]393b6cb2014-05-15 00:55:1215#include "content/public/browser/blob_handle.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]6b8a3c742014-07-25 00:25:3521#include "net/ssl/channel_id_service.h"
22#include "net/ssl/channel_id_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"
pilgrime92c5fcd2014-09-10 23:31:2325#include "storage/browser/database/database_tracker.h"
26#include "storage/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";
[email protected]6ef0c3912013-01-25 22:46:3439const char kStorageParitionMapKeyName[] = "content_storage_partition_map";
[email protected]e0ce8a1e2012-09-18 10:26:3640
[email protected]9afc14e22013-09-25 22:34:1441#if defined(OS_CHROMEOS)
42const char kMountPointsKey[] = "mount_points";
43#endif // defined(OS_CHROMEOS)
44
[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]6939075a2012-08-28 08:35:5371void SaveSessionStateOnIOThread(
72 const scoped_refptr<net::URLRequestContextGetter>& context_getter,
[email protected]98d6d4562014-06-25 20:57:5573 AppCacheServiceImpl* appcache_service) {
[email protected]6939075a2012-08-28 08:35:5374 net::URLRequestContext* context = context_getter->GetURLRequestContext();
75 context->cookie_store()->GetCookieMonster()->
[email protected]bf510ed2012-06-05 08:31:4376 SetForceKeepSessionState();
[email protected]6b8a3c742014-07-25 00:25:3577 context->channel_id_service()->GetChannelIDStore()->
[email protected]6939075a2012-08-28 08:35:5378 SetForceKeepSessionState();
79 appcache_service->set_force_keep_session_state();
[email protected]6e2d3d22012-02-24 18:10:3680}
81
[email protected]89acda82013-06-25 20:52:5082void SaveSessionStateOnIndexedDBThread(
[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
falken41f4175162014-10-29 07:03:4187void ShutdownServiceWorkerContext(StoragePartition* partition) {
88 ServiceWorkerContextWrapper* wrapper =
89 static_cast<ServiceWorkerContextWrapper*>(
90 partition->GetServiceWorkerContext());
91 wrapper->process_manager()->Shutdown();
92}
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
[email protected]cd501a72014-08-22 19:58:31135storage::ExternalMountPoints* BrowserContext::GetMountPoints(
[email protected]6ef0c3912013-01-25 22:46:34136 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)) {
[email protected]cd501a72014-08-22 19:58:31144 scoped_refptr<storage::ExternalMountPoints> mount_points =
145 storage::ExternalMountPoints::CreateRefCounted();
[email protected]6ef0c3912013-01-25 22:46:34146 context->SetUserData(
147 kMountPointsKey,
[email protected]cd501a72014-08-22 19:58:31148 new UserDataAdapter<storage::ExternalMountPoints>(mount_points.get()));
[email protected]6ef0c3912013-01-25 22:46:34149 }
150
[email protected]cd501a72014-08-22 19:58:31151 return UserDataAdapter<storage::ExternalMountPoints>::Get(context,
152 kMountPointsKey);
[email protected]6ef0c3912013-01-25 22:46:34153#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]393b6cb2014-05-15 00:55:12209void BrowserContext::CreateMemoryBackedBlob(BrowserContext* browser_context,
210 const char* data, size_t length,
211 const BlobCallback& callback) {
212 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
213
214 ChromeBlobStorageContext* blob_context =
215 ChromeBlobStorageContext::GetFor(browser_context);
216 BrowserThread::PostTaskAndReplyWithResult(
217 BrowserThread::IO, FROM_HERE,
218 base::Bind(&ChromeBlobStorageContext::CreateMemoryBackedBlob,
219 make_scoped_refptr(blob_context), data, length),
220 callback);
221}
222
[email protected]66e53d0282014-08-07 10:04:35223// static
224void BrowserContext::DeliverPushMessage(
225 BrowserContext* browser_context,
226 const GURL& origin,
227 int64 service_worker_registration_id,
228 const std::string& data,
johnmea80c2552014-10-17 14:51:40229 const base::Callback<void(PushDeliveryStatus)>& callback) {
[email protected]66e53d0282014-08-07 10:04:35230 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
231 PushMessagingRouter::DeliverMessage(
232 browser_context, origin, service_worker_registration_id, data, callback);
233}
234
falken41f4175162014-10-29 07:03:41235// static
236void BrowserContext::NotifyWillBeDestroyed(BrowserContext* browser_context) {
237 // Service Workers must shutdown before the browser context is destroyed,
238 // since they keep render process hosts alive and the codebase assumes that
239 // render process hosts die before their profile (browser context) dies.
240 ForEachStoragePartition(browser_context,
241 base::Bind(ShutdownServiceWorkerContext));
242}
243
[email protected]314c3e22012-02-21 03:57:42244void BrowserContext::EnsureResourceContextInitialized(BrowserContext* context) {
[email protected]7e26ac92012-02-27 20:15:05245 // This will be enough to tickle initialization of BrowserContext if
246 // necessary, which initializes ResourceContext. The reason we don't call
[email protected]4c3a23582012-08-18 08:54:34247 // ResourceContext::InitializeResourceContext() directly here is that
248 // ResourceContext initialization may call back into BrowserContext
249 // and when that call returns it'll end rewriting its UserData map. It will
250 // end up rewriting the same value but this still causes a race condition.
251 //
252 // See http://crbug.com/115678.
253 GetDefaultStoragePartition(context);
[email protected]55eb70e762012-02-20 17:38:39254}
255
[email protected]6e2d3d22012-02-24 18:10:36256void BrowserContext::SaveSessionState(BrowserContext* browser_context) {
[email protected]5c8e67c2012-08-29 00:48:52257 GetDefaultStoragePartition(browser_context)->GetDatabaseTracker()->
258 SetForceKeepSessionState();
[email protected]b1b502e2012-09-16 07:31:43259 StoragePartition* storage_partition =
260 BrowserContext::GetDefaultStoragePartition(browser_context);
[email protected]6e2d3d22012-02-24 18:10:36261
262 if (BrowserThread::IsMessageLoopValid(BrowserThread::IO)) {
263 BrowserThread::PostTask(
264 BrowserThread::IO, FROM_HERE,
[email protected]6939075a2012-08-28 08:35:53265 base::Bind(
266 &SaveSessionStateOnIOThread,
267 make_scoped_refptr(browser_context->GetRequestContext()),
[email protected]98d6d4562014-06-25 20:57:55268 static_cast<AppCacheServiceImpl*>(
[email protected]63ef85512014-06-05 14:21:26269 storage_partition->GetAppCacheService())));
[email protected]6e2d3d22012-02-24 18:10:36270 }
271
[email protected]5f2aa722013-08-07 16:59:41272 DOMStorageContextWrapper* dom_storage_context_proxy =
273 static_cast<DOMStorageContextWrapper*>(
[email protected]b1b502e2012-09-16 07:31:43274 storage_partition->GetDOMStorageContext());
[email protected]5f2aa722013-08-07 16:59:41275 dom_storage_context_proxy->SetForceKeepSessionState();
[email protected]735e20c2012-03-20 01:16:59276
[email protected]89acda82013-06-25 20:52:50277 IndexedDBContextImpl* indexed_db_context_impl =
278 static_cast<IndexedDBContextImpl*>(
[email protected]b1b502e2012-09-16 07:31:43279 storage_partition->GetIndexedDBContext());
[email protected]89acda82013-06-25 20:52:50280 // No task runner in unit tests.
281 if (indexed_db_context_impl->TaskRunner()) {
282 indexed_db_context_impl->TaskRunner()->PostTask(
283 FROM_HERE,
284 base::Bind(&SaveSessionStateOnIndexedDBThread,
285 make_scoped_refptr(indexed_db_context_impl)));
[email protected]6e2d3d22012-02-24 18:10:36286 }
287}
288
[email protected]e0ce8a1e2012-09-18 10:26:36289#endif // !OS_IOS
[email protected]6e2d3d22012-02-24 18:10:36290
[email protected]55eb70e762012-02-20 17:38:39291BrowserContext::~BrowserContext() {
[email protected]e0ce8a1e2012-09-18 10:26:36292#if !defined(OS_IOS)
[email protected]b441a8492012-06-06 14:55:57293 if (GetUserData(kDownloadManagerKeyName))
294 GetDownloadManager(this)->Shutdown();
[email protected]e0ce8a1e2012-09-18 10:26:36295#endif
[email protected]55eb70e762012-02-20 17:38:39296}
297
[email protected]55eb70e762012-02-20 17:38:39298} // namespace content