| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_STORAGE_MANAGER_H_ | |
| 6 #define COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_STORAGE_MANAGER_H_ | |
| 7 | |
| 8 #include <stdint.h> | |
| 9 | |
| 10 #include <vector> | |
| 11 | |
| 12 #include "base/callback.h" | |
| 13 #include "base/macros.h" | |
| 14 #include "base/memory/weak_ptr.h" | |
| 15 #include "base/time/time.h" | |
| 16 #include "components/offline_pages/archive_manager.h" | |
| 17 #include "components/offline_pages/offline_page_types.h" | |
| 18 | |
| 19 namespace base { | |
| 20 class Clock; | |
| 21 } // namespace base | |
| 22 | |
| 23 namespace offline_pages { | |
| 24 | |
| 25 // Maximum % of total available storage that will be occupied by offline pages | |
| 26 // before a storage clearup. | |
| 27 const double kOfflinePageStorageLimit = 0.3; | |
| 28 // The target % of storage usage we try to reach below when expiring pages. | |
| 29 const double kOfflinePageStorageClearThreshold = 0.1; | |
| 30 // The time that the storage cleanup will be triggered again since the last one. | |
| 31 const base::TimeDelta kClearStorageInterval = base::TimeDelta::FromMinutes(10); | |
| 32 // The time that the page record will be removed from the store since the page | |
| 33 // has been expired. | |
| 34 const base::TimeDelta kRemovePageItemInterval = base::TimeDelta::FromDays(21); | |
| 35 | |
| 36 class ClientPolicyController; | |
| 37 class OfflinePageModel; | |
| 38 | |
| 39 // This class is used for storage management of offline pages. It provides | |
| 40 // a ClearPagesIfNeeded method which is used to clear expired offline pages | |
| 41 // based on last_access_time and lifetime policy of its namespace. | |
| 42 // It has its own throttle mechanism so calling the method would not be | |
| 43 // guaranteed to clear the pages immediately. | |
| 44 // | |
| 45 // OfflinePageModel should own and control the lifecycle of this manager. | |
| 46 // And this manager would use OfflinePageModel to get/remove pages. | |
| 47 class OfflinePageStorageManager { | |
| 48 public: | |
| 49 enum class ClearStorageResult { | |
| 50 SUCCESS, // Cleared successfully. | |
| 51 UNNECESSARY, // No expired pages. | |
| 52 EXPIRE_FAILURE, // Expiration failed. | |
| 53 DELETE_FAILURE, // Deletion failed. | |
| 54 EXPIRE_AND_DELETE_FAILURES, // Both expiration and deletion failed. | |
| 55 // NOTE: always keep this entry at the end. Add new result types only | |
| 56 // immediately above this line. Make sure to update the corresponding | |
| 57 // histogram enum accordingly. | |
| 58 RESULT_COUNT, | |
| 59 }; | |
| 60 | |
| 61 // Callback used when calling ClearPagesIfNeeded. | |
| 62 // size_t: the number of expired pages. | |
| 63 // ClearStorageResult: result of expiring pages in storage. | |
| 64 typedef base::Callback<void(size_t, ClearStorageResult)> ClearStorageCallback; | |
| 65 | |
| 66 explicit OfflinePageStorageManager(OfflinePageModel* model, | |
| 67 ClientPolicyController* policy_controller, | |
| 68 ArchiveManager* archive_manager); | |
| 69 | |
| 70 ~OfflinePageStorageManager(); | |
| 71 | |
| 72 // The manager would *try* to clear pages when called. It may not delete any | |
| 73 // pages (if clearing condition wasn't satisfied). | |
| 74 // It clears the storage (expire pages) when it's using more disk space than a | |
| 75 // certain limit, or the time elapsed from last time clearing is longer than a | |
| 76 // certain interval. Both values are defined above. | |
| 77 void ClearPagesIfNeeded(const ClearStorageCallback& callback); | |
| 78 | |
| 79 // Sets the clock for testing. | |
| 80 void SetClockForTesting(std::unique_ptr<base::Clock> clock); | |
| 81 | |
| 82 private: | |
| 83 // Enum indicating how to clear the pages. | |
| 84 enum class ClearMode { | |
| 85 // Using normal expiration logic to expire pages. Will reduce the storage | |
| 86 // usage down below the threshold. | |
| 87 DEFAULT, | |
| 88 // No need to expire any page (no pages in the model or no expired | |
| 89 // pages and we're not exceeding the storage limit.) | |
| 90 NOT_NEEDED, | |
| 91 }; | |
| 92 | |
| 93 // Callback called after getting storage stats from archive manager. | |
| 94 void OnGetStorageStatsDoneForClearingPages( | |
| 95 const ClearStorageCallback& callback, | |
| 96 const ArchiveManager::StorageStats& pages); | |
| 97 | |
| 98 // Callback called after getting all pages from model. | |
| 99 void OnGetAllPagesDoneForClearingPages( | |
| 100 const ClearStorageCallback& callback, | |
| 101 const ArchiveManager::StorageStats& storage_stats, | |
| 102 const MultipleOfflinePageItemResult& pages); | |
| 103 | |
| 104 // Callback called after expired pages have been deleted. | |
| 105 void OnPagesExpired(const ClearStorageCallback& callback, | |
| 106 size_t pages_to_clear, | |
| 107 const std::vector<int64_t>& page_ids_to_remove, | |
| 108 bool expiration_succeeded); | |
| 109 | |
| 110 // Callback called after clearing outdated pages from model. | |
| 111 void OnOutdatedPagesCleared(const ClearStorageCallback& callback, | |
| 112 size_t pages_cleared, | |
| 113 bool expiration_succeeded, | |
| 114 DeletePageResult result); | |
| 115 | |
| 116 // Gets offline IDs of both pages that should be expired and the ones that | |
| 117 // need to be removed from metadata store. |page_ids_to_expire| will have | |
| 118 // the pages to be expired, |page_ids_to_remove| will have the pages to be | |
| 119 // removed. | |
| 120 void GetPageIdsToClear(const MultipleOfflinePageItemResult& pages, | |
| 121 const ArchiveManager::StorageStats& stats, | |
| 122 std::vector<int64_t>* page_ids_to_expire, | |
| 123 std::vector<int64_t>* page_ids_to_remove); | |
| 124 | |
| 125 // Determines if manager should clear pages. | |
| 126 ClearMode ShouldClearPages(const ArchiveManager::StorageStats& storage_stats); | |
| 127 | |
| 128 // Returns true if |page| is expired comparing to |clear_time_|. | |
| 129 bool ShouldBeExpired(const OfflinePageItem& page) const; | |
| 130 | |
| 131 // Returns true if we're currently doing a cleanup. | |
| 132 bool IsInProgress() const; | |
| 133 | |
| 134 // Not owned. | |
| 135 OfflinePageModel* model_; | |
| 136 | |
| 137 // Not owned. | |
| 138 ClientPolicyController* policy_controller_; | |
| 139 | |
| 140 // Not owned. | |
| 141 ArchiveManager* archive_manager_; | |
| 142 | |
| 143 // Starting time of the current storage cleanup. If this time is later than | |
| 144 // |last_clear_time_| it means we're doing a cleanup. | |
| 145 base::Time clear_time_; | |
| 146 | |
| 147 // Timestamp of last storage cleanup. | |
| 148 base::Time last_clear_time_; | |
| 149 | |
| 150 // Clock for getting time. | |
| 151 std::unique_ptr<base::Clock> clock_; | |
| 152 | |
| 153 base::WeakPtrFactory<OfflinePageStorageManager> weak_ptr_factory_; | |
| 154 | |
| 155 DISALLOW_COPY_AND_ASSIGN(OfflinePageStorageManager); | |
| 156 }; | |
| 157 | |
| 158 } // namespace offline_pages | |
| 159 | |
| 160 #endif // COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_STORAGE_MANAGER_H_ | |
| OLD | NEW |