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" |
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/sequenced_task_runner.h" | 15 #include "base/sequenced_task_runner.h" |
16 #include "base/thread_task_runner_handle.h" | 16 #include "base/thread_task_runner_handle.h" |
17 #include "base/time/time.h" | 17 #include "base/time/time.h" |
18 #include "components/bookmarks/browser/bookmark_model.h" | 18 #include "components/bookmarks/browser/bookmark_model.h" |
19 #include "components/bookmarks/browser/bookmark_node.h" | 19 #include "components/bookmarks/browser/bookmark_node.h" |
20 #include "components/offline_pages/offline_page_item.h" | 20 #include "components/offline_pages/offline_page_item.h" |
21 #include "components/offline_pages/offline_page_metadata_store.h" | |
22 #include "url/gurl.h" | 21 #include "url/gurl.h" |
23 | 22 |
24 using ArchiverResult = offline_pages::OfflinePageArchiver::ArchiverResult; | 23 using ArchiverResult = offline_pages::OfflinePageArchiver::ArchiverResult; |
25 using SavePageResult = offline_pages::OfflinePageModel::SavePageResult; | 24 using SavePageResult = offline_pages::OfflinePageModel::SavePageResult; |
26 | 25 |
27 namespace offline_pages { | 26 namespace offline_pages { |
28 | 27 |
29 namespace { | 28 namespace { |
30 | 29 |
30 // This enum is used in an UMA histogram. Hence the entries here shouldn't | |
31 // be deleted or re-ordered and new ones should be added to the end. | |
32 enum ClearAllStatus { | |
33 CLEAR_ALL_SUCCEEDED, | |
34 STORE_RESET_FAILED, | |
35 STORE_RELOAD_FAILED, | |
36 | |
37 // NOTE: always keep this entry at the end. | |
38 CLEAR_ALL_STATUS_COUNT | |
39 }; | |
40 | |
31 // Threshold for how old offline copy of a page should be before we offer to | 41 // Threshold for how old offline copy of a page should be before we offer to |
32 // delete it to free up space. | 42 // delete it to free up space. |
33 const base::TimeDelta kPageCleanUpThreshold = base::TimeDelta::FromDays(30); | 43 const base::TimeDelta kPageCleanUpThreshold = base::TimeDelta::FromDays(30); |
34 | 44 |
35 // The delay for the final deletion to kick in after the page is marked for | 45 // The delay for the final deletion to kick in after the page is marked for |
36 // deletion. The value set here is a bit longer that the duration of the | 46 // deletion. The value set here is a bit longer that the duration of the |
37 // snackbar that offers undo. | 47 // snackbar that offers undo. |
38 const base::TimeDelta kFinalDeletionDelay = base::TimeDelta::FromSeconds(6); | 48 const base::TimeDelta kFinalDeletionDelay = base::TimeDelta::FromSeconds(6); |
39 | 49 |
40 SavePageResult ToSavePageResult(ArchiverResult archiver_result) { | 50 SavePageResult ToSavePageResult(ArchiverResult archiver_result) { |
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
212 task_runner_->PostTaskAndReply( | 222 task_runner_->PostTaskAndReply( |
213 FROM_HERE, | 223 FROM_HERE, |
214 base::Bind(&DeleteArchiveFiles, paths_to_delete, success), | 224 base::Bind(&DeleteArchiveFiles, paths_to_delete, success), |
215 base::Bind(&OfflinePageModel::OnDeleteArchiveFilesDone, | 225 base::Bind(&OfflinePageModel::OnDeleteArchiveFilesDone, |
216 weak_ptr_factory_.GetWeakPtr(), | 226 weak_ptr_factory_.GetWeakPtr(), |
217 bookmark_ids, | 227 bookmark_ids, |
218 callback, | 228 callback, |
219 base::Owned(success))); | 229 base::Owned(success))); |
220 } | 230 } |
221 | 231 |
232 void OfflinePageModel::ClearAll(const base::Closure& callback) { | |
233 DCHECK(is_loaded_); | |
234 | |
235 std::vector<int64> bookmark_ids; | |
236 for (const auto& id_page_pair : offline_pages_) | |
237 bookmark_ids.push_back(id_page_pair.first); | |
238 DeletePagesByBookmarkId( | |
239 bookmark_ids, | |
240 base::Bind(&OfflinePageModel::OnRemoveAllFilesDoneForClearAll, | |
241 weak_ptr_factory_.GetWeakPtr(), | |
242 callback)); | |
243 } | |
244 | |
222 const std::vector<OfflinePageItem> OfflinePageModel::GetAllPages() const { | 245 const std::vector<OfflinePageItem> OfflinePageModel::GetAllPages() const { |
223 DCHECK(is_loaded_); | 246 DCHECK(is_loaded_); |
224 std::vector<OfflinePageItem> offline_pages; | 247 std::vector<OfflinePageItem> offline_pages; |
225 for (const auto& id_page_pair : offline_pages_) { | 248 for (const auto& id_page_pair : offline_pages_) { |
226 if (id_page_pair.second.IsMarkedForDeletion()) | 249 if (id_page_pair.second.IsMarkedForDeletion()) |
227 continue; | 250 continue; |
228 offline_pages.push_back(id_page_pair.second); | 251 offline_pages.push_back(id_page_pair.second); |
229 } | 252 } |
230 return offline_pages; | 253 return offline_pages; |
231 } | 254 } |
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
430 base::Bind(&OfflinePageModel::MarkPageForDeletion, | 453 base::Bind(&OfflinePageModel::MarkPageForDeletion, |
431 weak_ptr_factory_.GetWeakPtr(), | 454 weak_ptr_factory_.GetWeakPtr(), |
432 node->id(), | 455 node->id(), |
433 base::Bind(&EmptyDeleteCallback))); | 456 base::Bind(&EmptyDeleteCallback))); |
434 return; | 457 return; |
435 } | 458 } |
436 MarkPageForDeletion(node->id(), base::Bind(&EmptyDeleteCallback)); | 459 MarkPageForDeletion(node->id(), base::Bind(&EmptyDeleteCallback)); |
437 } | 460 } |
438 | 461 |
439 void OfflinePageModel::OnLoadDone( | 462 void OfflinePageModel::OnLoadDone( |
440 bool success, | 463 OfflinePageMetadataStore::LoadStatus load_status, |
441 const std::vector<OfflinePageItem>& offline_pages) { | 464 const std::vector<OfflinePageItem>& offline_pages) { |
442 DCHECK(!is_loaded_); | 465 DCHECK(!is_loaded_); |
443 is_loaded_ = true; | 466 is_loaded_ = true; |
444 | 467 |
445 if (success) { | 468 // TODO(jianli): rebuild the store upon failure. |
446 for (const auto& offline_page : offline_pages) | 469 |
447 offline_pages_[offline_page.bookmark_id] = offline_page; | 470 UMA_HISTOGRAM_ENUMERATION("OfflinePages.LoadStatus", |
448 } | 471 load_status, |
472 OfflinePageMetadataStore::LOAD_STATUS_COUNT); | |
473 if (load_status == OfflinePageMetadataStore::LOAD_SUCCEEDED) | |
474 CacheLoadedData(offline_pages); | |
449 | 475 |
450 // Run all the delayed tasks. | 476 // Run all the delayed tasks. |
451 for (const auto& delayed_task : delayed_tasks_) | 477 for (const auto& delayed_task : delayed_tasks_) |
452 delayed_task.Run(); | 478 delayed_task.Run(); |
453 delayed_tasks_.clear(); | 479 delayed_tasks_.clear(); |
454 | 480 |
455 // If there are pages that are marked for deletion, but not yet deleted and | 481 // If there are pages that are marked for deletion, but not yet deleted and |
456 // OfflinePageModel gets reloaded. Delete the pages now. | 482 // OfflinePageModel gets reloaded. Delete the pages now. |
457 FinalizePageDeletion(); | 483 FinalizePageDeletion(); |
458 | 484 |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
547 } | 573 } |
548 | 574 |
549 void OfflinePageModel::OnRemoveOfflinePagesMissingArchiveFileDone( | 575 void OfflinePageModel::OnRemoveOfflinePagesMissingArchiveFileDone( |
550 const std::vector<int64>& bookmark_ids, | 576 const std::vector<int64>& bookmark_ids, |
551 OfflinePageModel::DeletePageResult /* result */) { | 577 OfflinePageModel::DeletePageResult /* result */) { |
552 for (int64 bookmark_id : bookmark_ids) { | 578 for (int64 bookmark_id : bookmark_ids) { |
553 FOR_EACH_OBSERVER(Observer, observers_, OfflinePageDeleted(bookmark_id)); | 579 FOR_EACH_OBSERVER(Observer, observers_, OfflinePageDeleted(bookmark_id)); |
554 } | 580 } |
555 } | 581 } |
556 | 582 |
583 void OfflinePageModel::OnRemoveAllFilesDoneForClearAll( | |
584 const base::Closure& callback, | |
585 DeletePageResult result) { | |
586 store_->Reset(base::Bind(&OfflinePageModel::OnResetStoreDoneForClearAll, | |
587 weak_ptr_factory_.GetWeakPtr(), | |
588 callback)); | |
589 } | |
590 | |
591 void OfflinePageModel::OnResetStoreDoneForClearAll( | |
592 const base::Closure& callback, bool success) { | |
593 DCHECK(success); | |
fgorski
2015/10/27 20:13:09
DCHECK makes sense :)
| |
594 UMA_HISTOGRAM_ENUMERATION("OfflinePages.ClearAllStatus", | |
595 STORE_RESET_FAILED, | |
596 CLEAR_ALL_STATUS_COUNT); | |
597 | |
598 offline_pages_.clear(); | |
599 store_->Load(base::Bind(&OfflinePageModel::OnReloadStoreDoneForClearAll, | |
600 weak_ptr_factory_.GetWeakPtr(), | |
601 callback)); | |
602 } | |
603 | |
604 void OfflinePageModel::OnReloadStoreDoneForClearAll( | |
605 const base::Closure& callback, | |
606 OfflinePageMetadataStore::LoadStatus load_status, | |
607 const std::vector<OfflinePageItem>& offline_pages) { | |
608 DCHECK_EQ(OfflinePageMetadataStore::LOAD_SUCCEEDED, load_status); | |
609 UMA_HISTOGRAM_ENUMERATION( | |
610 "OfflinePages.ClearAllStatus", | |
611 load_status == OfflinePageMetadataStore::LOAD_SUCCEEDED ? | |
612 CLEAR_ALL_SUCCEEDED : STORE_RELOAD_FAILED, | |
613 CLEAR_ALL_STATUS_COUNT); | |
614 | |
615 CacheLoadedData(offline_pages); | |
616 callback.Run(); | |
617 } | |
618 | |
619 void OfflinePageModel::CacheLoadedData( | |
620 const std::vector<OfflinePageItem>& offline_pages) { | |
621 offline_pages_.clear(); | |
622 for (const auto& offline_page : offline_pages) | |
623 offline_pages_[offline_page.bookmark_id] = offline_page; | |
624 } | |
625 | |
557 } // namespace offline_pages | 626 } // namespace offline_pages |
OLD | NEW |