Chromium Code Reviews| 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 |
| 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 | |
| 51 // Asks the client to get all offline pages and invoke |callback|. | 60 // Asks the client to get all offline pages and invoke |callback|. |
|
jianli
2016/05/27 00:57:17
nit: invokes |callback| upon completion.
romax
2016/05/27 02:18:26
Done.
| |
| 52 virtual void GetAllPages( | 61 virtual void GetAllPages( |
| 53 const MultipleOfflinePageItemCallback& callback) = 0; | 62 const MultipleOfflinePageItemCallback& callback) = 0; |
| 54 | 63 |
| 55 // Asks the client to delete pages based on |offline_ids| and invoke | 64 // Asks the client to mark pages with |offline_ids| as expired and delete |
| 56 // |callback|. | 65 // the associated archive files. |
| 57 virtual void DeletePagesByOfflineId(const std::vector<int64_t>& offline_ids, | 66 virtual void ExpirePages(const std::vector<int64_t>& offline_ids, |
| 58 const DeletePageCallback& callback) = 0; | 67 const base::Time& expiration_time, |
| 68 const base::Callback<void(bool)>& callback) = 0; | |
| 59 }; | 69 }; |
| 60 | 70 |
| 61 enum class ClearStorageResult { | 71 enum class ClearStorageResult { |
| 62 SUCCESS, // Cleared successfully. | 72 SUCCESS, // Cleared successfully. |
| 63 UNNECESSARY, // No expired pages. | 73 UNNECESSARY, // No expired pages. |
| 74 EXPIRE_FAILURE, // Expiration failed. | |
| 64 DELETE_FAILURE, // Deletion failed. | 75 DELETE_FAILURE, // Deletion failed. |
| 76 BOTH_FAILURE, // Both expiration and deletion failed. | |
|
jianli
2016/05/27 00:57:17
Probably better to say EXPIRE_AND_DELETE_FAILURES
romax
2016/05/27 02:18:26
Done.
| |
| 65 }; | 77 }; |
| 66 | 78 |
| 67 // Callback used when calling ClearPagesIfNeeded. | 79 // Callback used when calling ClearPagesIfNeeded. |
| 68 // int: the number of expired pages. | 80 // size_t: the number of expired pages. |
| 69 // ClearStorageResult: result of expiring pages in storage. | 81 // ClearStorageResult: result of expiring pages in storage. |
| 70 typedef base::Callback<void(int, ClearStorageResult)> ClearPagesCallback; | 82 typedef base::Callback<void(size_t, ClearStorageResult)> ClearPagesCallback; |
| 71 | 83 |
| 72 explicit OfflinePageStorageManager(Client* client, | 84 explicit OfflinePageStorageManager(Client* client, |
| 73 ClientPolicyController* policy_controller, | 85 ClientPolicyController* policy_controller, |
| 74 ArchiveManager* archive_manager); | 86 ArchiveManager* archive_manager); |
| 75 | 87 |
| 76 ~OfflinePageStorageManager(); | 88 ~OfflinePageStorageManager(); |
| 77 | 89 |
| 78 // The manager would *try* to clear pages when called. It may not delete any | 90 // The manager would *try* to clear pages when called. It may not delete any |
| 79 // pages (if clearing condition wasn't satisfied). | 91 // pages (if clearing condition wasn't satisfied). |
| 80 // It clears the storage (expire pages) when it's using more disk space than a | 92 // 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 | 93 // certain limit, or the time elapsed from last time clearing is longer than a |
| 82 // certain interval. Both values are defined above. | 94 // certain interval. Both values are defined above. |
| 83 void ClearPagesIfNeeded(const ClearPagesCallback& callback); | 95 void ClearPagesIfNeeded(const ClearPagesCallback& callback); |
| 84 | 96 |
| 85 // Sets the clock for testing. | 97 // Sets the clock for testing. |
| 86 void SetClockForTesting(std::unique_ptr<base::Clock> clock); | 98 void SetClockForTesting(std::unique_ptr<base::Clock> clock); |
| 87 | 99 |
| 88 private: | 100 private: |
| 89 // Enum indicating how to clear the pages. | 101 // Enum indicating how to clear the pages. |
| 90 enum class ClearMode { | 102 enum class ClearMode { |
| 91 // Using normal expiration logic to expire pages. Will reduce the storage | 103 // Using normal expiration logic to expire pages. Will reduce the storage |
| 92 // usage down below the threshold. | 104 // usage down below the threshold. |
| 93 DEFAULT, | 105 DEFAULT, |
| 94 // No need to expire any page (no pages in the model or no expired | 106 // No need to expire any page (no pages in the model or no expired |
| 95 // pages and we're not exceeding the storage limit.) | 107 // pages and we're not exceeding the storage limit.) |
| 96 NOT_NEEDED, | 108 NOT_NEEDED, |
| 97 }; | 109 }; |
| 98 | 110 |
| 99 // Callback called after getting storage stats from archive manager. | 111 // Callback called after getting storage stats from archive manager. |
| 100 void OnGetStorageStatsDone(const ClearPagesCallback& callback, | 112 void OnGetStorageStatsDoneForClearingPages( |
| 101 const ArchiveManager::StorageStats& pages); | 113 const ClearPagesCallback& callback, |
| 114 const ArchiveManager::StorageStats& pages); | |
| 102 | 115 |
| 103 // Callback called after getting all pages from client done. | 116 // Callback called after getting all pages from client done. |
|
jianli
2016/05/27 00:57:17
nit: remove "done" from comment
romax
2016/05/27 02:18:26
Done.
| |
| 104 void OnGetAllPagesDone(const ClearPagesCallback& callback, | 117 void OnGetAllPagesDoneForClearingPages( |
| 105 const ArchiveManager::StorageStats& storage_stats, | 118 const ClearPagesCallback& callback, |
| 106 const MultipleOfflinePageItemResult& pages); | 119 const ArchiveManager::StorageStats& storage_stats, |
| 120 const MultipleOfflinePageItemResult& pages); | |
| 107 | 121 |
| 108 // Callback called after expired pages have been deleted. | 122 // Callback called after expired pages have been deleted. |
| 109 void OnExpiredPagesDeleted(const ClearPagesCallback& callback, | 123 void OnPagesExpired(const ClearPagesCallback& callback, |
| 110 int pages_to_clear, | 124 size_t pages_to_clear, |
| 111 DeletePageResult result); | 125 const std::vector<int64_t>& page_ids_to_remove, |
| 126 bool expiration_succeeded); | |
| 112 | 127 |
| 113 // Gets offline IDs of all expired pages and return in |offline_ids|. | 128 // Callback called after clearing outdated pages from client. |
| 114 void GetExpiredPageIds(const MultipleOfflinePageItemResult& pages, | 129 void OnOutdatedPagesCleared(const ClearPagesCallback& callback, |
| 130 size_t pages_cleared, | |
| 131 bool expiration_succeeded, | |
| 132 DeletePageResult result); | |
| 133 | |
| 134 // Gets offline IDs of both pages that should be expired and the ones that | |
| 135 // need to be removed from metadata store. |page_ids_to_expire| will have | |
| 136 // the pages to be expired, |page_ids_to_remove| will have the pages to be | |
| 137 // removed. | |
| 138 void GetPageIdsToClear(const MultipleOfflinePageItemResult& pages, | |
| 115 const ArchiveManager::StorageStats& stats, | 139 const ArchiveManager::StorageStats& stats, |
| 116 std::vector<int64_t>& offline_ids); | 140 std::vector<int64_t>* page_ids_to_expire, |
| 141 std::vector<int64_t>* page_ids_to_remove); | |
| 117 | 142 |
| 118 // Determine if manager should clear pages. | 143 // Determines if manager should clear pages. |
| 119 ClearMode ShouldClearPages(const ArchiveManager::StorageStats& storage_stats); | 144 ClearMode ShouldClearPages(const ArchiveManager::StorageStats& storage_stats); |
| 120 | 145 |
| 121 // Return true if |page| is expired comparing to |now|. | 146 // Returns true if |page| is expired comparing to |clear_time_|. |
| 122 bool ShouldBeExpired(const base::Time& now, const OfflinePageItem& page); | 147 bool ShouldBeExpired(const OfflinePageItem& page); |
|
jianli
2016/05/27 00:57:17
add const modifier
romax
2016/05/27 02:18:26
Done.
| |
| 148 | |
| 149 // Returns true if we're currently doing a cleanup. | |
| 150 bool IsInProgress(); | |
|
jianli
2016/05/27 00:57:17
nit: add const modifier
romax
2016/05/27 02:18:26
Done.
| |
| 123 | 151 |
| 124 // Not owned. | 152 // Not owned. |
| 125 Client* client_; | 153 Client* client_; |
| 126 | 154 |
| 127 // Not owned. | 155 // Not owned. |
| 128 ClientPolicyController* policy_controller_; | 156 ClientPolicyController* policy_controller_; |
| 129 | 157 |
| 130 // Not owned. | 158 // Not owned. |
| 131 ArchiveManager* archive_manager_; | 159 ArchiveManager* archive_manager_; |
| 132 | 160 |
| 133 bool in_progress_; | 161 // Starting time of the current storage cleanup. If this time is later than |
| 162 // |last_clear_time_| it means we're doing a cleanup. | |
| 163 base::Time clear_time_; | |
| 134 | 164 |
| 165 // Timestamp of last storage cleanup. | |
| 135 base::Time last_clear_time_; | 166 base::Time last_clear_time_; |
| 136 | 167 |
| 137 // Clock for getting time. | 168 // Clock for getting time. |
| 138 std::unique_ptr<base::Clock> clock_; | 169 std::unique_ptr<base::Clock> clock_; |
| 139 | 170 |
| 140 base::WeakPtrFactory<OfflinePageStorageManager> weak_ptr_factory_; | 171 base::WeakPtrFactory<OfflinePageStorageManager> weak_ptr_factory_; |
| 141 | 172 |
| 142 DISALLOW_COPY_AND_ASSIGN(OfflinePageStorageManager); | 173 DISALLOW_COPY_AND_ASSIGN(OfflinePageStorageManager); |
| 143 }; | 174 }; |
| 144 | 175 |
| 145 } // namespace offline_pages | 176 } // namespace offline_pages |
| 146 | 177 |
| 147 #endif // COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_STORAGE_MANAGER_H_ | 178 #endif // COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_STORAGE_MANAGER_H_ |
| OLD | NEW |