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_namespace_constants.h" | |
dewittj
2016/05/31 23:12:01
This can be removed once you finish porting Offlin
jianli
2016/05/31 23:50:08
Done.
| |
22 #include "components/offline_pages/client_policy_controller.h" | 23 #include "components/offline_pages/client_policy_controller.h" |
23 #include "components/offline_pages/offline_page_item.h" | 24 #include "components/offline_pages/offline_page_item.h" |
24 #include "components/offline_pages/offline_page_storage_manager.h" | 25 #include "components/offline_pages/offline_page_storage_manager.h" |
25 #include "url/gurl.h" | 26 #include "url/gurl.h" |
26 | 27 |
27 using ArchiverResult = offline_pages::OfflinePageArchiver::ArchiverResult; | 28 using ArchiverResult = offline_pages::OfflinePageArchiver::ArchiverResult; |
28 | 29 |
29 namespace offline_pages { | 30 namespace offline_pages { |
30 | 31 |
31 namespace { | 32 namespace { |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
115 int percentage_of_free = static_cast<int>( | 116 int percentage_of_free = static_cast<int>( |
116 1.0 * storage_stats.total_archives_size / | 117 1.0 * storage_stats.total_archives_size / |
117 (storage_stats.total_archives_size + storage_stats.free_disk_space) * | 118 (storage_stats.total_archives_size + storage_stats.free_disk_space) * |
118 100); | 119 100); |
119 UMA_HISTOGRAM_PERCENTAGE( | 120 UMA_HISTOGRAM_PERCENTAGE( |
120 "OfflinePages.DeletePage.TotalPageSizeAsPercentageOfFreeSpace", | 121 "OfflinePages.DeletePage.TotalPageSizeAsPercentageOfFreeSpace", |
121 percentage_of_free); | 122 percentage_of_free); |
122 } | 123 } |
123 } | 124 } |
124 | 125 |
126 void ReportPageHistogramAfterSave(const ClientId& client_id, | |
127 SavePageResult result) { | |
128 // The histogram below is an expansion of the UMA_HISTOGRAM_ENUMERATION | |
129 // macro adapted to allow for a dynamically suffixed histogram name. | |
130 // Note: The factory creates and owns the histogram. | |
131 base::HistogramBase* histogram = base::LinearHistogram::FactoryGet( | |
132 AddHistogramSuffix(client_id, "OfflinePages.SavePageResult"), | |
133 1, | |
134 static_cast<int>(SavePageResult::RESULT_COUNT), | |
135 static_cast<int>(SavePageResult::RESULT_COUNT) + 1, | |
136 base::HistogramBase::kUmaTargetedHistogramFlag); | |
137 histogram->Add(static_cast<int>(result)); | |
138 } | |
139 | |
140 void ReportPageHistogramsAfterDelete( | |
141 const std::map<int64_t, OfflinePageItem>& offline_pages, | |
142 const std::vector<int64_t>& deleted_offline_ids) { | |
143 const int max_minutes = base::TimeDelta::FromDays(365).InMinutes(); | |
144 base::Time now = base::Time::Now(); | |
145 int64_t total_size = 0; | |
146 for (int64_t offline_id : deleted_offline_ids) { | |
147 auto iter = offline_pages.find(offline_id); | |
148 if (iter == offline_pages.end()) | |
149 continue; | |
150 total_size += iter->second.file_size; | |
151 ClientId client_id = iter->second.client_id; | |
152 | |
153 // The histograms below are an expansion of the UMA_HISTOGRAM_CUSTOM_COUNTS | |
154 // macro adapted to allow for a dynamically suffixed histogram name. | |
155 // Note: The factory creates and owns the histogram. | |
156 base::HistogramBase* histogram = base::Histogram::FactoryGet( | |
157 AddHistogramSuffix(client_id, "OfflinePages.PageLifetime"), | |
158 1, max_minutes, 100, base::HistogramBase::kUmaTargetedHistogramFlag); | |
159 histogram->Add((now - iter->second.creation_time).InMinutes()); | |
160 | |
161 histogram = base::Histogram::FactoryGet( | |
162 AddHistogramSuffix( | |
163 client_id, "OfflinePages.DeletePage.TimeSinceLastOpen"), | |
164 1, max_minutes, 100, base::HistogramBase::kUmaTargetedHistogramFlag); | |
165 histogram->Add((now - iter->second.last_access_time).InMinutes()); | |
166 | |
167 histogram = base::Histogram::FactoryGet( | |
168 AddHistogramSuffix( | |
169 client_id, "OfflinePages.DeletePage.LastOpenToCreated"), | |
170 1, max_minutes, 100, base::HistogramBase::kUmaTargetedHistogramFlag); | |
171 histogram->Add( | |
172 (iter->second.last_access_time - iter->second.creation_time). | |
173 InMinutes()); | |
174 | |
175 histogram = base::Histogram::FactoryGet( | |
176 AddHistogramSuffix(client_id, "OfflinePages.DeletePage.PageSize"), | |
177 1, 10000, 50, base::HistogramBase::kUmaTargetedHistogramFlag); | |
178 histogram->Add(iter->second.file_size / 1024); | |
179 | |
180 histogram = base::Histogram::FactoryGet( | |
181 AddHistogramSuffix(client_id, "OfflinePages.DeletePage.AccessCount"), | |
182 1, 1000000, 50, base::HistogramBase::kUmaTargetedHistogramFlag); | |
183 histogram->Add(iter->second.access_count); | |
184 } | |
185 | |
186 if (deleted_offline_ids.size() > 1) { | |
187 UMA_HISTOGRAM_COUNTS("OfflinePages.BatchDelete.Count", | |
188 static_cast<int32_t>(deleted_offline_ids.size())); | |
189 UMA_HISTOGRAM_MEMORY_KB( | |
190 "OfflinePages.BatchDelete.TotalPageSize", total_size / 1024); | |
191 } | |
192 } | |
193 | |
194 void ReportPageHistogramsAfterAccess(const OfflinePageItem& offline_page_item) { | |
195 // The histogram below is an expansion of the UMA_HISTOGRAM_CUSTOM_COUNTS | |
196 // macro adapted to allow for a dynamically suffixed histogram name. | |
197 // Note: The factory creates and owns the histogram. | |
198 base::HistogramBase* histogram = base::Histogram::FactoryGet( | |
199 AddHistogramSuffix( | |
200 offline_page_item.client_id, | |
201 offline_page_item.access_count == 0 ? | |
202 "OfflinePages.FirstOpenSinceCreated" : | |
203 "OfflinePages.OpenSinceLastOpen"), | |
204 1, kMaxOpenedPageHistogramBucket.InMinutes(), 50, | |
205 base::HistogramBase::kUmaTargetedHistogramFlag); | |
206 histogram->Add( | |
207 (base::Time::Now() - offline_page_item.last_access_time).InMinutes()); | |
208 } | |
209 | |
125 } // namespace | 210 } // namespace |
126 | 211 |
127 // static | 212 // static |
128 bool OfflinePageModel::CanSavePage(const GURL& url) { | 213 bool OfflinePageModel::CanSavePage(const GURL& url) { |
129 return url.SchemeIsHTTPOrHTTPS(); | 214 return url.SchemeIsHTTPOrHTTPS(); |
130 } | 215 } |
131 | 216 |
132 // protected | 217 // protected |
133 OfflinePageModel::OfflinePageModel() | 218 OfflinePageModel::OfflinePageModel() |
134 : is_loaded_(false), weak_ptr_factory_(this) {} | 219 : is_loaded_(false), weak_ptr_factory_(this) {} |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
194 void OfflinePageModel::MarkPageAccessed(int64_t offline_id) { | 279 void OfflinePageModel::MarkPageAccessed(int64_t offline_id) { |
195 DCHECK(is_loaded_); | 280 DCHECK(is_loaded_); |
196 auto iter = offline_pages_.find(offline_id); | 281 auto iter = offline_pages_.find(offline_id); |
197 if (iter == offline_pages_.end()) | 282 if (iter == offline_pages_.end()) |
198 return; | 283 return; |
199 | 284 |
200 // Make a copy of the cached item and update it. The cached item should only | 285 // Make a copy of the cached item and update it. The cached item should only |
201 // be updated upon the successful store operation. | 286 // be updated upon the successful store operation. |
202 OfflinePageItem offline_page_item = iter->second; | 287 OfflinePageItem offline_page_item = iter->second; |
203 | 288 |
204 base::Time now = base::Time::Now(); | 289 ReportPageHistogramsAfterAccess(offline_page_item); |
205 base::TimeDelta time_since_last_accessed = | |
206 now - offline_page_item.last_access_time; | |
207 | 290 |
208 // When the access account is still zero, the page is opened for the first | 291 offline_page_item.last_access_time = base::Time::Now(); |
209 // time since its creation. | |
210 UMA_HISTOGRAM_CUSTOM_COUNTS( | |
211 AddHistogramSuffix( | |
212 offline_page_item.client_id, | |
213 (offline_page_item.access_count == 0) ? | |
214 "OfflinePages.FirstOpenSinceCreated" : | |
215 "OfflinePages.OpenSinceLastOpen").c_str(), | |
216 time_since_last_accessed.InMinutes(), 1, | |
217 kMaxOpenedPageHistogramBucket.InMinutes(), 50); | |
218 | |
219 offline_page_item.last_access_time = now; | |
220 offline_page_item.access_count++; | 292 offline_page_item.access_count++; |
221 | 293 |
222 store_->AddOrUpdateOfflinePage( | 294 store_->AddOrUpdateOfflinePage( |
223 offline_page_item, | 295 offline_page_item, |
224 base::Bind(&OfflinePageModel::OnMarkPageAccesseDone, | 296 base::Bind(&OfflinePageModel::OnMarkPageAccesseDone, |
225 weak_ptr_factory_.GetWeakPtr(), offline_page_item)); | 297 weak_ptr_factory_.GetWeakPtr(), offline_page_item)); |
226 } | 298 } |
227 | 299 |
228 void OfflinePageModel::DeletePagesByOfflineId( | 300 void OfflinePageModel::DeletePagesByOfflineId( |
229 const std::vector<int64_t>& offline_ids, | 301 const std::vector<int64_t>& offline_ids, |
(...skipping 365 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
595 } | 667 } |
596 | 668 |
597 void OfflinePageModel::OnAddOfflinePageDone(OfflinePageArchiver* archiver, | 669 void OfflinePageModel::OnAddOfflinePageDone(OfflinePageArchiver* archiver, |
598 const SavePageCallback& callback, | 670 const SavePageCallback& callback, |
599 const OfflinePageItem& offline_page, | 671 const OfflinePageItem& offline_page, |
600 bool success) { | 672 bool success) { |
601 SavePageResult result; | 673 SavePageResult result; |
602 if (success) { | 674 if (success) { |
603 offline_pages_[offline_page.offline_id] = offline_page; | 675 offline_pages_[offline_page.offline_id] = offline_page; |
604 result = SavePageResult::SUCCESS; | 676 result = SavePageResult::SUCCESS; |
605 UMA_HISTOGRAM_TIMES( | 677 if (offline_page.client_id.name_space == kBookmarkNamespace) { |
606 AddHistogramSuffix( | 678 UMA_HISTOGRAM_TIMES("OfflinePages.SavePageTime.bookmark", |
dewittj
2016/05/31 22:38:45
should port this to Histogram::FactoryGet as well.
jianli
2016/05/31 23:50:08
Done.
| |
607 offline_page.client_id, "OfflinePages.SavePageTime").c_str(), | 679 base::Time::Now() - offline_page.creation_time); |
608 base::Time::Now() - offline_page.creation_time); | 680 // 50 buckets capped between 1Kb and 10Mb. |
609 // 50 buckets capped between 1Kb and 10Mb. | 681 UMA_HISTOGRAM_COUNTS_10000("OfflinePages.PageSize.bookmark", |
610 UMA_HISTOGRAM_COUNTS_10000(AddHistogramSuffix( | 682 offline_page.file_size / 1024); |
611 offline_page.client_id, "OfflinePages.PageSize").c_str(), | 683 } else if (offline_page.client_id.name_space == kLastNNamespace) { |
612 offline_page.file_size / 1024); | 684 UMA_HISTOGRAM_TIMES("OfflinePages.SavePageTime.last_n", |
685 base::Time::Now() - offline_page.creation_time); | |
686 // 50 buckets capped between 1Kb and 10Mb. | |
687 UMA_HISTOGRAM_COUNTS_10000("OfflinePages.PageSize.last_n", | |
688 offline_page.file_size / 1024); | |
689 } else { | |
690 UMA_HISTOGRAM_TIMES("OfflinePages.SavePageTime", | |
691 base::Time::Now() - offline_page.creation_time); | |
692 // 50 buckets capped between 1Kb and 10Mb. | |
693 UMA_HISTOGRAM_COUNTS_10000("OfflinePages.PageSize", | |
694 offline_page.file_size / 1024); | |
695 } | |
613 } else { | 696 } else { |
614 result = SavePageResult::STORE_FAILURE; | 697 result = SavePageResult::STORE_FAILURE; |
615 } | 698 } |
616 InformSavePageDone(callback, result, offline_page.client_id, | 699 InformSavePageDone(callback, result, offline_page.client_id, |
617 offline_page.offline_id); | 700 offline_page.offline_id); |
618 DeletePendingArchiver(archiver); | 701 DeletePendingArchiver(archiver); |
619 | 702 |
620 FOR_EACH_OBSERVER(Observer, observers_, OfflinePageModelChanged(this)); | 703 FOR_EACH_OBSERVER(Observer, observers_, OfflinePageModelChanged(this)); |
621 } | 704 } |
622 | 705 |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
665 | 748 |
666 FOR_EACH_OBSERVER(Observer, observers_, OfflinePageModelLoaded(this)); | 749 FOR_EACH_OBSERVER(Observer, observers_, OfflinePageModelLoaded(this)); |
667 | 750 |
668 CheckForExternalFileDeletion(); | 751 CheckForExternalFileDeletion(); |
669 } | 752 } |
670 | 753 |
671 void OfflinePageModel::InformSavePageDone(const SavePageCallback& callback, | 754 void OfflinePageModel::InformSavePageDone(const SavePageCallback& callback, |
672 SavePageResult result, | 755 SavePageResult result, |
673 const ClientId& client_id, | 756 const ClientId& client_id, |
674 int64_t offline_id) { | 757 int64_t offline_id) { |
675 UMA_HISTOGRAM_ENUMERATION( | 758 ReportPageHistogramAfterSave(client_id, result); |
676 AddHistogramSuffix(client_id, "OfflinePages.SavePageResult").c_str(), | |
677 static_cast<int>(result), | |
678 static_cast<int>(SavePageResult::RESULT_COUNT)); | |
679 archive_manager_->GetStorageStats( | 759 archive_manager_->GetStorageStats( |
680 base::Bind(&ReportStorageHistogramsAfterSave)); | 760 base::Bind(&ReportStorageHistogramsAfterSave)); |
681 callback.Run(result, offline_id); | 761 callback.Run(result, offline_id); |
682 } | 762 } |
683 | 763 |
684 void OfflinePageModel::DeletePendingArchiver(OfflinePageArchiver* archiver) { | 764 void OfflinePageModel::DeletePendingArchiver(OfflinePageArchiver* archiver) { |
685 pending_archivers_.erase(std::find( | 765 pending_archivers_.erase(std::find( |
686 pending_archivers_.begin(), pending_archivers_.end(), archiver)); | 766 pending_archivers_.begin(), pending_archivers_.end(), archiver)); |
687 } | 767 } |
688 | 768 |
689 void OfflinePageModel::OnDeleteArchiveFilesDone( | 769 void OfflinePageModel::OnDeleteArchiveFilesDone( |
690 const std::vector<int64_t>& offline_ids, | 770 const std::vector<int64_t>& offline_ids, |
691 const DeletePageCallback& callback, | 771 const DeletePageCallback& callback, |
692 bool success) { | 772 bool success) { |
693 if (!success) { | 773 if (!success) { |
694 InformDeletePageDone(callback, DeletePageResult::DEVICE_FAILURE); | 774 InformDeletePageDone(callback, DeletePageResult::DEVICE_FAILURE); |
695 return; | 775 return; |
696 } | 776 } |
697 | 777 |
698 store_->RemoveOfflinePages( | 778 store_->RemoveOfflinePages( |
699 offline_ids, | 779 offline_ids, |
700 base::Bind(&OfflinePageModel::OnRemoveOfflinePagesDone, | 780 base::Bind(&OfflinePageModel::OnRemoveOfflinePagesDone, |
701 weak_ptr_factory_.GetWeakPtr(), offline_ids, callback)); | 781 weak_ptr_factory_.GetWeakPtr(), offline_ids, callback)); |
702 } | 782 } |
703 | 783 |
704 void OfflinePageModel::OnRemoveOfflinePagesDone( | 784 void OfflinePageModel::OnRemoveOfflinePagesDone( |
705 const std::vector<int64_t>& offline_ids, | 785 const std::vector<int64_t>& offline_ids, |
706 const DeletePageCallback& callback, | 786 const DeletePageCallback& callback, |
707 bool success) { | 787 bool success) { |
788 ReportPageHistogramsAfterDelete(offline_pages_, offline_ids); | |
789 | |
708 // Delete the offline page from the in memory cache regardless of success in | 790 // Delete the offline page from the in memory cache regardless of success in |
709 // store. | 791 // store. |
710 base::Time now = base::Time::Now(); | |
711 int64_t total_size = 0; | |
712 for (int64_t offline_id : offline_ids) { | 792 for (int64_t offline_id : offline_ids) { |
713 auto iter = offline_pages_.find(offline_id); | 793 auto iter = offline_pages_.find(offline_id); |
714 if (iter == offline_pages_.end()) | 794 if (iter == offline_pages_.end()) |
715 continue; | 795 continue; |
716 total_size += iter->second.file_size; | |
717 ClientId client_id = iter->second.client_id; | |
718 UMA_HISTOGRAM_CUSTOM_COUNTS( | |
719 AddHistogramSuffix(client_id, "OfflinePages.PageLifetime").c_str(), | |
720 (now - iter->second.creation_time).InMinutes(), | |
721 1, | |
722 base::TimeDelta::FromDays(365).InMinutes(), | |
723 100); | |
724 UMA_HISTOGRAM_CUSTOM_COUNTS( | |
725 AddHistogramSuffix( | |
726 client_id, "OfflinePages.DeletePage.TimeSinceLastOpen").c_str(), | |
727 (now - iter->second.last_access_time).InMinutes(), | |
728 1, | |
729 base::TimeDelta::FromDays(365).InMinutes(), | |
730 100); | |
731 UMA_HISTOGRAM_CUSTOM_COUNTS( | |
732 AddHistogramSuffix( | |
733 client_id, "OfflinePages.DeletePage.LastOpenToCreated").c_str(), | |
734 (iter->second.last_access_time - iter->second.creation_time). | |
735 InMinutes(), | |
736 1, | |
737 base::TimeDelta::FromDays(365).InMinutes(), | |
738 100); | |
739 UMA_HISTOGRAM_MEMORY_KB( | |
740 AddHistogramSuffix( | |
741 client_id, "OfflinePages.DeletePage.PageSize").c_str(), | |
742 iter->second.file_size / 1024); | |
743 UMA_HISTOGRAM_COUNTS( | |
744 AddHistogramSuffix( | |
745 client_id, "OfflinePages.DeletePage.AccessCount").c_str(), | |
746 iter->second.access_count); | |
747 FOR_EACH_OBSERVER( | 796 FOR_EACH_OBSERVER( |
748 Observer, observers_, | 797 Observer, observers_, |
749 OfflinePageDeleted(iter->second.offline_id, iter->second.client_id)); | 798 OfflinePageDeleted(iter->second.offline_id, iter->second.client_id)); |
750 offline_pages_.erase(iter); | 799 offline_pages_.erase(iter); |
751 } | 800 } |
752 if (offline_ids.size() > 1) { | 801 |
753 UMA_HISTOGRAM_COUNTS("OfflinePages.BatchDelete.Count", | |
754 static_cast<int32_t>(offline_ids.size())); | |
755 UMA_HISTOGRAM_MEMORY_KB( | |
756 "OfflinePages.BatchDelete.TotalPageSize", total_size / 1024); | |
757 } | |
758 // Deleting multiple pages always succeeds when it gets to this point. | 802 // Deleting multiple pages always succeeds when it gets to this point. |
759 InformDeletePageDone(callback, (success || offline_ids.size() > 1) | 803 InformDeletePageDone(callback, (success || offline_ids.size() > 1) |
760 ? DeletePageResult::SUCCESS | 804 ? DeletePageResult::SUCCESS |
761 : DeletePageResult::STORE_FAILURE); | 805 : DeletePageResult::STORE_FAILURE); |
762 } | 806 } |
763 | 807 |
764 void OfflinePageModel::InformDeletePageDone(const DeletePageCallback& callback, | 808 void OfflinePageModel::InformDeletePageDone(const DeletePageCallback& callback, |
765 DeletePageResult result) { | 809 DeletePageResult result) { |
766 UMA_HISTOGRAM_ENUMERATION( | 810 UMA_HISTOGRAM_ENUMERATION( |
767 "OfflinePages.DeletePageResult", | 811 "OfflinePages.DeletePageResult", |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
862 void OfflinePageModel::RunWhenLoaded(const base::Closure& task) { | 906 void OfflinePageModel::RunWhenLoaded(const base::Closure& task) { |
863 if (!is_loaded_) { | 907 if (!is_loaded_) { |
864 delayed_tasks_.push_back(task); | 908 delayed_tasks_.push_back(task); |
865 return; | 909 return; |
866 } | 910 } |
867 | 911 |
868 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, task); | 912 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, task); |
869 } | 913 } |
870 | 914 |
871 } // namespace offline_pages | 915 } // namespace offline_pages |
OLD | NEW |