[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] | b441a849 | 2012-06-06 14:55:57 | [diff] [blame] | 8 | #include "content/browser/download/download_manager_impl.h" |
[email protected] | 393b6cb | 2014-05-15 00:55:12 | [diff] [blame^] | 9 | #include "content/browser/fileapi/chrome_blob_storage_context.h" |
[email protected] | c4d28166 | 2013-03-31 00:35:08 | [diff] [blame] | 10 | #include "content/browser/indexed_db/indexed_db_context_impl.h" |
[email protected] | 678c036 | 2012-12-05 08:02:44 | [diff] [blame] | 11 | #include "content/browser/loader/resource_dispatcher_host_impl.h" |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 12 | #include "content/browser/storage_partition_impl_map.h" |
[email protected] | d7c7c98a | 2012-07-12 21:27:44 | [diff] [blame] | 13 | #include "content/common/child_process_host_impl.h" |
[email protected] | 393b6cb | 2014-05-15 00:55:12 | [diff] [blame^] | 14 | #include "content/public/browser/blob_handle.h" |
[email protected] | 55eb70e76 | 2012-02-20 17:38:39 | [diff] [blame] | 15 | #include "content/public/browser/browser_thread.h" |
[email protected] | b441a849 | 2012-06-06 14:55:57 | [diff] [blame] | 16 | #include "content/public/browser/content_browser_client.h" |
[email protected] | 536fd0b | 2013-03-14 17:41:57 | [diff] [blame] | 17 | #include "content/public/browser/site_instance.h" |
[email protected] | 4d7c4ef | 2012-03-16 01:47:12 | [diff] [blame] | 18 | #include "net/cookies/cookie_monster.h" |
| 19 | #include "net/cookies/cookie_store.h" |
[email protected] | 536fd0b | 2013-03-14 17:41:57 | [diff] [blame] | 20 | #include "net/ssl/server_bound_cert_service.h" |
| 21 | #include "net/ssl/server_bound_cert_store.h" |
[email protected] | 6e2d3d2 | 2012-02-24 18:10:36 | [diff] [blame] | 22 | #include "net/url_request/url_request_context.h" |
[email protected] | 6939075a | 2012-08-28 08:35:53 | [diff] [blame] | 23 | #include "net/url_request/url_request_context_getter.h" |
[email protected] | 08b1f75f | 2013-05-22 22:02:38 | [diff] [blame] | 24 | #include "webkit/browser/database/database_tracker.h" |
[email protected] | f25e113 | 2013-05-24 13:58:04 | [diff] [blame] | 25 | #include "webkit/browser/fileapi/external_mount_points.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. |
[email protected] | 6ef0c391 | 2013-01-25 22:46:34 | [diff] [blame] | 37 | const char kDownloadManagerKeyName[] = "download_manager"; |
[email protected] | 6ef0c391 | 2013-01-25 22:46:34 | [diff] [blame] | 38 | const char kStorageParitionMapKeyName[] = "content_storage_partition_map"; |
[email protected] | e0ce8a1e | 2012-09-18 10:26:36 | [diff] [blame] | 39 | |
[email protected] | 9afc14e2 | 2013-09-25 22:34:14 | [diff] [blame] | 40 | #if defined(OS_CHROMEOS) |
| 41 | const char kMountPointsKey[] = "mount_points"; |
| 42 | #endif // defined(OS_CHROMEOS) |
| 43 | |
[email protected] | 14acc64 | 2012-11-17 12:20:10 | [diff] [blame] | 44 | StoragePartitionImplMap* GetStoragePartitionMap( |
| 45 | BrowserContext* browser_context) { |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 46 | StoragePartitionImplMap* partition_map = |
| 47 | static_cast<StoragePartitionImplMap*>( |
| 48 | browser_context->GetUserData(kStorageParitionMapKeyName)); |
[email protected] | d7c7c98a | 2012-07-12 21:27:44 | [diff] [blame] | 49 | if (!partition_map) { |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 50 | partition_map = new StoragePartitionImplMap(browser_context); |
[email protected] | d7c7c98a | 2012-07-12 21:27:44 | [diff] [blame] | 51 | browser_context->SetUserData(kStorageParitionMapKeyName, partition_map); |
| 52 | } |
[email protected] | 14acc64 | 2012-11-17 12:20:10 | [diff] [blame] | 53 | return partition_map; |
| 54 | } |
| 55 | |
| 56 | StoragePartition* GetStoragePartitionFromConfig( |
| 57 | BrowserContext* browser_context, |
| 58 | const std::string& partition_domain, |
| 59 | const std::string& partition_name, |
| 60 | bool in_memory) { |
| 61 | StoragePartitionImplMap* partition_map = |
| 62 | GetStoragePartitionMap(browser_context); |
[email protected] | d7c7c98a | 2012-07-12 21:27:44 | [diff] [blame] | 63 | |
[email protected] | 1bc2831 | 2012-11-08 08:31:53 | [diff] [blame] | 64 | if (browser_context->IsOffTheRecord()) |
| 65 | in_memory = true; |
| 66 | |
[email protected] | 1bc2831 | 2012-11-08 08:31:53 | [diff] [blame] | 67 | return partition_map->Get(partition_domain, partition_name, in_memory); |
[email protected] | d1198fd | 2012-08-13 22:50:19 | [diff] [blame] | 68 | } |
| 69 | |
[email protected] | 6939075a | 2012-08-28 08:35:53 | [diff] [blame] | 70 | void SaveSessionStateOnIOThread( |
| 71 | const scoped_refptr<net::URLRequestContextGetter>& context_getter, |
| 72 | appcache::AppCacheService* appcache_service) { |
| 73 | net::URLRequestContext* context = context_getter->GetURLRequestContext(); |
| 74 | context->cookie_store()->GetCookieMonster()-> |
[email protected] | bf510ed | 2012-06-05 08:31:43 | [diff] [blame] | 75 | SetForceKeepSessionState(); |
[email protected] | 6939075a | 2012-08-28 08:35:53 | [diff] [blame] | 76 | context->server_bound_cert_service()->GetCertStore()-> |
| 77 | SetForceKeepSessionState(); |
| 78 | appcache_service->set_force_keep_session_state(); |
[email protected] | 6e2d3d2 | 2012-02-24 18:10:36 | [diff] [blame] | 79 | } |
| 80 | |
[email protected] | 89acda8 | 2013-06-25 20:52:50 | [diff] [blame] | 81 | void SaveSessionStateOnIndexedDBThread( |
[email protected] | 6e2d3d2 | 2012-02-24 18:10:36 | [diff] [blame] | 82 | scoped_refptr<IndexedDBContextImpl> indexed_db_context) { |
[email protected] | bf510ed | 2012-06-05 08:31:43 | [diff] [blame] | 83 | indexed_db_context->SetForceKeepSessionState(); |
[email protected] | 6e2d3d2 | 2012-02-24 18:10:36 | [diff] [blame] | 84 | } |
| 85 | |
[email protected] | 735e20c | 2012-03-20 01:16:59 | [diff] [blame] | 86 | } // namespace |
| 87 | |
[email protected] | 14acc64 | 2012-11-17 12:20:10 | [diff] [blame] | 88 | // static |
| 89 | void BrowserContext::AsyncObliterateStoragePartition( |
| 90 | BrowserContext* browser_context, |
[email protected] | 399583b | 2012-12-11 09:33:42 | [diff] [blame] | 91 | const GURL& site, |
| 92 | const base::Closure& on_gc_required) { |
| 93 | GetStoragePartitionMap(browser_context)->AsyncObliterate(site, |
| 94 | on_gc_required); |
| 95 | } |
| 96 | |
| 97 | // static |
| 98 | void BrowserContext::GarbageCollectStoragePartitions( |
| 99 | BrowserContext* browser_context, |
[email protected] | 2dec8ec | 2013-02-07 19:20:34 | [diff] [blame] | 100 | scoped_ptr<base::hash_set<base::FilePath> > active_paths, |
[email protected] | 399583b | 2012-12-11 09:33:42 | [diff] [blame] | 101 | const base::Closure& done) { |
| 102 | GetStoragePartitionMap(browser_context)->GarbageCollect( |
| 103 | active_paths.Pass(), done); |
[email protected] | 14acc64 | 2012-11-17 12:20:10 | [diff] [blame] | 104 | } |
| 105 | |
[email protected] | b441a849 | 2012-06-06 14:55:57 | [diff] [blame] | 106 | DownloadManager* BrowserContext::GetDownloadManager( |
| 107 | BrowserContext* context) { |
[email protected] | ecd3ad2 | 2012-07-10 20:02:40 | [diff] [blame] | 108 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | b441a849 | 2012-06-06 14:55:57 | [diff] [blame] | 109 | if (!context->GetUserData(kDownloadManagerKeyName)) { |
[email protected] | d25fda1 | 2012-06-12 17:05:03 | [diff] [blame] | 110 | ResourceDispatcherHostImpl* rdh = ResourceDispatcherHostImpl::Get(); |
| 111 | DCHECK(rdh); |
[email protected] | eba4a4d | 2013-05-29 02:18:06 | [diff] [blame] | 112 | DownloadManager* download_manager = |
[email protected] | d25fda1 | 2012-06-12 17:05:03 | [diff] [blame] | 113 | new DownloadManagerImpl( |
[email protected] | 1679869 | 2013-04-23 18:08:38 | [diff] [blame] | 114 | GetContentClient()->browser()->GetNetLog(), context); |
[email protected] | d25fda1 | 2012-06-12 17:05:03 | [diff] [blame] | 115 | |
[email protected] | b441a849 | 2012-06-06 14:55:57 | [diff] [blame] | 116 | context->SetUserData( |
| 117 | kDownloadManagerKeyName, |
[email protected] | eba4a4d | 2013-05-29 02:18:06 | [diff] [blame] | 118 | download_manager); |
[email protected] | b441a849 | 2012-06-06 14:55:57 | [diff] [blame] | 119 | download_manager->SetDelegate(context->GetDownloadManagerDelegate()); |
[email protected] | b441a849 | 2012-06-06 14:55:57 | [diff] [blame] | 120 | } |
| 121 | |
[email protected] | eba4a4d | 2013-05-29 02:18:06 | [diff] [blame] | 122 | return static_cast<DownloadManager*>( |
| 123 | context->GetUserData(kDownloadManagerKeyName)); |
[email protected] | b441a849 | 2012-06-06 14:55:57 | [diff] [blame] | 124 | } |
| 125 | |
[email protected] | 6ef0c391 | 2013-01-25 22:46:34 | [diff] [blame] | 126 | // static |
| 127 | fileapi::ExternalMountPoints* BrowserContext::GetMountPoints( |
| 128 | BrowserContext* context) { |
| 129 | // Ensure that these methods are called on the UI thread, except for |
| 130 | // unittests where a UI thread might not have been created. |
| 131 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || |
| 132 | !BrowserThread::IsMessageLoopValid(BrowserThread::UI)); |
| 133 | |
| 134 | #if defined(OS_CHROMEOS) |
| 135 | if (!context->GetUserData(kMountPointsKey)) { |
| 136 | scoped_refptr<fileapi::ExternalMountPoints> mount_points = |
| 137 | fileapi::ExternalMountPoints::CreateRefCounted(); |
| 138 | context->SetUserData( |
| 139 | kMountPointsKey, |
[email protected] | 144b6c4 | 2013-06-14 07:30:38 | [diff] [blame] | 140 | new UserDataAdapter<fileapi::ExternalMountPoints>(mount_points.get())); |
[email protected] | 6ef0c391 | 2013-01-25 22:46:34 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | return UserDataAdapter<fileapi::ExternalMountPoints>::Get( |
| 144 | context, kMountPointsKey); |
| 145 | #else |
| 146 | return NULL; |
| 147 | #endif |
| 148 | } |
| 149 | |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 150 | StoragePartition* BrowserContext::GetStoragePartition( |
| 151 | BrowserContext* browser_context, |
| 152 | SiteInstance* site_instance) { |
[email protected] | 1bc2831 | 2012-11-08 08:31:53 | [diff] [blame] | 153 | std::string partition_domain; |
| 154 | std::string partition_name; |
| 155 | bool in_memory = false; |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 156 | |
| 157 | // TODO(ajwong): After GetDefaultStoragePartition() is removed, get rid of |
| 158 | // this conditional and require that |site_instance| is non-NULL. |
| 159 | if (site_instance) { |
[email protected] | 1bc2831 | 2012-11-08 08:31:53 | [diff] [blame] | 160 | GetContentClient()->browser()->GetStoragePartitionConfigForSite( |
[email protected] | 14acc64 | 2012-11-17 12:20:10 | [diff] [blame] | 161 | browser_context, site_instance->GetSiteURL(), true, |
[email protected] | 1bc2831 | 2012-11-08 08:31:53 | [diff] [blame] | 162 | &partition_domain, &partition_name, &in_memory); |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 163 | } |
| 164 | |
[email protected] | 1bc2831 | 2012-11-08 08:31:53 | [diff] [blame] | 165 | return GetStoragePartitionFromConfig( |
| 166 | browser_context, partition_domain, partition_name, in_memory); |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 167 | } |
| 168 | |
[email protected] | e94bbcb | 2012-09-07 05:33:57 | [diff] [blame] | 169 | StoragePartition* BrowserContext::GetStoragePartitionForSite( |
| 170 | BrowserContext* browser_context, |
| 171 | const GURL& site) { |
[email protected] | 1bc2831 | 2012-11-08 08:31:53 | [diff] [blame] | 172 | std::string partition_domain; |
| 173 | std::string partition_name; |
| 174 | bool in_memory; |
[email protected] | e94bbcb | 2012-09-07 05:33:57 | [diff] [blame] | 175 | |
[email protected] | 1bc2831 | 2012-11-08 08:31:53 | [diff] [blame] | 176 | GetContentClient()->browser()->GetStoragePartitionConfigForSite( |
[email protected] | 14acc64 | 2012-11-17 12:20:10 | [diff] [blame] | 177 | browser_context, site, true, &partition_domain, &partition_name, |
| 178 | &in_memory); |
[email protected] | 1bc2831 | 2012-11-08 08:31:53 | [diff] [blame] | 179 | |
| 180 | return GetStoragePartitionFromConfig( |
| 181 | browser_context, partition_domain, partition_name, in_memory); |
[email protected] | e94bbcb | 2012-09-07 05:33:57 | [diff] [blame] | 182 | } |
| 183 | |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 184 | void BrowserContext::ForEachStoragePartition( |
| 185 | BrowserContext* browser_context, |
| 186 | const StoragePartitionCallback& callback) { |
| 187 | StoragePartitionImplMap* partition_map = |
| 188 | static_cast<StoragePartitionImplMap*>( |
| 189 | browser_context->GetUserData(kStorageParitionMapKeyName)); |
| 190 | if (!partition_map) |
| 191 | return; |
| 192 | |
| 193 | partition_map->ForEach(callback); |
| 194 | } |
| 195 | |
| 196 | StoragePartition* BrowserContext::GetDefaultStoragePartition( |
| 197 | BrowserContext* browser_context) { |
| 198 | return GetStoragePartition(browser_context, NULL); |
[email protected] | 55eb70e76 | 2012-02-20 17:38:39 | [diff] [blame] | 199 | } |
| 200 | |
[email protected] | 393b6cb | 2014-05-15 00:55:12 | [diff] [blame^] | 201 | void BrowserContext::CreateMemoryBackedBlob(BrowserContext* browser_context, |
| 202 | const char* data, size_t length, |
| 203 | const BlobCallback& callback) { |
| 204 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 205 | |
| 206 | ChromeBlobStorageContext* blob_context = |
| 207 | ChromeBlobStorageContext::GetFor(browser_context); |
| 208 | BrowserThread::PostTaskAndReplyWithResult( |
| 209 | BrowserThread::IO, FROM_HERE, |
| 210 | base::Bind(&ChromeBlobStorageContext::CreateMemoryBackedBlob, |
| 211 | make_scoped_refptr(blob_context), data, length), |
| 212 | callback); |
| 213 | } |
| 214 | |
[email protected] | 314c3e2 | 2012-02-21 03:57:42 | [diff] [blame] | 215 | void BrowserContext::EnsureResourceContextInitialized(BrowserContext* context) { |
[email protected] | 7e26ac9 | 2012-02-27 20:15:05 | [diff] [blame] | 216 | // This will be enough to tickle initialization of BrowserContext if |
| 217 | // necessary, which initializes ResourceContext. The reason we don't call |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 218 | // ResourceContext::InitializeResourceContext() directly here is that |
| 219 | // ResourceContext initialization may call back into BrowserContext |
| 220 | // and when that call returns it'll end rewriting its UserData map. It will |
| 221 | // end up rewriting the same value but this still causes a race condition. |
| 222 | // |
| 223 | // See http://crbug.com/115678. |
| 224 | GetDefaultStoragePartition(context); |
[email protected] | 55eb70e76 | 2012-02-20 17:38:39 | [diff] [blame] | 225 | } |
| 226 | |
[email protected] | 6e2d3d2 | 2012-02-24 18:10:36 | [diff] [blame] | 227 | void BrowserContext::SaveSessionState(BrowserContext* browser_context) { |
[email protected] | 5c8e67c | 2012-08-29 00:48:52 | [diff] [blame] | 228 | GetDefaultStoragePartition(browser_context)->GetDatabaseTracker()-> |
| 229 | SetForceKeepSessionState(); |
[email protected] | b1b502e | 2012-09-16 07:31:43 | [diff] [blame] | 230 | StoragePartition* storage_partition = |
| 231 | BrowserContext::GetDefaultStoragePartition(browser_context); |
[email protected] | 6e2d3d2 | 2012-02-24 18:10:36 | [diff] [blame] | 232 | |
| 233 | if (BrowserThread::IsMessageLoopValid(BrowserThread::IO)) { |
| 234 | BrowserThread::PostTask( |
| 235 | BrowserThread::IO, FROM_HERE, |
[email protected] | 6939075a | 2012-08-28 08:35:53 | [diff] [blame] | 236 | base::Bind( |
| 237 | &SaveSessionStateOnIOThread, |
| 238 | make_scoped_refptr(browser_context->GetRequestContext()), |
[email protected] | b1b502e | 2012-09-16 07:31:43 | [diff] [blame] | 239 | storage_partition->GetAppCacheService())); |
[email protected] | 6e2d3d2 | 2012-02-24 18:10:36 | [diff] [blame] | 240 | } |
| 241 | |
[email protected] | 5f2aa72 | 2013-08-07 16:59:41 | [diff] [blame] | 242 | DOMStorageContextWrapper* dom_storage_context_proxy = |
| 243 | static_cast<DOMStorageContextWrapper*>( |
[email protected] | b1b502e | 2012-09-16 07:31:43 | [diff] [blame] | 244 | storage_partition->GetDOMStorageContext()); |
[email protected] | 5f2aa72 | 2013-08-07 16:59:41 | [diff] [blame] | 245 | dom_storage_context_proxy->SetForceKeepSessionState(); |
[email protected] | 735e20c | 2012-03-20 01:16:59 | [diff] [blame] | 246 | |
[email protected] | 89acda8 | 2013-06-25 20:52:50 | [diff] [blame] | 247 | IndexedDBContextImpl* indexed_db_context_impl = |
| 248 | static_cast<IndexedDBContextImpl*>( |
[email protected] | b1b502e | 2012-09-16 07:31:43 | [diff] [blame] | 249 | storage_partition->GetIndexedDBContext()); |
[email protected] | 89acda8 | 2013-06-25 20:52:50 | [diff] [blame] | 250 | // No task runner in unit tests. |
| 251 | if (indexed_db_context_impl->TaskRunner()) { |
| 252 | indexed_db_context_impl->TaskRunner()->PostTask( |
| 253 | FROM_HERE, |
| 254 | base::Bind(&SaveSessionStateOnIndexedDBThread, |
| 255 | make_scoped_refptr(indexed_db_context_impl))); |
[email protected] | 6e2d3d2 | 2012-02-24 18:10:36 | [diff] [blame] | 256 | } |
| 257 | } |
| 258 | |
[email protected] | e0ce8a1e | 2012-09-18 10:26:36 | [diff] [blame] | 259 | #endif // !OS_IOS |
[email protected] | 6e2d3d2 | 2012-02-24 18:10:36 | [diff] [blame] | 260 | |
[email protected] | 55eb70e76 | 2012-02-20 17:38:39 | [diff] [blame] | 261 | BrowserContext::~BrowserContext() { |
[email protected] | e0ce8a1e | 2012-09-18 10:26:36 | [diff] [blame] | 262 | #if !defined(OS_IOS) |
[email protected] | b441a849 | 2012-06-06 14:55:57 | [diff] [blame] | 263 | if (GetUserData(kDownloadManagerKeyName)) |
| 264 | GetDownloadManager(this)->Shutdown(); |
[email protected] | e0ce8a1e | 2012-09-18 10:26:36 | [diff] [blame] | 265 | #endif |
[email protected] | 55eb70e76 | 2012-02-20 17:38:39 | [diff] [blame] | 266 | } |
| 267 | |
[email protected] | 55eb70e76 | 2012-02-20 17:38:39 | [diff] [blame] | 268 | } // namespace content |