| 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 "components/offline_pages/offline_page_model_impl.h" | 5 #include "components/offline_pages/offline_page_model_impl.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <limits> | 8 #include <limits> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 143 // Note: The factory creates and owns the histogram. | 143 // Note: The factory creates and owns the histogram. |
| 144 base::HistogramBase* histogram = base::LinearHistogram::FactoryGet( | 144 base::HistogramBase* histogram = base::LinearHistogram::FactoryGet( |
| 145 AddHistogramSuffix(client_id, "OfflinePages.SavePageResult"), | 145 AddHistogramSuffix(client_id, "OfflinePages.SavePageResult"), |
| 146 1, | 146 1, |
| 147 static_cast<int>(SavePageResult::RESULT_COUNT), | 147 static_cast<int>(SavePageResult::RESULT_COUNT), |
| 148 static_cast<int>(SavePageResult::RESULT_COUNT) + 1, | 148 static_cast<int>(SavePageResult::RESULT_COUNT) + 1, |
| 149 base::HistogramBase::kUmaTargetedHistogramFlag); | 149 base::HistogramBase::kUmaTargetedHistogramFlag); |
| 150 histogram->Add(static_cast<int>(result)); | 150 histogram->Add(static_cast<int>(result)); |
| 151 } | 151 } |
| 152 | 152 |
| 153 void ReportPageHistogramAfterSave(const OfflinePageItem& offline_page) { | 153 // Goes through the list of offline pages, compiling the following two metrics: |
| 154 // - a count of the pages with the same URL |
| 155 // - The difference between the |created_before| time and the creation time of |
| 156 // the page with the closest creation time before |created_before|. |
| 157 // Returns true if there was a page that was saved before |created_before| with |
| 158 // a matching URL. |
| 159 bool GetMatchingURLCountAndMostRecentCreationTime( |
| 160 const std::map<int64_t, OfflinePageItem>& offline_pages, |
| 161 std::string name_space, |
| 162 const GURL& url, |
| 163 base::Time created_before, |
| 164 int* matching_url_count, |
| 165 base::TimeDelta* most_recent_creation_time) { |
| 166 int count = 0; |
| 167 |
| 168 // Create a time that is very old, so that any valid time will be newer than |
| 169 // it. |
| 170 base::Time latest_time; |
| 171 bool matching_page = false; |
| 172 |
| 173 for (auto& id_page_pair : offline_pages) { |
| 174 if (id_page_pair.second.client_id.name_space == name_space && |
| 175 url == id_page_pair.second.url) { |
| 176 count++; |
| 177 base::Time page_creation_time = id_page_pair.second.creation_time; |
| 178 if (page_creation_time < created_before && |
| 179 page_creation_time > latest_time) { |
| 180 latest_time = page_creation_time; |
| 181 matching_page = true; |
| 182 } |
| 183 } |
| 184 } |
| 185 |
| 186 if (matching_url_count != nullptr) |
| 187 *matching_url_count = count; |
| 188 if (most_recent_creation_time != nullptr && latest_time != base::Time()) |
| 189 *most_recent_creation_time = created_before - latest_time; |
| 190 |
| 191 return matching_page; |
| 192 } |
| 193 |
| 194 void ReportPageHistogramAfterSave( |
| 195 const std::map<int64_t, OfflinePageItem>& offline_pages, |
| 196 const OfflinePageItem& offline_page) { |
| 154 // The histogram below is an expansion of the UMA_HISTOGRAM_TIMES | 197 // The histogram below is an expansion of the UMA_HISTOGRAM_TIMES |
| 155 // macro adapted to allow for a dynamically suffixed histogram name. | 198 // macro adapted to allow for a dynamically suffixed histogram name. |
| 156 // Note: The factory creates and owns the histogram. | 199 // Note: The factory creates and owns the histogram. |
| 157 base::HistogramBase* histogram = base::Histogram::FactoryTimeGet( | 200 base::HistogramBase* histogram = base::Histogram::FactoryTimeGet( |
| 158 AddHistogramSuffix(offline_page.client_id, "OfflinePages.SavePageTime"), | 201 AddHistogramSuffix(offline_page.client_id, "OfflinePages.SavePageTime"), |
| 159 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromSeconds(10), | 202 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromSeconds(10), |
| 160 50, base::HistogramBase::kUmaTargetedHistogramFlag); | 203 50, base::HistogramBase::kUmaTargetedHistogramFlag); |
| 161 histogram->AddTime(base::Time::Now() - offline_page.creation_time); | 204 histogram->AddTime(base::Time::Now() - offline_page.creation_time); |
| 162 | 205 |
| 163 // The histogram below is an expansion of the UMA_HISTOGRAM_CUSTOM_COUNTS | 206 // The histogram below is an expansion of the UMA_HISTOGRAM_CUSTOM_COUNTS |
| 164 // macro adapted to allow for a dynamically suffixed histogram name. | 207 // macro adapted to allow for a dynamically suffixed histogram name. |
| 165 // Note: The factory creates and owns the histogram. | 208 // Note: The factory creates and owns the histogram. |
| 166 // Reported as Kb between 1Kb and 10Mb. | 209 // Reported as Kb between 1Kb and 10Mb. |
| 167 histogram = base::Histogram::FactoryGet( | 210 histogram = base::Histogram::FactoryGet( |
| 168 AddHistogramSuffix(offline_page.client_id, "OfflinePages.PageSize"), | 211 AddHistogramSuffix(offline_page.client_id, "OfflinePages.PageSize"), |
| 169 1, 10000, 50, base::HistogramBase::kUmaTargetedHistogramFlag); | 212 1, 10000, 50, base::HistogramBase::kUmaTargetedHistogramFlag); |
| 170 histogram->Add(offline_page.file_size / 1024); | 213 histogram->Add(offline_page.file_size / 1024); |
| 214 |
| 215 if (offline_page.client_id.name_space == kDownloadNamespace) { |
| 216 int matching_url_count; |
| 217 base::TimeDelta time_since_most_recent_duplicate; |
| 218 if (GetMatchingURLCountAndMostRecentCreationTime( |
| 219 offline_pages, offline_page.client_id.name_space, offline_page.url, |
| 220 offline_page.creation_time, &matching_url_count, |
| 221 &time_since_most_recent_duplicate)) { |
| 222 // Using CUSTOM_COUNTS instead of time-oriented histogram to record |
| 223 // samples in seconds rather than milliseconds. |
| 224 UMA_HISTOGRAM_CUSTOM_COUNTS( |
| 225 "OfflinePages.DownloadSavedPageTimeSinceDuplicateSaved", |
| 226 time_since_most_recent_duplicate.InSeconds(), |
| 227 base::TimeDelta::FromSeconds(1).InSeconds(), |
| 228 base::TimeDelta::FromDays(7).InSeconds(), 50); |
| 229 } |
| 230 UMA_HISTOGRAM_CUSTOM_COUNTS("OfflinePages.DownloadSavedPageDuplicateCount", |
| 231 matching_url_count, 1, 20, 10); |
| 232 } |
| 171 } | 233 } |
| 172 | 234 |
| 173 void ReportPageHistogramsAfterDelete( | 235 void ReportPageHistogramsAfterDelete( |
| 174 const std::map<int64_t, OfflinePageItem>& offline_pages, | 236 const std::map<int64_t, OfflinePageItem>& offline_pages, |
| 175 const std::vector<int64_t>& deleted_offline_ids) { | 237 const std::vector<int64_t>& deleted_offline_ids) { |
| 176 const int max_minutes = base::TimeDelta::FromDays(365).InMinutes(); | 238 const int max_minutes = base::TimeDelta::FromDays(365).InMinutes(); |
| 177 base::Time now = base::Time::Now(); | 239 base::Time now = base::Time::Now(); |
| 178 int64_t total_size = 0; | 240 int64_t total_size = 0; |
| 179 for (int64_t offline_id : deleted_offline_ids) { | 241 for (int64_t offline_id : deleted_offline_ids) { |
| 180 auto iter = offline_pages.find(offline_id); | 242 auto iter = offline_pages.find(offline_id); |
| 181 if (iter == offline_pages.end()) | 243 if (iter == offline_pages.end()) |
| 182 continue; | 244 continue; |
| 245 |
| 183 total_size += iter->second.file_size; | 246 total_size += iter->second.file_size; |
| 184 ClientId client_id = iter->second.client_id; | 247 ClientId client_id = iter->second.client_id; |
| 185 | 248 |
| 249 if (client_id.name_space == kDownloadNamespace) { |
| 250 int remaining_pages_with_url; |
| 251 GetMatchingURLCountAndMostRecentCreationTime( |
| 252 offline_pages, iter->second.client_id.name_space, iter->second.url, |
| 253 base::Time::Max(), &remaining_pages_with_url, nullptr); |
| 254 UMA_HISTOGRAM_CUSTOM_COUNTS( |
| 255 "OfflinePages.DownloadDeletedPageDuplicateCount", |
| 256 remaining_pages_with_url, 1, 20, 10); |
| 257 } |
| 258 |
| 186 // The histograms below are an expansion of the UMA_HISTOGRAM_CUSTOM_COUNTS | 259 // The histograms below are an expansion of the UMA_HISTOGRAM_CUSTOM_COUNTS |
| 187 // macro adapted to allow for a dynamically suffixed histogram name. | 260 // macro adapted to allow for a dynamically suffixed histogram name. |
| 188 // Note: The factory creates and owns the histogram. | 261 // Note: The factory creates and owns the histogram. |
| 189 base::HistogramBase* histogram = base::Histogram::FactoryGet( | 262 base::HistogramBase* histogram = base::Histogram::FactoryGet( |
| 190 AddHistogramSuffix(client_id, "OfflinePages.PageLifetime"), | 263 AddHistogramSuffix(client_id, "OfflinePages.PageLifetime"), |
| 191 1, max_minutes, 100, base::HistogramBase::kUmaTargetedHistogramFlag); | 264 1, max_minutes, 100, base::HistogramBase::kUmaTargetedHistogramFlag); |
| 192 histogram->Add((now - iter->second.creation_time).InMinutes()); | 265 histogram->Add((now - iter->second.creation_time).InMinutes()); |
| 193 | 266 |
| 194 histogram = base::Histogram::FactoryGet( | 267 histogram = base::Histogram::FactoryGet( |
| 195 AddHistogramSuffix( | 268 AddHistogramSuffix( |
| (...skipping 533 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 729 | 802 |
| 730 void OfflinePageModelImpl::OnAddOfflinePageDone( | 803 void OfflinePageModelImpl::OnAddOfflinePageDone( |
| 731 OfflinePageArchiver* archiver, | 804 OfflinePageArchiver* archiver, |
| 732 const SavePageCallback& callback, | 805 const SavePageCallback& callback, |
| 733 const OfflinePageItem& offline_page, | 806 const OfflinePageItem& offline_page, |
| 734 bool success) { | 807 bool success) { |
| 735 SavePageResult result; | 808 SavePageResult result; |
| 736 if (success) { | 809 if (success) { |
| 737 offline_pages_[offline_page.offline_id] = offline_page; | 810 offline_pages_[offline_page.offline_id] = offline_page; |
| 738 result = SavePageResult::SUCCESS; | 811 result = SavePageResult::SUCCESS; |
| 739 ReportPageHistogramAfterSave(offline_page); | 812 ReportPageHistogramAfterSave(offline_pages_, offline_page); |
| 740 offline_event_logger_.RecordPageSaved( | 813 offline_event_logger_.RecordPageSaved( |
| 741 offline_page.client_id.name_space, offline_page.url.spec(), | 814 offline_page.client_id.name_space, offline_page.url.spec(), |
| 742 std::to_string(offline_page.offline_id)); | 815 std::to_string(offline_page.offline_id)); |
| 743 } else { | 816 } else { |
| 744 result = SavePageResult::STORE_FAILURE; | 817 result = SavePageResult::STORE_FAILURE; |
| 745 } | 818 } |
| 746 InformSavePageDone(callback, result, offline_page.client_id, | 819 InformSavePageDone(callback, result, offline_page.client_id, |
| 747 offline_page.offline_id); | 820 offline_page.offline_id); |
| 748 if (result == SavePageResult::SUCCESS) { | 821 if (result == SavePageResult::SUCCESS) { |
| 749 DeleteExistingPagesWithSameURL(offline_page); | 822 DeleteExistingPagesWithSameURL(offline_page); |
| (...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1070 void OfflinePageModelImpl::RunWhenLoaded(const base::Closure& task) { | 1143 void OfflinePageModelImpl::RunWhenLoaded(const base::Closure& task) { |
| 1071 if (!is_loaded_) { | 1144 if (!is_loaded_) { |
| 1072 delayed_tasks_.push_back(task); | 1145 delayed_tasks_.push_back(task); |
| 1073 return; | 1146 return; |
| 1074 } | 1147 } |
| 1075 | 1148 |
| 1076 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, task); | 1149 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, task); |
| 1077 } | 1150 } |
| 1078 | 1151 |
| 1079 } // namespace offline_pages | 1152 } // namespace offline_pages |
| OLD | NEW |