Chromium Code Reviews| Index: chrome/browser/browsing_data_quota_helper_impl.cc |
| diff --git a/chrome/browser/browsing_data_quota_helper_impl.cc b/chrome/browser/browsing_data_quota_helper_impl.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..4950c8e71acdb2de434f2a3f60a36a42bf753d72 |
| --- /dev/null |
| +++ b/chrome/browser/browsing_data_quota_helper_impl.cc |
| @@ -0,0 +1,201 @@ |
| +// 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 "chrome/browser/browsing_data_quota_helper_impl.h" |
| + |
| +#include <map> |
| +#include <set> |
| + |
| +#include "base/logging.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "webkit/quota/quota_manager.h" |
| + |
| +BrowsingDataQuotaHelper::QuotaInfo::QuotaInfo() |
| + : temporary_usage(-1), |
| + persistent_usage(-1), |
| + persistent_quota(-1) { } |
| + |
| +BrowsingDataQuotaHelper::QuotaInfo::QuotaInfo(const std::string& host) |
| + : host(host), |
| + temporary_usage(-1), |
| + persistent_usage(-1), |
| + persistent_quota(-1) { } |
| + |
| +BrowsingDataQuotaHelper::QuotaInfo::QuotaInfo(const std::string& host, |
| + int64 temporary_usage, |
| + int64 persistent_usage, |
| + int64 persistent_quota) |
| + : host(host), |
| + temporary_usage(temporary_usage), |
| + persistent_usage(persistent_usage), |
| + persistent_quota(persistent_quota) { } |
| + |
| +BrowsingDataQuotaHelper::QuotaInfo::~QuotaInfo() { } |
| + |
| +// static |
| +BrowsingDataQuotaHelper* BrowsingDataQuotaHelper::Create(Profile* profile) { |
| + return new BrowsingDataQuotaHelperImpl( |
| + BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI), |
| + BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO), |
| + profile->GetQuotaManager()); |
| +} |
| + |
| +BrowsingDataQuotaHelperImpl::BrowsingDataQuotaHelperImpl( |
| + base::MessageLoopProxy* ui_thread, |
| + base::MessageLoopProxy* io_thread, |
| + quota::QuotaManager* quota_manager) |
| + : quota_manager_(quota_manager), |
| + is_fetching_(false), |
| + ui_thread_(ui_thread), |
| + io_thread_(io_thread), |
| + callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
| + DCHECK(quota_manager); |
| +} |
| + |
| +BrowsingDataQuotaHelperImpl::~BrowsingDataQuotaHelperImpl() {} |
| + |
| +void BrowsingDataQuotaHelperImpl::StartFetching(FetchResultCallback* callback) { |
| + DCHECK(callback); |
| + DCHECK(!callback_.get()); |
| + DCHECK(!is_fetching_); |
| + callback_.reset(callback); |
| + quota_info_.clear(); |
| + is_fetching_ = true; |
| + |
| + FetchQuotaInfo(); |
| +} |
| + |
| +void BrowsingDataQuotaHelperImpl::CancelNotification() { |
| + callback_.reset(); |
| +} |
| + |
| +void BrowsingDataQuotaHelperImpl::FetchQuotaInfo() { |
| + if (!io_thread_->BelongsToCurrentThread()) { |
| + io_thread_->PostTask( |
| + FROM_HERE, |
| + NewRunnableMethod( |
| + this, |
| + &BrowsingDataQuotaHelperImpl::FetchQuotaInfo)); |
| + return; |
| + } |
| + |
| + quota_manager_->GetOriginsModifiedSince( |
| + quota::kStorageTypeTemporary, |
| + base::Time(), |
| + callback_factory_.NewCallback( |
| + &BrowsingDataQuotaHelperImpl::GotTemporaryStorageOrigins)); |
| +} |
| + |
| +void BrowsingDataQuotaHelperImpl::GotTemporaryStorageOrigins( |
| + const std::set<GURL>& origins) { |
| + GotOrigins(quota::kStorageTypeTemporary, origins); |
| + |
| + quota_manager_->GetOriginsModifiedSince( |
| + quota::kStorageTypePersistent, |
| + base::Time(), |
| + callback_factory_.NewCallback( |
| + &BrowsingDataQuotaHelperImpl::GotPersistentStorageOrigins)); |
| +} |
| + |
| +void BrowsingDataQuotaHelperImpl::GotPersistentStorageOrigins( |
| + const std::set<GURL>& origins) { |
| + GotOrigins(quota::kStorageTypePersistent, origins); |
| + |
| + ProcessPendingHosts(); |
| +} |
| + |
| +void BrowsingDataQuotaHelperImpl::GotOrigins( |
| + quota::StorageType type, const std::set<GURL>& origins) { |
| + for (std::set<GURL>::const_iterator itr = origins.begin(); |
| + itr != origins.end(); |
| + ++itr) |
| + pending_hosts_.insert(std::make_pair(itr->host(), type)); |
| +} |
| + |
| +void BrowsingDataQuotaHelperImpl::ProcessPendingHosts() { |
| + if (pending_hosts_.empty()) { |
| + OnComplete(); |
| + return; |
| + } |
| + |
| + PendingHosts::iterator itr = pending_hosts_.begin(); |
| + std::string host = itr->first; |
| + quota::StorageType type = itr->second; |
| + pending_hosts_.erase(itr); |
| + GetHostUsage(host, type); |
| +} |
| + |
| +void BrowsingDataQuotaHelperImpl::GetHostUsage(const std::string& host, |
| + quota::StorageType type) { |
|
kinuko
2011/07/25 13:22:41
nit: weird indentation
tzik
2011/07/26 10:52:41
Done.
|
| + DCHECK(quota_manager_.get()); |
| + quota_manager_->GetHostUsage( |
| + host, type, |
| + callback_factory_.NewCallback( |
| + &BrowsingDataQuotaHelperImpl::GotHostUsage)); |
| +} |
| + |
| +void BrowsingDataQuotaHelperImpl::GotHostUsage(const std::string& host, |
| + quota::StorageType type, |
| + int64 usage) { |
|
kinuko
2011/07/25 13:22:41
ditto
tzik
2011/07/26 10:52:41
Done.
|
| + switch (type) { |
| + case quota::kStorageTypeTemporary: |
| + quota_info_[host].temporary_usage = usage; |
| + break; |
| + case quota::kStorageTypePersistent: |
| + quota_info_[host].persistent_usage = usage; |
| + break; |
| + default: |
| + NOTREACHED(); |
| + } |
| + ProcessPendingHosts(); |
| +} |
| + |
| +void BrowsingDataQuotaHelperImpl::OnComplete() { |
| + // Check if CancelNotification was called |
| + if (!callback_.get()) |
| + return; |
| + |
| + if (!ui_thread_->BelongsToCurrentThread()) { |
| + ui_thread_->PostTask( |
| + FROM_HERE, |
| + NewRunnableMethod( |
| + this, |
| + &BrowsingDataQuotaHelperImpl::OnComplete)); |
| + return; |
| + } |
| + |
| + is_fetching_ = false; |
| + |
| + QuotaInfoList result; |
| + result.reserve(quota_info_.size()); |
| + |
| + for (std::map<std::string, QuotaInfo>::iterator itr = quota_info_.begin(); |
| + itr != quota_info_.end(); |
| + ++itr) { |
| + itr->second.host = itr->first; |
| + result.push_back(itr->second); |
| + } |
| + |
| + callback_->Run(result); |
| + callback_.reset(); |
| +} |
| + |
| +bool operator <(const BrowsingDataQuotaHelper::QuotaInfo& lhs, |
| + const BrowsingDataQuotaHelper::QuotaInfo& rhs) { |
| + if (lhs.host != rhs.host) |
| + return lhs.host < rhs.host; |
| + if (lhs.temporary_usage != rhs.temporary_usage) |
| + return lhs.temporary_usage < rhs.temporary_usage; |
| + if (lhs.persistent_usage != rhs.persistent_usage) |
| + return lhs.persistent_usage < rhs.persistent_usage; |
| + return lhs.persistent_quota < rhs.persistent_quota; |
| +} |
| + |
| +bool operator ==(const BrowsingDataQuotaHelper::QuotaInfo& lhs, |
| + const BrowsingDataQuotaHelper::QuotaInfo& rhs) { |
|
kinuko
2011/07/25 13:22:41
nit: indent
tzik
2011/07/26 10:52:41
Done.
|
| + return lhs.host == rhs.host && |
| + lhs.temporary_usage == rhs.temporary_usage && |
| + lhs.persistent_usage == rhs.persistent_usage && |
| + lhs.persistent_quota == rhs.persistent_quota; |
| +} |