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