File: site_data_size_collector.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (112 lines) | stat: -rw-r--r-- 4,021 bytes parent folder | download | duplicates (5)
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_);
}