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