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