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::GotTemporaryStorageOrigins)); |
| 67 } |
| 68 |
| 69 void BrowsingDataQuotaHelperImpl::GotTemporaryStorageOrigins( |
| 70 const std::set<GURL>& origins) { |
| 71 GotOrigins(quota::kStorageTypeTemporary, origins); |
| 72 |
| 73 quota_manager_->GetOriginsModifiedSince( |
| 74 quota::kStorageTypePersistent, |
| 75 base::Time(), |
| 76 callback_factory_.NewCallback( |
| 77 &BrowsingDataQuotaHelperImpl::GotPersistentStorageOrigins)); |
| 78 } |
| 79 |
| 80 void BrowsingDataQuotaHelperImpl::GotPersistentStorageOrigins( |
| 81 const std::set<GURL>& origins) { |
| 82 GotOrigins(quota::kStorageTypePersistent, origins); |
| 83 |
| 84 ProcessPendingHosts(); |
| 85 } |
| 86 |
| 87 void BrowsingDataQuotaHelperImpl::GotOrigins( |
| 88 quota::StorageType type, const std::set<GURL>& origins) { |
| 89 for (std::set<GURL>::const_iterator itr = origins.begin(); |
| 90 itr != origins.end(); |
| 91 ++itr) |
| 92 pending_hosts_.insert(std::make_pair(itr->host(), type)); |
| 93 } |
| 94 |
| 95 void BrowsingDataQuotaHelperImpl::ProcessPendingHosts() { |
| 96 if (pending_hosts_.empty()) { |
| 97 OnComplete(); |
| 98 return; |
| 99 } |
| 100 |
| 101 PendingHosts::iterator itr = pending_hosts_.begin(); |
| 102 std::string host = itr->first; |
| 103 quota::StorageType type = itr->second; |
| 104 pending_hosts_.erase(itr); |
| 105 GetHostUsage(host, type); |
| 106 } |
| 107 |
| 108 void BrowsingDataQuotaHelperImpl::GetHostUsage(const std::string& host, |
| 109 quota::StorageType type) { |
| 110 DCHECK(quota_manager_.get()); |
| 111 quota_manager_->GetHostUsage( |
| 112 host, type, |
| 113 callback_factory_.NewCallback( |
| 114 &BrowsingDataQuotaHelperImpl::GotHostUsage)); |
| 115 } |
| 116 |
| 117 void BrowsingDataQuotaHelperImpl::GotHostUsage(const std::string& host, |
| 118 quota::StorageType type, |
| 119 int64 usage) { |
| 120 switch (type) { |
| 121 case quota::kStorageTypeTemporary: |
| 122 quota_info_[host].temporary_usage = usage; |
| 123 break; |
| 124 case quota::kStorageTypePersistent: |
| 125 quota_info_[host].persistent_usage = usage; |
| 126 break; |
| 127 default: |
| 128 NOTREACHED(); |
| 129 } |
| 130 ProcessPendingHosts(); |
| 131 } |
| 132 |
| 133 void BrowsingDataQuotaHelperImpl::OnComplete() { |
| 134 // Check if CancelNotification was called |
| 135 if (!callback_.get()) |
| 136 return; |
| 137 |
| 138 if (!ui_thread_->BelongsToCurrentThread()) { |
| 139 ui_thread_->PostTask( |
| 140 FROM_HERE, |
| 141 NewRunnableMethod( |
| 142 this, |
| 143 &BrowsingDataQuotaHelperImpl::OnComplete)); |
| 144 return; |
| 145 } |
| 146 |
| 147 is_fetching_ = false; |
| 148 |
| 149 QuotaInfoList result; |
| 150 result.reserve(quota_info_.size()); |
| 151 |
| 152 for (std::map<std::string, QuotaInfo>::iterator itr = quota_info_.begin(); |
| 153 itr != quota_info_.end(); |
| 154 ++itr) { |
| 155 QuotaInfo* info = &itr->second; |
| 156 // Skip unused entries |
| 157 if (info->temporary_usage <= 0 && |
| 158 info->persistent_usage <= 0) |
| 159 continue; |
| 160 |
| 161 info->host = itr->first; |
| 162 result.push_back(*info); |
| 163 } |
| 164 |
| 165 callback_->Run(result); |
| 166 callback_.reset(); |
| 167 } |
OLD | NEW |