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
|
// Copyright 2016 The Chromium Authors
// 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 <utility>
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/task/thread_pool.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/pref_names.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/storage_usage_info.h"
#include "content/public/common/content_constants.h"
namespace {
int64_t GetFileSizeBlocking(const base::FilePath& file_path) {
return base::GetFileSize(file_path).value_or(-1);
}
} // namespace
SiteDataSizeCollector::SiteDataSizeCollector(
const base::FilePath& default_storage_partition_path,
scoped_refptr<browsing_data::CookieHelper> cookie_helper,
scoped_refptr<browsing_data::LocalStorageHelper> local_storage_helper,
scoped_refptr<BrowsingDataQuotaHelper> quota_helper)
: default_storage_partition_path_(default_storage_partition_path),
cookie_helper_(std::move(cookie_helper)),
local_storage_helper_(std::move(local_storage_helper)),
quota_helper_(std::move(quota_helper)),
in_flight_operations_(0),
total_bytes_(0) {}
SiteDataSizeCollector::~SiteDataSizeCollector() = default;
void SiteDataSizeCollector::Fetch(FetchCallback callback) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
DCHECK(!callback.is_null());
fetch_callback_ = std::move(callback);
total_bytes_ = 0;
in_flight_operations_ = 0;
if (cookie_helper_.get()) {
cookie_helper_->StartFetching(
base::BindOnce(&SiteDataSizeCollector::OnCookiesModelInfoLoaded,
weak_ptr_factory_.GetWeakPtr()));
in_flight_operations_++;
}
if (local_storage_helper_.get()) {
local_storage_helper_->StartFetching(
base::BindOnce(&SiteDataSizeCollector::OnLocalStorageModelInfoLoaded,
weak_ptr_factory_.GetWeakPtr()));
in_flight_operations_++;
}
if (quota_helper_.get()) {
quota_helper_->StartFetching(
base::BindOnce(&SiteDataSizeCollector::OnQuotaModelInfoLoaded,
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::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::ThreadPool::PostTaskAndReplyWithResult(
FROM_HERE, {base::MayBlock(), base::TaskPriority::BEST_EFFORT},
base::BindOnce(&GetFileSizeBlocking, cookie_file_path),
base::BindOnce(&SiteDataSizeCollector::OnStorageSizeFetched,
weak_ptr_factory_.GetWeakPtr()));
}
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.total_size_bytes;
OnStorageSizeFetched(total_size);
}
void SiteDataSizeCollector::OnQuotaModelInfoLoaded(
const QuotaStorageUsageInfoList& quota_storage_info_list) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
int64_t total_size = 0;
for (const auto& quota_info : quota_storage_info_list) {
total_size += quota_info.usage;
}
OnStorageSizeFetched(total_size);
}
void SiteDataSizeCollector::OnStorageSizeFetched(int64_t size) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
if (size > 0)
total_bytes_ += size;
if (--in_flight_operations_ == 0)
std::move(fetch_callback_).Run(total_bytes_);
}
|