| 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/browser/indexed_db/indexed_db_quota_client.h" | 5 #include "content/browser/indexed_db/indexed_db_quota_client.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/message_loop_proxy.h" | 10 #include "base/message_loop_proxy.h" |
| 11 #include "content/browser/indexed_db/indexed_db_context_impl.h" | 11 #include "content/browser/indexed_db/indexed_db_context_impl.h" |
| 12 #include "content/public/browser/browser_thread.h" | 12 #include "content/public/browser/browser_thread.h" |
| 13 #include "net/base/net_util.h" | 13 #include "net/base/net_util.h" |
| 14 #include "webkit/browser/database/database_util.h" | 14 #include "webkit/browser/database/database_util.h" |
| 15 | 15 |
| 16 using quota::QuotaClient; | 16 using quota::QuotaClient; |
| 17 using webkit_database::DatabaseUtil; | 17 using webkit_database::DatabaseUtil; |
| 18 | 18 |
| 19 namespace content { | 19 namespace content { |
| 20 namespace { | 20 namespace { |
| 21 | 21 |
| 22 quota::QuotaStatusCode DeleteOriginDataOnWebKitThread( | 22 quota::QuotaStatusCode DeleteOriginDataOnWebKitThread( |
| 23 IndexedDBContextImpl* context, | 23 IndexedDBContextImpl* context, |
| 24 const GURL& origin) { | 24 const GURL& origin) { |
| 25 context->DeleteForOrigin(origin); | 25 context->DeleteForOrigin(origin); |
| 26 return quota::kQuotaStatusOk; | 26 return quota::kQuotaStatusOk; |
| 27 } | 27 } |
| 28 | 28 |
| 29 int64 GetOriginUsageOnWebKitThread( | 29 int64 GetOriginUsageOnWebKitThread(IndexedDBContextImpl* context, |
| 30 IndexedDBContextImpl* context, | 30 const GURL& origin) { |
| 31 const GURL& origin) { | |
| 32 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT_DEPRECATED)); | 31 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT_DEPRECATED)); |
| 33 return context->GetOriginDiskUsage(origin); | 32 return context->GetOriginDiskUsage(origin); |
| 34 } | 33 } |
| 35 | 34 |
| 36 void GetAllOriginsOnWebKitThread( | 35 void GetAllOriginsOnWebKitThread(IndexedDBContextImpl* context, |
| 37 IndexedDBContextImpl* context, | 36 std::set<GURL>* origins_to_return) { |
| 38 std::set<GURL>* origins_to_return) { | |
| 39 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT_DEPRECATED)); | 37 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT_DEPRECATED)); |
| 40 std::vector<GURL> all_origins = context->GetAllOrigins(); | 38 std::vector<GURL> all_origins = context->GetAllOrigins(); |
| 41 origins_to_return->insert(all_origins.begin(), all_origins.end()); | 39 origins_to_return->insert(all_origins.begin(), all_origins.end()); |
| 42 } | 40 } |
| 43 | 41 |
| 44 void DidGetOrigins( | 42 void DidGetOrigins(const IndexedDBQuotaClient::GetOriginsCallback& callback, |
| 45 const IndexedDBQuotaClient::GetOriginsCallback& callback, | 43 const std::set<GURL>* origins) { |
| 46 const std::set<GURL>* origins) { | |
| 47 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 44 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 48 callback.Run(*origins); | 45 callback.Run(*origins); |
| 49 } | 46 } |
| 50 | 47 |
| 51 void GetOriginsForHostOnWebKitThread( | 48 void GetOriginsForHostOnWebKitThread(IndexedDBContextImpl* context, |
| 52 IndexedDBContextImpl* context, | 49 const std::string& host, |
| 53 const std::string& host, | 50 std::set<GURL>* origins_to_return) { |
| 54 std::set<GURL>* origins_to_return) { | |
| 55 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT_DEPRECATED)); | 51 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT_DEPRECATED)); |
| 56 std::vector<GURL> all_origins = context->GetAllOrigins(); | 52 std::vector<GURL> all_origins = context->GetAllOrigins(); |
| 57 for (std::vector<GURL>::const_iterator iter = all_origins.begin(); | 53 for (std::vector<GURL>::const_iterator iter = all_origins.begin(); |
| 58 iter != all_origins.end(); ++iter) { | 54 iter != all_origins.end(); |
| 55 ++iter) { |
| 59 if (host == net::GetHostOrSpecFromURL(*iter)) | 56 if (host == net::GetHostOrSpecFromURL(*iter)) |
| 60 origins_to_return->insert(*iter); | 57 origins_to_return->insert(*iter); |
| 61 } | 58 } |
| 62 } | 59 } |
| 63 | 60 |
| 64 } // namespace | 61 } // namespace |
| 65 | 62 |
| 66 // IndexedDBQuotaClient -------------------------------------------------------- | 63 // IndexedDBQuotaClient -------------------------------------------------------- |
| 67 | 64 |
| 68 IndexedDBQuotaClient::IndexedDBQuotaClient( | 65 IndexedDBQuotaClient::IndexedDBQuotaClient( |
| 69 base::MessageLoopProxy* webkit_thread_message_loop, | 66 base::MessageLoopProxy* webkit_thread_message_loop, |
| 70 IndexedDBContextImpl* indexed_db_context) | 67 IndexedDBContextImpl* indexed_db_context) |
| 71 : webkit_thread_message_loop_(webkit_thread_message_loop), | 68 : webkit_thread_message_loop_(webkit_thread_message_loop), |
| 72 indexed_db_context_(indexed_db_context) { | 69 indexed_db_context_(indexed_db_context) {} |
| 73 } | |
| 74 | 70 |
| 75 IndexedDBQuotaClient::~IndexedDBQuotaClient() { | 71 IndexedDBQuotaClient::~IndexedDBQuotaClient() {} |
| 76 } | |
| 77 | 72 |
| 78 QuotaClient::ID IndexedDBQuotaClient::id() const { | 73 QuotaClient::ID IndexedDBQuotaClient::id() const { return kIndexedDatabase; } |
| 79 return kIndexedDatabase; | |
| 80 } | |
| 81 | 74 |
| 82 void IndexedDBQuotaClient::OnQuotaManagerDestroyed() { | 75 void IndexedDBQuotaClient::OnQuotaManagerDestroyed() { delete this; } |
| 83 delete this; | |
| 84 } | |
| 85 | 76 |
| 86 void IndexedDBQuotaClient::GetOriginUsage( | 77 void IndexedDBQuotaClient::GetOriginUsage(const GURL& origin_url, |
| 87 const GURL& origin_url, | 78 quota::StorageType type, |
| 88 quota::StorageType type, | 79 const GetUsageCallback& callback) { |
| 89 const GetUsageCallback& callback) { | |
| 90 DCHECK(!callback.is_null()); | 80 DCHECK(!callback.is_null()); |
| 91 DCHECK(indexed_db_context_.get()); | 81 DCHECK(indexed_db_context_.get()); |
| 92 | 82 |
| 93 // IndexedDB is in the temp namespace for now. | 83 // IndexedDB is in the temp namespace for now. |
| 94 if (type != quota::kStorageTypeTemporary) { | 84 if (type != quota::kStorageTypeTemporary) { |
| 95 callback.Run(0); | 85 callback.Run(0); |
| 96 return; | 86 return; |
| 97 } | 87 } |
| 98 | 88 |
| 99 base::PostTaskAndReplyWithResult( | 89 base::PostTaskAndReplyWithResult( |
| 100 webkit_thread_message_loop_, | 90 webkit_thread_message_loop_, |
| 101 FROM_HERE, | 91 FROM_HERE, |
| 102 base::Bind(&GetOriginUsageOnWebKitThread, | 92 base::Bind( |
| 103 indexed_db_context_, | 93 &GetOriginUsageOnWebKitThread, indexed_db_context_, origin_url), |
| 104 origin_url), | |
| 105 callback); | 94 callback); |
| 106 } | 95 } |
| 107 | 96 |
| 108 void IndexedDBQuotaClient::GetOriginsForType( | 97 void IndexedDBQuotaClient::GetOriginsForType( |
| 109 quota::StorageType type, | 98 quota::StorageType type, |
| 110 const GetOriginsCallback& callback) { | 99 const GetOriginsCallback& callback) { |
| 111 DCHECK(!callback.is_null()); | 100 DCHECK(!callback.is_null()); |
| 112 DCHECK(indexed_db_context_.get()); | 101 DCHECK(indexed_db_context_.get()); |
| 113 | 102 |
| 114 // All databases are in the temp namespace for now. | 103 // All databases are in the temp namespace for now. |
| 115 if (type != quota::kStorageTypeTemporary) { | 104 if (type != quota::kStorageTypeTemporary) { |
| 116 callback.Run(std::set<GURL>()); | 105 callback.Run(std::set<GURL>()); |
| 117 return; | 106 return; |
| 118 } | 107 } |
| 119 | 108 |
| 120 std::set<GURL>* origins_to_return = new std::set<GURL>(); | 109 std::set<GURL>* origins_to_return = new std::set<GURL>(); |
| 121 webkit_thread_message_loop_->PostTaskAndReply( | 110 webkit_thread_message_loop_->PostTaskAndReply( |
| 122 FROM_HERE, | 111 FROM_HERE, |
| 123 base::Bind(&GetAllOriginsOnWebKitThread, | 112 base::Bind(&GetAllOriginsOnWebKitThread, |
| 124 indexed_db_context_, | 113 indexed_db_context_, |
| 125 base::Unretained(origins_to_return)), | 114 base::Unretained(origins_to_return)), |
| 126 base::Bind(&DidGetOrigins, | 115 base::Bind(&DidGetOrigins, callback, base::Owned(origins_to_return))); |
| 127 callback, | |
| 128 base::Owned(origins_to_return))); | |
| 129 } | 116 } |
| 130 | 117 |
| 131 void IndexedDBQuotaClient::GetOriginsForHost( | 118 void IndexedDBQuotaClient::GetOriginsForHost( |
| 132 quota::StorageType type, | 119 quota::StorageType type, |
| 133 const std::string& host, | 120 const std::string& host, |
| 134 const GetOriginsCallback& callback) { | 121 const GetOriginsCallback& callback) { |
| 135 DCHECK(!callback.is_null()); | 122 DCHECK(!callback.is_null()); |
| 136 DCHECK(indexed_db_context_.get()); | 123 DCHECK(indexed_db_context_.get()); |
| 137 | 124 |
| 138 // All databases are in the temp namespace for now. | 125 // All databases are in the temp namespace for now. |
| 139 if (type != quota::kStorageTypeTemporary) { | 126 if (type != quota::kStorageTypeTemporary) { |
| 140 callback.Run(std::set<GURL>()); | 127 callback.Run(std::set<GURL>()); |
| 141 return; | 128 return; |
| 142 } | 129 } |
| 143 | 130 |
| 144 std::set<GURL>* origins_to_return = new std::set<GURL>(); | 131 std::set<GURL>* origins_to_return = new std::set<GURL>(); |
| 145 webkit_thread_message_loop_->PostTaskAndReply( | 132 webkit_thread_message_loop_->PostTaskAndReply( |
| 146 FROM_HERE, | 133 FROM_HERE, |
| 147 base::Bind(&GetOriginsForHostOnWebKitThread, | 134 base::Bind(&GetOriginsForHostOnWebKitThread, |
| 148 indexed_db_context_, | 135 indexed_db_context_, |
| 149 host, | 136 host, |
| 150 base::Unretained(origins_to_return)), | 137 base::Unretained(origins_to_return)), |
| 151 base::Bind(&DidGetOrigins, | 138 base::Bind(&DidGetOrigins, callback, base::Owned(origins_to_return))); |
| 152 callback, | |
| 153 base::Owned(origins_to_return))); | |
| 154 } | 139 } |
| 155 | 140 |
| 156 void IndexedDBQuotaClient::DeleteOriginData( | 141 void IndexedDBQuotaClient::DeleteOriginData(const GURL& origin, |
| 157 const GURL& origin, | 142 quota::StorageType type, |
| 158 quota::StorageType type, | 143 const DeletionCallback& callback) { |
| 159 const DeletionCallback& callback) { | |
| 160 if (type != quota::kStorageTypeTemporary) { | 144 if (type != quota::kStorageTypeTemporary) { |
| 161 callback.Run(quota::kQuotaErrorNotSupported); | 145 callback.Run(quota::kQuotaErrorNotSupported); |
| 162 return; | 146 return; |
| 163 } | 147 } |
| 164 | 148 |
| 165 base::PostTaskAndReplyWithResult( | 149 base::PostTaskAndReplyWithResult( |
| 166 webkit_thread_message_loop_, | 150 webkit_thread_message_loop_, |
| 167 FROM_HERE, | 151 FROM_HERE, |
| 168 base::Bind(&DeleteOriginDataOnWebKitThread, | 152 base::Bind(&DeleteOriginDataOnWebKitThread, indexed_db_context_, origin), |
| 169 indexed_db_context_, | |
| 170 origin), | |
| 171 callback); | 153 callback); |
| 172 } | 154 } |
| 173 | 155 |
| 174 } // namespace content | 156 } // namespace content |
| OLD | NEW |