Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [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 | |
| 7 | #if !defined(OS_IOS) |
| 8 | #include "content/browser/appcache/chrome_appcache_service.h" |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 9 | #include "content/browser/dom_storage/dom_storage_context_wrapper.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 10 | #include "content/browser/download/download_manager_impl.h" |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 11 | #include "content/browser/indexed_db/indexed_db_context_impl.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 12 | #include "content/browser/loader/resource_dispatcher_host_impl.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 13 | #include "content/browser/storage_partition_impl.h" |
| 14 | #include "content/browser/storage_partition_impl_map.h" |
| 15 | #include "content/common/child_process_host_impl.h" |
| 16 | #include "content/public/browser/browser_thread.h" |
| 17 | #include "content/public/browser/content_browser_client.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 18 | #include "content/public/browser/site_instance.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 19 | #include "net/cookies/cookie_monster.h" |
| 20 | #include "net/cookies/cookie_store.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 21 | #include "net/ssl/server_bound_cert_service.h" |
| 22 | #include "net/ssl/server_bound_cert_store.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 23 | #include "net/url_request/url_request_context.h" |
| 24 | #include "net/url_request/url_request_context_getter.h" |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 25 | #include "webkit/browser/database/database_tracker.h" |
| 26 | #include "webkit/browser/fileapi/external_mount_points.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 27 | #endif // !OS_IOS |
| 28 | |
| 29 | using base::UserDataAdapter; |
| 30 | |
| 31 | namespace content { |
| 32 | |
| 33 | // Only ~BrowserContext() is needed on iOS. |
| 34 | #if !defined(OS_IOS) |
| 35 | namespace { |
| 36 | |
| 37 | // Key names on BrowserContext. |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 38 | const char kDownloadManagerKeyName[] = "download_manager"; |
| 39 | const char kMountPointsKey[] = "mount_points"; |
| 40 | const char kStorageParitionMapKeyName[] = "content_storage_partition_map"; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 41 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 42 | StoragePartitionImplMap* GetStoragePartitionMap( |
| 43 | BrowserContext* browser_context) { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 44 | StoragePartitionImplMap* partition_map = |
| 45 | static_cast<StoragePartitionImplMap*>( |
| 46 | browser_context->GetUserData(kStorageParitionMapKeyName)); |
| 47 | if (!partition_map) { |
| 48 | partition_map = new StoragePartitionImplMap(browser_context); |
| 49 | browser_context->SetUserData(kStorageParitionMapKeyName, partition_map); |
| 50 | } |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 51 | return partition_map; |
| 52 | } |
| 53 | |
| 54 | StoragePartition* GetStoragePartitionFromConfig( |
| 55 | BrowserContext* browser_context, |
| 56 | const std::string& partition_domain, |
| 57 | const std::string& partition_name, |
| 58 | bool in_memory) { |
| 59 | StoragePartitionImplMap* partition_map = |
| 60 | GetStoragePartitionMap(browser_context); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 61 | |
| 62 | if (browser_context->IsOffTheRecord()) |
| 63 | in_memory = true; |
| 64 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 65 | return partition_map->Get(partition_domain, partition_name, in_memory); |
| 66 | } |
| 67 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 68 | // Run |callback| on each DOMStorageContextWrapper in |browser_context|. |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 69 | void PurgeDOMStorageContextInPartition(StoragePartition* storage_partition) { |
| 70 | static_cast<StoragePartitionImpl*>(storage_partition)-> |
| 71 | GetDOMStorageContext()->PurgeMemory(); |
| 72 | } |
| 73 | |
| 74 | void SaveSessionStateOnIOThread( |
| 75 | const scoped_refptr<net::URLRequestContextGetter>& context_getter, |
| 76 | appcache::AppCacheService* appcache_service) { |
| 77 | net::URLRequestContext* context = context_getter->GetURLRequestContext(); |
| 78 | context->cookie_store()->GetCookieMonster()-> |
| 79 | SetForceKeepSessionState(); |
| 80 | context->server_bound_cert_service()->GetCertStore()-> |
| 81 | SetForceKeepSessionState(); |
| 82 | appcache_service->set_force_keep_session_state(); |
| 83 | } |
| 84 | |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 85 | void SaveSessionStateOnIndexedDBThread( |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 86 | scoped_refptr<IndexedDBContextImpl> indexed_db_context) { |
| 87 | indexed_db_context->SetForceKeepSessionState(); |
| 88 | } |
| 89 | |
| 90 | void PurgeMemoryOnIOThread(appcache::AppCacheService* appcache_service) { |
| 91 | appcache_service->PurgeMemory(); |
| 92 | } |
| 93 | |
| 94 | } // namespace |
| 95 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 96 | // static |
| 97 | void BrowserContext::AsyncObliterateStoragePartition( |
| 98 | BrowserContext* browser_context, |
| 99 | const GURL& site, |
| 100 | const base::Closure& on_gc_required) { |
| 101 | GetStoragePartitionMap(browser_context)->AsyncObliterate(site, |
| 102 | on_gc_required); |
| 103 | } |
| 104 | |
| 105 | // static |
| 106 | void BrowserContext::GarbageCollectStoragePartitions( |
| 107 | BrowserContext* browser_context, |
| 108 | scoped_ptr<base::hash_set<base::FilePath> > active_paths, |
| 109 | const base::Closure& done) { |
| 110 | GetStoragePartitionMap(browser_context)->GarbageCollect( |
| 111 | active_paths.Pass(), done); |
| 112 | } |
| 113 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 114 | DownloadManager* BrowserContext::GetDownloadManager( |
| 115 | BrowserContext* context) { |
| 116 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 117 | if (!context->GetUserData(kDownloadManagerKeyName)) { |
| 118 | ResourceDispatcherHostImpl* rdh = ResourceDispatcherHostImpl::Get(); |
| 119 | DCHECK(rdh); |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 120 | DownloadManager* download_manager = |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 121 | new DownloadManagerImpl( |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 122 | GetContentClient()->browser()->GetNetLog(), context); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 123 | |
| 124 | context->SetUserData( |
| 125 | kDownloadManagerKeyName, |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 126 | download_manager); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 127 | download_manager->SetDelegate(context->GetDownloadManagerDelegate()); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 130 | return static_cast<DownloadManager*>( |
| 131 | context->GetUserData(kDownloadManagerKeyName)); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 134 | // static |
| 135 | fileapi::ExternalMountPoints* BrowserContext::GetMountPoints( |
| 136 | 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)) { |
| 144 | scoped_refptr<fileapi::ExternalMountPoints> mount_points = |
| 145 | fileapi::ExternalMountPoints::CreateRefCounted(); |
| 146 | context->SetUserData( |
| 147 | kMountPointsKey, |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 148 | new UserDataAdapter<fileapi::ExternalMountPoints>(mount_points.get())); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | return UserDataAdapter<fileapi::ExternalMountPoints>::Get( |
| 152 | context, kMountPointsKey); |
| 153 | #else |
| 154 | return NULL; |
| 155 | #endif |
| 156 | } |
| 157 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 158 | StoragePartition* BrowserContext::GetStoragePartition( |
| 159 | BrowserContext* browser_context, |
| 160 | SiteInstance* site_instance) { |
| 161 | std::string partition_domain; |
| 162 | std::string partition_name; |
| 163 | bool in_memory = false; |
| 164 | |
| 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) { |
| 168 | GetContentClient()->browser()->GetStoragePartitionConfigForSite( |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 169 | browser_context, site_instance->GetSiteURL(), true, |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 170 | &partition_domain, &partition_name, &in_memory); |
| 171 | } |
| 172 | |
| 173 | return GetStoragePartitionFromConfig( |
| 174 | browser_context, partition_domain, partition_name, in_memory); |
| 175 | } |
| 176 | |
| 177 | StoragePartition* BrowserContext::GetStoragePartitionForSite( |
| 178 | BrowserContext* browser_context, |
| 179 | const GURL& site) { |
| 180 | std::string partition_domain; |
| 181 | std::string partition_name; |
| 182 | bool in_memory; |
| 183 | |
| 184 | GetContentClient()->browser()->GetStoragePartitionConfigForSite( |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 185 | browser_context, site, true, &partition_domain, &partition_name, |
| 186 | &in_memory); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 187 | |
| 188 | return GetStoragePartitionFromConfig( |
| 189 | browser_context, partition_domain, partition_name, in_memory); |
| 190 | } |
| 191 | |
| 192 | void 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 | |
| 204 | StoragePartition* BrowserContext::GetDefaultStoragePartition( |
| 205 | BrowserContext* browser_context) { |
| 206 | return GetStoragePartition(browser_context, NULL); |
| 207 | } |
| 208 | |
| 209 | void BrowserContext::EnsureResourceContextInitialized(BrowserContext* context) { |
| 210 | // This will be enough to tickle initialization of BrowserContext if |
| 211 | // necessary, which initializes ResourceContext. The reason we don't call |
| 212 | // ResourceContext::InitializeResourceContext() directly here is that |
| 213 | // ResourceContext initialization may call back into BrowserContext |
| 214 | // and when that call returns it'll end rewriting its UserData map. It will |
| 215 | // end up rewriting the same value but this still causes a race condition. |
| 216 | // |
| 217 | // See http://crbug.com/115678. |
| 218 | GetDefaultStoragePartition(context); |
| 219 | } |
| 220 | |
| 221 | void BrowserContext::SaveSessionState(BrowserContext* browser_context) { |
| 222 | GetDefaultStoragePartition(browser_context)->GetDatabaseTracker()-> |
| 223 | SetForceKeepSessionState(); |
| 224 | StoragePartition* storage_partition = |
| 225 | BrowserContext::GetDefaultStoragePartition(browser_context); |
| 226 | |
| 227 | if (BrowserThread::IsMessageLoopValid(BrowserThread::IO)) { |
| 228 | BrowserThread::PostTask( |
| 229 | BrowserThread::IO, FROM_HERE, |
| 230 | base::Bind( |
| 231 | &SaveSessionStateOnIOThread, |
| 232 | make_scoped_refptr(browser_context->GetRequestContext()), |
| 233 | storage_partition->GetAppCacheService())); |
| 234 | } |
| 235 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 236 | DOMStorageContextWrapper* dom_storage_context_proxy = |
| 237 | static_cast<DOMStorageContextWrapper*>( |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 238 | storage_partition->GetDOMStorageContext()); |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 239 | dom_storage_context_proxy->SetForceKeepSessionState(); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 240 | |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 241 | IndexedDBContextImpl* indexed_db_context_impl = |
| 242 | static_cast<IndexedDBContextImpl*>( |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 243 | storage_partition->GetIndexedDBContext()); |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 244 | // No task runner in unit tests. |
| 245 | if (indexed_db_context_impl->TaskRunner()) { |
| 246 | indexed_db_context_impl->TaskRunner()->PostTask( |
| 247 | FROM_HERE, |
| 248 | base::Bind(&SaveSessionStateOnIndexedDBThread, |
| 249 | make_scoped_refptr(indexed_db_context_impl))); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | |
| 253 | void BrowserContext::PurgeMemory(BrowserContext* browser_context) { |
| 254 | if (BrowserThread::IsMessageLoopValid(BrowserThread::IO)) { |
| 255 | BrowserThread::PostTask( |
| 256 | BrowserThread::IO, FROM_HERE, |
| 257 | base::Bind( |
| 258 | &PurgeMemoryOnIOThread, |
| 259 | BrowserContext::GetDefaultStoragePartition(browser_context)-> |
| 260 | GetAppCacheService())); |
| 261 | } |
| 262 | |
| 263 | ForEachStoragePartition(browser_context, |
| 264 | base::Bind(&PurgeDOMStorageContextInPartition)); |
| 265 | } |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 266 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 267 | #endif // !OS_IOS |
| 268 | |
| 269 | BrowserContext::~BrowserContext() { |
| 270 | #if !defined(OS_IOS) |
| 271 | if (GetUserData(kDownloadManagerKeyName)) |
| 272 | GetDownloadManager(this)->Shutdown(); |
| 273 | #endif |
| 274 | } |
| 275 | |
| 276 | } // namespace content |