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