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 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
|
// Copyright 2016 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/site_data_size_collector.h"
#include "base/files/file_util.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/pref_names.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/common/content_constants.h"
namespace {
int64_t GetFileSizeOnBlockingPool(const base::FilePath& file_path) {
int64_t size = 0;
bool success = base::GetFileSize(file_path, &size);
return success ? size : -1;
}
} // namespace
SiteDataSizeCollector::SiteDataSizeCollector(
const base::FilePath& default_storage_partition_path,
BrowsingDataCookieHelper* cookie_helper,
BrowsingDataDatabaseHelper* database_helper,
BrowsingDataLocalStorageHelper* local_storage_helper,
BrowsingDataAppCacheHelper* appcache_helper,
BrowsingDataIndexedDBHelper* indexed_db_helper,
BrowsingDataFileSystemHelper* file_system_helper,
BrowsingDataChannelIDHelper* channel_id_helper,
BrowsingDataServiceWorkerHelper* service_worker_helper,
BrowsingDataCacheStorageHelper* cache_storage_helper,
BrowsingDataFlashLSOHelper* flash_lso_helper)
: default_storage_partition_path_(default_storage_partition_path),
appcache_helper_(appcache_helper),
cookie_helper_(cookie_helper),
database_helper_(database_helper),
local_storage_helper_(local_storage_helper),
indexed_db_helper_(indexed_db_helper),
file_system_helper_(file_system_helper),
channel_id_helper_(channel_id_helper),
service_worker_helper_(service_worker_helper),
cache_storage_helper_(cache_storage_helper),
flash_lso_helper_(flash_lso_helper),
in_flight_operations_(0),
total_bytes_(0),
weak_ptr_factory_(this) {}
SiteDataSizeCollector::~SiteDataSizeCollector() {
}
void SiteDataSizeCollector::Fetch(const FetchCallback& callback) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
DCHECK(!callback.is_null());
fetch_callback_ = callback;
total_bytes_ = 0;
in_flight_operations_ = 0;
if (appcache_helper_.get()) {
appcache_helper_->StartFetching(
base::Bind(&SiteDataSizeCollector::OnAppCacheModelInfoLoaded,
weak_ptr_factory_.GetWeakPtr()));
in_flight_operations_++;
}
if (cookie_helper_.get()) {
cookie_helper_->StartFetching(
base::Bind(&SiteDataSizeCollector::OnCookiesModelInfoLoaded,
weak_ptr_factory_.GetWeakPtr()));
in_flight_operations_++;
}
if (database_helper_.get()) {
database_helper_->StartFetching(
base::Bind(&SiteDataSizeCollector::OnDatabaseModelInfoLoaded,
weak_ptr_factory_.GetWeakPtr()));
in_flight_operations_++;
}
if (local_storage_helper_.get()) {
local_storage_helper_->StartFetching(
base::Bind(&SiteDataSizeCollector::OnLocalStorageModelInfoLoaded,
weak_ptr_factory_.GetWeakPtr()));
in_flight_operations_++;
}
if (indexed_db_helper_.get()) {
indexed_db_helper_->StartFetching(
base::Bind(&SiteDataSizeCollector::OnIndexedDBModelInfoLoaded,
weak_ptr_factory_.GetWeakPtr()));
in_flight_operations_++;
}
if (file_system_helper_.get()) {
file_system_helper_->StartFetching(
base::Bind(&SiteDataSizeCollector::OnFileSystemModelInfoLoaded,
weak_ptr_factory_.GetWeakPtr()));
in_flight_operations_++;
}
if (channel_id_helper_.get()) {
channel_id_helper_->StartFetching(
base::Bind(&SiteDataSizeCollector::OnChannelIDModelInfoLoaded,
weak_ptr_factory_.GetWeakPtr()));
in_flight_operations_++;
}
if (service_worker_helper_.get()) {
service_worker_helper_->StartFetching(
base::Bind(&SiteDataSizeCollector::OnServiceWorkerModelInfoLoaded,
weak_ptr_factory_.GetWeakPtr()));
in_flight_operations_++;
}
if (cache_storage_helper_.get()) {
cache_storage_helper_->StartFetching(
base::Bind(&SiteDataSizeCollector::OnCacheStorageModelInfoLoaded,
weak_ptr_factory_.GetWeakPtr()));
in_flight_operations_++;
}
if (flash_lso_helper_.get()) {
flash_lso_helper_->StartFetching(
base::Bind(&SiteDataSizeCollector::OnFlashLSOInfoLoaded,
weak_ptr_factory_.GetWeakPtr()));
in_flight_operations_++;
}
// TODO(fukino): SITE_USAGE_DATA and WEB_APP_DATA should be counted too.
// All data types included in REMOVE_SITE_USAGE_DATA should be counted.
}
void SiteDataSizeCollector::OnAppCacheModelInfoLoaded(
scoped_refptr<content::AppCacheInfoCollection> appcache_info) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
int64_t total_size = 0;
if (appcache_info.get()) {
for (const auto& origin : appcache_info->infos_by_origin) {
for (const auto& info : origin.second)
total_size += info.size;
}
}
OnStorageSizeFetched(total_size);
}
void SiteDataSizeCollector::OnCookiesModelInfoLoaded(
const net::CookieList& cookie_list) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
if (cookie_list.empty()) {
OnStorageSizeFetched(0);
return;
}
base::FilePath cookie_file_path = default_storage_partition_path_
.Append(chrome::kCookieFilename);
base::PostTaskAndReplyWithResult(
content::BrowserThread::GetBlockingPool(),
FROM_HERE,
base::Bind(&GetFileSizeOnBlockingPool, cookie_file_path),
base::Bind(&SiteDataSizeCollector::OnStorageSizeFetched,
weak_ptr_factory_.GetWeakPtr()));
}
void SiteDataSizeCollector::OnDatabaseModelInfoLoaded(
const DatabaseInfoList& database_info_list) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
int64_t total_size = 0;
for (const auto& database_info : database_info_list)
total_size += database_info.size;
OnStorageSizeFetched(total_size);
}
void SiteDataSizeCollector::OnLocalStorageModelInfoLoaded(
const LocalStorageInfoList& local_storage_info_list) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
int64_t total_size = 0;
for (const auto& local_storage_info : local_storage_info_list)
total_size += local_storage_info.size;
OnStorageSizeFetched(total_size);
}
void SiteDataSizeCollector::OnIndexedDBModelInfoLoaded(
const std::list<content::IndexedDBInfo>& indexed_db_info_list) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
int64_t total_size = 0;
for (const auto& indexed_db_info : indexed_db_info_list)
total_size += indexed_db_info.size;
OnStorageSizeFetched(total_size);
}
void SiteDataSizeCollector::OnFileSystemModelInfoLoaded(
const FileSystemInfoList& file_system_info_list) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
int64_t total_size = 0;
for (const auto& file_system_info : file_system_info_list) {
for (const auto& usage : file_system_info.usage_map)
total_size += usage.second;
}
OnStorageSizeFetched(total_size);
}
void SiteDataSizeCollector::OnChannelIDModelInfoLoaded(
const ChannelIDList& channel_id_list) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
if (channel_id_list.empty()) {
OnStorageSizeFetched(0);
return;
}
base::FilePath channel_id_file_path = default_storage_partition_path_
.Append(chrome::kChannelIDFilename);
base::PostTaskAndReplyWithResult(
content::BrowserThread::GetBlockingPool(),
FROM_HERE,
base::Bind(&GetFileSizeOnBlockingPool, channel_id_file_path),
base::Bind(&SiteDataSizeCollector::OnStorageSizeFetched,
weak_ptr_factory_.GetWeakPtr()));
}
void SiteDataSizeCollector::OnServiceWorkerModelInfoLoaded(
const ServiceWorkerUsageInfoList& service_worker_info_list) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
int64_t total_size = 0;
for (const auto& service_worker_info : service_worker_info_list)
total_size += service_worker_info.total_size_bytes;
OnStorageSizeFetched(total_size);
}
void SiteDataSizeCollector::OnCacheStorageModelInfoLoaded(
const CacheStorageUsageInfoList& cache_storage_info_list) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
int64_t total_size = 0;
for (const auto& cache_storage_info : cache_storage_info_list)
total_size += cache_storage_info.total_size_bytes;
OnStorageSizeFetched(total_size);
}
void SiteDataSizeCollector::OnFlashLSOInfoLoaded(
const FlashLSODomainList& domains) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
// TODO(fukino): Flash is not the only plugin. We should check all types of
// plugin data.
if (domains.empty()) {
OnStorageSizeFetched(0);
return;
}
base::FilePath pepper_data_dir_path = default_storage_partition_path_
.Append(content::kPepperDataDirname);
base::PostTaskAndReplyWithResult(
content::BrowserThread::GetBlockingPool(),
FROM_HERE,
base::Bind(&base::ComputeDirectorySize, pepper_data_dir_path),
base::Bind(&SiteDataSizeCollector::OnStorageSizeFetched,
weak_ptr_factory_.GetWeakPtr()));
}
void SiteDataSizeCollector::OnStorageSizeFetched(int64_t size) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
if (size > 0)
total_bytes_ += size;
if (--in_flight_operations_ == 0)
fetch_callback_.Run(total_bytes_);
}
|