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