| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "components/offline_pages/offline_page_model.h" | 5 #include "components/offline_pages/offline_page_model.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
| 12 #include "base/location.h" | 12 #include "base/location.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/metrics/histogram_macros.h" | 14 #include "base/metrics/histogram_macros.h" |
| 15 #include "base/optional.h" | 15 #include "base/optional.h" |
| 16 #include "base/rand_util.h" | 16 #include "base/rand_util.h" |
| 17 #include "base/sequenced_task_runner.h" | 17 #include "base/sequenced_task_runner.h" |
| 18 #include "base/strings/string_number_conversions.h" | 18 #include "base/strings/string_number_conversions.h" |
| 19 #include "base/threading/thread_task_runner_handle.h" | 19 #include "base/threading/thread_task_runner_handle.h" |
| 20 #include "base/time/time.h" | 20 #include "base/time/time.h" |
| 21 #include "components/offline_pages/archive_manager.h" | 21 #include "components/offline_pages/archive_manager.h" |
| 22 #include "components/offline_pages/client_policy_controller.h" | 22 #include "components/offline_pages/client_policy_controller.h" |
| 23 #include "components/offline_pages/offline_page_item.h" | 23 #include "components/offline_pages/offline_page_item.h" |
| 24 #include "components/offline_pages/offline_page_storage_manager.h" | 24 #include "components/offline_pages/offline_page_storage_manager.h" |
| 25 #include "url/gurl.h" | 25 #include "url/gurl.h" |
| 26 | 26 |
| 27 using ArchiverResult = offline_pages::OfflinePageArchiver::ArchiverResult; | 27 using ArchiverResult = offline_pages::OfflinePageArchiver::ArchiverResult; |
| 28 using ClearStorageCallback = |
| 29 offline_pages::OfflinePageStorageManager::ClearStorageCallback; |
| 30 using ClearStorageResult = |
| 31 offline_pages::OfflinePageStorageManager::ClearStorageResult; |
| 28 | 32 |
| 29 namespace offline_pages { | 33 namespace offline_pages { |
| 30 | 34 |
| 31 namespace { | 35 namespace { |
| 32 | 36 |
| 37 // The delay used to schedule the first clear storage request for storage |
| 38 // manager after the model is loaded. |
| 39 const base::TimeDelta kStorageManagerStartingDelay = |
| 40 base::TimeDelta::FromSeconds(20); |
| 41 |
| 33 // This enum is used in an UMA histogram. Hence the entries here shouldn't | 42 // This enum is used in an UMA histogram. Hence the entries here shouldn't |
| 34 // be deleted or re-ordered and new ones should be added to the end. | 43 // be deleted or re-ordered and new ones should be added to the end. |
| 35 enum ClearAllStatus { | 44 enum ClearAllStatus { |
| 36 CLEAR_ALL_SUCCEEDED, | 45 CLEAR_ALL_SUCCEEDED, |
| 37 STORE_RESET_FAILED, | 46 STORE_RESET_FAILED, |
| 38 STORE_RELOAD_FAILED, | 47 STORE_RELOAD_FAILED, |
| 39 | 48 |
| 40 // NOTE: always keep this entry at the end. | 49 // NOTE: always keep this entry at the end. |
| 41 CLEAR_ALL_STATUS_COUNT | 50 CLEAR_ALL_STATUS_COUNT |
| 42 }; | 51 }; |
| (...skipping 485 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 528 if (paths_to_delete.empty()) { | 537 if (paths_to_delete.empty()) { |
| 529 callback.Run(true); | 538 callback.Run(true); |
| 530 return; | 539 return; |
| 531 } | 540 } |
| 532 archive_manager_->DeleteMultipleArchives(paths_to_delete, callback); | 541 archive_manager_->DeleteMultipleArchives(paths_to_delete, callback); |
| 533 } | 542 } |
| 534 | 543 |
| 535 void OfflinePageModel::OnExpirePageDone(int64_t offline_id, | 544 void OfflinePageModel::OnExpirePageDone(int64_t offline_id, |
| 536 const base::Time& expiration_time, | 545 const base::Time& expiration_time, |
| 537 bool success) { | 546 bool success) { |
| 538 // TODO(romax): Report UMA about successful expiration. | 547 UMA_HISTOGRAM_BOOLEAN("OfflinePages.ExpirePage.StoreUpdateResult", success); |
| 539 if (success) { | 548 if (!success) |
| 540 auto iter = offline_pages_.find(offline_id); | 549 return; |
| 541 if (iter != offline_pages_.end()) | 550 const auto& iter = offline_pages_.find(offline_id); |
| 542 iter->second.expiration_time = expiration_time; | 551 if (iter != offline_pages_.end()) { |
| 552 iter->second.expiration_time = expiration_time; |
| 553 ClientId client_id = iter->second.client_id; |
| 554 UMA_HISTOGRAM_CUSTOM_COUNTS( |
| 555 AddHistogramSuffix(client_id, "OfflinePages.ExpirePage.PageLifetime") |
| 556 .c_str(), |
| 557 (expiration_time - iter->second.creation_time).InMinutes(), 1, |
| 558 base::TimeDelta::FromDays(30).InMinutes(), 50); |
| 559 UMA_HISTOGRAM_CUSTOM_COUNTS( |
| 560 AddHistogramSuffix(client_id, |
| 561 "OfflinePages.ExpirePage.TimeSinceLastAccess") |
| 562 .c_str(), |
| 563 (expiration_time - iter->second.last_access_time).InMinutes(), 1, |
| 564 base::TimeDelta::FromDays(30).InMinutes(), 50); |
| 543 } | 565 } |
| 544 } | 566 } |
| 545 | 567 |
| 546 ClientPolicyController* OfflinePageModel::GetPolicyController() { | 568 ClientPolicyController* OfflinePageModel::GetPolicyController() { |
| 547 return policy_controller_.get(); | 569 return policy_controller_.get(); |
| 548 } | 570 } |
| 549 | 571 |
| 550 OfflinePageMetadataStore* OfflinePageModel::GetStoreForTesting() { | 572 OfflinePageMetadataStore* OfflinePageModel::GetStoreForTesting() { |
| 551 return store_.get(); | 573 return store_.get(); |
| 552 } | 574 } |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 659 this, GetPolicyController(), archive_manager_.get())); | 681 this, GetPolicyController(), archive_manager_.get())); |
| 660 | 682 |
| 661 // Run all the delayed tasks. | 683 // Run all the delayed tasks. |
| 662 for (const auto& delayed_task : delayed_tasks_) | 684 for (const auto& delayed_task : delayed_tasks_) |
| 663 delayed_task.Run(); | 685 delayed_task.Run(); |
| 664 delayed_tasks_.clear(); | 686 delayed_tasks_.clear(); |
| 665 | 687 |
| 666 FOR_EACH_OBSERVER(Observer, observers_, OfflinePageModelLoaded(this)); | 688 FOR_EACH_OBSERVER(Observer, observers_, OfflinePageModelLoaded(this)); |
| 667 | 689 |
| 668 CheckForExternalFileDeletion(); | 690 CheckForExternalFileDeletion(); |
| 691 |
| 692 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 693 FROM_HERE, base::Bind(&OfflinePageModel::ClearStorageIfNeeded, |
| 694 weak_ptr_factory_.GetWeakPtr(), |
| 695 base::Bind(&OfflinePageModel::OnStorageCleared, |
| 696 weak_ptr_factory_.GetWeakPtr())), |
| 697 kStorageManagerStartingDelay); |
| 669 } | 698 } |
| 670 | 699 |
| 671 void OfflinePageModel::InformSavePageDone(const SavePageCallback& callback, | 700 void OfflinePageModel::InformSavePageDone(const SavePageCallback& callback, |
| 672 SavePageResult result, | 701 SavePageResult result, |
| 673 const ClientId& client_id, | 702 const ClientId& client_id, |
| 674 int64_t offline_id) { | 703 int64_t offline_id) { |
| 675 UMA_HISTOGRAM_ENUMERATION( | 704 UMA_HISTOGRAM_ENUMERATION( |
| 676 AddHistogramSuffix(client_id, "OfflinePages.SavePageResult").c_str(), | 705 AddHistogramSuffix(client_id, "OfflinePages.SavePageResult").c_str(), |
| 677 static_cast<int>(result), | 706 static_cast<int>(result), |
| 678 static_cast<int>(SavePageResult::RESULT_COUNT)); | 707 static_cast<int>(SavePageResult::RESULT_COUNT)); |
| 679 archive_manager_->GetStorageStats( | 708 archive_manager_->GetStorageStats( |
| 680 base::Bind(&ReportStorageHistogramsAfterSave)); | 709 base::Bind(&ReportStorageHistogramsAfterSave)); |
| 710 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 711 FROM_HERE, base::Bind(&OfflinePageModel::ClearStorageIfNeeded, |
| 712 weak_ptr_factory_.GetWeakPtr(), |
| 713 base::Bind(&OfflinePageModel::OnStorageCleared, |
| 714 weak_ptr_factory_.GetWeakPtr()))); |
| 681 callback.Run(result, offline_id); | 715 callback.Run(result, offline_id); |
| 682 } | 716 } |
| 683 | 717 |
| 684 void OfflinePageModel::DeletePendingArchiver(OfflinePageArchiver* archiver) { | 718 void OfflinePageModel::DeletePendingArchiver(OfflinePageArchiver* archiver) { |
| 685 pending_archivers_.erase(std::find( | 719 pending_archivers_.erase(std::find( |
| 686 pending_archivers_.begin(), pending_archivers_.end(), archiver)); | 720 pending_archivers_.begin(), pending_archivers_.end(), archiver)); |
| 687 } | 721 } |
| 688 | 722 |
| 689 void OfflinePageModel::OnDeleteArchiveFilesDone( | 723 void OfflinePageModel::OnDeleteArchiveFilesDone( |
| 690 const std::vector<int64_t>& offline_ids, | 724 const std::vector<int64_t>& offline_ids, |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 848 callback.Run(); | 882 callback.Run(); |
| 849 } | 883 } |
| 850 | 884 |
| 851 void OfflinePageModel::CacheLoadedData( | 885 void OfflinePageModel::CacheLoadedData( |
| 852 const std::vector<OfflinePageItem>& offline_pages) { | 886 const std::vector<OfflinePageItem>& offline_pages) { |
| 853 offline_pages_.clear(); | 887 offline_pages_.clear(); |
| 854 for (const auto& offline_page : offline_pages) | 888 for (const auto& offline_page : offline_pages) |
| 855 offline_pages_[offline_page.offline_id] = offline_page; | 889 offline_pages_[offline_page.offline_id] = offline_page; |
| 856 } | 890 } |
| 857 | 891 |
| 892 void OfflinePageModel::ClearStorageIfNeeded( |
| 893 const ClearStorageCallback& callback) { |
| 894 storage_manager_->ClearPagesIfNeeded(callback); |
| 895 } |
| 896 |
| 897 void OfflinePageModel::OnStorageCleared(size_t expired_page_count, |
| 898 ClearStorageResult result) { |
| 899 UMA_HISTOGRAM_ENUMERATION("OfflinePages.ClearStorageResult", |
| 900 static_cast<int>(result), |
| 901 static_cast<int>(ClearStorageResult::RESULT_COUNT)); |
| 902 if (expired_page_count > 0) { |
| 903 UMA_HISTOGRAM_COUNTS("OfflinePages.ExpirePage.BatchSize", |
| 904 static_cast<int32_t>(expired_page_count)); |
| 905 } |
| 906 } |
| 907 |
| 908 // static |
| 858 int64_t OfflinePageModel::GenerateOfflineId() { | 909 int64_t OfflinePageModel::GenerateOfflineId() { |
| 859 return base::RandGenerator(std::numeric_limits<int64_t>::max()) + 1; | 910 return base::RandGenerator(std::numeric_limits<int64_t>::max()) + 1; |
| 860 } | 911 } |
| 861 | 912 |
| 862 void OfflinePageModel::RunWhenLoaded(const base::Closure& task) { | 913 void OfflinePageModel::RunWhenLoaded(const base::Closure& task) { |
| 863 if (!is_loaded_) { | 914 if (!is_loaded_) { |
| 864 delayed_tasks_.push_back(task); | 915 delayed_tasks_.push_back(task); |
| 865 return; | 916 return; |
| 866 } | 917 } |
| 867 | 918 |
| 868 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, task); | 919 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, task); |
| 869 } | 920 } |
| 870 | 921 |
| 871 } // namespace offline_pages | 922 } // namespace offline_pages |
| OLD | NEW |