Chromium Code Reviews| 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 323 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 334 return; | 334 return; |
| 335 } | 335 } |
| 336 DoDeletePagesByOfflineId(offline_ids, callback); | 336 DoDeletePagesByOfflineId(offline_ids, callback); |
| 337 } | 337 } |
| 338 | 338 |
| 339 void OfflinePageModelImpl::DoDeletePagesByOfflineId( | 339 void OfflinePageModelImpl::DoDeletePagesByOfflineId( |
| 340 const std::vector<int64_t>& offline_ids, | 340 const std::vector<int64_t>& offline_ids, |
| 341 const DeletePageCallback& callback) { | 341 const DeletePageCallback& callback) { |
| 342 DCHECK(is_loaded_); | 342 DCHECK(is_loaded_); |
| 343 | 343 |
| 344 // If an empty list of offline id is passed in, consider deletion as success. | |
| 345 if (offline_ids.empty()) { | |
| 346 InformDeletePageDone(callback, DeletePageResult::SUCCESS); | |
| 347 return; | |
| 348 } | |
| 349 | |
| 344 std::vector<base::FilePath> paths_to_delete; | 350 std::vector<base::FilePath> paths_to_delete; |
| 345 for (const auto& offline_id : offline_ids) { | 351 for (const auto& offline_id : offline_ids) { |
| 346 auto iter = offline_pages_.find(offline_id); | 352 auto iter = offline_pages_.find(offline_id); |
| 347 if (iter != offline_pages_.end() && !iter->second.IsExpired()) { | 353 if (iter != offline_pages_.end() && !iter->second.IsExpired()) { |
| 348 paths_to_delete.push_back(iter->second.file_path); | 354 paths_to_delete.push_back(iter->second.file_path); |
| 349 } | 355 } |
| 350 } | 356 } |
| 351 | 357 |
| 358 // Shortcut here, if none of the offline id is found and no other valid | |
|
fgorski
2016/06/21 18:02:23
Too long of the comment.
If there are no pages to
romax
2016/06/21 19:41:37
Done.
| |
| 359 // offline page to delete, just invoke the callback as not found. But if only | |
| 360 // some of the pages are not found, will go through the process and delete the | |
| 361 // valid ones, and invoke as not found when deletion completed. | |
| 352 if (paths_to_delete.empty()) { | 362 if (paths_to_delete.empty()) { |
| 353 InformDeletePageDone(callback, DeletePageResult::NOT_FOUND); | 363 InformDeletePageDone(callback, DeletePageResult::NOT_FOUND); |
| 354 return; | 364 return; |
| 355 } | 365 } |
| 356 | 366 |
| 357 archive_manager_->DeleteMultipleArchives( | 367 archive_manager_->DeleteMultipleArchives( |
| 358 paths_to_delete, | 368 paths_to_delete, |
| 359 base::Bind(&OfflinePageModelImpl::OnDeleteArchiveFilesDone, | 369 base::Bind(&OfflinePageModelImpl::OnDeleteArchiveFilesDone, |
| 360 weak_ptr_factory_.GetWeakPtr(), offline_ids, callback)); | 370 weak_ptr_factory_.GetWeakPtr(), offline_ids, callback)); |
| 361 } | 371 } |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 388 void OfflinePageModelImpl::DoDeletePagesByURLPredicate( | 398 void OfflinePageModelImpl::DoDeletePagesByURLPredicate( |
| 389 const UrlPredicate& predicate, | 399 const UrlPredicate& predicate, |
| 390 const DeletePageCallback& callback) { | 400 const DeletePageCallback& callback) { |
| 391 DCHECK(is_loaded_); | 401 DCHECK(is_loaded_); |
| 392 | 402 |
| 393 std::vector<int64_t> offline_ids; | 403 std::vector<int64_t> offline_ids; |
| 394 for (const auto& id_page_pair : offline_pages_) { | 404 for (const auto& id_page_pair : offline_pages_) { |
| 395 if (predicate.Run(id_page_pair.second.url)) | 405 if (predicate.Run(id_page_pair.second.url)) |
| 396 offline_ids.push_back(id_page_pair.first); | 406 offline_ids.push_back(id_page_pair.first); |
| 397 } | 407 } |
| 398 DoDeletePagesByOfflineId(offline_ids, callback); | 408 // If no pages found by |predicate|, will invoke callback with not found. |
| 409 if (offline_ids.empty()) { | |
| 410 InformDeletePageDone(callback, DeletePageResult::NOT_FOUND); | |
| 411 } else { | |
| 412 DoDeletePagesByOfflineId(offline_ids, callback); | |
| 413 } | |
| 399 } | 414 } |
| 400 | 415 |
| 401 void OfflinePageModelImpl::HasPages(const std::string& name_space, | 416 void OfflinePageModelImpl::HasPages(const std::string& name_space, |
| 402 const HasPagesCallback& callback) { | 417 const HasPagesCallback& callback) { |
| 403 RunWhenLoaded(base::Bind(&OfflinePageModelImpl::HasPagesAfterLoadDone, | 418 RunWhenLoaded(base::Bind(&OfflinePageModelImpl::HasPagesAfterLoadDone, |
| 404 weak_ptr_factory_.GetWeakPtr(), name_space, | 419 weak_ptr_factory_.GetWeakPtr(), name_space, |
| 405 callback)); | 420 callback)); |
| 406 } | 421 } |
| 407 | 422 |
| 408 void OfflinePageModelImpl::HasPagesAfterLoadDone( | 423 void OfflinePageModelImpl::HasPagesAfterLoadDone( |
| (...skipping 417 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 826 } | 841 } |
| 827 | 842 |
| 828 void OfflinePageModelImpl::OnRemoveOfflinePagesDone( | 843 void OfflinePageModelImpl::OnRemoveOfflinePagesDone( |
| 829 const std::vector<int64_t>& offline_ids, | 844 const std::vector<int64_t>& offline_ids, |
| 830 const DeletePageCallback& callback, | 845 const DeletePageCallback& callback, |
| 831 bool success) { | 846 bool success) { |
| 832 ReportPageHistogramsAfterDelete(offline_pages_, offline_ids); | 847 ReportPageHistogramsAfterDelete(offline_pages_, offline_ids); |
| 833 | 848 |
| 834 // Delete the offline page from the in memory cache regardless of success in | 849 // Delete the offline page from the in memory cache regardless of success in |
| 835 // store. | 850 // store. |
| 851 bool page_not_found = false; | |
|
fgorski
2016/06/21 18:02:23
I don't think we need this logic.
If you identifie
romax
2016/06/21 19:41:37
Done.
| |
| 836 for (int64_t offline_id : offline_ids) { | 852 for (int64_t offline_id : offline_ids) { |
| 837 auto iter = offline_pages_.find(offline_id); | 853 auto iter = offline_pages_.find(offline_id); |
| 838 if (iter == offline_pages_.end()) | 854 if (iter == offline_pages_.end()) { |
| 855 page_not_found = true; | |
| 839 continue; | 856 continue; |
| 857 } | |
| 840 FOR_EACH_OBSERVER( | 858 FOR_EACH_OBSERVER( |
| 841 Observer, observers_, | 859 Observer, observers_, |
| 842 OfflinePageDeleted(iter->second.offline_id, iter->second.client_id)); | 860 OfflinePageDeleted(iter->second.offline_id, iter->second.client_id)); |
| 843 offline_pages_.erase(iter); | 861 offline_pages_.erase(iter); |
| 844 } | 862 } |
| 863 | |
| 864 if (page_not_found) | |
| 865 InformDeletePageDone(callback, DeletePageResult::NOT_FOUND); | |
| 866 | |
| 845 // Deleting multiple pages always succeeds when it gets to this point. | 867 // Deleting multiple pages always succeeds when it gets to this point. |
| 846 InformDeletePageDone(callback, (success || offline_ids.size() > 1) | 868 InformDeletePageDone(callback, (success || offline_ids.size() > 1) |
| 847 ? DeletePageResult::SUCCESS | 869 ? DeletePageResult::SUCCESS |
| 848 : DeletePageResult::STORE_FAILURE); | 870 : DeletePageResult::STORE_FAILURE); |
| 849 } | 871 } |
| 850 | 872 |
| 851 void OfflinePageModelImpl::InformDeletePageDone( | 873 void OfflinePageModelImpl::InformDeletePageDone( |
| 852 const DeletePageCallback& callback, | 874 const DeletePageCallback& callback, |
| 853 DeletePageResult result) { | 875 DeletePageResult result) { |
| 854 UMA_HISTOGRAM_ENUMERATION("OfflinePages.DeletePageResult", | 876 UMA_HISTOGRAM_ENUMERATION("OfflinePages.DeletePageResult", |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 983 void OfflinePageModelImpl::RunWhenLoaded(const base::Closure& task) { | 1005 void OfflinePageModelImpl::RunWhenLoaded(const base::Closure& task) { |
| 984 if (!is_loaded_) { | 1006 if (!is_loaded_) { |
| 985 delayed_tasks_.push_back(task); | 1007 delayed_tasks_.push_back(task); |
| 986 return; | 1008 return; |
| 987 } | 1009 } |
| 988 | 1010 |
| 989 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, task); | 1011 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, task); |
| 990 } | 1012 } |
| 991 | 1013 |
| 992 } // namespace offline_pages | 1014 } // namespace offline_pages |
| OLD | NEW |