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 clearing. |
|
jianli
2016/05/26 21:59:31
nit: storage clearing => storage cleanup
romax
2016/05/27 00:41:38
Done.
| |
| 27 // reducing the usage below threshold. | |
| 28 const double kOfflinePageStorageLimit = 0.3; | 27 const double kOfflinePageStorageLimit = 0.3; |
|
jianli
2016/05/26 21:59:32
If these constants are only needed in .cc file, pl
romax
2016/05/27 00:41:38
per discussion I'm leaving them here.
| |
| 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 // Storage manager will clear storage if it has been longer than this interval | |
|
jianli
2016/05/26 21:59:31
nit: The time that the storage cleanup will be tri
romax
2016/05/27 00:41:38
Done.
| |
| 31 // since last clearing. | |
| 30 const base::TimeDelta kClearStorageInterval = base::TimeDelta::FromMinutes(10); | 32 const base::TimeDelta kClearStorageInterval = base::TimeDelta::FromMinutes(10); |
| 33 // Storage manager will remove a page from metadata store once the page has been | |
|
jianli
2016/05/26 21:59:32
nit: The time that the page record will be removed
romax
2016/05/27 00:41:38
Done.
| |
| 34 // expired longer than this interval. | |
| 35 const base::TimeDelta kRemovePageItemInterval = base::TimeDelta::FromDays(21); | |
| 31 | 36 |
| 32 class ArchiveManager; | 37 class ArchiveManager; |
| 33 class ClientPolicyController; | 38 class ClientPolicyController; |
| 34 | 39 |
| 35 // This class is used for storage management of offline pages. It provides | 40 // This class is used for storage management of offline pages. It provides |
| 36 // a ClearPagesIfNeeded method which is used to clear expired offline pages | 41 // a ClearPagesIfNeeded method which is used to clear expired offline pages |
| 37 // based on last_access_time and lifetime policy of its namespace. | 42 // 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 | 43 // It has its own throttle mechanism so calling the method would not be |
| 39 // guaranteed to clear the pages immediately. | 44 // guaranteed to clear the pages immediately. |
| 40 // | 45 // |
| 41 // OfflinePageModel should own and control the lifecycle of this manager. | 46 // OfflinePageModel should own and control the lifecycle of this manager. |
| 42 // And this manager would use OfflinePageModel to get/remove pages. | 47 // And this manager would use OfflinePageModel to get/remove pages. |
| 43 class OfflinePageStorageManager { | 48 class OfflinePageStorageManager { |
| 44 public: | 49 public: |
| 45 // This interface should have no knowledge of offline page model. | 50 // This interface should have no knowledge of offline page model. |
| 46 // This interface should be implemented by clients managed by storage manager. | 51 // This interface should be implemented by clients managed by storage manager. |
| 47 class Client { | 52 class Client { |
| 48 public: | 53 public: |
| 49 virtual ~Client() {} | 54 virtual ~Client() {} |
| 50 | 55 |
| 56 // Asks the client to delete pages based on |ofline_ids| and invoke | |
|
jianli
2016/05/26 21:59:32
nit: invoke => invokes
also consider adding "upon
romax
2016/05/27 00:41:38
Done.
| |
| 57 // |callback|. | |
| 58 virtual void DeletePagesByOfflineId(const std::vector<int64_t>& offline_ids, | |
| 59 const DeletePageCallback& callback) = 0; | |
| 60 | |
| 51 // Asks the client to get all offline pages and invoke |callback|. | 61 // Asks the client to get all offline pages and invoke |callback|. |
| 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 the |
|
jianli
2016/05/26 21:59:32
nit: add "deletes" after "and".
romax
2016/05/27 00:41:38
Done.
| |
| 56 // |callback|. | 66 // 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. |
| 75 EXPIRE_FAILURE, // Expiration failed. | |
| 64 DELETE_FAILURE, // Deletion failed. | 76 DELETE_FAILURE, // Deletion failed. |
| 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 |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 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 OnGetStorageStatsDone(const ClearPagesCallback& callback, |
| 101 const ArchiveManager::StorageStats& pages); | 113 const ArchiveManager::StorageStats& pages); |
| 102 | 114 |
| 103 // Callback called after getting all pages from client done. | 115 // Callback called after getting all pages from client done. |
| 104 void OnGetAllPagesDone(const ClearPagesCallback& callback, | 116 void OnGetAllPagesDone(const ClearPagesCallback& callback, |
| 105 const ArchiveManager::StorageStats& storage_stats, | 117 const ArchiveManager::StorageStats& storage_stats, |
| 118 const base::Time& now, | |
| 106 const MultipleOfflinePageItemResult& pages); | 119 const MultipleOfflinePageItemResult& pages); |
| 107 | 120 |
| 108 // Callback called after expired pages have been deleted. | 121 // Callback called after expired pages have been deleted. |
| 109 void OnExpiredPagesDeleted(const ClearPagesCallback& callback, | 122 void OnPagesExpired(const ClearPagesCallback& callback, |
| 110 int pages_to_clear, | 123 size_t pages_to_clear, |
| 111 DeletePageResult result); | 124 std::vector<int64_t>* page_ids_to_remove, |
| 125 bool success); | |
| 112 | 126 |
| 113 // Gets offline IDs of all expired pages and return in |offline_ids|. | 127 // Callback called after clearing outdated pages from client. |
| 114 void GetExpiredPageIds(const MultipleOfflinePageItemResult& pages, | 128 void OnDeadPagesCleared(const ClearPagesCallback& callback, |
|
jianli
2016/05/26 21:59:32
replace Dead with Outdated
romax
2016/05/27 00:41:38
Done.
| |
| 129 size_t pages_cleared, | |
| 130 bool success, | |
| 131 DeletePageResult result); | |
| 132 | |
| 133 // Gets offline IDs of both pages that should be expired and the ones that | |
| 134 // need to be removed from metadata store. |page_ids_to_expire| will have | |
| 135 // the pages to be expired, |page_ids_to_remove| will have the pages to be | |
| 136 // removed. |now| is used to keep the time consistecy with other operations. | |
| 137 void GetPageIdsToClear(const MultipleOfflinePageItemResult& pages, | |
| 115 const ArchiveManager::StorageStats& stats, | 138 const ArchiveManager::StorageStats& stats, |
| 116 std::vector<int64_t>& offline_ids); | 139 const base::Time& now, |
| 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 // Determine if manager should clear pages. |
| 119 ClearMode ShouldClearPages(const ArchiveManager::StorageStats& storage_stats); | 144 ClearMode ShouldClearPages(const ArchiveManager::StorageStats& storage_stats, |
| 145 const base::Time& now); | |
| 120 | 146 |
| 121 // Return true if |page| is expired comparing to |now|. | 147 // Return true if |page| is expired comparing to |now|. |
| 122 bool ShouldBeExpired(const base::Time& now, const OfflinePageItem& page); | 148 bool ShouldBeExpired(const base::Time& now, const OfflinePageItem& page); |
| 123 | 149 |
| 124 // Not owned. | 150 // Not owned. |
| 125 Client* client_; | 151 Client* client_; |
| 126 | 152 |
| 127 // Not owned. | 153 // Not owned. |
| 128 ClientPolicyController* policy_controller_; | 154 ClientPolicyController* policy_controller_; |
| 129 | 155 |
| 130 // Not owned. | 156 // Not owned. |
| 131 ArchiveManager* archive_manager_; | 157 ArchiveManager* archive_manager_; |
| 132 | 158 |
| 133 bool in_progress_; | 159 bool in_progress_; |
| 134 | 160 |
| 135 base::Time last_clear_time_; | 161 base::Time last_clear_time_; |
| 136 | 162 |
| 137 // Clock for getting time. | 163 // Clock for getting time. |
| 138 std::unique_ptr<base::Clock> clock_; | 164 std::unique_ptr<base::Clock> clock_; |
| 139 | 165 |
| 140 base::WeakPtrFactory<OfflinePageStorageManager> weak_ptr_factory_; | 166 base::WeakPtrFactory<OfflinePageStorageManager> weak_ptr_factory_; |
| 141 | 167 |
| 142 DISALLOW_COPY_AND_ASSIGN(OfflinePageStorageManager); | 168 DISALLOW_COPY_AND_ASSIGN(OfflinePageStorageManager); |
| 143 }; | 169 }; |
| 144 | 170 |
| 145 } // namespace offline_pages | 171 } // namespace offline_pages |
| 146 | 172 |
| 147 #endif // COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_STORAGE_MANAGER_H_ | 173 #endif // COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_STORAGE_MANAGER_H_ |
| OLD | NEW |