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

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

Issue 1981093002: [Offline pages] Hooking up Archive Manager to Offline Page Model (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@archive-manager
Patch Set: Fixing the windows bot Created 4 years, 7 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/client_policy_controller.h" 22 #include "components/offline_pages/client_policy_controller.h"
22 #include "components/offline_pages/offline_page_item.h" 23 #include "components/offline_pages/offline_page_item.h"
23 #include "components/offline_pages/offline_page_storage_manager.h" 24 #include "components/offline_pages/offline_page_storage_manager.h"
24 #include "components/offline_pages/proto/offline_pages.pb.h" 25 #include "components/offline_pages/proto/offline_pages.pb.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
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
71 case ArchiverResult::ERROR_SECURITY_CERTIFICATE: 72 case ArchiverResult::ERROR_SECURITY_CERTIFICATE:
72 result = SavePageResult::SECURITY_CERTIFICATE_ERROR; 73 result = SavePageResult::SECURITY_CERTIFICATE_ERROR;
73 break; 74 break;
74 default: 75 default:
75 NOTREACHED(); 76 NOTREACHED();
76 result = SavePageResult::CONTENT_UNAVAILABLE; 77 result = SavePageResult::CONTENT_UNAVAILABLE;
77 } 78 }
78 return result; 79 return result;
79 } 80 }
80 81
81 void DeleteArchiveFiles(const std::vector<base::FilePath>& paths_to_delete,
82 bool* success) {
83 DCHECK(success);
84 for (const auto& file_path : paths_to_delete) {
85 // Make sure delete happens on the left of || so that it is always executed.
86 *success = base::DeleteFile(file_path, false) || *success;
87 }
88 }
89
90 void FindPagesMissingArchiveFile(
91 const std::vector<std::pair<int64_t, base::FilePath>>& id_path_pairs,
92 std::vector<int64_t>* ids_of_pages_missing_archive_file) {
93 DCHECK(ids_of_pages_missing_archive_file);
94
95 for (const auto& id_path : id_path_pairs) {
96 if (!base::PathExists(id_path.second) ||
97 base::DirectoryExists(id_path.second)) {
98 ids_of_pages_missing_archive_file->push_back(id_path.first);
99 }
100 }
101 }
102
103 void EnsureArchivesDirCreated(const base::FilePath& archives_dir) {
104 CHECK(base::CreateDirectory(archives_dir));
105 }
106
107 std::string AddHistogramSuffix(const ClientId& client_id, 82 std::string AddHistogramSuffix(const ClientId& client_id,
108 const char* histogram_name) { 83 const char* histogram_name) {
109 if (client_id.name_space.empty()) { 84 if (client_id.name_space.empty()) {
110 NOTREACHED(); 85 NOTREACHED();
111 return histogram_name; 86 return histogram_name;
112 } 87 }
113 std::string adjusted_histogram_name(histogram_name); 88 std::string adjusted_histogram_name(histogram_name);
114 adjusted_histogram_name += "."; 89 adjusted_histogram_name += ".";
115 adjusted_histogram_name += client_id.name_space; 90 adjusted_histogram_name += client_id.name_space;
116 return adjusted_histogram_name; 91 return adjusted_histogram_name;
(...skipping 10 matching lines...) Expand all
127 OfflinePageModel::OfflinePageModel() 102 OfflinePageModel::OfflinePageModel()
128 : is_loaded_(false), weak_ptr_factory_(this) {} 103 : is_loaded_(false), weak_ptr_factory_(this) {}
129 104
130 OfflinePageModel::OfflinePageModel( 105 OfflinePageModel::OfflinePageModel(
131 std::unique_ptr<OfflinePageMetadataStore> store, 106 std::unique_ptr<OfflinePageMetadataStore> store,
132 const base::FilePath& archives_dir, 107 const base::FilePath& archives_dir,
133 const scoped_refptr<base::SequencedTaskRunner>& task_runner) 108 const scoped_refptr<base::SequencedTaskRunner>& task_runner)
134 : store_(std::move(store)), 109 : store_(std::move(store)),
135 archives_dir_(archives_dir), 110 archives_dir_(archives_dir),
136 is_loaded_(false), 111 is_loaded_(false),
137 task_runner_(task_runner),
138 policy_controller_(new ClientPolicyController()), 112 policy_controller_(new ClientPolicyController()),
113 archive_manager_(new ArchiveManager(archives_dir, task_runner)),
139 weak_ptr_factory_(this) { 114 weak_ptr_factory_(this) {
140 task_runner_->PostTaskAndReply( 115 archive_manager_->EnsureArchivesDirCreated(
141 FROM_HERE, base::Bind(EnsureArchivesDirCreated, archives_dir_),
142 base::Bind(&OfflinePageModel::OnEnsureArchivesDirCreatedDone, 116 base::Bind(&OfflinePageModel::OnEnsureArchivesDirCreatedDone,
143 weak_ptr_factory_.GetWeakPtr(), base::TimeTicks::Now())); 117 weak_ptr_factory_.GetWeakPtr(), base::TimeTicks::Now()));
144 } 118 }
145 119
146 OfflinePageModel::~OfflinePageModel() { 120 OfflinePageModel::~OfflinePageModel() {
147 } 121 }
148 122
149 void OfflinePageModel::AddObserver(Observer* observer) { 123 void OfflinePageModel::AddObserver(Observer* observer) {
150 observers_.AddObserver(observer); 124 observers_.AddObserver(observer);
151 } 125 }
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
244 if (iter != offline_pages_.end()) { 218 if (iter != offline_pages_.end()) {
245 paths_to_delete.push_back(iter->second.file_path); 219 paths_to_delete.push_back(iter->second.file_path);
246 } 220 }
247 } 221 }
248 222
249 if (paths_to_delete.empty()) { 223 if (paths_to_delete.empty()) {
250 InformDeletePageDone(callback, DeletePageResult::NOT_FOUND); 224 InformDeletePageDone(callback, DeletePageResult::NOT_FOUND);
251 return; 225 return;
252 } 226 }
253 227
254 bool* success = new bool(false); 228 archive_manager_->DeleteMultipleArchives(
255 task_runner_->PostTaskAndReply( 229 paths_to_delete,
256 FROM_HERE, base::Bind(&DeleteArchiveFiles, paths_to_delete, success),
257 base::Bind(&OfflinePageModel::OnDeleteArchiveFilesDone, 230 base::Bind(&OfflinePageModel::OnDeleteArchiveFilesDone,
258 weak_ptr_factory_.GetWeakPtr(), offline_ids, callback, 231 weak_ptr_factory_.GetWeakPtr(), offline_ids, callback));
259 base::Owned(success)));
260 } 232 }
261 233
262 void OfflinePageModel::ClearAll(const base::Closure& callback) { 234 void OfflinePageModel::ClearAll(const base::Closure& callback) {
263 DCHECK(is_loaded_); 235 DCHECK(is_loaded_);
264 236
265 std::vector<int64_t> offline_ids; 237 std::vector<int64_t> offline_ids;
266 for (const auto& id_page_pair : offline_pages_) 238 for (const auto& id_page_pair : offline_pages_)
267 offline_ids.push_back(id_page_pair.first); 239 offline_ids.push_back(id_page_pair.first);
268 DeletePagesByOfflineId( 240 DeletePagesByOfflineId(
269 offline_ids, 241 offline_ids,
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
366 base::Bind(&OfflinePageModel::GetOfflineIdsForClientIdWhenLoadDone, 338 base::Bind(&OfflinePageModel::GetOfflineIdsForClientIdWhenLoadDone,
367 weak_ptr_factory_.GetWeakPtr(), client_id, callback)); 339 weak_ptr_factory_.GetWeakPtr(), client_id, callback));
368 } 340 }
369 341
370 void OfflinePageModel::GetOfflineIdsForClientIdWhenLoadDone( 342 void OfflinePageModel::GetOfflineIdsForClientIdWhenLoadDone(
371 const ClientId& client_id, 343 const ClientId& client_id,
372 const MultipleOfflineIdCallback& callback) const { 344 const MultipleOfflineIdCallback& callback) const {
373 callback.Run(MaybeGetOfflineIdsForClientId(client_id)); 345 callback.Run(MaybeGetOfflineIdsForClientId(client_id));
374 } 346 }
375 347
376 // TODO(fgorski): Remove include_deleted, as it no longer makes sense.
377 const std::vector<int64_t> OfflinePageModel::MaybeGetOfflineIdsForClientId( 348 const std::vector<int64_t> OfflinePageModel::MaybeGetOfflineIdsForClientId(
378 const ClientId& client_id) const { 349 const ClientId& client_id) const {
379 DCHECK(is_loaded_); 350 DCHECK(is_loaded_);
380 std::vector<int64_t> results; 351 std::vector<int64_t> results;
381 352
382 // We want only all pages, including those marked for deletion. 353 // We want only all pages, including those marked for deletion.
383 // TODO(bburns): actually use an index rather than linear scan. 354 // TODO(bburns): actually use an index rather than linear scan.
384 for (const auto& id_page_pair : offline_pages_) { 355 for (const auto& id_page_pair : offline_pages_) {
385 if (id_page_pair.second.client_id == client_id) 356 if (id_page_pair.second.client_id == client_id)
386 results.push_back(id_page_pair.second.offline_id); 357 results.push_back(id_page_pair.second.offline_id);
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
493 if (!result || id_page_pair.second.creation_time > result->creation_time) 464 if (!result || id_page_pair.second.creation_time > result->creation_time)
494 result = &(id_page_pair.second); 465 result = &(id_page_pair.second);
495 } 466 }
496 } 467 }
497 return result; 468 return result;
498 } 469 }
499 470
500 void OfflinePageModel::CheckForExternalFileDeletion() { 471 void OfflinePageModel::CheckForExternalFileDeletion() {
501 DCHECK(is_loaded_); 472 DCHECK(is_loaded_);
502 473
503 std::vector<std::pair<int64_t, base::FilePath>> id_path_pairs; 474 archive_manager_->GetAllArchives(
504 for (const auto& id_page_pair : offline_pages_) { 475 base::Bind(&OfflinePageModel::ScanForMissingArchiveFiles,
505 id_path_pairs.push_back( 476 weak_ptr_factory_.GetWeakPtr()));
506 std::make_pair(id_page_pair.first, id_page_pair.second.file_path));
507 }
508
509 std::vector<int64_t>* ids_of_pages_missing_archive_file =
510 new std::vector<int64_t>();
511 task_runner_->PostTaskAndReply(
512 FROM_HERE, base::Bind(&FindPagesMissingArchiveFile, id_path_pairs,
513 ids_of_pages_missing_archive_file),
514 base::Bind(&OfflinePageModel::OnFindPagesMissingArchiveFile,
515 weak_ptr_factory_.GetWeakPtr(),
516 base::Owned(ids_of_pages_missing_archive_file)));
517 } 477 }
518 478
519 void OfflinePageModel::RecordStorageHistograms(int64_t total_space_bytes, 479 void OfflinePageModel::RecordStorageHistograms(int64_t total_space_bytes,
520 int64_t free_space_bytes, 480 int64_t free_space_bytes,
521 bool reporting_after_delete) { 481 bool reporting_after_delete) {
522 // Total space taken by offline pages. 482 // Total space taken by offline pages.
523 int64_t total_page_size = 0; 483 int64_t total_page_size = 0;
524 for (const auto& id_page_pair : offline_pages_) { 484 for (const auto& id_page_pair : offline_pages_) {
525 total_page_size += id_page_pair.second.file_size; 485 total_page_size += id_page_pair.second.file_size;
526 } 486 }
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after
683 } 643 }
684 644
685 void OfflinePageModel::DeletePendingArchiver(OfflinePageArchiver* archiver) { 645 void OfflinePageModel::DeletePendingArchiver(OfflinePageArchiver* archiver) {
686 pending_archivers_.erase(std::find( 646 pending_archivers_.erase(std::find(
687 pending_archivers_.begin(), pending_archivers_.end(), archiver)); 647 pending_archivers_.begin(), pending_archivers_.end(), archiver));
688 } 648 }
689 649
690 void OfflinePageModel::OnDeleteArchiveFilesDone( 650 void OfflinePageModel::OnDeleteArchiveFilesDone(
691 const std::vector<int64_t>& offline_ids, 651 const std::vector<int64_t>& offline_ids,
692 const DeletePageCallback& callback, 652 const DeletePageCallback& callback,
693 const bool* success) { 653 bool success) {
694 DCHECK(success); 654 if (!success) {
695
696 if (!*success) {
697 InformDeletePageDone(callback, DeletePageResult::DEVICE_FAILURE); 655 InformDeletePageDone(callback, DeletePageResult::DEVICE_FAILURE);
698 return; 656 return;
699 } 657 }
700 658
701 store_->RemoveOfflinePages( 659 store_->RemoveOfflinePages(
702 offline_ids, 660 offline_ids,
703 base::Bind(&OfflinePageModel::OnRemoveOfflinePagesDone, 661 base::Bind(&OfflinePageModel::OnRemoveOfflinePagesDone,
704 weak_ptr_factory_.GetWeakPtr(), offline_ids, callback)); 662 weak_ptr_factory_.GetWeakPtr(), offline_ids, callback));
705 } 663 }
706 664
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
766 void OfflinePageModel::InformDeletePageDone(const DeletePageCallback& callback, 724 void OfflinePageModel::InformDeletePageDone(const DeletePageCallback& callback,
767 DeletePageResult result) { 725 DeletePageResult result) {
768 UMA_HISTOGRAM_ENUMERATION( 726 UMA_HISTOGRAM_ENUMERATION(
769 "OfflinePages.DeletePageResult", 727 "OfflinePages.DeletePageResult",
770 static_cast<int>(result), 728 static_cast<int>(result),
771 static_cast<int>(DeletePageResult::RESULT_COUNT)); 729 static_cast<int>(DeletePageResult::RESULT_COUNT));
772 if (!callback.is_null()) 730 if (!callback.is_null())
773 callback.Run(result); 731 callback.Run(result);
774 } 732 }
775 733
776 void OfflinePageModel::OnFindPagesMissingArchiveFile( 734 void OfflinePageModel::ScanForMissingArchiveFiles(
777 const std::vector<int64_t>* ids_of_pages_missing_archive_file) { 735 const std::set<base::FilePath>& archive_paths) {
778 DCHECK(ids_of_pages_missing_archive_file); 736 std::vector<int64_t> ids_of_pages_missing_archive_file;
779 if (ids_of_pages_missing_archive_file->empty())
780 return;
781
782 std::vector<std::pair<int64_t, ClientId>> offline_client_id_pairs; 737 std::vector<std::pair<int64_t, ClientId>> offline_client_id_pairs;
783 for (auto offline_id : *ids_of_pages_missing_archive_file) { 738 for (const auto& id_page_pair : offline_pages_) {
784 // Since we might have deleted pages in between so we have to purge 739 if (archive_paths.count(id_page_pair.second.file_path) == 0UL) {
785 // the list to make sure we still care about them. 740 ids_of_pages_missing_archive_file.push_back(id_page_pair.first);
786 auto iter = offline_pages_.find(offline_id);
787 if (iter != offline_pages_.end()) {
788 offline_client_id_pairs.push_back( 741 offline_client_id_pairs.push_back(
789 std::make_pair(offline_id, iter->second.client_id)); 742 std::make_pair(id_page_pair.first, id_page_pair.second.client_id));
790 } 743 }
791 } 744 }
792 745
793 DeletePageCallback done_callback( 746 // No offline pages missing archive files, we can bail out.
747 if (ids_of_pages_missing_archive_file.empty())
748 return;
749
750 DeletePageCallback remove_pages_done_callback(
794 base::Bind(&OfflinePageModel::OnRemoveOfflinePagesMissingArchiveFileDone, 751 base::Bind(&OfflinePageModel::OnRemoveOfflinePagesMissingArchiveFileDone,
795 weak_ptr_factory_.GetWeakPtr(), offline_client_id_pairs)); 752 weak_ptr_factory_.GetWeakPtr(), offline_client_id_pairs));
796 753
797 store_->RemoveOfflinePages( 754 store_->RemoveOfflinePages(
798 *ids_of_pages_missing_archive_file, 755 ids_of_pages_missing_archive_file,
799 base::Bind(&OfflinePageModel::OnRemoveOfflinePagesDone, 756 base::Bind(&OfflinePageModel::OnRemoveOfflinePagesDone,
800 weak_ptr_factory_.GetWeakPtr(), 757 weak_ptr_factory_.GetWeakPtr(),
801 *ids_of_pages_missing_archive_file, 758 ids_of_pages_missing_archive_file,
802 done_callback)); 759 remove_pages_done_callback));
803 } 760 }
804 761
805 void OfflinePageModel::OnRemoveOfflinePagesMissingArchiveFileDone( 762 void OfflinePageModel::OnRemoveOfflinePagesMissingArchiveFileDone(
806 const std::vector<std::pair<int64_t, ClientId>>& offline_client_id_pairs, 763 const std::vector<std::pair<int64_t, ClientId>>& offline_client_id_pairs,
807 DeletePageResult /* result */) { 764 DeletePageResult /* result */) {
808 for (const auto& id_pair : offline_client_id_pairs) { 765 for (const auto& id_pair : offline_client_id_pairs) {
809 FOR_EACH_OBSERVER(Observer, observers_, 766 FOR_EACH_OBSERVER(Observer, observers_,
810 OfflinePageDeleted(id_pair.first, id_pair.second)); 767 OfflinePageDeleted(id_pair.first, id_pair.second));
811 } 768 }
812 } 769 }
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
863 void OfflinePageModel::RunWhenLoaded(const base::Closure& task) { 820 void OfflinePageModel::RunWhenLoaded(const base::Closure& task) {
864 if (!is_loaded_) { 821 if (!is_loaded_) {
865 delayed_tasks_.push_back(task); 822 delayed_tasks_.push_back(task);
866 return; 823 return;
867 } 824 }
868 825
869 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, task); 826 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, task);
870 } 827 }
871 828
872 } // namespace offline_pages 829 } // namespace offline_pages
OLDNEW
« no previous file with comments | « components/offline_pages/offline_page_model.h ('k') | components/offline_pages/offline_page_model_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698