Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(165)

Unified Diff: webkit/appcache/appcache_quota_client.cc

Issue 7031065: AppCache + Quota integration (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: webkit/appcache/appcache_quota_client.cc
===================================================================
--- webkit/appcache/appcache_quota_client.cc (revision 0)
+++ webkit/appcache/appcache_quota_client.cc (revision 0)
@@ -0,0 +1,257 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "webkit/appcache/appcache_quota_client.h"
+
+#include <algorithm>
+#include <map>
+
+#include "webkit/appcache/appcache_service.h"
+
+using quota::QuotaClient;
+
+namespace {
+
+quota::QuotaStatusCode NetErrorCodeToQuotaStatus(int code) {
+ if (code == net::OK)
+ return quota::kQuotaStatusOk;
+ else if (code == net::ERR_ABORTED)
+ return quota::kQuotaErrorAbort;
+ else
+ return quota::kQuotaStatusUnknown;
+}
+
+template <class RequestType>
+void DeleteCallbackHelper(const RequestType& request) {
+ delete request.callback;
+}
+
+}
+
+namespace appcache {
+
+AppCacheQuotaClient::AppCacheQuotaClient(AppCacheService* service)
+ : ALLOW_THIS_IN_INITIALIZER_LIST(service_delete_callback_(
+ this, &AppCacheQuotaClient::DidDeleteAppCachesForOrigin)),
+ service_(service), appcache_is_ready_(false),
+ quota_manager_is_destroyed_(false) {
+}
+
+AppCacheQuotaClient::~AppCacheQuotaClient() {
+ DeletePendingRequests();
kinuko 2011/06/02 08:38:06 do we need this? we seem to be deleting callbacks
michaeln 2011/06/02 22:46:55 I don't think we do... thnx.
+}
+
+QuotaClient::ID AppCacheQuotaClient::id() const {
+ return kAppcache;
+}
+
+void AppCacheQuotaClient::OnQuotaManagerDestroyed() {
+ DeletePendingRequests();
+ quota_manager_is_destroyed_ = true;
+ if (!service_)
+ delete this;
+}
+
+void AppCacheQuotaClient::GetOriginUsage(
+ const GURL& origin,
+ quota::StorageType type,
+ GetUsageCallback* callback_ptr) {
+ DCHECK(callback_ptr);
+ DCHECK(!quota_manager_is_destroyed_);
+ // Note: Requests may not be processed in order.
kinuko 2011/06/02 08:38:06 Does the QM's usage tracker need to be able to han
michaeln 2011/06/02 22:46:55 I'll rearrange things here to respond in order ins
+
+ scoped_ptr<GetUsageCallback> callback(callback_ptr);
+ if (!service_ || type != quota::kStorageTypeTemporary) {
+ callback->Run(0);
+ return;
+ }
+
+ if (!appcache_is_ready_) {
+ pending_usage_requests_.push_back(UsageRequest());
+ pending_usage_requests_.back().origin = origin;
+ pending_usage_requests_.back().callback = callback.release();
+ return;
+ }
+
+ const AppCacheStorage::UsageMap* map = GetUsageMap();
+ AppCacheStorage::UsageMap::const_iterator found = map->find(origin);
+ if (found == map->end()) {
+ callback->Run(0);
+ return;
+ }
+ callback->Run(found->second);
+}
+
+void AppCacheQuotaClient::GetOriginsForType(
+ quota::StorageType type,
+ GetOriginsCallback* callback_ptr) {
+ GetOriginsHelper(type, std::string(), callback_ptr);
+}
+
+void AppCacheQuotaClient::GetOriginsForHost(
+ quota::StorageType type,
+ const std::string& host,
+ GetOriginsCallback* callback_ptr) {
+ DCHECK(!host.empty());
+ GetOriginsHelper(type, host, callback_ptr);
+}
+
+void AppCacheQuotaClient::DeleteOriginData(const GURL& origin,
+ quota::StorageType type,
+ DeletionCallback* callback_ptr) {
+ DCHECK(callback_ptr);
+ DCHECK(!quota_manager_is_destroyed_);
+ // Note: Requests may not be processed in order.
+
+ scoped_ptr<DeletionCallback> callback(callback_ptr);
+ if (!service_ || type != quota::kStorageTypeTemporary) {
+ callback->Run(quota::kQuotaStatusOk);
+ return;
+ }
+
+ if (!appcache_is_ready_ || current_delete_request_callback_.get()) {
+ pending_delete_requests_.push_back(DeleteRequest());
+ pending_delete_requests_.back().origin = origin;
+ pending_delete_requests_.back().callback = callback.release();
+ return;
+ }
+
+ current_delete_request_callback_.swap(callback);
+ service_->DeleteAppCachesForOrigin(origin, &service_delete_callback_);
+}
+
+void AppCacheQuotaClient::DidDeleteAppCachesForOrigin(int rv) {
+ if (quota_manager_is_destroyed_)
+ return;
+
+ // Finish the request by calling our callers callback.
+ current_delete_request_callback_->Run(NetErrorCodeToQuotaStatus(rv));
+ current_delete_request_callback_.reset();
+ if (pending_delete_requests_.empty())
+ return;
+
+ // Start the next in the queue.
+ DeleteRequest& next_request = pending_delete_requests_.front();
+ current_delete_request_callback_.reset(next_request.callback);
+ service_->DeleteAppCachesForOrigin(next_request.origin,
+ &service_delete_callback_);
+ pending_delete_requests_.pop_front();
+}
+
+void AppCacheQuotaClient::GetOriginsHelper(
+ quota::StorageType type,
+ const std::string& opt_host,
+ GetOriginsCallback* callback_ptr) {
+ DCHECK(callback_ptr);
+ DCHECK(!quota_manager_is_destroyed_);
+ // Note: Requests may not be processed in order.
+
+ scoped_ptr<GetOriginsCallback> callback(callback_ptr);
+ if (!service_ || type != quota::kStorageTypeTemporary) {
+ callback->Run(std::set<GURL>());
+ return;
+ }
+
+ if (!appcache_is_ready_) {
+ pending_origins_requests_.push_back(OriginsRequest());
+ pending_origins_requests_.back().callback = callback.release();
+ pending_origins_requests_.back().opt_host = opt_host;
+ return;
+ }
+
+ const AppCacheStorage::UsageMap* map = GetUsageMap();
+ std::set<GURL> origins;
+ for (AppCacheStorage::UsageMap::const_iterator iter = map->begin();
+ iter != map->end(); ++iter) {
+ if (!opt_host.empty() || iter->first.host() == opt_host)
+ origins.insert(iter->first);
+ }
+ callback->Run(origins);
+}
+
+void AppCacheQuotaClient::ProcessPendingRequests() {
+ DCHECK(appcache_is_ready_);
+ while (!pending_usage_requests_.empty()) {
+ UsageRequest& request = pending_usage_requests_.front();
+ GetOriginUsage(request.origin, quota::kStorageTypeTemporary,
+ request.callback);
+ pending_usage_requests_.pop_front();
+ }
+
+ while (!pending_origins_requests_.empty()) {
+ OriginsRequest& request = pending_origins_requests_.front();
+ GetOriginsHelper(quota::kStorageTypeTemporary, request.opt_host,
+ request.callback);
+ pending_origins_requests_.pop_front();
+ }
+
+ if (!pending_delete_requests_.empty()) {
+ // Just start the first delete, others will follow upon completion.
+ DeleteRequest& request = pending_delete_requests_.front();
+ DeleteOriginData(request.origin, quota::kStorageTypeTemporary,
+ request.callback);
+ pending_delete_requests_.pop_front();
+ }
+}
+
+void AppCacheQuotaClient::AbortPendingRequests() {
+ std::for_each(pending_usage_requests_.begin(),
+ pending_usage_requests_.end(),
+ AbortUsageRequest);
+ std::for_each(pending_origins_requests_.begin(),
+ pending_origins_requests_.end(),
+ AbortOriginsRequest);
+ std::for_each(pending_delete_requests_.begin(),
+ pending_delete_requests_.end(),
+ AbortDeleteRequest);
+}
+
+void AppCacheQuotaClient::DeletePendingRequests() {
+ std::for_each(pending_usage_requests_.begin(),
+ pending_usage_requests_.end(),
+ DeleteCallbackHelper<UsageRequest>);
+ std::for_each(pending_origins_requests_.begin(),
+ pending_origins_requests_.end(),
+ DeleteCallbackHelper<OriginsRequest>);
+ std::for_each(pending_delete_requests_.begin(),
+ pending_delete_requests_.end(),
+ DeleteCallbackHelper<DeleteRequest>);
+}
+
+const AppCacheStorage::UsageMap* AppCacheQuotaClient::GetUsageMap() {
+ DCHECK(service_);
+ return service_->storage()->usage_map();
+}
+
+void AppCacheQuotaClient::NotifyAppCacheReady() {
+ appcache_is_ready_ = true;
+ ProcessPendingRequests();
+}
+
+void AppCacheQuotaClient::NotifyAppCacheDestroyed() {
+ service_ = NULL;
+ AbortPendingRequests();
+ if (quota_manager_is_destroyed_)
+ delete this;
+}
+
+// static
+void AppCacheQuotaClient::AbortUsageRequest(const UsageRequest& request) {
+ request.callback->Run(0);
+ delete request.callback;
+}
+
+// static
+void AppCacheQuotaClient::AbortOriginsRequest(const OriginsRequest& request) {
+ request.callback->Run(std::set<GURL>());
+ delete request.callback;
+}
+
+// static
+void AppCacheQuotaClient::AbortDeleteRequest(const DeleteRequest& request) {
+ request.callback->Run(quota::kQuotaErrorAbort);
+ delete request.callback;
+}
+
+} // namespace appcache
Property changes on: webkit\appcache\appcache_quota_client.cc
___________________________________________________________________
Added: svn:eol-style
+ LF

Powered by Google App Engine
This is Rietveld 408576698