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 // Limit of the total storage space occupied by offline pages should be 30% of |
| 26 // available storage. And we clear storage when it is over the threshold, | 26 // available storage. And we clear storage when it is over the threshold, |
| 27 // reducing the usage below threshold. | 27 // reducing the usage below the 10% threshold. |
|
jianli
2016/05/24 23:43:32
nit: try to avoid mentioning the value in the comm
romax
2016/05/25 20:05:23
Done.
| |
| 28 const double kOfflinePageStorageLimit = 0.3; | 28 const double kOfflinePageStorageLimit = 0.3; |
| 29 const double kOfflinePageStorageClearThreshold = 0.1; | 29 const double kOfflinePageStorageClearThreshold = 0.1; |
| 30 // If it's more than 10 minutes since the last storage clearing, it's a good | |
| 31 // time to clear storage again to check for expired pages. And if a page has | |
| 32 // been expired more than 21 days it should be deleted from metadata store. | |
|
jianli
2016/05/24 23:43:32
ditto
romax
2016/05/25 20:05:23
Done.
| |
| 30 const base::TimeDelta kClearStorageInterval = base::TimeDelta::FromMinutes(10); | 33 const base::TimeDelta kClearStorageInterval = base::TimeDelta::FromMinutes(10); |
| 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 get all offline pages and invoke |callback|. |
| 52 virtual void GetAllPages( | 56 virtual void GetAllPages( |
| 53 const MultipleOfflinePageItemCallback& callback) = 0; | 57 const MultipleOfflinePageItemCallback& callback) = 0; |
| 54 | 58 |
| 55 // Asks the client to delete pages based on |offline_ids| and invoke | 59 // Asks the client to mark pages with |offline_ids| as expired and the |
| 56 // |callback|. | 60 // associated archive files. |
| 57 virtual void DeletePagesByOfflineId(const std::vector<int64_t>& offline_ids, | 61 virtual void ExpirePages(const std::vector<int64_t>& offline_ids, |
| 58 const DeletePageCallback& callback) = 0; | 62 const base::Time& expiration_time, |
| 63 const DeletePageCallback& callback) = 0; | |
|
jianli
2016/05/24 23:43:32
nit: try not to use DeletePageCallback since Delet
romax
2016/05/25 20:05:23
Done.
I realized there's no need to add a enum her
| |
| 64 | |
| 65 // Asks the client to remove page items with |offline_ids| from metadata | |
| 66 // store and in memory mapping. | |
| 67 virtual void RemovePageItems( | |
| 68 const std::vector<int64_t>& offline_ids, | |
| 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 DELETE_FAILURE, // Deletion failed. |
| 65 }; | 76 }; |
| 66 | 77 |
| 67 // Callback used when calling ClearPagesIfNeeded. | 78 // Callback used when calling ClearPagesIfNeeded. |
| 68 // int: the number of expired pages. | 79 // int: the number of expired pages. |
| 69 // ClearStorageResult: result of expiring pages in storage. | 80 // ClearStorageResult: result of expiring pages in storage. |
| 70 typedef base::Callback<void(int, ClearStorageResult)> ClearPagesCallback; | 81 // bool: true if removing dead pages from metadata store is successful. |
| 82 typedef base::Callback<void(int, ClearStorageResult, bool)> | |
| 83 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 |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 101 const ArchiveManager::StorageStats& pages); | 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. |
| 104 void OnGetAllPagesDone(const ClearPagesCallback& callback, | 117 void OnGetAllPagesDone(const ClearPagesCallback& callback, |
| 105 const ArchiveManager::StorageStats& storage_stats, | 118 const ArchiveManager::StorageStats& storage_stats, |
| 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 OnExpiredPagesDeleted(const ClearPagesCallback& callback, |
| 110 int pages_to_clear, | 123 int pages_to_clear, |
| 124 const std::vector<int64_t>& page_ids_to_remove, | |
| 111 DeletePageResult result); | 125 DeletePageResult result); |
| 112 | 126 |
| 127 // Callback called after clearing outdated pages from client. | |
| 128 void OnExpiredPagesCleared(const ClearPagesCallback& callback, | |
| 129 int pages_cleared, | |
| 130 DeletePageResult result, | |
| 131 bool success); | |
| 132 | |
| 113 // Gets offline IDs of all expired pages and return in |offline_ids|. | 133 // Gets offline IDs of all expired pages and return in |offline_ids|. |
|
jianli
2016/05/24 23:43:32
Please update the comment. Please also explain the
romax
2016/05/25 20:05:23
Done.
| |
| 114 void GetExpiredPageIds(const MultipleOfflinePageItemResult& pages, | 134 void GetExpiredPageIds(const MultipleOfflinePageItemResult& pages, |
| 115 const ArchiveManager::StorageStats& stats, | 135 const ArchiveManager::StorageStats& stats, |
| 116 std::vector<int64_t>& offline_ids); | 136 std::vector<int64_t>& page_ids_to_expire, |
| 137 std::vector<int64_t>& page_ids_to_remove); | |
| 117 | 138 |
| 118 // Determine if manager should clear pages. | 139 // Determine if manager should clear pages. |
| 119 ClearMode ShouldClearPages(const ArchiveManager::StorageStats& storage_stats); | 140 ClearMode ShouldClearPages(const ArchiveManager::StorageStats& storage_stats); |
| 120 | 141 |
| 121 // Return true if |page| is expired comparing to |now|. | 142 // Return true if |page| is expired comparing to |now|. |
| 122 bool ShouldBeExpired(const base::Time& now, const OfflinePageItem& page); | 143 bool ShouldBeExpired(const base::Time& now, const OfflinePageItem& page); |
| 123 | 144 |
| 124 // Not owned. | 145 // Not owned. |
| 125 Client* client_; | 146 Client* client_; |
| 126 | 147 |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 138 std::unique_ptr<base::Clock> clock_; | 159 std::unique_ptr<base::Clock> clock_; |
| 139 | 160 |
| 140 base::WeakPtrFactory<OfflinePageStorageManager> weak_ptr_factory_; | 161 base::WeakPtrFactory<OfflinePageStorageManager> weak_ptr_factory_; |
| 141 | 162 |
| 142 DISALLOW_COPY_AND_ASSIGN(OfflinePageStorageManager); | 163 DISALLOW_COPY_AND_ASSIGN(OfflinePageStorageManager); |
| 143 }; | 164 }; |
| 144 | 165 |
| 145 } // namespace offline_pages | 166 } // namespace offline_pages |
| 146 | 167 |
| 147 #endif // COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_STORAGE_MANAGER_H_ | 168 #endif // COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_STORAGE_MANAGER_H_ |
| OLD | NEW |