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 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
212 task_runner_->PostTaskAndReply( | 212 task_runner_->PostTaskAndReply( |
213 FROM_HERE, | 213 FROM_HERE, |
214 base::Bind(&DeleteArchiveFiles, paths_to_delete, success), | 214 base::Bind(&DeleteArchiveFiles, paths_to_delete, success), |
215 base::Bind(&OfflinePageModel::OnDeleteArchiveFilesDone, | 215 base::Bind(&OfflinePageModel::OnDeleteArchiveFilesDone, |
216 weak_ptr_factory_.GetWeakPtr(), | 216 weak_ptr_factory_.GetWeakPtr(), |
217 bookmark_ids, | 217 bookmark_ids, |
218 callback, | 218 callback, |
219 base::Owned(success))); | 219 base::Owned(success))); |
220 } | 220 } |
221 | 221 |
222 void OfflinePageModel::ClearAll(const base::Closure& callback) { | |
223 DCHECK(is_loaded_); | |
224 | |
225 std::vector<int64> bookmark_ids; | |
226 for (const auto& id_page_pair : offline_pages_) | |
227 bookmark_ids.push_back(id_page_pair.second.bookmark_id); | |
fgorski
2015/10/23 20:55:03
bookmark_ids.push_back(id_page_pair.first);
jianli
2015/10/26 21:42:43
Done.
| |
228 DeletePagesByBookmarkId( | |
229 bookmark_ids, | |
230 base::Bind(&OfflinePageModel::OnRemoveAllFilesDoneForClearAll, | |
231 weak_ptr_factory_.GetWeakPtr(), | |
232 callback)); | |
233 } | |
234 | |
222 const std::vector<OfflinePageItem> OfflinePageModel::GetAllPages() const { | 235 const std::vector<OfflinePageItem> OfflinePageModel::GetAllPages() const { |
223 DCHECK(is_loaded_); | 236 DCHECK(is_loaded_); |
224 std::vector<OfflinePageItem> offline_pages; | 237 std::vector<OfflinePageItem> offline_pages; |
225 for (const auto& id_page_pair : offline_pages_) { | 238 for (const auto& id_page_pair : offline_pages_) { |
226 if (id_page_pair.second.IsMarkedForDeletion()) | 239 if (id_page_pair.second.IsMarkedForDeletion()) |
227 continue; | 240 continue; |
228 offline_pages.push_back(id_page_pair.second); | 241 offline_pages.push_back(id_page_pair.second); |
229 } | 242 } |
230 return offline_pages; | 243 return offline_pages; |
231 } | 244 } |
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
435 } | 448 } |
436 MarkPageForDeletion(node->id(), base::Bind(&EmptyDeleteCallback)); | 449 MarkPageForDeletion(node->id(), base::Bind(&EmptyDeleteCallback)); |
437 } | 450 } |
438 | 451 |
439 void OfflinePageModel::OnLoadDone( | 452 void OfflinePageModel::OnLoadDone( |
440 bool success, | 453 bool success, |
441 const std::vector<OfflinePageItem>& offline_pages) { | 454 const std::vector<OfflinePageItem>& offline_pages) { |
442 DCHECK(!is_loaded_); | 455 DCHECK(!is_loaded_); |
443 is_loaded_ = true; | 456 is_loaded_ = true; |
444 | 457 |
445 if (success) { | 458 // TODO(jianli): rebuild the store upon failure. |
446 for (const auto& offline_page : offline_pages) | 459 |
447 offline_pages_[offline_page.bookmark_id] = offline_page; | 460 if (success) |
448 } | 461 CacheLoadedData(offline_pages); |
449 | 462 |
450 // Run all the delayed tasks. | 463 // Run all the delayed tasks. |
451 for (const auto& delayed_task : delayed_tasks_) | 464 for (const auto& delayed_task : delayed_tasks_) |
452 delayed_task.Run(); | 465 delayed_task.Run(); |
453 delayed_tasks_.clear(); | 466 delayed_tasks_.clear(); |
454 | 467 |
455 // If there are pages that are marked for deletion, but not yet deleted and | 468 // If there are pages that are marked for deletion, but not yet deleted and |
456 // OfflinePageModel gets reloaded. Delete the pages now. | 469 // OfflinePageModel gets reloaded. Delete the pages now. |
457 FinalizePageDeletion(); | 470 FinalizePageDeletion(); |
458 | 471 |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
547 } | 560 } |
548 | 561 |
549 void OfflinePageModel::OnRemoveOfflinePagesMissingArchiveFileDone( | 562 void OfflinePageModel::OnRemoveOfflinePagesMissingArchiveFileDone( |
550 const std::vector<int64>& bookmark_ids, | 563 const std::vector<int64>& bookmark_ids, |
551 OfflinePageModel::DeletePageResult /* result */) { | 564 OfflinePageModel::DeletePageResult /* result */) { |
552 for (int64 bookmark_id : bookmark_ids) { | 565 for (int64 bookmark_id : bookmark_ids) { |
553 FOR_EACH_OBSERVER(Observer, observers_, OfflinePageDeleted(bookmark_id)); | 566 FOR_EACH_OBSERVER(Observer, observers_, OfflinePageDeleted(bookmark_id)); |
554 } | 567 } |
555 } | 568 } |
556 | 569 |
570 void OfflinePageModel::OnRemoveAllFilesDoneForClearAll( | |
571 const base::Closure& callback, | |
572 DeletePageResult result) { | |
573 store_->Reset(base::Bind(&OfflinePageModel::OnResetStoreDoneForClearAll, | |
574 weak_ptr_factory_.GetWeakPtr(), | |
fgorski
2015/10/23 20:55:03
nit: alignment is off
jianli
2015/10/26 21:42:43
Done.
| |
575 callback)); | |
576 } | |
577 | |
578 void OfflinePageModel::OnResetStoreDoneForClearAll( | |
579 const base::Closure& callback, bool success) { | |
fgorski
2015/10/23 20:55:03
can you more explicitly ignore success here?
jianli
2015/10/26 21:42:43
Done.
| |
580 offline_pages_.clear(); | |
581 store_->Load(base::Bind(&OfflinePageModel::OnReloadStoreDoneForClearAll, | |
582 weak_ptr_factory_.GetWeakPtr(), | |
583 callback)); | |
584 } | |
585 | |
586 void OfflinePageModel::OnReloadStoreDoneForClearAll( | |
587 const base::Closure& callback, | |
588 bool success, | |
589 const std::vector<OfflinePageItem>& offline_pages) { | |
590 DCHECK(success); | |
591 | |
592 CacheLoadedData(offline_pages); | |
593 callback.Run(); | |
594 } | |
595 | |
596 void OfflinePageModel::CacheLoadedData( | |
597 const std::vector<OfflinePageItem>& offline_pages) { | |
fgorski
2015/10/23 20:55:03
offline_pages.clear(); should probably go here as
jianli
2015/10/26 21:42:44
Done.
| |
598 for (const auto& offline_page : offline_pages) | |
599 offline_pages_[offline_page.bookmark_id] = offline_page; | |
600 } | |
601 | |
557 } // namespace offline_pages | 602 } // namespace offline_pages |
OLD | NEW |