[email protected] | 55eb70e76 | 2012-02-20 17:38:39 | [diff] [blame] | 1 | // 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] | e0ce8a1e | 2012-09-18 10:26:36 | [diff] [blame] | 7 | #if !defined(OS_IOS) |
[email protected] | 55eb70e76 | 2012-02-20 17:38:39 | [diff] [blame] | 8 | #include "content/browser/appcache/chrome_appcache_service.h" |
[email protected] | 1ea3c79 | 2012-04-17 01:25:04 | [diff] [blame] | 9 | #include "content/browser/dom_storage/dom_storage_context_impl.h" |
[email protected] | b441a849 | 2012-06-06 14:55:57 | [diff] [blame] | 10 | #include "content/browser/download/download_manager_impl.h" |
[email protected] | 6e2d3d2 | 2012-02-24 18:10:36 | [diff] [blame] | 11 | #include "content/browser/in_process_webkit/indexed_db_context_impl.h" |
[email protected] | d25fda1 | 2012-06-12 17:05:03 | [diff] [blame] | 12 | #include "content/browser/renderer_host/resource_dispatcher_host_impl.h" |
[email protected] | e94bbcb | 2012-09-07 05:33:57 | [diff] [blame] | 13 | #include "content/public/browser/site_instance.h" |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 14 | #include "content/browser/storage_partition_impl.h" |
| 15 | #include "content/browser/storage_partition_impl_map.h" |
[email protected] | d7c7c98a | 2012-07-12 21:27:44 | [diff] [blame] | 16 | #include "content/common/child_process_host_impl.h" |
[email protected] | 55eb70e76 | 2012-02-20 17:38:39 | [diff] [blame] | 17 | #include "content/public/browser/browser_thread.h" |
[email protected] | b441a849 | 2012-06-06 14:55:57 | [diff] [blame] | 18 | #include "content/public/browser/content_browser_client.h" |
[email protected] | bf510ed | 2012-06-05 08:31:43 | [diff] [blame] | 19 | #include "net/base/server_bound_cert_service.h" |
| 20 | #include "net/base/server_bound_cert_store.h" |
[email protected] | 4d7c4ef | 2012-03-16 01:47:12 | [diff] [blame] | 21 | #include "net/cookies/cookie_monster.h" |
| 22 | #include "net/cookies/cookie_store.h" |
[email protected] | 6e2d3d2 | 2012-02-24 18:10:36 | [diff] [blame] | 23 | #include "net/url_request/url_request_context.h" |
[email protected] | 6939075a | 2012-08-28 08:35:53 | [diff] [blame] | 24 | #include "net/url_request/url_request_context_getter.h" |
[email protected] | 53ac00e8 | 2012-10-18 20:59:20 | [diff] [blame] | 25 | #include "webkit/database/database_tracker.h" |
[email protected] | e0ce8a1e | 2012-09-18 10:26:36 | [diff] [blame] | 26 | #endif // !OS_IOS |
[email protected] | 55eb70e76 | 2012-02-20 17:38:39 | [diff] [blame] | 27 | |
[email protected] | 314c3e2 | 2012-02-21 03:57:42 | [diff] [blame] | 28 | using base::UserDataAdapter; |
[email protected] | 55eb70e76 | 2012-02-20 17:38:39 | [diff] [blame] | 29 | |
[email protected] | 55eb70e76 | 2012-02-20 17:38:39 | [diff] [blame] | 30 | namespace content { |
| 31 | |
[email protected] | e0ce8a1e | 2012-09-18 10:26:36 | [diff] [blame] | 32 | // Only ~BrowserContext() is needed on iOS. |
| 33 | #if !defined(OS_IOS) |
[email protected] | 735e20c | 2012-03-20 01:16:59 | [diff] [blame] | 34 | namespace { |
| 35 | |
[email protected] | e0ce8a1e | 2012-09-18 10:26:36 | [diff] [blame] | 36 | // Key names on BrowserContext. |
| 37 | const char* kDownloadManagerKeyName = "download_manager"; |
| 38 | const char* kStorageParitionMapKeyName = "content_storage_partition_map"; |
| 39 | |
[email protected] | 14acc64 | 2012-11-17 12:20:10 | [diff] [blame^] | 40 | StoragePartitionImplMap* GetStoragePartitionMap( |
| 41 | BrowserContext* browser_context) { |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 42 | StoragePartitionImplMap* partition_map = |
| 43 | static_cast<StoragePartitionImplMap*>( |
| 44 | browser_context->GetUserData(kStorageParitionMapKeyName)); |
[email protected] | d7c7c98a | 2012-07-12 21:27:44 | [diff] [blame] | 45 | if (!partition_map) { |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 46 | partition_map = new StoragePartitionImplMap(browser_context); |
[email protected] | d7c7c98a | 2012-07-12 21:27:44 | [diff] [blame] | 47 | browser_context->SetUserData(kStorageParitionMapKeyName, partition_map); |
| 48 | } |
[email protected] | 14acc64 | 2012-11-17 12:20:10 | [diff] [blame^] | 49 | return partition_map; |
| 50 | } |
| 51 | |
| 52 | StoragePartition* GetStoragePartitionFromConfig( |
| 53 | BrowserContext* browser_context, |
| 54 | const std::string& partition_domain, |
| 55 | const std::string& partition_name, |
| 56 | bool in_memory) { |
| 57 | StoragePartitionImplMap* partition_map = |
| 58 | GetStoragePartitionMap(browser_context); |
[email protected] | d7c7c98a | 2012-07-12 21:27:44 | [diff] [blame] | 59 | |
[email protected] | 1bc2831 | 2012-11-08 08:31:53 | [diff] [blame] | 60 | if (browser_context->IsOffTheRecord()) |
| 61 | in_memory = true; |
| 62 | |
[email protected] | 1bc2831 | 2012-11-08 08:31:53 | [diff] [blame] | 63 | return partition_map->Get(partition_domain, partition_name, in_memory); |
[email protected] | d1198fd | 2012-08-13 22:50:19 | [diff] [blame] | 64 | } |
| 65 | |
[email protected] | d7c7c98a | 2012-07-12 21:27:44 | [diff] [blame] | 66 | // Run |callback| on each DOMStorageContextImpl in |browser_context|. |
[email protected] | 1bc2831 | 2012-11-08 08:31:53 | [diff] [blame] | 67 | void PurgeDOMStorageContextInPartition(StoragePartition* storage_partition) { |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 68 | static_cast<StoragePartitionImpl*>(storage_partition)-> |
| 69 | GetDOMStorageContext()->PurgeMemory(); |
[email protected] | 55eb70e76 | 2012-02-20 17:38:39 | [diff] [blame] | 70 | } |
| 71 | |
[email protected] | 6939075a | 2012-08-28 08:35:53 | [diff] [blame] | 72 | void SaveSessionStateOnIOThread( |
| 73 | const scoped_refptr<net::URLRequestContextGetter>& context_getter, |
| 74 | appcache::AppCacheService* appcache_service) { |
| 75 | net::URLRequestContext* context = context_getter->GetURLRequestContext(); |
| 76 | context->cookie_store()->GetCookieMonster()-> |
[email protected] | bf510ed | 2012-06-05 08:31:43 | [diff] [blame] | 77 | SetForceKeepSessionState(); |
[email protected] | 6939075a | 2012-08-28 08:35:53 | [diff] [blame] | 78 | context->server_bound_cert_service()->GetCertStore()-> |
| 79 | SetForceKeepSessionState(); |
| 80 | appcache_service->set_force_keep_session_state(); |
[email protected] | 6e2d3d2 | 2012-02-24 18:10:36 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | void SaveSessionStateOnWebkitThread( |
[email protected] | 6e2d3d2 | 2012-02-24 18:10:36 | [diff] [blame] | 84 | scoped_refptr<IndexedDBContextImpl> indexed_db_context) { |
[email protected] | bf510ed | 2012-06-05 08:31:43 | [diff] [blame] | 85 | indexed_db_context->SetForceKeepSessionState(); |
[email protected] | 6e2d3d2 | 2012-02-24 18:10:36 | [diff] [blame] | 86 | } |
| 87 | |
[email protected] | 6939075a | 2012-08-28 08:35:53 | [diff] [blame] | 88 | void PurgeMemoryOnIOThread(appcache::AppCacheService* appcache_service) { |
| 89 | appcache_service->PurgeMemory(); |
[email protected] | 6e2d3d2 | 2012-02-24 18:10:36 | [diff] [blame] | 90 | } |
| 91 | |
[email protected] | 735e20c | 2012-03-20 01:16:59 | [diff] [blame] | 92 | } // namespace |
| 93 | |
[email protected] | 14acc64 | 2012-11-17 12:20:10 | [diff] [blame^] | 94 | // static |
| 95 | void BrowserContext::AsyncObliterateStoragePartition( |
| 96 | BrowserContext* browser_context, |
| 97 | const GURL& site) { |
| 98 | GetStoragePartitionMap(browser_context)->AsyncObliterate(site); |
| 99 | } |
| 100 | |
[email protected] | b441a849 | 2012-06-06 14:55:57 | [diff] [blame] | 101 | DownloadManager* BrowserContext::GetDownloadManager( |
| 102 | BrowserContext* context) { |
[email protected] | ecd3ad2 | 2012-07-10 20:02:40 | [diff] [blame] | 103 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | b441a849 | 2012-06-06 14:55:57 | [diff] [blame] | 104 | if (!context->GetUserData(kDownloadManagerKeyName)) { |
[email protected] | d25fda1 | 2012-06-12 17:05:03 | [diff] [blame] | 105 | ResourceDispatcherHostImpl* rdh = ResourceDispatcherHostImpl::Get(); |
| 106 | DCHECK(rdh); |
[email protected] | d25fda1 | 2012-06-12 17:05:03 | [diff] [blame] | 107 | scoped_refptr<DownloadManager> download_manager = |
| 108 | new DownloadManagerImpl( |
[email protected] | d25fda1 | 2012-06-12 17:05:03 | [diff] [blame] | 109 | GetContentClient()->browser()->GetNetLog()); |
| 110 | |
[email protected] | b441a849 | 2012-06-06 14:55:57 | [diff] [blame] | 111 | context->SetUserData( |
| 112 | kDownloadManagerKeyName, |
| 113 | new UserDataAdapter<DownloadManager>(download_manager)); |
| 114 | download_manager->SetDelegate(context->GetDownloadManagerDelegate()); |
| 115 | download_manager->Init(context); |
| 116 | } |
| 117 | |
| 118 | return UserDataAdapter<DownloadManager>::Get( |
| 119 | context, kDownloadManagerKeyName); |
| 120 | } |
| 121 | |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 122 | StoragePartition* BrowserContext::GetStoragePartition( |
| 123 | BrowserContext* browser_context, |
| 124 | SiteInstance* site_instance) { |
[email protected] | 1bc2831 | 2012-11-08 08:31:53 | [diff] [blame] | 125 | std::string partition_domain; |
| 126 | std::string partition_name; |
| 127 | bool in_memory = false; |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 128 | |
| 129 | // TODO(ajwong): After GetDefaultStoragePartition() is removed, get rid of |
| 130 | // this conditional and require that |site_instance| is non-NULL. |
| 131 | if (site_instance) { |
[email protected] | 1bc2831 | 2012-11-08 08:31:53 | [diff] [blame] | 132 | GetContentClient()->browser()->GetStoragePartitionConfigForSite( |
[email protected] | 14acc64 | 2012-11-17 12:20:10 | [diff] [blame^] | 133 | browser_context, site_instance->GetSiteURL(), true, |
[email protected] | 1bc2831 | 2012-11-08 08:31:53 | [diff] [blame] | 134 | &partition_domain, &partition_name, &in_memory); |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 135 | } |
| 136 | |
[email protected] | 1bc2831 | 2012-11-08 08:31:53 | [diff] [blame] | 137 | return GetStoragePartitionFromConfig( |
| 138 | browser_context, partition_domain, partition_name, in_memory); |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 139 | } |
| 140 | |
[email protected] | e94bbcb | 2012-09-07 05:33:57 | [diff] [blame] | 141 | StoragePartition* BrowserContext::GetStoragePartitionForSite( |
| 142 | BrowserContext* browser_context, |
| 143 | const GURL& site) { |
[email protected] | 1bc2831 | 2012-11-08 08:31:53 | [diff] [blame] | 144 | std::string partition_domain; |
| 145 | std::string partition_name; |
| 146 | bool in_memory; |
[email protected] | e94bbcb | 2012-09-07 05:33:57 | [diff] [blame] | 147 | |
[email protected] | 1bc2831 | 2012-11-08 08:31:53 | [diff] [blame] | 148 | GetContentClient()->browser()->GetStoragePartitionConfigForSite( |
[email protected] | 14acc64 | 2012-11-17 12:20:10 | [diff] [blame^] | 149 | browser_context, site, true, &partition_domain, &partition_name, |
| 150 | &in_memory); |
[email protected] | 1bc2831 | 2012-11-08 08:31:53 | [diff] [blame] | 151 | |
| 152 | return GetStoragePartitionFromConfig( |
| 153 | browser_context, partition_domain, partition_name, in_memory); |
[email protected] | e94bbcb | 2012-09-07 05:33:57 | [diff] [blame] | 154 | } |
| 155 | |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 156 | void BrowserContext::ForEachStoragePartition( |
| 157 | BrowserContext* browser_context, |
| 158 | const StoragePartitionCallback& callback) { |
| 159 | StoragePartitionImplMap* partition_map = |
| 160 | static_cast<StoragePartitionImplMap*>( |
| 161 | browser_context->GetUserData(kStorageParitionMapKeyName)); |
| 162 | if (!partition_map) |
| 163 | return; |
| 164 | |
| 165 | partition_map->ForEach(callback); |
| 166 | } |
| 167 | |
| 168 | StoragePartition* BrowserContext::GetDefaultStoragePartition( |
| 169 | BrowserContext* browser_context) { |
| 170 | return GetStoragePartition(browser_context, NULL); |
[email protected] | 55eb70e76 | 2012-02-20 17:38:39 | [diff] [blame] | 171 | } |
| 172 | |
[email protected] | 314c3e2 | 2012-02-21 03:57:42 | [diff] [blame] | 173 | void BrowserContext::EnsureResourceContextInitialized(BrowserContext* context) { |
[email protected] | 7e26ac9 | 2012-02-27 20:15:05 | [diff] [blame] | 174 | // This will be enough to tickle initialization of BrowserContext if |
| 175 | // necessary, which initializes ResourceContext. The reason we don't call |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 176 | // ResourceContext::InitializeResourceContext() directly here is that |
| 177 | // ResourceContext initialization may call back into BrowserContext |
| 178 | // and when that call returns it'll end rewriting its UserData map. It will |
| 179 | // end up rewriting the same value but this still causes a race condition. |
| 180 | // |
| 181 | // See http://crbug.com/115678. |
| 182 | GetDefaultStoragePartition(context); |
[email protected] | 55eb70e76 | 2012-02-20 17:38:39 | [diff] [blame] | 183 | } |
| 184 | |
[email protected] | 6e2d3d2 | 2012-02-24 18:10:36 | [diff] [blame] | 185 | void BrowserContext::SaveSessionState(BrowserContext* browser_context) { |
[email protected] | 5c8e67c | 2012-08-29 00:48:52 | [diff] [blame] | 186 | GetDefaultStoragePartition(browser_context)->GetDatabaseTracker()-> |
| 187 | SetForceKeepSessionState(); |
[email protected] | b1b502e | 2012-09-16 07:31:43 | [diff] [blame] | 188 | StoragePartition* storage_partition = |
| 189 | BrowserContext::GetDefaultStoragePartition(browser_context); |
[email protected] | 6e2d3d2 | 2012-02-24 18:10:36 | [diff] [blame] | 190 | |
| 191 | if (BrowserThread::IsMessageLoopValid(BrowserThread::IO)) { |
| 192 | BrowserThread::PostTask( |
| 193 | BrowserThread::IO, FROM_HERE, |
[email protected] | 6939075a | 2012-08-28 08:35:53 | [diff] [blame] | 194 | base::Bind( |
| 195 | &SaveSessionStateOnIOThread, |
| 196 | make_scoped_refptr(browser_context->GetRequestContext()), |
[email protected] | b1b502e | 2012-09-16 07:31:43 | [diff] [blame] | 197 | storage_partition->GetAppCacheService())); |
[email protected] | 6e2d3d2 | 2012-02-24 18:10:36 | [diff] [blame] | 198 | } |
| 199 | |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 200 | DOMStorageContextImpl* dom_storage_context_impl = |
| 201 | static_cast<DOMStorageContextImpl*>( |
[email protected] | b1b502e | 2012-09-16 07:31:43 | [diff] [blame] | 202 | storage_partition->GetDOMStorageContext()); |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 203 | dom_storage_context_impl->SetForceKeepSessionState(); |
[email protected] | 735e20c | 2012-03-20 01:16:59 | [diff] [blame] | 204 | |
[email protected] | 6e2d3d2 | 2012-02-24 18:10:36 | [diff] [blame] | 205 | if (BrowserThread::IsMessageLoopValid(BrowserThread::WEBKIT_DEPRECATED)) { |
[email protected] | 6e2d3d2 | 2012-02-24 18:10:36 | [diff] [blame] | 206 | IndexedDBContextImpl* indexed_db = static_cast<IndexedDBContextImpl*>( |
[email protected] | b1b502e | 2012-09-16 07:31:43 | [diff] [blame] | 207 | storage_partition->GetIndexedDBContext()); |
[email protected] | 6e2d3d2 | 2012-02-24 18:10:36 | [diff] [blame] | 208 | BrowserThread::PostTask( |
| 209 | BrowserThread::WEBKIT_DEPRECATED, FROM_HERE, |
| 210 | base::Bind(&SaveSessionStateOnWebkitThread, |
[email protected] | 6e2d3d2 | 2012-02-24 18:10:36 | [diff] [blame] | 211 | make_scoped_refptr(indexed_db))); |
| 212 | } |
| 213 | } |
| 214 | |
[email protected] | 6e2d3d2 | 2012-02-24 18:10:36 | [diff] [blame] | 215 | void BrowserContext::PurgeMemory(BrowserContext* browser_context) { |
| 216 | if (BrowserThread::IsMessageLoopValid(BrowserThread::IO)) { |
| 217 | BrowserThread::PostTask( |
| 218 | BrowserThread::IO, FROM_HERE, |
[email protected] | 6939075a | 2012-08-28 08:35:53 | [diff] [blame] | 219 | base::Bind( |
| 220 | &PurgeMemoryOnIOThread, |
| 221 | BrowserContext::GetDefaultStoragePartition(browser_context)-> |
| 222 | GetAppCacheService())); |
[email protected] | 6e2d3d2 | 2012-02-24 18:10:36 | [diff] [blame] | 223 | } |
| 224 | |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 225 | ForEachStoragePartition(browser_context, |
| 226 | base::Bind(&PurgeDOMStorageContextInPartition)); |
[email protected] | 6e2d3d2 | 2012-02-24 18:10:36 | [diff] [blame] | 227 | } |
[email protected] | e0ce8a1e | 2012-09-18 10:26:36 | [diff] [blame] | 228 | #endif // !OS_IOS |
[email protected] | 6e2d3d2 | 2012-02-24 18:10:36 | [diff] [blame] | 229 | |
[email protected] | 55eb70e76 | 2012-02-20 17:38:39 | [diff] [blame] | 230 | BrowserContext::~BrowserContext() { |
[email protected] | e0ce8a1e | 2012-09-18 10:26:36 | [diff] [blame] | 231 | #if !defined(OS_IOS) |
[email protected] | b441a849 | 2012-06-06 14:55:57 | [diff] [blame] | 232 | if (GetUserData(kDownloadManagerKeyName)) |
| 233 | GetDownloadManager(this)->Shutdown(); |
[email protected] | e0ce8a1e | 2012-09-18 10:26:36 | [diff] [blame] | 234 | #endif |
[email protected] | 55eb70e76 | 2012-02-20 17:38:39 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | } // namespace content |