[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 | |
| 7 | #include "content/browser/appcache/chrome_appcache_service.h" |
[email protected] | d7c7c98a | 2012-07-12 21:27:44 | [diff] [blame^] | 8 | #include "webkit/database/database_tracker.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] | d25fda1 | 2012-06-12 17:05:03 | [diff] [blame] | 10 | #include "content/browser/download/download_file_manager.h" |
[email protected] | b441a849 | 2012-06-06 14:55:57 | [diff] [blame] | 11 | #include "content/browser/download/download_manager_impl.h" |
[email protected] | 6e2d3d2 | 2012-02-24 18:10:36 | [diff] [blame] | 12 | #include "content/browser/in_process_webkit/indexed_db_context_impl.h" |
[email protected] | d25fda1 | 2012-06-12 17:05:03 | [diff] [blame] | 13 | #include "content/browser/renderer_host/resource_dispatcher_host_impl.h" |
[email protected] | d7c7c98a | 2012-07-12 21:27:44 | [diff] [blame^] | 14 | #include "content/public/browser/resource_context.h" |
| 15 | #include "content/browser/storage_partition.h" |
| 16 | #include "content/browser/storage_partition_map.h" |
| 17 | #include "content/common/child_process_host_impl.h" |
[email protected] | 55eb70e76 | 2012-02-20 17:38:39 | [diff] [blame] | 18 | #include "content/public/browser/browser_thread.h" |
[email protected] | b441a849 | 2012-06-06 14:55:57 | [diff] [blame] | 19 | #include "content/public/browser/content_browser_client.h" |
[email protected] | bf510ed | 2012-06-05 08:31:43 | [diff] [blame] | 20 | #include "net/base/server_bound_cert_service.h" |
| 21 | #include "net/base/server_bound_cert_store.h" |
[email protected] | 4d7c4ef | 2012-03-16 01:47:12 | [diff] [blame] | 22 | #include "net/cookies/cookie_monster.h" |
| 23 | #include "net/cookies/cookie_store.h" |
[email protected] | 6e2d3d2 | 2012-02-24 18:10:36 | [diff] [blame] | 24 | #include "net/url_request/url_request_context.h" |
[email protected] | 55eb70e76 | 2012-02-20 17:38:39 | [diff] [blame] | 25 | |
[email protected] | 314c3e2 | 2012-02-21 03:57:42 | [diff] [blame] | 26 | using base::UserDataAdapter; |
[email protected] | 55eb70e76 | 2012-02-20 17:38:39 | [diff] [blame] | 27 | |
[email protected] | 314c3e2 | 2012-02-21 03:57:42 | [diff] [blame] | 28 | // Key names on BrowserContext. |
[email protected] | b441a849 | 2012-06-06 14:55:57 | [diff] [blame] | 29 | static const char* kDownloadManagerKeyName = "download_manager"; |
[email protected] | d7c7c98a | 2012-07-12 21:27:44 | [diff] [blame^] | 30 | static const char* kStorageParitionMapKeyName = "content_storage_partition_map"; |
[email protected] | 55eb70e76 | 2012-02-20 17:38:39 | [diff] [blame] | 31 | |
| 32 | namespace content { |
| 33 | |
[email protected] | 735e20c | 2012-03-20 01:16:59 | [diff] [blame] | 34 | namespace { |
| 35 | |
[email protected] | d7c7c98a | 2012-07-12 21:27:44 | [diff] [blame^] | 36 | StoragePartition* GetStoragePartition(BrowserContext* browser_context, |
| 37 | int renderer_child_id) { |
| 38 | StoragePartitionMap* partition_map = static_cast<StoragePartitionMap*>( |
| 39 | browser_context->GetUserData(kStorageParitionMapKeyName)); |
| 40 | if (!partition_map) { |
| 41 | partition_map = new StoragePartitionMap(browser_context); |
| 42 | browser_context->SetUserData(kStorageParitionMapKeyName, partition_map); |
| 43 | } |
| 44 | |
| 45 | const std::string& partition_id = |
| 46 | GetContentClient()->browser()->GetStoragePartitionIdForChildProcess( |
| 47 | browser_context, |
| 48 | renderer_child_id); |
| 49 | |
| 50 | return partition_map->Get(partition_id); |
| 51 | } |
| 52 | |
| 53 | // Run |callback| on each storage partition in |browser_context|. |
| 54 | void ForEachStoragePartition( |
| 55 | BrowserContext* browser_context, |
| 56 | const base::Callback<void(StoragePartition*)>& callback) { |
| 57 | StoragePartitionMap* partition_map = static_cast<StoragePartitionMap*>( |
| 58 | browser_context->GetUserData(kStorageParitionMapKeyName)); |
| 59 | if (!partition_map) { |
[email protected] | 55eb70e76 | 2012-02-20 17:38:39 | [diff] [blame] | 60 | return; |
| 61 | } |
| 62 | |
[email protected] | d7c7c98a | 2012-07-12 21:27:44 | [diff] [blame^] | 63 | partition_map->ForEach(callback); |
| 64 | } |
[email protected] | 55eb70e76 | 2012-02-20 17:38:39 | [diff] [blame] | 65 | |
[email protected] | d7c7c98a | 2012-07-12 21:27:44 | [diff] [blame^] | 66 | // Used to convert a callback meant to take a DOMStorageContextImpl* into one |
| 67 | // that can take a StoragePartition*. |
| 68 | void ProcessDOMStorageContext( |
| 69 | const base::Callback<void(DOMStorageContextImpl*)>& callback, |
| 70 | StoragePartition* partition) { |
| 71 | callback.Run(partition->dom_storage_context()); |
| 72 | } |
[email protected] | 55eb70e76 | 2012-02-20 17:38:39 | [diff] [blame] | 73 | |
[email protected] | d7c7c98a | 2012-07-12 21:27:44 | [diff] [blame^] | 74 | // Run |callback| on each DOMStorageContextImpl in |browser_context|. |
| 75 | void ForEachDOMStorageContext( |
| 76 | BrowserContext* browser_context, |
| 77 | const base::Callback<void(DOMStorageContextImpl*)>& callback) { |
| 78 | ForEachStoragePartition(browser_context, |
| 79 | base::Bind(&ProcessDOMStorageContext, callback)); |
[email protected] | 55eb70e76 | 2012-02-20 17:38:39 | [diff] [blame] | 80 | } |
| 81 | |
[email protected] | 6e2d3d2 | 2012-02-24 18:10:36 | [diff] [blame] | 82 | void SaveSessionStateOnIOThread(ResourceContext* resource_context) { |
| 83 | resource_context->GetRequestContext()->cookie_store()->GetCookieMonster()-> |
[email protected] | bf510ed | 2012-06-05 08:31:43 | [diff] [blame] | 84 | SetForceKeepSessionState(); |
| 85 | resource_context->GetRequestContext()->server_bound_cert_service()-> |
| 86 | GetCertStore()->SetForceKeepSessionState(); |
| 87 | ResourceContext::GetAppCacheService(resource_context)-> |
| 88 | set_force_keep_session_state(); |
[email protected] | 6e2d3d2 | 2012-02-24 18:10:36 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | void SaveSessionStateOnWebkitThread( |
[email protected] | 6e2d3d2 | 2012-02-24 18:10:36 | [diff] [blame] | 92 | scoped_refptr<IndexedDBContextImpl> indexed_db_context) { |
[email protected] | bf510ed | 2012-06-05 08:31:43 | [diff] [blame] | 93 | indexed_db_context->SetForceKeepSessionState(); |
[email protected] | 6e2d3d2 | 2012-02-24 18:10:36 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | void PurgeMemoryOnIOThread(ResourceContext* resource_context) { |
| 97 | ResourceContext::GetAppCacheService(resource_context)->PurgeMemory(); |
| 98 | } |
| 99 | |
[email protected] | d7c7c98a | 2012-07-12 21:27:44 | [diff] [blame^] | 100 | DOMStorageContextImpl* GetDefaultDOMStorageContextImpl( |
| 101 | BrowserContext* context) { |
[email protected] | 735e20c | 2012-03-20 01:16:59 | [diff] [blame] | 102 | return static_cast<DOMStorageContextImpl*>( |
[email protected] | d7c7c98a | 2012-07-12 21:27:44 | [diff] [blame^] | 103 | BrowserContext::GetDefaultDOMStorageContext(context)); |
[email protected] | 6e2d3d2 | 2012-02-24 18:10:36 | [diff] [blame] | 104 | } |
| 105 | |
[email protected] | 735e20c | 2012-03-20 01:16:59 | [diff] [blame] | 106 | } // namespace |
| 107 | |
[email protected] | b441a849 | 2012-06-06 14:55:57 | [diff] [blame] | 108 | DownloadManager* BrowserContext::GetDownloadManager( |
| 109 | BrowserContext* context) { |
[email protected] | ecd3ad2 | 2012-07-10 20:02:40 | [diff] [blame] | 110 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | b441a849 | 2012-06-06 14:55:57 | [diff] [blame] | 111 | if (!context->GetUserData(kDownloadManagerKeyName)) { |
[email protected] | d25fda1 | 2012-06-12 17:05:03 | [diff] [blame] | 112 | ResourceDispatcherHostImpl* rdh = ResourceDispatcherHostImpl::Get(); |
| 113 | DCHECK(rdh); |
| 114 | DownloadFileManager* file_manager = rdh->download_file_manager(); |
| 115 | DCHECK(file_manager); |
| 116 | scoped_refptr<DownloadManager> download_manager = |
| 117 | new DownloadManagerImpl( |
| 118 | file_manager, |
| 119 | scoped_ptr<DownloadItemFactory>(), |
| 120 | GetContentClient()->browser()->GetNetLog()); |
| 121 | |
[email protected] | b441a849 | 2012-06-06 14:55:57 | [diff] [blame] | 122 | context->SetUserData( |
| 123 | kDownloadManagerKeyName, |
| 124 | new UserDataAdapter<DownloadManager>(download_manager)); |
| 125 | download_manager->SetDelegate(context->GetDownloadManagerDelegate()); |
| 126 | download_manager->Init(context); |
| 127 | } |
| 128 | |
| 129 | return UserDataAdapter<DownloadManager>::Get( |
| 130 | context, kDownloadManagerKeyName); |
| 131 | } |
| 132 | |
[email protected] | d7c7c98a | 2012-07-12 21:27:44 | [diff] [blame^] | 133 | quota::QuotaManager* BrowserContext::GetQuotaManager( |
| 134 | BrowserContext* browser_context) { |
| 135 | // TODO(ajwong): Change this API to require a process id instead of using |
| 136 | // kInvalidChildProcessId. |
| 137 | StoragePartition* partition = |
| 138 | GetStoragePartition(browser_context, |
| 139 | ChildProcessHostImpl::kInvalidChildProcessId); |
| 140 | return partition->quota_manager(); |
| 141 | } |
| 142 | |
| 143 | DOMStorageContext* BrowserContext::GetDefaultDOMStorageContext( |
| 144 | BrowserContext* browser_context) { |
| 145 | // TODO(ajwong): Force all users to know which process id they are performing |
| 146 | // actions on behalf of, migrate them to GetDOMStorageContext(), and then |
| 147 | // delete this function. |
| 148 | return GetDOMStorageContext(browser_context, |
| 149 | ChildProcessHostImpl::kInvalidChildProcessId); |
[email protected] | 55eb70e76 | 2012-02-20 17:38:39 | [diff] [blame] | 150 | } |
| 151 | |
[email protected] | c1fff07 | 2012-02-24 23:38:12 | [diff] [blame] | 152 | DOMStorageContext* BrowserContext::GetDOMStorageContext( |
[email protected] | d7c7c98a | 2012-07-12 21:27:44 | [diff] [blame^] | 153 | BrowserContext* browser_context, |
| 154 | int render_child_id) { |
| 155 | StoragePartition* partition = |
| 156 | GetStoragePartition(browser_context, render_child_id); |
| 157 | return partition->dom_storage_context(); |
[email protected] | c1fff07 | 2012-02-24 23:38:12 | [diff] [blame] | 158 | } |
| 159 | |
[email protected] | d7c7c98a | 2012-07-12 21:27:44 | [diff] [blame^] | 160 | IndexedDBContext* BrowserContext::GetIndexedDBContext( |
[email protected] | 55eb70e76 | 2012-02-20 17:38:39 | [diff] [blame] | 161 | BrowserContext* browser_context) { |
[email protected] | d7c7c98a | 2012-07-12 21:27:44 | [diff] [blame^] | 162 | // TODO(ajwong): Change this API to require a process id instead of using |
| 163 | // kInvalidChildProcessId. |
| 164 | StoragePartition* partition = |
| 165 | GetStoragePartition(browser_context, |
| 166 | ChildProcessHostImpl::kInvalidChildProcessId); |
| 167 | return partition->indexed_db_context(); |
[email protected] | 55eb70e76 | 2012-02-20 17:38:39 | [diff] [blame] | 168 | } |
| 169 | |
[email protected] | d7c7c98a | 2012-07-12 21:27:44 | [diff] [blame^] | 170 | webkit_database::DatabaseTracker* BrowserContext::GetDatabaseTracker( |
[email protected] | 55eb70e76 | 2012-02-20 17:38:39 | [diff] [blame] | 171 | BrowserContext* browser_context) { |
[email protected] | d7c7c98a | 2012-07-12 21:27:44 | [diff] [blame^] | 172 | // TODO(ajwong): Change this API to require a process id instead of using |
| 173 | // kInvalidChildProcessId. |
| 174 | StoragePartition* partition = |
| 175 | GetStoragePartition(browser_context, |
| 176 | ChildProcessHostImpl::kInvalidChildProcessId); |
| 177 | return partition->database_tracker(); |
| 178 | } |
| 179 | |
| 180 | appcache::AppCacheService* BrowserContext::GetAppCacheService( |
| 181 | BrowserContext* browser_context) { |
| 182 | // TODO(ajwong): Change this API to require a process id instead of using |
| 183 | // kInvalidChildProcessId. |
| 184 | StoragePartition* partition = |
| 185 | GetStoragePartition(browser_context, |
| 186 | ChildProcessHostImpl::kInvalidChildProcessId); |
| 187 | return partition->appcache_service(); |
| 188 | } |
| 189 | |
| 190 | fileapi::FileSystemContext* BrowserContext::GetFileSystemContext( |
| 191 | BrowserContext* browser_context) { |
| 192 | // TODO(ajwong): Change this API to require a process id instead of using |
| 193 | // kInvalidChildProcessId. |
| 194 | StoragePartition* partition = |
| 195 | GetStoragePartition(browser_context, |
| 196 | ChildProcessHostImpl::kInvalidChildProcessId); |
| 197 | return partition->filesystem_context(); |
[email protected] | 55eb70e76 | 2012-02-20 17:38:39 | [diff] [blame] | 198 | } |
| 199 | |
[email protected] | 314c3e2 | 2012-02-21 03:57:42 | [diff] [blame] | 200 | void BrowserContext::EnsureResourceContextInitialized(BrowserContext* context) { |
[email protected] | 7e26ac9 | 2012-02-27 20:15:05 | [diff] [blame] | 201 | // This will be enough to tickle initialization of BrowserContext if |
| 202 | // necessary, which initializes ResourceContext. The reason we don't call |
| 203 | // ResourceContext::InitializeResourceContext directly here is that if |
| 204 | // ResourceContext ends up initializing it will call back into BrowserContext |
[email protected] | d7c7c98a | 2012-07-12 21:27:44 | [diff] [blame^] | 205 | // and when that call returns it'll end rewriting its UserData map (with the |
[email protected] | 7e26ac9 | 2012-02-27 20:15:05 | [diff] [blame] | 206 | // same value) but this causes a race condition. See http://crbug.com/115678. |
[email protected] | d7c7c98a | 2012-07-12 21:27:44 | [diff] [blame^] | 207 | GetStoragePartition(context, ChildProcessHostImpl::kInvalidChildProcessId); |
[email protected] | 55eb70e76 | 2012-02-20 17:38:39 | [diff] [blame] | 208 | } |
| 209 | |
[email protected] | 6e2d3d2 | 2012-02-24 18:10:36 | [diff] [blame] | 210 | void BrowserContext::SaveSessionState(BrowserContext* browser_context) { |
[email protected] | bf510ed | 2012-06-05 08:31:43 | [diff] [blame] | 211 | GetDatabaseTracker(browser_context)->SetForceKeepSessionState(); |
[email protected] | 6e2d3d2 | 2012-02-24 18:10:36 | [diff] [blame] | 212 | |
| 213 | if (BrowserThread::IsMessageLoopValid(BrowserThread::IO)) { |
| 214 | BrowserThread::PostTask( |
| 215 | BrowserThread::IO, FROM_HERE, |
| 216 | base::Bind(&SaveSessionStateOnIOThread, |
| 217 | browser_context->GetResourceContext())); |
| 218 | } |
| 219 | |
[email protected] | d7c7c98a | 2012-07-12 21:27:44 | [diff] [blame^] | 220 | // TODO(ajwong): This is the only usage of GetDefaultDOMStorageContextImpl(). |
| 221 | // After we migrate this to support multiple DOMStorageContexts, don't forget |
| 222 | // to remove the GetDefaultDOMStorageContextImpl() function as well. |
| 223 | GetDefaultDOMStorageContextImpl(browser_context)->SetForceKeepSessionState(); |
[email protected] | 735e20c | 2012-03-20 01:16:59 | [diff] [blame] | 224 | |
[email protected] | 6e2d3d2 | 2012-02-24 18:10:36 | [diff] [blame] | 225 | if (BrowserThread::IsMessageLoopValid(BrowserThread::WEBKIT_DEPRECATED)) { |
[email protected] | 6e2d3d2 | 2012-02-24 18:10:36 | [diff] [blame] | 226 | IndexedDBContextImpl* indexed_db = static_cast<IndexedDBContextImpl*>( |
[email protected] | c1fff07 | 2012-02-24 23:38:12 | [diff] [blame] | 227 | GetIndexedDBContext(browser_context)); |
[email protected] | 6e2d3d2 | 2012-02-24 18:10:36 | [diff] [blame] | 228 | BrowserThread::PostTask( |
| 229 | BrowserThread::WEBKIT_DEPRECATED, FROM_HERE, |
| 230 | base::Bind(&SaveSessionStateOnWebkitThread, |
[email protected] | 6e2d3d2 | 2012-02-24 18:10:36 | [diff] [blame] | 231 | make_scoped_refptr(indexed_db))); |
| 232 | } |
| 233 | } |
| 234 | |
[email protected] | 6e2d3d2 | 2012-02-24 18:10:36 | [diff] [blame] | 235 | void BrowserContext::PurgeMemory(BrowserContext* browser_context) { |
| 236 | if (BrowserThread::IsMessageLoopValid(BrowserThread::IO)) { |
| 237 | BrowserThread::PostTask( |
| 238 | BrowserThread::IO, FROM_HERE, |
| 239 | base::Bind(&PurgeMemoryOnIOThread, |
| 240 | browser_context->GetResourceContext())); |
| 241 | } |
| 242 | |
[email protected] | d7c7c98a | 2012-07-12 21:27:44 | [diff] [blame^] | 243 | ForEachDOMStorageContext(browser_context, |
| 244 | base::Bind(&DOMStorageContextImpl::PurgeMemory)); |
[email protected] | 6e2d3d2 | 2012-02-24 18:10:36 | [diff] [blame] | 245 | } |
| 246 | |
[email protected] | 55eb70e76 | 2012-02-20 17:38:39 | [diff] [blame] | 247 | BrowserContext::~BrowserContext() { |
[email protected] | b441a849 | 2012-06-06 14:55:57 | [diff] [blame] | 248 | if (GetUserData(kDownloadManagerKeyName)) |
| 249 | GetDownloadManager(this)->Shutdown(); |
[email protected] | 55eb70e76 | 2012-02-20 17:38:39 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | } // namespace content |