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