Chromium Code Reviews| 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" |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 96 base::DirectoryExists(id_path.second)) { | 96 base::DirectoryExists(id_path.second)) { |
| 97 ids_of_pages_missing_archive_file->push_back(id_path.first); | 97 ids_of_pages_missing_archive_file->push_back(id_path.first); |
| 98 } | 98 } |
| 99 } | 99 } |
| 100 } | 100 } |
| 101 | 101 |
| 102 void EnsureArchivesDirCreated(const base::FilePath& archives_dir) { | 102 void EnsureArchivesDirCreated(const base::FilePath& archives_dir) { |
| 103 CHECK(base::CreateDirectory(archives_dir)); | 103 CHECK(base::CreateDirectory(archives_dir)); |
| 104 } | 104 } |
| 105 | 105 |
| 106 std::string MightAddHistogramSuffix(const ClientId& client_id, | |
| 107 const char* histogram_name) { | |
| 108 if (client_id.name_space.empty()) { | |
| 109 NOTREACHED(); | |
| 110 return histogram_name; | |
| 111 } | |
| 112 std::string adjusted_histogram_name; | |
|
fgorski
2016/05/16 16:20:04
std::string adjusted_histogram_name(histogram_name
jianli
2016/05/16 22:35:25
Done.
| |
| 113 adjusted_histogram_name += "."; | |
| 114 adjusted_histogram_name += client_id.name_space; | |
| 115 return adjusted_histogram_name; | |
| 116 } | |
| 117 | |
| 106 } // namespace | 118 } // namespace |
| 107 | 119 |
| 108 // static | 120 // static |
| 109 bool OfflinePageModel::CanSavePage(const GURL& url) { | 121 bool OfflinePageModel::CanSavePage(const GURL& url) { |
| 110 return url.SchemeIsHTTPOrHTTPS(); | 122 return url.SchemeIsHTTPOrHTTPS(); |
| 111 } | 123 } |
| 112 | 124 |
| 113 OfflinePageModel::OfflinePageModel( | 125 OfflinePageModel::OfflinePageModel( |
| 114 std::unique_ptr<OfflinePageMetadataStore> store, | 126 std::unique_ptr<OfflinePageMetadataStore> store, |
| 115 const base::FilePath& archives_dir, | 127 const base::FilePath& archives_dir, |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 138 | 150 |
| 139 void OfflinePageModel::SavePage(const GURL& url, | 151 void OfflinePageModel::SavePage(const GURL& url, |
| 140 const ClientId& client_id, | 152 const ClientId& client_id, |
| 141 std::unique_ptr<OfflinePageArchiver> archiver, | 153 std::unique_ptr<OfflinePageArchiver> archiver, |
| 142 const SavePageCallback& callback) { | 154 const SavePageCallback& callback) { |
| 143 DCHECK(is_loaded_); | 155 DCHECK(is_loaded_); |
| 144 | 156 |
| 145 // Skip saving the page that is not intended to be saved, like local file | 157 // Skip saving the page that is not intended to be saved, like local file |
| 146 // page. | 158 // page. |
| 147 if (!CanSavePage(url)) { | 159 if (!CanSavePage(url)) { |
| 148 InformSavePageDone(callback, SavePageResult::SKIPPED, kInvalidOfflineId); | 160 InformSavePageDone(callback, SavePageResult::SKIPPED, client_id, |
| 161 kInvalidOfflineId); | |
| 149 return; | 162 return; |
| 150 } | 163 } |
| 151 | 164 |
| 152 DCHECK(archiver.get()); | 165 DCHECK(archiver.get()); |
| 153 | 166 |
| 154 int64_t offline_id = GenerateOfflineId(); | 167 int64_t offline_id = GenerateOfflineId(); |
| 155 | 168 |
| 156 archiver->CreateArchive( | 169 archiver->CreateArchive( |
| 157 archives_dir_, | 170 archives_dir_, |
| 158 offline_id, | 171 offline_id, |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 169 return; | 182 return; |
| 170 | 183 |
| 171 // Make a copy of the cached item and update it. The cached item should only | 184 // Make a copy of the cached item and update it. The cached item should only |
| 172 // be updated upon the successful store operation. | 185 // be updated upon the successful store operation. |
| 173 OfflinePageItem offline_page_item = iter->second; | 186 OfflinePageItem offline_page_item = iter->second; |
| 174 | 187 |
| 175 base::Time now = base::Time::Now(); | 188 base::Time now = base::Time::Now(); |
| 176 base::TimeDelta time_since_last_accessed = | 189 base::TimeDelta time_since_last_accessed = |
| 177 now - offline_page_item.last_access_time; | 190 now - offline_page_item.last_access_time; |
| 178 | 191 |
| 179 // The last access time was set to same as creation time when the page was | 192 // When the access account is still zero, the page is opened for the first |
| 180 // created. | 193 // time since its creation. |
| 181 if (offline_page_item.creation_time == offline_page_item.last_access_time) { | 194 UMA_HISTOGRAM_CUSTOM_COUNTS( |
| 182 UMA_HISTOGRAM_CUSTOM_COUNTS("OfflinePages.FirstOpenSinceCreated", | 195 MightAddHistogramSuffix( |
| 183 time_since_last_accessed.InMinutes(), 1, | 196 offline_page_item.client_id, |
| 184 kMaxOpenedPageHistogramBucket.InMinutes(), 50); | 197 (offline_page_item.access_count == 0) ? |
| 185 } else { | 198 "OfflinePages.FirstOpenSinceCreated" : |
| 186 UMA_HISTOGRAM_CUSTOM_COUNTS("OfflinePages.OpenSinceLastOpen", | 199 "OfflinePages.OpenSinceLastOpen").c_str(), |
| 187 time_since_last_accessed.InMinutes(), 1, | 200 time_since_last_accessed.InMinutes(), 1, |
| 188 kMaxOpenedPageHistogramBucket.InMinutes(), 50); | 201 kMaxOpenedPageHistogramBucket.InMinutes(), 50); |
| 189 } | |
| 190 | 202 |
| 191 offline_page_item.last_access_time = now; | 203 offline_page_item.last_access_time = now; |
| 192 offline_page_item.access_count++; | 204 offline_page_item.access_count++; |
| 193 | 205 |
| 194 store_->AddOrUpdateOfflinePage( | 206 store_->AddOrUpdateOfflinePage( |
| 195 offline_page_item, | 207 offline_page_item, |
| 196 base::Bind(&OfflinePageModel::OnMarkPageAccesseDone, | 208 base::Bind(&OfflinePageModel::OnMarkPageAccesseDone, |
| 197 weak_ptr_factory_.GetWeakPtr(), offline_page_item)); | 209 weak_ptr_factory_.GetWeakPtr(), offline_page_item)); |
| 198 } | 210 } |
| 199 | 211 |
| (...skipping 312 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 512 OfflinePageArchiver* archiver, | 524 OfflinePageArchiver* archiver, |
| 513 ArchiverResult archiver_result, | 525 ArchiverResult archiver_result, |
| 514 const GURL& url, | 526 const GURL& url, |
| 515 const base::FilePath& file_path, | 527 const base::FilePath& file_path, |
| 516 int64_t file_size) { | 528 int64_t file_size) { |
| 517 if (requested_url != url) { | 529 if (requested_url != url) { |
| 518 DVLOG(1) << "Saved URL does not match requested URL."; | 530 DVLOG(1) << "Saved URL does not match requested URL."; |
| 519 // TODO(fgorski): We have created an archive for a wrong URL. It should be | 531 // TODO(fgorski): We have created an archive for a wrong URL. It should be |
| 520 // deleted from here, once archiver has the right functionality. | 532 // deleted from here, once archiver has the right functionality. |
| 521 InformSavePageDone(callback, SavePageResult::ARCHIVE_CREATION_FAILED, | 533 InformSavePageDone(callback, SavePageResult::ARCHIVE_CREATION_FAILED, |
| 522 offline_id); | 534 client_id, offline_id); |
| 523 DeletePendingArchiver(archiver); | 535 DeletePendingArchiver(archiver); |
| 524 return; | 536 return; |
| 525 } | 537 } |
| 526 | 538 |
| 527 if (archiver_result != ArchiverResult::SUCCESSFULLY_CREATED) { | 539 if (archiver_result != ArchiverResult::SUCCESSFULLY_CREATED) { |
| 528 SavePageResult result = ToSavePageResult(archiver_result); | 540 SavePageResult result = ToSavePageResult(archiver_result); |
| 529 InformSavePageDone(callback, result, offline_id); | 541 InformSavePageDone(callback, result, client_id, offline_id); |
| 530 DeletePendingArchiver(archiver); | 542 DeletePendingArchiver(archiver); |
| 531 return; | 543 return; |
| 532 } | 544 } |
| 533 OfflinePageItem offline_page_item(url, offline_id, client_id, file_path, | 545 OfflinePageItem offline_page_item(url, offline_id, client_id, file_path, |
| 534 file_size, start_time); | 546 file_size, start_time); |
| 535 store_->AddOrUpdateOfflinePage( | 547 store_->AddOrUpdateOfflinePage( |
| 536 offline_page_item, | 548 offline_page_item, |
| 537 base::Bind(&OfflinePageModel::OnAddOfflinePageDone, | 549 base::Bind(&OfflinePageModel::OnAddOfflinePageDone, |
| 538 weak_ptr_factory_.GetWeakPtr(), archiver, callback, | 550 weak_ptr_factory_.GetWeakPtr(), archiver, callback, |
| 539 offline_page_item)); | 551 offline_page_item)); |
| 540 } | 552 } |
| 541 | 553 |
| 542 void OfflinePageModel::OnAddOfflinePageDone(OfflinePageArchiver* archiver, | 554 void OfflinePageModel::OnAddOfflinePageDone(OfflinePageArchiver* archiver, |
| 543 const SavePageCallback& callback, | 555 const SavePageCallback& callback, |
| 544 const OfflinePageItem& offline_page, | 556 const OfflinePageItem& offline_page, |
| 545 bool success) { | 557 bool success) { |
| 546 SavePageResult result; | 558 SavePageResult result; |
| 547 if (success) { | 559 if (success) { |
| 548 offline_pages_[offline_page.offline_id] = offline_page; | 560 offline_pages_[offline_page.offline_id] = offline_page; |
| 549 result = SavePageResult::SUCCESS; | 561 result = SavePageResult::SUCCESS; |
| 550 UMA_HISTOGRAM_TIMES( | 562 UMA_HISTOGRAM_TIMES( |
| 551 "OfflinePages.SavePageTime", | 563 MightAddHistogramSuffix( |
| 564 offline_page.client_id, "OfflinePages.SavePageTime").c_str(), | |
| 552 base::Time::Now() - offline_page.creation_time); | 565 base::Time::Now() - offline_page.creation_time); |
| 553 UMA_HISTOGRAM_MEMORY_KB( | 566 UMA_HISTOGRAM_MEMORY_KB( |
| 554 "OfflinePages.PageSize", offline_page.file_size / 1024); | 567 MightAddHistogramSuffix( |
| 568 offline_page.client_id, "OfflinePages.PageSize").c_str(), | |
| 569 offline_page.file_size / 1024); | |
| 555 } else { | 570 } else { |
| 556 result = SavePageResult::STORE_FAILURE; | 571 result = SavePageResult::STORE_FAILURE; |
| 557 } | 572 } |
| 558 InformSavePageDone(callback, result, offline_page.offline_id); | 573 InformSavePageDone(callback, result, offline_page.client_id, |
| 574 offline_page.offline_id); | |
| 559 DeletePendingArchiver(archiver); | 575 DeletePendingArchiver(archiver); |
| 560 | 576 |
| 561 FOR_EACH_OBSERVER(Observer, observers_, OfflinePageModelChanged(this)); | 577 FOR_EACH_OBSERVER(Observer, observers_, OfflinePageModelChanged(this)); |
| 562 } | 578 } |
| 563 | 579 |
| 564 void OfflinePageModel::OnMarkPageAccesseDone( | 580 void OfflinePageModel::OnMarkPageAccesseDone( |
| 565 const OfflinePageItem& offline_page_item, bool success) { | 581 const OfflinePageItem& offline_page_item, bool success) { |
| 566 // Update the item in the cache only upon success. | 582 // Update the item in the cache only upon success. |
| 567 if (success) | 583 if (success) |
| 568 offline_pages_[offline_page_item.offline_id] = offline_page_item; | 584 offline_pages_[offline_page_item.offline_id] = offline_page_item; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 600 delayed_task.Run(); | 616 delayed_task.Run(); |
| 601 delayed_tasks_.clear(); | 617 delayed_tasks_.clear(); |
| 602 | 618 |
| 603 FOR_EACH_OBSERVER(Observer, observers_, OfflinePageModelLoaded(this)); | 619 FOR_EACH_OBSERVER(Observer, observers_, OfflinePageModelLoaded(this)); |
| 604 | 620 |
| 605 CheckForExternalFileDeletion(); | 621 CheckForExternalFileDeletion(); |
| 606 } | 622 } |
| 607 | 623 |
| 608 void OfflinePageModel::InformSavePageDone(const SavePageCallback& callback, | 624 void OfflinePageModel::InformSavePageDone(const SavePageCallback& callback, |
| 609 SavePageResult result, | 625 SavePageResult result, |
| 626 const ClientId& client_id, | |
| 610 int64_t offline_id) { | 627 int64_t offline_id) { |
| 611 UMA_HISTOGRAM_ENUMERATION( | 628 UMA_HISTOGRAM_ENUMERATION( |
| 612 "OfflinePages.SavePageResult", | 629 MightAddHistogramSuffix(client_id, "OfflinePages.SavePageResult").c_str(), |
| 613 static_cast<int>(result), | 630 static_cast<int>(result), |
| 614 static_cast<int>(SavePageResult::RESULT_COUNT)); | 631 static_cast<int>(SavePageResult::RESULT_COUNT)); |
| 615 callback.Run(result, offline_id); | 632 callback.Run(result, offline_id); |
| 616 } | 633 } |
| 617 | 634 |
| 618 void OfflinePageModel::DeletePendingArchiver(OfflinePageArchiver* archiver) { | 635 void OfflinePageModel::DeletePendingArchiver(OfflinePageArchiver* archiver) { |
| 619 pending_archivers_.erase(std::find( | 636 pending_archivers_.erase(std::find( |
| 620 pending_archivers_.begin(), pending_archivers_.end(), archiver)); | 637 pending_archivers_.begin(), pending_archivers_.end(), archiver)); |
| 621 } | 638 } |
| 622 | 639 |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 643 bool success) { | 660 bool success) { |
| 644 // Delete the offline page from the in memory cache regardless of success in | 661 // Delete the offline page from the in memory cache regardless of success in |
| 645 // store. | 662 // store. |
| 646 base::Time now = base::Time::Now(); | 663 base::Time now = base::Time::Now(); |
| 647 int64_t total_size = 0; | 664 int64_t total_size = 0; |
| 648 for (int64_t offline_id : offline_ids) { | 665 for (int64_t offline_id : offline_ids) { |
| 649 auto iter = offline_pages_.find(offline_id); | 666 auto iter = offline_pages_.find(offline_id); |
| 650 if (iter == offline_pages_.end()) | 667 if (iter == offline_pages_.end()) |
| 651 continue; | 668 continue; |
| 652 total_size += iter->second.file_size; | 669 total_size += iter->second.file_size; |
| 670 ClientId client_id = iter->second.client_id; | |
| 653 UMA_HISTOGRAM_CUSTOM_COUNTS( | 671 UMA_HISTOGRAM_CUSTOM_COUNTS( |
| 654 "OfflinePages.PageLifetime", | 672 MightAddHistogramSuffix( |
| 673 client_id, "OfflinePages.PageLifetime").c_str(), | |
| 655 (now - iter->second.creation_time).InMinutes(), | 674 (now - iter->second.creation_time).InMinutes(), |
| 656 1, | 675 1, |
| 657 base::TimeDelta::FromDays(365).InMinutes(), | 676 base::TimeDelta::FromDays(365).InMinutes(), |
| 658 100); | 677 100); |
| 659 UMA_HISTOGRAM_CUSTOM_COUNTS( | 678 UMA_HISTOGRAM_CUSTOM_COUNTS( |
| 660 "OfflinePages.DeletePage.TimeSinceLastOpen", | 679 MightAddHistogramSuffix( |
| 680 client_id, "OfflinePages.DeletePage.TimeSinceLastOpen").c_str(), | |
| 661 (now - iter->second.last_access_time).InMinutes(), | 681 (now - iter->second.last_access_time).InMinutes(), |
| 662 1, | 682 1, |
| 663 base::TimeDelta::FromDays(365).InMinutes(), | 683 base::TimeDelta::FromDays(365).InMinutes(), |
| 664 100); | 684 100); |
| 665 UMA_HISTOGRAM_CUSTOM_COUNTS( | 685 UMA_HISTOGRAM_CUSTOM_COUNTS( |
| 666 "OfflinePages.DeletePage.LastOpenToCreated", | 686 MightAddHistogramSuffix( |
| 687 client_id, "OfflinePages.DeletePage.LastOpenToCreated").c_str(), | |
| 667 (iter->second.last_access_time - iter->second.creation_time). | 688 (iter->second.last_access_time - iter->second.creation_time). |
| 668 InMinutes(), | 689 InMinutes(), |
| 669 1, | 690 1, |
| 670 base::TimeDelta::FromDays(365).InMinutes(), | 691 base::TimeDelta::FromDays(365).InMinutes(), |
| 671 100); | 692 100); |
| 672 UMA_HISTOGRAM_MEMORY_KB( | 693 UMA_HISTOGRAM_MEMORY_KB( |
| 673 "OfflinePages.DeletePage.PageSize", iter->second.file_size / 1024); | 694 MightAddHistogramSuffix( |
| 695 client_id, "OfflinePages.DeletePage.PageSize").c_str(), | |
| 696 iter->second.file_size / 1024); | |
| 674 UMA_HISTOGRAM_COUNTS( | 697 UMA_HISTOGRAM_COUNTS( |
| 675 "OfflinePages.DeletePage.AccessCount", iter->second.access_count); | 698 MightAddHistogramSuffix( |
| 699 client_id, "OfflinePages.DeletePage.AccessCount").c_str(), | |
| 700 iter->second.access_count); | |
| 676 FOR_EACH_OBSERVER( | 701 FOR_EACH_OBSERVER( |
| 677 Observer, observers_, | 702 Observer, observers_, |
| 678 OfflinePageDeleted(iter->second.offline_id, iter->second.client_id)); | 703 OfflinePageDeleted(iter->second.offline_id, iter->second.client_id)); |
| 679 offline_pages_.erase(iter); | 704 offline_pages_.erase(iter); |
| 680 } | 705 } |
| 681 if (offline_ids.size() > 1) { | 706 if (offline_ids.size() > 1) { |
| 682 UMA_HISTOGRAM_COUNTS("OfflinePages.BatchDelete.Count", offline_ids.size()); | 707 UMA_HISTOGRAM_COUNTS("OfflinePages.BatchDelete.Count", offline_ids.size()); |
| 683 UMA_HISTOGRAM_MEMORY_KB( | 708 UMA_HISTOGRAM_MEMORY_KB( |
| 684 "OfflinePages.BatchDelete.TotalPageSize", total_size / 1024); | 709 "OfflinePages.BatchDelete.TotalPageSize", total_size / 1024); |
| 685 } | 710 } |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 789 void OfflinePageModel::RunWhenLoaded(const base::Closure& task) { | 814 void OfflinePageModel::RunWhenLoaded(const base::Closure& task) { |
| 790 if (!is_loaded_) { | 815 if (!is_loaded_) { |
| 791 delayed_tasks_.push_back(task); | 816 delayed_tasks_.push_back(task); |
| 792 return; | 817 return; |
| 793 } | 818 } |
| 794 | 819 |
| 795 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, task); | 820 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, task); |
| 796 } | 821 } |
| 797 | 822 |
| 798 } // namespace offline_pages | 823 } // namespace offline_pages |
| OLD | NEW |