[email protected] | a9e3ec4 | 2011-08-09 06:48:30 | [diff] [blame] | 1 | // Copyright (c) 2011 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 "chrome/browser/browsing_data_quota_helper_impl.h" |
| 6 | |
| 7 | #include <map> |
| 8 | #include <set> |
| 9 | |
[email protected] | 4d99be5 | 2011-10-18 14:11:03 | [diff] [blame] | 10 | #include "base/bind.h" |
[email protected] | a9e3ec4 | 2011-08-09 06:48:30 | [diff] [blame] | 11 | #include "base/logging.h" |
| 12 | #include "chrome/browser/profiles/profile.h" |
| 13 | #include "webkit/quota/quota_manager.h" |
| 14 | |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 15 | using content::BrowserThread; |
| 16 | |
[email protected] | a9e3ec4 | 2011-08-09 06:48:30 | [diff] [blame] | 17 | // static |
| 18 | BrowsingDataQuotaHelper* BrowsingDataQuotaHelper::Create(Profile* profile) { |
| 19 | return new BrowsingDataQuotaHelperImpl( |
| 20 | BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI), |
| 21 | BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO), |
| 22 | profile->GetQuotaManager()); |
| 23 | } |
| 24 | |
[email protected] | 91b06c0 | 2011-11-10 06:17:13 | [diff] [blame] | 25 | void BrowsingDataQuotaHelperImpl::StartFetching( |
| 26 | const FetchResultCallback& callback) { |
| 27 | DCHECK_EQ(false, callback.is_null()); |
| 28 | DCHECK(callback_.is_null()); |
[email protected] | a9e3ec4 | 2011-08-09 06:48:30 | [diff] [blame] | 29 | DCHECK(!is_fetching_); |
[email protected] | 91b06c0 | 2011-11-10 06:17:13 | [diff] [blame] | 30 | callback_ = callback; |
[email protected] | a9e3ec4 | 2011-08-09 06:48:30 | [diff] [blame] | 31 | quota_info_.clear(); |
| 32 | is_fetching_ = true; |
| 33 | |
| 34 | FetchQuotaInfo(); |
| 35 | } |
| 36 | |
| 37 | void BrowsingDataQuotaHelperImpl::CancelNotification() { |
[email protected] | 91b06c0 | 2011-11-10 06:17:13 | [diff] [blame] | 38 | callback_.Reset(); |
[email protected] | a9e3ec4 | 2011-08-09 06:48:30 | [diff] [blame] | 39 | } |
| 40 | |
[email protected] | 7b96328 | 2011-10-03 06:00:12 | [diff] [blame] | 41 | void BrowsingDataQuotaHelperImpl::RevokeHostQuota(const std::string& host) { |
| 42 | if (!io_thread_->BelongsToCurrentThread()) { |
| 43 | io_thread_->PostTask( |
| 44 | FROM_HERE, |
[email protected] | 006284f0 | 2011-10-19 22:06:23 | [diff] [blame] | 45 | base::Bind(&BrowsingDataQuotaHelperImpl::RevokeHostQuota, this, host)); |
[email protected] | 7b96328 | 2011-10-03 06:00:12 | [diff] [blame] | 46 | return; |
| 47 | } |
| 48 | |
| 49 | quota_manager_->SetPersistentHostQuota( |
[email protected] | 4d99be5 | 2011-10-18 14:11:03 | [diff] [blame] | 50 | host, 0, |
| 51 | base::Bind(&BrowsingDataQuotaHelperImpl::DidRevokeHostQuota, |
| 52 | weak_factory_.GetWeakPtr())); |
[email protected] | 7b96328 | 2011-10-03 06:00:12 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | BrowsingDataQuotaHelperImpl::BrowsingDataQuotaHelperImpl( |
| 56 | base::MessageLoopProxy* ui_thread, |
| 57 | base::MessageLoopProxy* io_thread, |
| 58 | quota::QuotaManager* quota_manager) |
| 59 | : BrowsingDataQuotaHelper(io_thread), |
| 60 | quota_manager_(quota_manager), |
| 61 | is_fetching_(false), |
| 62 | ui_thread_(ui_thread), |
| 63 | io_thread_(io_thread), |
[email protected] | 4d99be5 | 2011-10-18 14:11:03 | [diff] [blame] | 64 | weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
[email protected] | 7b96328 | 2011-10-03 06:00:12 | [diff] [blame] | 65 | DCHECK(quota_manager); |
| 66 | } |
| 67 | |
| 68 | BrowsingDataQuotaHelperImpl::~BrowsingDataQuotaHelperImpl() {} |
| 69 | |
[email protected] | a9e3ec4 | 2011-08-09 06:48:30 | [diff] [blame] | 70 | void BrowsingDataQuotaHelperImpl::FetchQuotaInfo() { |
| 71 | if (!io_thread_->BelongsToCurrentThread()) { |
| 72 | io_thread_->PostTask( |
| 73 | FROM_HERE, |
[email protected] | 006284f0 | 2011-10-19 22:06:23 | [diff] [blame] | 74 | base::Bind(&BrowsingDataQuotaHelperImpl::FetchQuotaInfo, this)); |
[email protected] | a9e3ec4 | 2011-08-09 06:48:30 | [diff] [blame] | 75 | return; |
| 76 | } |
| 77 | |
| 78 | quota_manager_->GetOriginsModifiedSince( |
| 79 | quota::kStorageTypeTemporary, |
| 80 | base::Time(), |
[email protected] | 4d99be5 | 2011-10-18 14:11:03 | [diff] [blame] | 81 | base::Bind(&BrowsingDataQuotaHelperImpl::GotOrigins, |
| 82 | weak_factory_.GetWeakPtr())); |
[email protected] | a9e3ec4 | 2011-08-09 06:48:30 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | void BrowsingDataQuotaHelperImpl::GotOrigins( |
| 86 | const std::set<GURL>& origins, quota::StorageType type) { |
| 87 | for (std::set<GURL>::const_iterator itr = origins.begin(); |
| 88 | itr != origins.end(); |
| 89 | ++itr) |
| 90 | pending_hosts_.insert(std::make_pair(itr->host(), type)); |
| 91 | |
| 92 | DCHECK(type == quota::kStorageTypeTemporary || |
| 93 | type == quota::kStorageTypePersistent); |
| 94 | |
| 95 | if (type == quota::kStorageTypeTemporary) { |
| 96 | quota_manager_->GetOriginsModifiedSince( |
| 97 | quota::kStorageTypePersistent, |
| 98 | base::Time(), |
[email protected] | 4d99be5 | 2011-10-18 14:11:03 | [diff] [blame] | 99 | base::Bind(&BrowsingDataQuotaHelperImpl::GotOrigins, |
| 100 | weak_factory_.GetWeakPtr())); |
[email protected] | a9e3ec4 | 2011-08-09 06:48:30 | [diff] [blame] | 101 | } else { |
| 102 | // type == quota::kStorageTypePersistent |
| 103 | ProcessPendingHosts(); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | void BrowsingDataQuotaHelperImpl::ProcessPendingHosts() { |
| 108 | if (pending_hosts_.empty()) { |
| 109 | OnComplete(); |
| 110 | return; |
| 111 | } |
| 112 | |
| 113 | PendingHosts::iterator itr = pending_hosts_.begin(); |
| 114 | std::string host = itr->first; |
| 115 | quota::StorageType type = itr->second; |
| 116 | pending_hosts_.erase(itr); |
| 117 | GetHostUsage(host, type); |
| 118 | } |
| 119 | |
| 120 | void BrowsingDataQuotaHelperImpl::GetHostUsage(const std::string& host, |
| 121 | quota::StorageType type) { |
| 122 | DCHECK(quota_manager_.get()); |
| 123 | quota_manager_->GetHostUsage( |
| 124 | host, type, |
[email protected] | 4d99be5 | 2011-10-18 14:11:03 | [diff] [blame] | 125 | base::Bind(&BrowsingDataQuotaHelperImpl::GotHostUsage, |
| 126 | weak_factory_.GetWeakPtr())); |
[email protected] | a9e3ec4 | 2011-08-09 06:48:30 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | void BrowsingDataQuotaHelperImpl::GotHostUsage(const std::string& host, |
| 130 | quota::StorageType type, |
| 131 | int64 usage) { |
| 132 | switch (type) { |
| 133 | case quota::kStorageTypeTemporary: |
| 134 | quota_info_[host].temporary_usage = usage; |
| 135 | break; |
| 136 | case quota::kStorageTypePersistent: |
| 137 | quota_info_[host].persistent_usage = usage; |
| 138 | break; |
| 139 | default: |
| 140 | NOTREACHED(); |
| 141 | } |
| 142 | ProcessPendingHosts(); |
| 143 | } |
| 144 | |
| 145 | void BrowsingDataQuotaHelperImpl::OnComplete() { |
| 146 | // Check if CancelNotification was called |
[email protected] | 91b06c0 | 2011-11-10 06:17:13 | [diff] [blame] | 147 | if (callback_.is_null()) |
[email protected] | a9e3ec4 | 2011-08-09 06:48:30 | [diff] [blame] | 148 | return; |
| 149 | |
| 150 | if (!ui_thread_->BelongsToCurrentThread()) { |
| 151 | ui_thread_->PostTask( |
| 152 | FROM_HERE, |
[email protected] | 006284f0 | 2011-10-19 22:06:23 | [diff] [blame] | 153 | base::Bind(&BrowsingDataQuotaHelperImpl::OnComplete, this)); |
[email protected] | a9e3ec4 | 2011-08-09 06:48:30 | [diff] [blame] | 154 | return; |
| 155 | } |
| 156 | |
| 157 | is_fetching_ = false; |
| 158 | |
| 159 | QuotaInfoArray result; |
[email protected] | a9e3ec4 | 2011-08-09 06:48:30 | [diff] [blame] | 160 | |
| 161 | for (std::map<std::string, QuotaInfo>::iterator itr = quota_info_.begin(); |
| 162 | itr != quota_info_.end(); |
| 163 | ++itr) { |
| 164 | QuotaInfo* info = &itr->second; |
| 165 | // Skip unused entries |
| 166 | if (info->temporary_usage <= 0 && |
| 167 | info->persistent_usage <= 0) |
| 168 | continue; |
| 169 | |
| 170 | info->host = itr->first; |
| 171 | result.push_back(*info); |
| 172 | } |
| 173 | |
[email protected] | 91b06c0 | 2011-11-10 06:17:13 | [diff] [blame] | 174 | callback_.Run(result); |
| 175 | callback_.Reset(); |
[email protected] | a9e3ec4 | 2011-08-09 06:48:30 | [diff] [blame] | 176 | } |
[email protected] | 7b96328 | 2011-10-03 06:00:12 | [diff] [blame] | 177 | |
| 178 | void BrowsingDataQuotaHelperImpl::DidRevokeHostQuota( |
| 179 | quota::QuotaStatusCode status_unused, |
| 180 | const std::string& host_unused, |
| 181 | quota::StorageType type_unused, |
| 182 | int64 quota_unused) { |
| 183 | } |