| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/webui/options/chromeos/storage_manager_handler.h" | 5 #include "chrome/browser/ui/webui/options/chromeos/storage_manager_handler.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <numeric> | 8 #include <numeric> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
| 12 #include "base/sys_info.h" | 12 #include "base/sys_info.h" |
| 13 #include "chrome/browser/browser_process.h" | 13 #include "chrome/browser/browser_process.h" |
| 14 #include "chrome/browser/browsing_data/browsing_data_appcache_helper.h" |
| 15 #include "chrome/browser/browsing_data/browsing_data_cache_storage_helper.h" |
| 16 #include "chrome/browser/browsing_data/browsing_data_channel_id_helper.h" |
| 17 #include "chrome/browser/browsing_data/browsing_data_cookie_helper.h" |
| 18 #include "chrome/browser/browsing_data/browsing_data_database_helper.h" |
| 19 #include "chrome/browser/browsing_data/browsing_data_file_system_helper.h" |
| 20 #include "chrome/browser/browsing_data/browsing_data_flash_lso_helper.h" |
| 21 #include "chrome/browser/browsing_data/browsing_data_indexed_db_helper.h" |
| 22 #include "chrome/browser/browsing_data/browsing_data_local_storage_helper.h" |
| 23 #include "chrome/browser/browsing_data/browsing_data_service_worker_helper.h" |
| 14 #include "chrome/browser/chromeos/arc/arc_auth_service.h" | 24 #include "chrome/browser/chromeos/arc/arc_auth_service.h" |
| 15 #include "chrome/browser/chromeos/drive/file_system_util.h" | 25 #include "chrome/browser/chromeos/drive/file_system_util.h" |
| 16 #include "chrome/browser/chromeos/file_manager/path_util.h" | 26 #include "chrome/browser/chromeos/file_manager/path_util.h" |
| 17 #include "chrome/browser/platform_util.h" | 27 #include "chrome/browser/platform_util.h" |
| 18 #include "chrome/browser/profiles/profile.h" | 28 #include "chrome/browser/profiles/profile.h" |
| 19 #include "chrome/grit/generated_resources.h" | 29 #include "chrome/grit/generated_resources.h" |
| 20 #include "chromeos/cryptohome/homedir_methods.h" | 30 #include "chromeos/cryptohome/homedir_methods.h" |
| 21 #include "components/browsing_data/storage_partition_http_cache_data_remover.h" | 31 #include "components/browsing_data/storage_partition_http_cache_data_remover.h" |
| 22 #include "components/drive/chromeos/file_system_interface.h" | 32 #include "components/drive/chromeos/file_system_interface.h" |
| 23 #include "components/user_manager/user_manager.h" | 33 #include "components/user_manager/user_manager.h" |
| 24 #include "content/public/browser/browser_context.h" | 34 #include "content/public/browser/browser_context.h" |
| 25 #include "content/public/browser/browser_thread.h" | 35 #include "content/public/browser/browser_thread.h" |
| 36 #include "content/public/browser/storage_partition.h" |
| 26 #include "ui/base/l10n/l10n_util.h" | 37 #include "ui/base/l10n/l10n_util.h" |
| 27 #include "ui/base/text/bytes_formatting.h" | 38 #include "ui/base/text/bytes_formatting.h" |
| 28 | 39 |
| 29 namespace chromeos { | 40 namespace chromeos { |
| 30 namespace options { | 41 namespace options { |
| 31 namespace { | 42 namespace { |
| 32 | 43 |
| 33 void GetSizeStatOnBlockingPool(const base::FilePath& mount_path, | 44 void GetSizeStatOnBlockingPool(const base::FilePath& mount_path, |
| 34 int64_t* total_size, | 45 int64_t* total_size, |
| 35 int64_t* available_size) { | 46 int64_t* available_size) { |
| 36 int64_t size = base::SysInfo::AmountOfTotalDiskSpace(mount_path); | 47 int64_t size = base::SysInfo::AmountOfTotalDiskSpace(mount_path); |
| 37 if (size >= 0) | 48 if (size >= 0) |
| 38 *total_size = size; | 49 *total_size = size; |
| 39 size = base::SysInfo::AmountOfFreeDiskSpace(mount_path); | 50 size = base::SysInfo::AmountOfFreeDiskSpace(mount_path); |
| 40 if (size >= 0) | 51 if (size >= 0) |
| 41 *available_size = size; | 52 *available_size = size; |
| 42 } | 53 } |
| 43 | 54 |
| 44 // Threshold to show a message indicating space is critically low (512 MB). | 55 // Threshold to show a message indicating space is critically low (512 MB). |
| 45 const int64_t kSpaceCriticallyLowBytes = 512 * 1024 * 1024; | 56 const int64_t kSpaceCriticallyLowBytes = 512 * 1024 * 1024; |
| 46 | 57 |
| 47 // Threshold to show a message indicating space is low (1 GB). | 58 // Threshold to show a message indicating space is low (1 GB). |
| 48 const int64_t kSpaceLowBytes = 1 * 1024 * 1024 * 1024; | 59 const int64_t kSpaceLowBytes = 1 * 1024 * 1024 * 1024; |
| 49 | 60 |
| 50 } // namespace | 61 } // namespace |
| 51 | 62 |
| 52 StorageManagerHandler::StorageManagerHandler() | 63 StorageManagerHandler::StorageManagerHandler() |
| 53 : browser_cache_size_(0), | 64 : browser_cache_size_(-1), |
| 54 browser_site_data_size_(0), | 65 has_browser_cache_size_(false), |
| 66 browser_site_data_size_(-1), |
| 67 has_browser_site_data_size_(false), |
| 55 weak_ptr_factory_(this) { | 68 weak_ptr_factory_(this) { |
| 56 } | 69 } |
| 57 | 70 |
| 58 StorageManagerHandler::~StorageManagerHandler() { | 71 StorageManagerHandler::~StorageManagerHandler() { |
| 59 } | 72 } |
| 60 | 73 |
| 61 void StorageManagerHandler::GetLocalizedValues( | 74 void StorageManagerHandler::GetLocalizedValues( |
| 62 base::DictionaryValue* localized_strings) { | 75 base::DictionaryValue* localized_strings) { |
| 63 DCHECK(localized_strings); | 76 DCHECK(localized_strings); |
| 64 | 77 |
| (...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 251 weak_ptr_factory_.GetWeakPtr())); | 264 weak_ptr_factory_.GetWeakPtr())); |
| 252 } | 265 } |
| 253 | 266 |
| 254 void StorageManagerHandler::OnGetDriveCacheSize(int64_t size) { | 267 void StorageManagerHandler::OnGetDriveCacheSize(int64_t size) { |
| 255 web_ui()->CallJavascriptFunctionUnsafe( | 268 web_ui()->CallJavascriptFunctionUnsafe( |
| 256 "options.StorageManager.setDriveCacheSize", | 269 "options.StorageManager.setDriveCacheSize", |
| 257 base::StringValue(ui::FormatBytes(size))); | 270 base::StringValue(ui::FormatBytes(size))); |
| 258 } | 271 } |
| 259 | 272 |
| 260 void StorageManagerHandler::UpdateBrowsingDataSize() { | 273 void StorageManagerHandler::UpdateBrowsingDataSize() { |
| 274 has_browser_cache_size_ = false; |
| 275 has_browser_site_data_size_ = false; |
| 261 Profile* const profile = Profile::FromWebUI(web_ui()); | 276 Profile* const profile = Profile::FromWebUI(web_ui()); |
| 262 // Fetch the size of http cache in browsing data. | 277 // Fetch the size of http cache in browsing data. |
| 263 // StoragePartitionHttpCacheDataRemover deletes itself when it is done. | 278 // StoragePartitionHttpCacheDataRemover deletes itself when it is done. |
| 264 browsing_data::StoragePartitionHttpCacheDataRemover::CreateForRange( | 279 browsing_data::StoragePartitionHttpCacheDataRemover::CreateForRange( |
| 265 content::BrowserContext::GetDefaultStoragePartition(profile), | 280 content::BrowserContext::GetDefaultStoragePartition(profile), |
| 266 base::Time(), | 281 base::Time(), |
| 267 base::Time::Max())->Count( | 282 base::Time::Max())->Count( |
| 268 base::Bind(&StorageManagerHandler::OnGetBrowsingDataSize, | 283 base::Bind(&StorageManagerHandler::OnGetBrowsingDataSize, |
| 269 weak_ptr_factory_.GetWeakPtr(), false)); | 284 weak_ptr_factory_.GetWeakPtr(), false)); |
| 270 | 285 |
| 271 // TODO(fukino): fetch the size of site data in browsing data. | 286 // Fetch the size of site data in browsing data. |
| 287 if (!site_data_size_collector_.get()) { |
| 288 content::StoragePartition* storage_partition = |
| 289 content::BrowserContext::GetDefaultStoragePartition(profile); |
| 290 site_data_size_collector_.reset(new SiteDataSizeCollector( |
| 291 storage_partition->GetPath(), |
| 292 new BrowsingDataCookieHelper(profile->GetRequestContext()), |
| 293 new BrowsingDataDatabaseHelper(profile), |
| 294 new BrowsingDataLocalStorageHelper(profile), |
| 295 new BrowsingDataAppCacheHelper(profile), |
| 296 new BrowsingDataIndexedDBHelper( |
| 297 storage_partition->GetIndexedDBContext()), |
| 298 BrowsingDataFileSystemHelper::Create( |
| 299 storage_partition->GetFileSystemContext()), |
| 300 BrowsingDataChannelIDHelper::Create(profile->GetRequestContext()), |
| 301 new BrowsingDataServiceWorkerHelper( |
| 302 storage_partition->GetServiceWorkerContext()), |
| 303 new BrowsingDataCacheStorageHelper( |
| 304 storage_partition->GetCacheStorageContext()), |
| 305 BrowsingDataFlashLSOHelper::Create(profile))); |
| 306 } |
| 307 site_data_size_collector_->Fetch( |
| 308 base::Bind(&StorageManagerHandler::OnGetBrowsingDataSize, |
| 309 weak_ptr_factory_.GetWeakPtr(), true)); |
| 272 } | 310 } |
| 273 | 311 |
| 274 void StorageManagerHandler::OnGetBrowsingDataSize(bool is_site_data, | 312 void StorageManagerHandler::OnGetBrowsingDataSize(bool is_site_data, |
| 275 int64_t size) { | 313 int64_t size) { |
| 276 if (is_site_data) | 314 if (is_site_data) { |
| 315 has_browser_site_data_size_ = true; |
| 277 browser_site_data_size_ = size; | 316 browser_site_data_size_ = size; |
| 278 else | 317 } else { |
| 318 has_browser_cache_size_ = true; |
| 279 browser_cache_size_ = size; | 319 browser_cache_size_ = size; |
| 280 | 320 } |
| 281 web_ui()->CallJavascriptFunctionUnsafe( | 321 if (has_browser_cache_size_ && has_browser_site_data_size_) { |
| 282 "options.StorageManager.setBrowsingDataSize", | 322 base::string16 size_string; |
| 283 base::StringValue(ui::FormatBytes(static_cast<int64_t>( | 323 if (browser_cache_size_ >= 0 && browser_site_data_size_ >= 0) { |
| 284 browser_cache_size_ + browser_site_data_size_)))); | 324 size_string = ui::FormatBytes( |
| 325 browser_site_data_size_ + browser_cache_size_); |
| 326 } else { |
| 327 size_string = l10n_util::GetStringUTF16( |
| 328 IDS_OPTIONS_SETTINGS_STORAGE_SIZE_UNKNOWN); |
| 329 } |
| 330 web_ui()->CallJavascriptFunctionUnsafe( |
| 331 "options.StorageManager.setBrowsingDataSize", |
| 332 base::StringValue(size_string)); |
| 333 } |
| 285 } | 334 } |
| 286 | 335 |
| 287 void StorageManagerHandler::UpdateOtherUsersSize() { | 336 void StorageManagerHandler::UpdateOtherUsersSize() { |
| 288 other_users_.clear(); | 337 other_users_.clear(); |
| 289 user_sizes_.clear(); | 338 user_sizes_.clear(); |
| 290 const user_manager::UserList& users = | 339 const user_manager::UserList& users = |
| 291 user_manager::UserManager::Get()->GetUsers(); | 340 user_manager::UserManager::Get()->GetUsers(); |
| 292 for (const auto& user : users) { | 341 for (const auto& user : users) { |
| 293 if (user->is_active()) | 342 if (user->is_active()) |
| 294 continue; | 343 continue; |
| 295 other_users_.push_back(user); | 344 other_users_.push_back(user); |
| 296 cryptohome::HomedirMethods::GetInstance()->GetAccountDiskUsage( | 345 cryptohome::HomedirMethods::GetInstance()->GetAccountDiskUsage( |
| 297 cryptohome::Identification(user->GetAccountId()), | 346 cryptohome::Identification(user->GetAccountId()), |
| 298 base::Bind(&StorageManagerHandler::OnGetOtherUserSize, | 347 base::Bind(&StorageManagerHandler::OnGetOtherUserSize, |
| 299 weak_ptr_factory_.GetWeakPtr())); | 348 weak_ptr_factory_.GetWeakPtr())); |
| 300 } | 349 } |
| 301 } | 350 } |
| 302 | 351 |
| 303 void StorageManagerHandler::OnGetOtherUserSize(bool success, int64_t size) { | 352 void StorageManagerHandler::OnGetOtherUserSize(bool success, int64_t size) { |
| 304 user_sizes_.push_back(success ? size : -1); | 353 user_sizes_.push_back(success ? size : -1); |
| 305 if (user_sizes_.size() == other_users_.size()) { | 354 if (user_sizes_.size() == other_users_.size()) { |
| 306 base::StringValue other_users_size(l10n_util::GetStringUTF16( | 355 base::string16 size_string; |
| 307 IDS_OPTIONS_SETTINGS_STORAGE_SIZE_UNKNOWN)); | |
| 308 // If all the requests succeed, shows the total bytes in the UI. | 356 // If all the requests succeed, shows the total bytes in the UI. |
| 309 if (std::count(user_sizes_.begin(), user_sizes_.end(), -1) == 0) { | 357 if (std::count(user_sizes_.begin(), user_sizes_.end(), -1) == 0) { |
| 310 other_users_size = base::StringValue(ui::FormatBytes( | 358 size_string = ui::FormatBytes( |
| 311 std::accumulate(user_sizes_.begin(), user_sizes_.end(), 0LL))); | 359 std::accumulate(user_sizes_.begin(), user_sizes_.end(), 0LL)); |
| 360 } else { |
| 361 size_string = l10n_util::GetStringUTF16( |
| 362 IDS_OPTIONS_SETTINGS_STORAGE_SIZE_UNKNOWN); |
| 312 } | 363 } |
| 313 web_ui()->CallJavascriptFunctionUnsafe( | 364 web_ui()->CallJavascriptFunctionUnsafe( |
| 314 "options.StorageManager.setOtherUsersSize", other_users_size); | 365 "options.StorageManager.setOtherUsersSize", |
| 366 base::StringValue(size_string)); |
| 315 } | 367 } |
| 316 } | 368 } |
| 317 | 369 |
| 318 void StorageManagerHandler::UpdateArcSize() { | 370 void StorageManagerHandler::UpdateArcSize() { |
| 319 Profile* const profile = Profile::FromWebUI(web_ui()); | 371 Profile* const profile = Profile::FromWebUI(web_ui()); |
| 320 if (!arc::ArcAuthService::IsAllowedForProfile(profile) || | 372 if (!arc::ArcAuthService::IsAllowedForProfile(profile) || |
| 321 arc::ArcAuthService::IsOptInVerificationDisabled() || | 373 arc::ArcAuthService::IsOptInVerificationDisabled() || |
| 322 !arc::ArcAuthService::Get()->IsArcEnabled()) { | 374 !arc::ArcAuthService::Get()->IsArcEnabled()) { |
| 323 return; | 375 return; |
| 324 } | 376 } |
| 325 | 377 |
| 326 // Shows the item "Android apps and cache" and start calculating size. | 378 // Shows the item "Android apps and cache" and start calculating size. |
| 327 web_ui()->CallJavascriptFunctionUnsafe( | 379 web_ui()->CallJavascriptFunctionUnsafe( |
| 328 "options.StorageManager.showArcItem"); | 380 "options.StorageManager.showArcItem"); |
| 329 bool success = arc::ArcStorageManager::Get()->GetApplicationsSize( | 381 bool success = arc::ArcStorageManager::Get()->GetApplicationsSize( |
| 330 base::Bind(&StorageManagerHandler::OnGetArcSize, | 382 base::Bind(&StorageManagerHandler::OnGetArcSize, |
| 331 base::Unretained(this))); | 383 base::Unretained(this))); |
| 332 if (!success) { | 384 if (!success) { |
| 333 // TODO(fukino): Retry updating arc size later if the arc bridge service is | 385 // TODO(fukino): Retry updating arc size later if the arc bridge service is |
| 334 // not ready. | 386 // not ready. |
| 335 web_ui()->CallJavascriptFunctionUnsafe( | 387 web_ui()->CallJavascriptFunctionUnsafe( |
| 336 "options.StorageManager.setArcSize", | 388 "options.StorageManager.setArcSize", |
| 337 base::StringValue(l10n_util::GetStringUTF16( | 389 base::StringValue(l10n_util::GetStringUTF16( |
| 338 IDS_OPTIONS_SETTINGS_STORAGE_SIZE_UNKNOWN))); | 390 IDS_OPTIONS_SETTINGS_STORAGE_SIZE_UNKNOWN))); |
| 339 } | 391 } |
| 340 } | 392 } |
| 341 | 393 |
| 342 void StorageManagerHandler::OnGetArcSize(bool succeeded, | 394 void StorageManagerHandler::OnGetArcSize(bool succeeded, |
| 343 arc::mojom::ApplicationsSizePtr size) { | 395 arc::mojom::ApplicationsSizePtr size) { |
| 344 base::StringValue arc_size(l10n_util::GetStringUTF16( | 396 base::string16 size_string; |
| 345 IDS_OPTIONS_SETTINGS_STORAGE_SIZE_UNKNOWN)); | |
| 346 if (succeeded) { | 397 if (succeeded) { |
| 347 uint64_t total_bytes = size->total_code_bytes + | 398 uint64_t total_bytes = size->total_code_bytes + |
| 348 size->total_data_bytes + | 399 size->total_data_bytes + |
| 349 size->total_cache_bytes; | 400 size->total_cache_bytes; |
| 350 arc_size = base::StringValue(ui::FormatBytes(total_bytes)); | 401 size_string = ui::FormatBytes(total_bytes); |
| 402 } else { |
| 403 size_string = l10n_util::GetStringUTF16( |
| 404 IDS_OPTIONS_SETTINGS_STORAGE_SIZE_UNKNOWN); |
| 351 } | 405 } |
| 352 web_ui()->CallJavascriptFunctionUnsafe("options.StorageManager.setArcSize", | 406 web_ui()->CallJavascriptFunctionUnsafe("options.StorageManager.setArcSize", |
| 353 arc_size); | 407 base::StringValue(size_string)); |
| 354 } | 408 } |
| 355 | 409 |
| 356 void StorageManagerHandler::OnClearDriveCacheDone(bool success) { | 410 void StorageManagerHandler::OnClearDriveCacheDone(bool success) { |
| 357 UpdateDriveCacheSize(); | 411 UpdateDriveCacheSize(); |
| 358 } | 412 } |
| 359 | 413 |
| 360 } // namespace options | 414 } // namespace options |
| 361 } // namespace chromeos | 415 } // namespace chromeos |
| OLD | NEW |