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