| 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 "webkit/browser/database/database_quota_client.h" | 5 #include "webkit/browser/database/database_quota_client.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| 11 #include "base/location.h" | 11 #include "base/location.h" |
| 12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/message_loop/message_loop_proxy.h" | 13 #include "base/message_loop/message_loop_proxy.h" |
| 14 #include "base/task_runner_util.h" | 14 #include "base/task_runner_util.h" |
| 15 #include "net/base/net_errors.h" | 15 #include "net/base/net_errors.h" |
| 16 #include "net/base/net_util.h" | 16 #include "net/base/net_util.h" |
| 17 #include "webkit/browser/database/database_tracker.h" | 17 #include "webkit/browser/database/database_tracker.h" |
| 18 #include "webkit/browser/database/database_util.h" | 18 #include "webkit/browser/database/database_util.h" |
| 19 #include "webkit/common/database/database_identifier.h" | 19 #include "webkit/common/database/database_identifier.h" |
| 20 | 20 |
| 21 using quota::QuotaClient; | 21 using storage::QuotaClient; |
| 22 | 22 |
| 23 namespace webkit_database { | 23 namespace storage { |
| 24 | 24 |
| 25 namespace { | 25 namespace { |
| 26 | 26 |
| 27 int64 GetOriginUsageOnDBThread( | 27 int64 GetOriginUsageOnDBThread( |
| 28 DatabaseTracker* db_tracker, | 28 DatabaseTracker* db_tracker, |
| 29 const GURL& origin_url) { | 29 const GURL& origin_url) { |
| 30 OriginInfo info; | 30 OriginInfo info; |
| 31 if (db_tracker->GetOriginInfo( | 31 if (db_tracker->GetOriginInfo(storage::GetIdentifierFromOrigin(origin_url), |
| 32 webkit_database::GetIdentifierFromOrigin(origin_url), &info)) | 32 &info)) |
| 33 return info.TotalSize(); | 33 return info.TotalSize(); |
| 34 return 0; | 34 return 0; |
| 35 } | 35 } |
| 36 | 36 |
| 37 void GetOriginsOnDBThread( | 37 void GetOriginsOnDBThread( |
| 38 DatabaseTracker* db_tracker, | 38 DatabaseTracker* db_tracker, |
| 39 std::set<GURL>* origins_ptr) { | 39 std::set<GURL>* origins_ptr) { |
| 40 std::vector<std::string> origin_identifiers; | 40 std::vector<std::string> origin_identifiers; |
| 41 if (db_tracker->GetAllOriginIdentifiers(&origin_identifiers)) { | 41 if (db_tracker->GetAllOriginIdentifiers(&origin_identifiers)) { |
| 42 for (std::vector<std::string>::const_iterator iter = | 42 for (std::vector<std::string>::const_iterator iter = |
| 43 origin_identifiers.begin(); | 43 origin_identifiers.begin(); |
| 44 iter != origin_identifiers.end(); ++iter) { | 44 iter != origin_identifiers.end(); ++iter) { |
| 45 GURL origin = webkit_database::GetOriginFromIdentifier(*iter); | 45 GURL origin = storage::GetOriginFromIdentifier(*iter); |
| 46 origins_ptr->insert(origin); | 46 origins_ptr->insert(origin); |
| 47 } | 47 } |
| 48 } | 48 } |
| 49 } | 49 } |
| 50 | 50 |
| 51 void GetOriginsForHostOnDBThread( | 51 void GetOriginsForHostOnDBThread( |
| 52 DatabaseTracker* db_tracker, | 52 DatabaseTracker* db_tracker, |
| 53 std::set<GURL>* origins_ptr, | 53 std::set<GURL>* origins_ptr, |
| 54 const std::string& host) { | 54 const std::string& host) { |
| 55 std::vector<std::string> origin_identifiers; | 55 std::vector<std::string> origin_identifiers; |
| 56 if (db_tracker->GetAllOriginIdentifiers(&origin_identifiers)) { | 56 if (db_tracker->GetAllOriginIdentifiers(&origin_identifiers)) { |
| 57 for (std::vector<std::string>::const_iterator iter = | 57 for (std::vector<std::string>::const_iterator iter = |
| 58 origin_identifiers.begin(); | 58 origin_identifiers.begin(); |
| 59 iter != origin_identifiers.end(); ++iter) { | 59 iter != origin_identifiers.end(); ++iter) { |
| 60 GURL origin = webkit_database::GetOriginFromIdentifier(*iter); | 60 GURL origin = storage::GetOriginFromIdentifier(*iter); |
| 61 if (host == net::GetHostOrSpecFromURL(origin)) | 61 if (host == net::GetHostOrSpecFromURL(origin)) |
| 62 origins_ptr->insert(origin); | 62 origins_ptr->insert(origin); |
| 63 } | 63 } |
| 64 } | 64 } |
| 65 } | 65 } |
| 66 | 66 |
| 67 void DidGetOrigins( | 67 void DidGetOrigins( |
| 68 const QuotaClient::GetOriginsCallback& callback, | 68 const QuotaClient::GetOriginsCallback& callback, |
| 69 std::set<GURL>* origins_ptr) { | 69 std::set<GURL>* origins_ptr) { |
| 70 callback.Run(*origins_ptr); | 70 callback.Run(*origins_ptr); |
| 71 } | 71 } |
| 72 | 72 |
| 73 void DidDeleteOriginData( | 73 void DidDeleteOriginData( |
| 74 base::SingleThreadTaskRunner* original_task_runner, | 74 base::SingleThreadTaskRunner* original_task_runner, |
| 75 const QuotaClient::DeletionCallback& callback, | 75 const QuotaClient::DeletionCallback& callback, |
| 76 int result) { | 76 int result) { |
| 77 if (result == net::ERR_IO_PENDING) { | 77 if (result == net::ERR_IO_PENDING) { |
| 78 // The callback will be invoked via | 78 // The callback will be invoked via |
| 79 // DatabaseTracker::ScheduleDatabasesForDeletion. | 79 // DatabaseTracker::ScheduleDatabasesForDeletion. |
| 80 return; | 80 return; |
| 81 } | 81 } |
| 82 | 82 |
| 83 quota::QuotaStatusCode status; | 83 storage::QuotaStatusCode status; |
| 84 if (result == net::OK) | 84 if (result == net::OK) |
| 85 status = quota::kQuotaStatusOk; | 85 status = storage::kQuotaStatusOk; |
| 86 else | 86 else |
| 87 status = quota::kQuotaStatusUnknown; | 87 status = storage::kQuotaStatusUnknown; |
| 88 | 88 |
| 89 if (original_task_runner->BelongsToCurrentThread()) | 89 if (original_task_runner->BelongsToCurrentThread()) |
| 90 callback.Run(status); | 90 callback.Run(status); |
| 91 else | 91 else |
| 92 original_task_runner->PostTask(FROM_HERE, base::Bind(callback, status)); | 92 original_task_runner->PostTask(FROM_HERE, base::Bind(callback, status)); |
| 93 } | 93 } |
| 94 | 94 |
| 95 } // namespace | 95 } // namespace |
| 96 | 96 |
| 97 DatabaseQuotaClient::DatabaseQuotaClient( | 97 DatabaseQuotaClient::DatabaseQuotaClient( |
| (...skipping 14 matching lines...) Expand all Loading... |
| 112 } | 112 } |
| 113 | 113 |
| 114 QuotaClient::ID DatabaseQuotaClient::id() const { | 114 QuotaClient::ID DatabaseQuotaClient::id() const { |
| 115 return kDatabase; | 115 return kDatabase; |
| 116 } | 116 } |
| 117 | 117 |
| 118 void DatabaseQuotaClient::OnQuotaManagerDestroyed() { | 118 void DatabaseQuotaClient::OnQuotaManagerDestroyed() { |
| 119 delete this; | 119 delete this; |
| 120 } | 120 } |
| 121 | 121 |
| 122 void DatabaseQuotaClient::GetOriginUsage( | 122 void DatabaseQuotaClient::GetOriginUsage(const GURL& origin_url, |
| 123 const GURL& origin_url, | 123 storage::StorageType type, |
| 124 quota::StorageType type, | 124 const GetUsageCallback& callback) { |
| 125 const GetUsageCallback& callback) { | |
| 126 DCHECK(!callback.is_null()); | 125 DCHECK(!callback.is_null()); |
| 127 DCHECK(db_tracker_.get()); | 126 DCHECK(db_tracker_.get()); |
| 128 | 127 |
| 129 // All databases are in the temp namespace for now. | 128 // All databases are in the temp namespace for now. |
| 130 if (type != quota::kStorageTypeTemporary) { | 129 if (type != storage::kStorageTypeTemporary) { |
| 131 callback.Run(0); | 130 callback.Run(0); |
| 132 return; | 131 return; |
| 133 } | 132 } |
| 134 | 133 |
| 135 base::PostTaskAndReplyWithResult( | 134 base::PostTaskAndReplyWithResult( |
| 136 db_tracker_thread_.get(), | 135 db_tracker_thread_.get(), |
| 137 FROM_HERE, | 136 FROM_HERE, |
| 138 base::Bind(&GetOriginUsageOnDBThread, db_tracker_, origin_url), | 137 base::Bind(&GetOriginUsageOnDBThread, db_tracker_, origin_url), |
| 139 callback); | 138 callback); |
| 140 } | 139 } |
| 141 | 140 |
| 142 void DatabaseQuotaClient::GetOriginsForType( | 141 void DatabaseQuotaClient::GetOriginsForType( |
| 143 quota::StorageType type, | 142 storage::StorageType type, |
| 144 const GetOriginsCallback& callback) { | 143 const GetOriginsCallback& callback) { |
| 145 DCHECK(!callback.is_null()); | 144 DCHECK(!callback.is_null()); |
| 146 DCHECK(db_tracker_.get()); | 145 DCHECK(db_tracker_.get()); |
| 147 | 146 |
| 148 // All databases are in the temp namespace for now. | 147 // All databases are in the temp namespace for now. |
| 149 if (type != quota::kStorageTypeTemporary) { | 148 if (type != storage::kStorageTypeTemporary) { |
| 150 callback.Run(std::set<GURL>()); | 149 callback.Run(std::set<GURL>()); |
| 151 return; | 150 return; |
| 152 } | 151 } |
| 153 | 152 |
| 154 std::set<GURL>* origins_ptr = new std::set<GURL>(); | 153 std::set<GURL>* origins_ptr = new std::set<GURL>(); |
| 155 db_tracker_thread_->PostTaskAndReply( | 154 db_tracker_thread_->PostTaskAndReply( |
| 156 FROM_HERE, | 155 FROM_HERE, |
| 157 base::Bind(&GetOriginsOnDBThread, | 156 base::Bind(&GetOriginsOnDBThread, |
| 158 db_tracker_, | 157 db_tracker_, |
| 159 base::Unretained(origins_ptr)), | 158 base::Unretained(origins_ptr)), |
| 160 base::Bind(&DidGetOrigins, | 159 base::Bind(&DidGetOrigins, |
| 161 callback, | 160 callback, |
| 162 base::Owned(origins_ptr))); | 161 base::Owned(origins_ptr))); |
| 163 } | 162 } |
| 164 | 163 |
| 165 void DatabaseQuotaClient::GetOriginsForHost( | 164 void DatabaseQuotaClient::GetOriginsForHost( |
| 166 quota::StorageType type, | 165 storage::StorageType type, |
| 167 const std::string& host, | 166 const std::string& host, |
| 168 const GetOriginsCallback& callback) { | 167 const GetOriginsCallback& callback) { |
| 169 DCHECK(!callback.is_null()); | 168 DCHECK(!callback.is_null()); |
| 170 DCHECK(db_tracker_.get()); | 169 DCHECK(db_tracker_.get()); |
| 171 | 170 |
| 172 // All databases are in the temp namespace for now. | 171 // All databases are in the temp namespace for now. |
| 173 if (type != quota::kStorageTypeTemporary) { | 172 if (type != storage::kStorageTypeTemporary) { |
| 174 callback.Run(std::set<GURL>()); | 173 callback.Run(std::set<GURL>()); |
| 175 return; | 174 return; |
| 176 } | 175 } |
| 177 | 176 |
| 178 std::set<GURL>* origins_ptr = new std::set<GURL>(); | 177 std::set<GURL>* origins_ptr = new std::set<GURL>(); |
| 179 db_tracker_thread_->PostTaskAndReply( | 178 db_tracker_thread_->PostTaskAndReply( |
| 180 FROM_HERE, | 179 FROM_HERE, |
| 181 base::Bind(&GetOriginsForHostOnDBThread, | 180 base::Bind(&GetOriginsForHostOnDBThread, |
| 182 db_tracker_, | 181 db_tracker_, |
| 183 base::Unretained(origins_ptr), | 182 base::Unretained(origins_ptr), |
| 184 host), | 183 host), |
| 185 base::Bind(&DidGetOrigins, | 184 base::Bind(&DidGetOrigins, |
| 186 callback, | 185 callback, |
| 187 base::Owned(origins_ptr))); | 186 base::Owned(origins_ptr))); |
| 188 } | 187 } |
| 189 | 188 |
| 190 void DatabaseQuotaClient::DeleteOriginData( | 189 void DatabaseQuotaClient::DeleteOriginData(const GURL& origin, |
| 191 const GURL& origin, | 190 storage::StorageType type, |
| 192 quota::StorageType type, | 191 const DeletionCallback& callback) { |
| 193 const DeletionCallback& callback) { | |
| 194 DCHECK(!callback.is_null()); | 192 DCHECK(!callback.is_null()); |
| 195 DCHECK(db_tracker_.get()); | 193 DCHECK(db_tracker_.get()); |
| 196 | 194 |
| 197 // All databases are in the temp namespace for now, so nothing to delete. | 195 // All databases are in the temp namespace for now, so nothing to delete. |
| 198 if (type != quota::kStorageTypeTemporary) { | 196 if (type != storage::kStorageTypeTemporary) { |
| 199 callback.Run(quota::kQuotaStatusOk); | 197 callback.Run(storage::kQuotaStatusOk); |
| 200 return; | 198 return; |
| 201 } | 199 } |
| 202 | 200 |
| 203 base::Callback<void(int)> delete_callback = | 201 base::Callback<void(int)> delete_callback = |
| 204 base::Bind(&DidDeleteOriginData, | 202 base::Bind(&DidDeleteOriginData, |
| 205 base::MessageLoopProxy::current(), | 203 base::MessageLoopProxy::current(), |
| 206 callback); | 204 callback); |
| 207 | 205 |
| 208 PostTaskAndReplyWithResult( | 206 PostTaskAndReplyWithResult( |
| 209 db_tracker_thread_.get(), | 207 db_tracker_thread_.get(), |
| 210 FROM_HERE, | 208 FROM_HERE, |
| 211 base::Bind(&DatabaseTracker::DeleteDataForOrigin, | 209 base::Bind(&DatabaseTracker::DeleteDataForOrigin, |
| 212 db_tracker_, | 210 db_tracker_, |
| 213 webkit_database::GetIdentifierFromOrigin(origin), | 211 storage::GetIdentifierFromOrigin(origin), |
| 214 delete_callback), | 212 delete_callback), |
| 215 delete_callback); | 213 delete_callback); |
| 216 } | 214 } |
| 217 | 215 |
| 218 bool DatabaseQuotaClient::DoesSupport(quota::StorageType type) const { | 216 bool DatabaseQuotaClient::DoesSupport(storage::StorageType type) const { |
| 219 return type == quota::kStorageTypeTemporary; | 217 return type == storage::kStorageTypeTemporary; |
| 220 } | 218 } |
| 221 | 219 |
| 222 } // namespace webkit_database | 220 } // namespace storage |
| OLD | NEW |