| 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   if (load_status == OfflinePageMetadataStore::LOAD_SUCCEEDED) | 
| 448   } | 471     CacheLoadedData(offline_pages); | 
| 449 | 472 | 
| 450   // Run all the delayed tasks. | 473   // Run all the delayed tasks. | 
| 451   for (const auto& delayed_task : delayed_tasks_) | 474   for (const auto& delayed_task : delayed_tasks_) | 
| 452     delayed_task.Run(); | 475     delayed_task.Run(); | 
| 453   delayed_tasks_.clear(); | 476   delayed_tasks_.clear(); | 
| 454 | 477 | 
| 455   // If there are pages that are marked for deletion, but not yet deleted and | 478   // If there are pages that are marked for deletion, but not yet deleted and | 
| 456   // OfflinePageModel gets reloaded. Delete the pages now. | 479   // OfflinePageModel gets reloaded. Delete the pages now. | 
| 457   FinalizePageDeletion(); | 480   FinalizePageDeletion(); | 
| 458 | 481 | 
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 547 } | 570 } | 
| 548 | 571 | 
| 549 void OfflinePageModel::OnRemoveOfflinePagesMissingArchiveFileDone( | 572 void OfflinePageModel::OnRemoveOfflinePagesMissingArchiveFileDone( | 
| 550     const std::vector<int64>& bookmark_ids, | 573     const std::vector<int64>& bookmark_ids, | 
| 551     OfflinePageModel::DeletePageResult /* result */) { | 574     OfflinePageModel::DeletePageResult /* result */) { | 
| 552   for (int64 bookmark_id : bookmark_ids) { | 575   for (int64 bookmark_id : bookmark_ids) { | 
| 553     FOR_EACH_OBSERVER(Observer, observers_, OfflinePageDeleted(bookmark_id)); | 576     FOR_EACH_OBSERVER(Observer, observers_, OfflinePageDeleted(bookmark_id)); | 
| 554   } | 577   } | 
| 555 } | 578 } | 
| 556 | 579 | 
|  | 580 void OfflinePageModel::OnRemoveAllFilesDoneForClearAll( | 
|  | 581     const base::Closure& callback, | 
|  | 582     DeletePageResult result) { | 
|  | 583   store_->Reset(base::Bind(&OfflinePageModel::OnResetStoreDoneForClearAll, | 
|  | 584                            weak_ptr_factory_.GetWeakPtr(), | 
|  | 585                            callback)); | 
|  | 586 } | 
|  | 587 | 
|  | 588 void OfflinePageModel::OnResetStoreDoneForClearAll( | 
|  | 589     const base::Closure& callback, bool success) { | 
|  | 590   DCHECK(success); | 
|  | 591   UMA_HISTOGRAM_ENUMERATION("OfflinePages.ClearAllStatus", | 
|  | 592                             STORE_RESET_FAILED, | 
|  | 593                             CLEAR_ALL_STATUS_COUNT); | 
|  | 594 | 
|  | 595   offline_pages_.clear(); | 
|  | 596   store_->Load(base::Bind(&OfflinePageModel::OnReloadStoreDoneForClearAll, | 
|  | 597                           weak_ptr_factory_.GetWeakPtr(), | 
|  | 598                           callback)); | 
|  | 599 } | 
|  | 600 | 
|  | 601 void OfflinePageModel::OnReloadStoreDoneForClearAll( | 
|  | 602     const base::Closure& callback, | 
|  | 603     OfflinePageMetadataStore::LoadStatus load_status, | 
|  | 604     const std::vector<OfflinePageItem>& offline_pages) { | 
|  | 605   DCHECK_EQ(OfflinePageMetadataStore::LOAD_SUCCEEDED, load_status); | 
|  | 606   UMA_HISTOGRAM_ENUMERATION( | 
|  | 607       "OfflinePages.ClearAllStatus", | 
|  | 608       load_status == OfflinePageMetadataStore::LOAD_SUCCEEDED ? | 
|  | 609           CLEAR_ALL_SUCCEEDED : STORE_RELOAD_FAILED, | 
|  | 610       CLEAR_ALL_STATUS_COUNT); | 
|  | 611 | 
|  | 612   CacheLoadedData(offline_pages); | 
|  | 613   callback.Run(); | 
|  | 614 } | 
|  | 615 | 
|  | 616 void OfflinePageModel::CacheLoadedData( | 
|  | 617     const std::vector<OfflinePageItem>& offline_pages) { | 
|  | 618   offline_pages_.clear(); | 
|  | 619   for (const auto& offline_page : offline_pages) | 
|  | 620     offline_pages_[offline_page.bookmark_id] = offline_page; | 
|  | 621 } | 
|  | 622 | 
| 557 }  // namespace offline_pages | 623 }  // namespace offline_pages | 
| OLD | NEW | 
|---|