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