| OLD | NEW |
| (Empty) | |
| 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" |
| 8 #include "content/browser/chrome_blob_storage_context.h" |
| 9 #include "content/browser/file_system/browser_file_system_helper.h" |
| 10 #include "content/browser/in_process_webkit/webkit_context.h" |
| 11 #include "content/public/browser/browser_thread.h" |
| 12 #include "content/public/common/content_constants.h" |
| 13 #include "webkit/database/database_tracker.h" |
| 14 #include "webkit/quota/quota_manager.h" |
| 15 |
| 16 using content::BrowserThread; |
| 17 using fileapi::FileSystemContext; |
| 18 using quota::QuotaManager; |
| 19 using webkit_database::DatabaseTracker; |
| 20 |
| 21 static const char* kAppCacheServicKeyName = "content_appcache_service_tracker"; |
| 22 static const char* kBlobStorageContextKeyName = "content_blob_storage_context"; |
| 23 static const char* kDatabaseTrackerKeyName = "content_database_tracker"; |
| 24 static const char* kFileSystemContextKeyName = "content_file_system_context"; |
| 25 static const char* kQuotaManagerKeyName = "content_quota_manager"; |
| 26 static const char* kWebKitContextKeyName = "content_webkit_context"; |
| 27 |
| 28 namespace content { |
| 29 |
| 30 // Adapter class that releases a refcounted object when the |
| 31 // SupportsUserData::Data object is deleted. |
| 32 template <typename T> |
| 33 class UserDataAdapter : public base::SupportsUserData::Data { |
| 34 public: |
| 35 static T* Get(BrowserContext* context, const char* key) { |
| 36 UserDataAdapter* data = |
| 37 static_cast<UserDataAdapter*>(context->GetUserData(key)); |
| 38 return static_cast<T*>(data->object_.get()); |
| 39 } |
| 40 |
| 41 UserDataAdapter(T* object) : object_(object) {} |
| 42 |
| 43 private: |
| 44 scoped_refptr<T> object_; |
| 45 |
| 46 DISALLOW_COPY_AND_ASSIGN(UserDataAdapter); |
| 47 }; |
| 48 |
| 49 void CreateQuotaManagerAndClients(BrowserContext* context) { |
| 50 if (context->GetUserData(kQuotaManagerKeyName)) { |
| 51 DCHECK(context->GetUserData(kDatabaseTrackerKeyName)); |
| 52 DCHECK(context->GetUserData(kFileSystemContextKeyName)); |
| 53 DCHECK(context->GetUserData(kWebKitContextKeyName)); |
| 54 return; |
| 55 } |
| 56 |
| 57 // All of the clients have to be created and registered with the |
| 58 // QuotaManager prior to the QuotaManger being used. So we do them |
| 59 // all together here prior to handing out a reference to anything |
| 60 // that utlizes the QuotaManager. |
| 61 scoped_refptr<QuotaManager> quota_manager = new quota::QuotaManager( |
| 62 context->IsOffTheRecord(), context->GetPath(), |
| 63 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO), |
| 64 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB), |
| 65 context->GetSpecialStoragePolicy()); |
| 66 context->SetUserData(kQuotaManagerKeyName, |
| 67 new UserDataAdapter<QuotaManager>(quota_manager)); |
| 68 |
| 69 // Each consumer is responsible for registering its QuotaClient during |
| 70 // its construction. |
| 71 scoped_refptr<FileSystemContext> filesystem_context = CreateFileSystemContext( |
| 72 context->GetPath(), context->IsOffTheRecord(), |
| 73 context->GetSpecialStoragePolicy(), quota_manager->proxy()); |
| 74 context->SetUserData( |
| 75 kFileSystemContextKeyName, |
| 76 new UserDataAdapter<FileSystemContext>(filesystem_context)); |
| 77 |
| 78 scoped_refptr<DatabaseTracker> db_tracker = new DatabaseTracker( |
| 79 context->GetPath(), context->IsOffTheRecord(), false, |
| 80 context->GetSpecialStoragePolicy(), quota_manager->proxy(), |
| 81 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE)); |
| 82 context->SetUserData(kDatabaseTrackerKeyName, |
| 83 new UserDataAdapter<DatabaseTracker>(db_tracker)); |
| 84 |
| 85 scoped_refptr<WebKitContext> webkit_context = new WebKitContext( |
| 86 context->IsOffTheRecord(), context->GetPath(), |
| 87 context->GetSpecialStoragePolicy(), false, quota_manager->proxy(), |
| 88 BrowserThread::GetMessageLoopProxyForThread( |
| 89 BrowserThread::WEBKIT_DEPRECATED)); |
| 90 context->SetUserData(kWebKitContextKeyName, |
| 91 new UserDataAdapter<WebKitContext>(webkit_context)); |
| 92 |
| 93 scoped_refptr<ChromeAppCacheService> appcache_service = |
| 94 new ChromeAppCacheService(quota_manager->proxy()); |
| 95 context->SetUserData( |
| 96 kAppCacheServicKeyName, |
| 97 new UserDataAdapter<ChromeAppCacheService>(appcache_service)); |
| 98 |
| 99 // Check first to avoid memory leak in unittests. |
| 100 if (BrowserThread::IsMessageLoopValid(BrowserThread::IO)) { |
| 101 BrowserThread::PostTask( |
| 102 BrowserThread::IO, FROM_HERE, |
| 103 base::Bind(&ChromeAppCacheService::InitializeOnIOThread, |
| 104 appcache_service, |
| 105 context->IsOffTheRecord() ? FilePath() : |
| 106 context->GetPath().Append(content::kAppCacheDirname), |
| 107 context->GetResourceContext(), |
| 108 make_scoped_refptr(context->GetSpecialStoragePolicy()))); |
| 109 } |
| 110 } |
| 111 |
| 112 QuotaManager* BrowserContext::GetQuotaManager(BrowserContext* context) { |
| 113 CreateQuotaManagerAndClients(context); |
| 114 return UserDataAdapter<QuotaManager>::Get(context, kQuotaManagerKeyName); |
| 115 } |
| 116 |
| 117 WebKitContext* BrowserContext::GetWebKitContext(BrowserContext* context) { |
| 118 CreateQuotaManagerAndClients(context); |
| 119 return UserDataAdapter<WebKitContext>::Get(context, kWebKitContextKeyName); |
| 120 } |
| 121 |
| 122 DatabaseTracker* BrowserContext::GetDatabaseTracker(BrowserContext* context) { |
| 123 CreateQuotaManagerAndClients(context); |
| 124 return UserDataAdapter<DatabaseTracker>::Get( |
| 125 context, kDatabaseTrackerKeyName); |
| 126 } |
| 127 |
| 128 ChromeAppCacheService* BrowserContext::GetAppCacheService( |
| 129 BrowserContext* browser_context) { |
| 130 CreateQuotaManagerAndClients(browser_context); |
| 131 return UserDataAdapter<ChromeAppCacheService>::Get( |
| 132 browser_context, kAppCacheServicKeyName); |
| 133 } |
| 134 |
| 135 FileSystemContext* BrowserContext::GetFileSystemContext( |
| 136 BrowserContext* browser_context) { |
| 137 CreateQuotaManagerAndClients(browser_context); |
| 138 return UserDataAdapter<FileSystemContext>::Get( |
| 139 browser_context, kFileSystemContextKeyName); |
| 140 } |
| 141 |
| 142 ChromeBlobStorageContext* BrowserContext::GetBlobStorageContext( |
| 143 BrowserContext* context) { |
| 144 if (!context->GetUserData(kBlobStorageContextKeyName)) { |
| 145 scoped_refptr<ChromeBlobStorageContext> blob = |
| 146 new ChromeBlobStorageContext(); |
| 147 BrowserThread::PostTask( |
| 148 BrowserThread::IO, FROM_HERE, |
| 149 base::Bind(&ChromeBlobStorageContext::InitializeOnIOThread, blob)); |
| 150 context->SetUserData(kBlobStorageContextKeyName, |
| 151 new UserDataAdapter<ChromeBlobStorageContext>(blob)); |
| 152 } |
| 153 |
| 154 return UserDataAdapter<ChromeBlobStorageContext>::Get( |
| 155 context, kBlobStorageContextKeyName); |
| 156 } |
| 157 |
| 158 BrowserContext::~BrowserContext() { |
| 159 if (GetUserData(kDatabaseTrackerKeyName) && |
| 160 BrowserThread::IsMessageLoopValid(BrowserThread::FILE)) { |
| 161 BrowserThread::PostTask( |
| 162 BrowserThread::FILE, FROM_HERE, |
| 163 base::Bind(&webkit_database::DatabaseTracker::Shutdown, |
| 164 GetDatabaseTracker(this))); |
| 165 } |
| 166 } |
| 167 |
| 168 } // namespace content |
| OLD | NEW |