OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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/site_data_size_collector.h" |
| 6 |
| 7 #include "base/files/file_util.h" |
| 8 #include "chrome/common/chrome_constants.h" |
| 9 #include "chrome/common/pref_names.h" |
| 10 #include "content/public/browser/browser_thread.h" |
| 11 #include "content/public/common/content_constants.h" |
| 12 |
| 13 namespace { |
| 14 |
| 15 int64_t GetFileSizeOnBlockingPool(const base::FilePath& file_path) { |
| 16 int64_t size = 0; |
| 17 bool success = base::GetFileSize(file_path, &size); |
| 18 return success ? size : -1; |
| 19 } |
| 20 |
| 21 } // namespace |
| 22 |
| 23 SiteDataSizeCollector::SiteDataSizeCollector( |
| 24 const base::FilePath& default_storage_partition_path, |
| 25 BrowsingDataCookieHelper* cookie_helper, |
| 26 BrowsingDataDatabaseHelper* database_helper, |
| 27 BrowsingDataLocalStorageHelper* local_storage_helper, |
| 28 BrowsingDataAppCacheHelper* appcache_helper, |
| 29 BrowsingDataIndexedDBHelper* indexed_db_helper, |
| 30 BrowsingDataFileSystemHelper* file_system_helper, |
| 31 BrowsingDataChannelIDHelper* channel_id_helper, |
| 32 BrowsingDataServiceWorkerHelper* service_worker_helper, |
| 33 BrowsingDataCacheStorageHelper* cache_storage_helper, |
| 34 BrowsingDataFlashLSOHelper* flash_lso_helper) |
| 35 : default_storage_partition_path_(default_storage_partition_path), |
| 36 appcache_helper_(appcache_helper), |
| 37 cookie_helper_(cookie_helper), |
| 38 database_helper_(database_helper), |
| 39 local_storage_helper_(local_storage_helper), |
| 40 indexed_db_helper_(indexed_db_helper), |
| 41 file_system_helper_(file_system_helper), |
| 42 channel_id_helper_(channel_id_helper), |
| 43 service_worker_helper_(service_worker_helper), |
| 44 cache_storage_helper_(cache_storage_helper), |
| 45 flash_lso_helper_(flash_lso_helper), |
| 46 in_flight_operations_(0), |
| 47 total_bytes_(0), |
| 48 weak_ptr_factory_(this) {} |
| 49 |
| 50 SiteDataSizeCollector::~SiteDataSizeCollector() { |
| 51 } |
| 52 |
| 53 void SiteDataSizeCollector::Fetch(const FetchCallback& callback) { |
| 54 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 55 DCHECK(!callback.is_null()); |
| 56 |
| 57 fetch_callback_ = callback; |
| 58 total_bytes_ = 0; |
| 59 in_flight_operations_ = 0; |
| 60 |
| 61 if (appcache_helper_.get()) { |
| 62 appcache_helper_->StartFetching( |
| 63 base::Bind(&SiteDataSizeCollector::OnAppCacheModelInfoLoaded, |
| 64 weak_ptr_factory_.GetWeakPtr())); |
| 65 in_flight_operations_++; |
| 66 } |
| 67 if (cookie_helper_.get()) { |
| 68 cookie_helper_->StartFetching( |
| 69 base::Bind(&SiteDataSizeCollector::OnCookiesModelInfoLoaded, |
| 70 weak_ptr_factory_.GetWeakPtr())); |
| 71 in_flight_operations_++; |
| 72 } |
| 73 if (database_helper_.get()) { |
| 74 database_helper_->StartFetching( |
| 75 base::Bind(&SiteDataSizeCollector::OnDatabaseModelInfoLoaded, |
| 76 weak_ptr_factory_.GetWeakPtr())); |
| 77 in_flight_operations_++; |
| 78 } |
| 79 if (local_storage_helper_.get()) { |
| 80 local_storage_helper_->StartFetching( |
| 81 base::Bind(&SiteDataSizeCollector::OnLocalStorageModelInfoLoaded, |
| 82 weak_ptr_factory_.GetWeakPtr())); |
| 83 in_flight_operations_++; |
| 84 } |
| 85 if (indexed_db_helper_.get()) { |
| 86 indexed_db_helper_->StartFetching( |
| 87 base::Bind(&SiteDataSizeCollector::OnIndexedDBModelInfoLoaded, |
| 88 weak_ptr_factory_.GetWeakPtr())); |
| 89 in_flight_operations_++; |
| 90 } |
| 91 if (file_system_helper_.get()) { |
| 92 file_system_helper_->StartFetching( |
| 93 base::Bind(&SiteDataSizeCollector::OnFileSystemModelInfoLoaded, |
| 94 weak_ptr_factory_.GetWeakPtr())); |
| 95 in_flight_operations_++; |
| 96 } |
| 97 if (channel_id_helper_.get()) { |
| 98 channel_id_helper_->StartFetching( |
| 99 base::Bind(&SiteDataSizeCollector::OnChannelIDModelInfoLoaded, |
| 100 weak_ptr_factory_.GetWeakPtr())); |
| 101 in_flight_operations_++; |
| 102 } |
| 103 if (service_worker_helper_.get()) { |
| 104 service_worker_helper_->StartFetching( |
| 105 base::Bind(&SiteDataSizeCollector::OnServiceWorkerModelInfoLoaded, |
| 106 weak_ptr_factory_.GetWeakPtr())); |
| 107 in_flight_operations_++; |
| 108 } |
| 109 if (cache_storage_helper_.get()) { |
| 110 cache_storage_helper_->StartFetching( |
| 111 base::Bind(&SiteDataSizeCollector::OnCacheStorageModelInfoLoaded, |
| 112 weak_ptr_factory_.GetWeakPtr())); |
| 113 in_flight_operations_++; |
| 114 } |
| 115 if (flash_lso_helper_.get()) { |
| 116 flash_lso_helper_->StartFetching( |
| 117 base::Bind(&SiteDataSizeCollector::OnFlashLSOInfoLoaded, |
| 118 weak_ptr_factory_.GetWeakPtr())); |
| 119 in_flight_operations_++; |
| 120 } |
| 121 // TODO(fukino): SITE_USAGE_DATA and WEB_APP_DATA should be counted too. |
| 122 // All data types included in REMOVE_SITE_USAGE_DATA should be counted. |
| 123 } |
| 124 |
| 125 void SiteDataSizeCollector::OnAppCacheModelInfoLoaded( |
| 126 scoped_refptr<content::AppCacheInfoCollection> appcache_info) { |
| 127 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 128 int64_t total_size = 0; |
| 129 if (appcache_info.get()) { |
| 130 for (const auto& origin : appcache_info->infos_by_origin) { |
| 131 for (const auto& info : origin.second) |
| 132 total_size += info.size; |
| 133 } |
| 134 } |
| 135 OnStorageSizeFetched(total_size); |
| 136 } |
| 137 |
| 138 void SiteDataSizeCollector::OnCookiesModelInfoLoaded( |
| 139 const net::CookieList& cookie_list) { |
| 140 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 141 |
| 142 if (cookie_list.empty()) { |
| 143 OnStorageSizeFetched(0); |
| 144 return; |
| 145 } |
| 146 base::FilePath cookie_file_path = default_storage_partition_path_ |
| 147 .Append(chrome::kCookieFilename); |
| 148 base::PostTaskAndReplyWithResult( |
| 149 content::BrowserThread::GetBlockingPool(), |
| 150 FROM_HERE, |
| 151 base::Bind(&GetFileSizeOnBlockingPool, cookie_file_path), |
| 152 base::Bind(&SiteDataSizeCollector::OnStorageSizeFetched, |
| 153 weak_ptr_factory_.GetWeakPtr())); |
| 154 } |
| 155 |
| 156 void SiteDataSizeCollector::OnDatabaseModelInfoLoaded( |
| 157 const DatabaseInfoList& database_info_list) { |
| 158 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 159 int64_t total_size = 0; |
| 160 for (const auto& database_info : database_info_list) |
| 161 total_size += database_info.size; |
| 162 OnStorageSizeFetched(total_size); |
| 163 } |
| 164 |
| 165 void SiteDataSizeCollector::OnLocalStorageModelInfoLoaded( |
| 166 const LocalStorageInfoList& local_storage_info_list) { |
| 167 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 168 int64_t total_size = 0; |
| 169 for (const auto& local_storage_info : local_storage_info_list) |
| 170 total_size += local_storage_info.size; |
| 171 OnStorageSizeFetched(total_size); |
| 172 } |
| 173 |
| 174 void SiteDataSizeCollector::OnIndexedDBModelInfoLoaded( |
| 175 const std::list<content::IndexedDBInfo>& indexed_db_info_list) { |
| 176 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 177 int64_t total_size = 0; |
| 178 for (const auto& indexed_db_info : indexed_db_info_list) |
| 179 total_size += indexed_db_info.size; |
| 180 OnStorageSizeFetched(total_size); |
| 181 } |
| 182 |
| 183 void SiteDataSizeCollector::OnFileSystemModelInfoLoaded( |
| 184 const FileSystemInfoList& file_system_info_list) { |
| 185 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 186 int64_t total_size = 0; |
| 187 for (const auto& file_system_info : file_system_info_list) { |
| 188 for (const auto& usage : file_system_info.usage_map) |
| 189 total_size += usage.second; |
| 190 } |
| 191 OnStorageSizeFetched(total_size); |
| 192 } |
| 193 |
| 194 void SiteDataSizeCollector::OnChannelIDModelInfoLoaded( |
| 195 const ChannelIDList& channel_id_list) { |
| 196 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 197 |
| 198 if (channel_id_list.empty()) { |
| 199 OnStorageSizeFetched(0); |
| 200 return; |
| 201 } |
| 202 base::FilePath channel_id_file_path = default_storage_partition_path_ |
| 203 .Append(chrome::kChannelIDFilename); |
| 204 base::PostTaskAndReplyWithResult( |
| 205 content::BrowserThread::GetBlockingPool(), |
| 206 FROM_HERE, |
| 207 base::Bind(&GetFileSizeOnBlockingPool, channel_id_file_path), |
| 208 base::Bind(&SiteDataSizeCollector::OnStorageSizeFetched, |
| 209 weak_ptr_factory_.GetWeakPtr())); |
| 210 } |
| 211 |
| 212 void SiteDataSizeCollector::OnServiceWorkerModelInfoLoaded( |
| 213 const ServiceWorkerUsageInfoList& service_worker_info_list) { |
| 214 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 215 int64_t total_size = 0; |
| 216 for (const auto& service_worker_info : service_worker_info_list) |
| 217 total_size += service_worker_info.total_size_bytes; |
| 218 OnStorageSizeFetched(total_size); |
| 219 } |
| 220 |
| 221 void SiteDataSizeCollector::OnCacheStorageModelInfoLoaded( |
| 222 const CacheStorageUsageInfoList& cache_storage_info_list) { |
| 223 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 224 int64_t total_size = 0; |
| 225 for (const auto& cache_storage_info : cache_storage_info_list) |
| 226 total_size += cache_storage_info.total_size_bytes; |
| 227 OnStorageSizeFetched(total_size); |
| 228 } |
| 229 |
| 230 void SiteDataSizeCollector::OnFlashLSOInfoLoaded( |
| 231 const FlashLSODomainList& domains) { |
| 232 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 233 |
| 234 // TODO(fukino): Flash is not the only plugin. We should check all types of |
| 235 // plugin data. |
| 236 if (domains.empty()) { |
| 237 OnStorageSizeFetched(0); |
| 238 return; |
| 239 } |
| 240 base::FilePath pepper_data_dir_path = default_storage_partition_path_ |
| 241 .Append(content::kPepperDataDirname); |
| 242 base::PostTaskAndReplyWithResult( |
| 243 content::BrowserThread::GetBlockingPool(), |
| 244 FROM_HERE, |
| 245 base::Bind(&base::ComputeDirectorySize, pepper_data_dir_path), |
| 246 base::Bind(&SiteDataSizeCollector::OnStorageSizeFetched, |
| 247 weak_ptr_factory_.GetWeakPtr())); |
| 248 } |
| 249 |
| 250 void SiteDataSizeCollector::OnStorageSizeFetched(int64_t size) { |
| 251 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 252 if (size > 0) |
| 253 total_bytes_ += size; |
| 254 if (--in_flight_operations_ == 0) |
| 255 fetch_callback_.Run(total_bytes_); |
| 256 } |
OLD | NEW |