Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(346)

Side by Side Diff: components/offline_pages/offline_page_model.cc

Issue 2015793002: [Offline Pages] Linking storage manager and model with UMAs. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@wisp
Patch Set: Addressing comments. Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 {
36 const base::TimeDelta kStorageManagerStartingDelay =
jianli 2016/05/27 20:46:21 nit: Add an empty line before this line Also pleas
37 base::TimeDelta::FromSeconds(20);
32 38
33 // This enum is used in an UMA histogram. Hence the entries here shouldn't 39 // 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. 40 // be deleted or re-ordered and new ones should be added to the end.
35 enum ClearAllStatus { 41 enum ClearAllStatus {
36 CLEAR_ALL_SUCCEEDED, 42 CLEAR_ALL_SUCCEEDED,
37 STORE_RESET_FAILED, 43 STORE_RESET_FAILED,
38 STORE_RELOAD_FAILED, 44 STORE_RELOAD_FAILED,
39 45
40 // NOTE: always keep this entry at the end. 46 // NOTE: always keep this entry at the end.
41 CLEAR_ALL_STATUS_COUNT 47 CLEAR_ALL_STATUS_COUNT
(...skipping 486 matching lines...) Expand 10 before | Expand all | Expand 10 after
528 if (paths_to_delete.empty()) { 534 if (paths_to_delete.empty()) {
529 callback.Run(true); 535 callback.Run(true);
530 return; 536 return;
531 } 537 }
532 archive_manager_->DeleteMultipleArchives(paths_to_delete, callback); 538 archive_manager_->DeleteMultipleArchives(paths_to_delete, callback);
533 } 539 }
534 540
535 void OfflinePageModel::OnExpirePageDone(int64_t offline_id, 541 void OfflinePageModel::OnExpirePageDone(int64_t offline_id,
536 const base::Time& expiration_time, 542 const base::Time& expiration_time,
537 bool success) { 543 bool success) {
538 // TODO(romax): Report UMA about successful expiration. 544 UMA_HISTOGRAM_BOOLEAN("OfflinePages.ExpirePage.StoreUpdateResult", success);
539 if (success) { 545 if (!success)
540 auto iter = offline_pages_.find(offline_id); 546 return;
541 if (iter != offline_pages_.end()) 547 const auto& iter = offline_pages_.find(offline_id);
542 iter->second.expiration_time = expiration_time; 548 if (iter != offline_pages_.end()) {
549 iter->second.expiration_time = expiration_time;
550 ClientId client_id = iter->second.client_id;
551 UMA_HISTOGRAM_CUSTOM_COUNTS(
552 AddHistogramSuffix(client_id, "OfflinePages.ExpirePage.PageLifetime")
553 .c_str(),
554 (expiration_time - iter->second.creation_time).InMinutes(), 1,
555 base::TimeDelta::FromDays(30).InMinutes(), 50);
556 UMA_HISTOGRAM_CUSTOM_COUNTS(
557 AddHistogramSuffix(client_id,
558 "OfflinePages.ExpirePage.TimeSinceLastAccess")
559 .c_str(),
560 (expiration_time - iter->second.last_access_time).InMinutes(), 1,
561 base::TimeDelta::FromDays(30).InMinutes(), 50);
543 } 562 }
544 } 563 }
545 564
546 ClientPolicyController* OfflinePageModel::GetPolicyController() { 565 ClientPolicyController* OfflinePageModel::GetPolicyController() {
547 return policy_controller_.get(); 566 return policy_controller_.get();
548 } 567 }
549 568
550 OfflinePageMetadataStore* OfflinePageModel::GetStoreForTesting() { 569 OfflinePageMetadataStore* OfflinePageModel::GetStoreForTesting() {
551 return store_.get(); 570 return store_.get();
552 } 571 }
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
659 this, GetPolicyController(), archive_manager_.get())); 678 this, GetPolicyController(), archive_manager_.get()));
660 679
661 // Run all the delayed tasks. 680 // Run all the delayed tasks.
662 for (const auto& delayed_task : delayed_tasks_) 681 for (const auto& delayed_task : delayed_tasks_)
663 delayed_task.Run(); 682 delayed_task.Run();
664 delayed_tasks_.clear(); 683 delayed_tasks_.clear();
665 684
666 FOR_EACH_OBSERVER(Observer, observers_, OfflinePageModelLoaded(this)); 685 FOR_EACH_OBSERVER(Observer, observers_, OfflinePageModelLoaded(this));
667 686
668 CheckForExternalFileDeletion(); 687 CheckForExternalFileDeletion();
688
689 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
690 FROM_HERE, base::Bind(&OfflinePageModel::ClearStorageIfNeeded,
691 weak_ptr_factory_.GetWeakPtr(),
692 base::Bind(&OfflinePageModel::OnStorageCleared,
693 weak_ptr_factory_.GetWeakPtr())),
694 kStorageManagerStartingDelay);
669 } 695 }
670 696
671 void OfflinePageModel::InformSavePageDone(const SavePageCallback& callback, 697 void OfflinePageModel::InformSavePageDone(const SavePageCallback& callback,
672 SavePageResult result, 698 SavePageResult result,
673 const ClientId& client_id, 699 const ClientId& client_id,
674 int64_t offline_id) { 700 int64_t offline_id) {
675 UMA_HISTOGRAM_ENUMERATION( 701 UMA_HISTOGRAM_ENUMERATION(
676 AddHistogramSuffix(client_id, "OfflinePages.SavePageResult").c_str(), 702 AddHistogramSuffix(client_id, "OfflinePages.SavePageResult").c_str(),
677 static_cast<int>(result), 703 static_cast<int>(result),
678 static_cast<int>(SavePageResult::RESULT_COUNT)); 704 static_cast<int>(SavePageResult::RESULT_COUNT));
679 archive_manager_->GetStorageStats( 705 archive_manager_->GetStorageStats(
680 base::Bind(&ReportStorageHistogramsAfterSave)); 706 base::Bind(&ReportStorageHistogramsAfterSave));
707 base::ThreadTaskRunnerHandle::Get()->PostTask(
708 FROM_HERE, base::Bind(&OfflinePageModel::ClearStorageIfNeeded,
709 weak_ptr_factory_.GetWeakPtr(),
710 base::Bind(&OfflinePageModel::OnStorageCleared,
711 weak_ptr_factory_.GetWeakPtr())));
681 callback.Run(result, offline_id); 712 callback.Run(result, offline_id);
682 } 713 }
683 714
684 void OfflinePageModel::DeletePendingArchiver(OfflinePageArchiver* archiver) { 715 void OfflinePageModel::DeletePendingArchiver(OfflinePageArchiver* archiver) {
685 pending_archivers_.erase(std::find( 716 pending_archivers_.erase(std::find(
686 pending_archivers_.begin(), pending_archivers_.end(), archiver)); 717 pending_archivers_.begin(), pending_archivers_.end(), archiver));
687 } 718 }
688 719
689 void OfflinePageModel::OnDeleteArchiveFilesDone( 720 void OfflinePageModel::OnDeleteArchiveFilesDone(
690 const std::vector<int64_t>& offline_ids, 721 const std::vector<int64_t>& offline_ids,
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after
848 callback.Run(); 879 callback.Run();
849 } 880 }
850 881
851 void OfflinePageModel::CacheLoadedData( 882 void OfflinePageModel::CacheLoadedData(
852 const std::vector<OfflinePageItem>& offline_pages) { 883 const std::vector<OfflinePageItem>& offline_pages) {
853 offline_pages_.clear(); 884 offline_pages_.clear();
854 for (const auto& offline_page : offline_pages) 885 for (const auto& offline_page : offline_pages)
855 offline_pages_[offline_page.offline_id] = offline_page; 886 offline_pages_[offline_page.offline_id] = offline_page;
856 } 887 }
857 888
889 void OfflinePageModel::ClearStorageIfNeeded(
890 const ClearStorageCallback& callback) {
891 storage_manager_->ClearPagesIfNeeded(callback);
892 }
893
894 void OfflinePageModel::OnStorageCleared(size_t expired_page_count,
895 ClearStorageResult result) {
896 UMA_HISTOGRAM_ENUMERATION("OfflinePages.ClearStorageResult",
897 static_cast<int>(result),
898 static_cast<int>(ClearStorageResult::RESULT_COUNT));
899 if (expired_page_count > 0) {
900 UMA_HISTOGRAM_COUNTS("OfflinePages.ExpirePage.BatchSize",
901 static_cast<int32_t>(expired_page_count));
902 }
903 }
904
905 // static
858 int64_t OfflinePageModel::GenerateOfflineId() { 906 int64_t OfflinePageModel::GenerateOfflineId() {
859 return base::RandGenerator(std::numeric_limits<int64_t>::max()) + 1; 907 return base::RandGenerator(std::numeric_limits<int64_t>::max()) + 1;
860 } 908 }
861 909
862 void OfflinePageModel::RunWhenLoaded(const base::Closure& task) { 910 void OfflinePageModel::RunWhenLoaded(const base::Closure& task) {
863 if (!is_loaded_) { 911 if (!is_loaded_) {
864 delayed_tasks_.push_back(task); 912 delayed_tasks_.push_back(task);
865 return; 913 return;
866 } 914 }
867 915
868 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, task); 916 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, task);
869 } 917 }
870 918
871 } // namespace offline_pages 919 } // namespace offline_pages
OLDNEW
« no previous file with comments | « components/offline_pages/offline_page_model.h ('k') | components/offline_pages/offline_page_storage_manager.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698