| 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" |
| 16 #include "components/offline_pages/archive_manager.h" |
| 15 #include "components/offline_pages/offline_page_types.h" | 17 #include "components/offline_pages/offline_page_types.h" |
| 16 | 18 |
| 17 namespace base { | 19 namespace base { |
| 18 class Clock; | 20 class Clock; |
| 19 } | 21 } // namespace base |
| 20 | 22 |
| 21 namespace offline_pages { | 23 namespace offline_pages { |
| 22 | 24 |
| 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, |
| 27 // reducing the usage below threshold. |
| 28 const double kOfflinePageStorageLimit = 0.3; |
| 29 const double kOfflinePageStorageClearThreshold = 0.1; |
| 30 const base::TimeDelta kClearStorageInterval = base::TimeDelta::FromMinutes(10); |
| 31 |
| 32 class ArchiveManager; |
| 23 class ClientPolicyController; | 33 class ClientPolicyController; |
| 24 | 34 |
| 25 // This class is used for storage management of offline pages. It provides | 35 // This class is used for storage management of offline pages. It provides |
| 26 // a ClearPagesIfNeeded method which is used to clear expired offline pages | 36 // a ClearPagesIfNeeded method which is used to clear expired offline pages |
| 27 // based on last_access_time and lifetime policy of its namespace. | 37 // based on last_access_time and lifetime policy of its namespace. |
| 28 // It has its own throttle mechanism so calling the method would not be | 38 // It has its own throttle mechanism so calling the method would not be |
| 29 // guaranteed to clear the pages immediately. | 39 // guaranteed to clear the pages immediately. |
| 30 // | 40 // |
| 31 // OfflinePageModel should own and control the lifecycle of this manager. | 41 // OfflinePageModel should own and control the lifecycle of this manager. |
| 32 // And this manager would use OfflinePageModel to get/remove pages. | 42 // And this manager would use OfflinePageModel to get/remove pages. |
| 33 class OfflinePageStorageManager { | 43 class OfflinePageStorageManager { |
| 34 public: | 44 public: |
| 35 // This interface should have no knowledge of offline page model. | 45 // This interface should have no knowledge of offline page model. |
| 36 // This interface should be implemented by clients managed by storage manager. | 46 // This interface should be implemented by clients managed by storage manager. |
| 37 class Client { | 47 class Client { |
| 38 public: | 48 public: |
| 39 virtual ~Client() {} | 49 virtual ~Client() {} |
| 40 | 50 |
| 41 // Asks the client to get all offline pages and invoke |callback|. | 51 // Asks the client to get all offline pages and invoke |callback|. |
| 42 virtual void GetAllPages( | 52 virtual void GetAllPages( |
| 43 const MultipleOfflinePageItemCallback& callback) = 0; | 53 const MultipleOfflinePageItemCallback& callback) = 0; |
| 44 | 54 |
| 45 // Asks the client to delete pages based on |offline_ids| and invoke | 55 // Asks the client to delete pages based on |offline_ids| and invoke |
| 46 // |callback|. | 56 // |callback|. |
| 47 virtual void DeletePagesByOfflineId(const std::vector<int64_t>& offline_ids, | 57 virtual void DeletePagesByOfflineId(const std::vector<int64_t>& offline_ids, |
| 48 const DeletePageCallback& callback) = 0; | 58 const DeletePageCallback& callback) = 0; |
| 49 }; | 59 }; |
| 50 | 60 |
| 61 enum class ClearStorageResult { |
| 62 SUCCESS, // Cleared successfully. |
| 63 UNNECESSARY, // No expired pages. |
| 64 DELETE_FAILURE, // Deletion failed. |
| 65 }; |
| 66 |
| 51 // Callback used when calling ClearPagesIfNeeded. | 67 // Callback used when calling ClearPagesIfNeeded. |
| 52 // int: the number of deleted pages. | 68 // int: the number of expired pages. |
| 53 // DeletePageResult: result of deleting pages. | 69 // ClearStorageResult: result of expiring pages in storage. |
| 54 typedef base::Callback<void(int, DeletePageResult)> ClearPageCallback; | 70 typedef base::Callback<void(int, ClearStorageResult)> ClearPagesCallback; |
| 55 | 71 |
| 56 explicit OfflinePageStorageManager(Client* client, | 72 explicit OfflinePageStorageManager(Client* client, |
| 57 ClientPolicyController* policy_controller); | 73 ClientPolicyController* policy_controller, |
| 74 ArchiveManager* archive_manager); |
| 58 | 75 |
| 59 ~OfflinePageStorageManager(); | 76 ~OfflinePageStorageManager(); |
| 60 | 77 |
| 61 // 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 |
| 62 // pages (if clearing condition wasn't satisfied). | 79 // pages (if clearing condition wasn't satisfied). |
| 63 void ClearPagesIfNeeded(const ClearPageCallback& callback); | 80 // 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 |
| 82 // certain interval. Both values are defined above. |
| 83 void ClearPagesIfNeeded(const ClearPagesCallback& callback); |
| 64 | 84 |
| 65 // Sets the clock for testing. | 85 // Sets the clock for testing. |
| 66 void SetClockForTesting(std::unique_ptr<base::Clock> clock); | 86 void SetClockForTesting(std::unique_ptr<base::Clock> clock); |
| 67 | 87 |
| 68 private: | 88 private: |
| 69 // Selects and removes pages that need to be expired. Triggered as a callback | 89 // Enum indicating how to clear the pages. |
| 70 // to |GetAllPages|. | 90 enum class ClearMode { |
| 71 void ClearExpiredPages(const ClearPageCallback& callback, | 91 // Using normal expiration logic to expire pages. Will reduce the storage |
| 92 // usage down below the threshold. |
| 93 DEFAULT, |
| 94 // No need to expire any page (no pages in the model or no expired |
| 95 // pages and we're not exceeding the storage limit.) |
| 96 NOT_NEEDED, |
| 97 }; |
| 98 |
| 99 // Callback called after getting storage stats from archive manager. |
| 100 void OnGetStorageStatsDone(const ClearPagesCallback& callback, |
| 101 const ArchiveManager::StorageStats& pages); |
| 102 |
| 103 // Callback called after getting all pages from client done. |
| 104 void OnGetAllPagesDone(const ClearPagesCallback& callback, |
| 105 const ArchiveManager::StorageStats& storage_stats, |
| 72 const MultipleOfflinePageItemResult& pages); | 106 const MultipleOfflinePageItemResult& pages); |
| 73 | 107 |
| 108 // Callback called after expired pages have been deleted. |
| 109 void OnExpiredPagesDeleted(const ClearPagesCallback& callback, |
| 110 int pages_to_clear, |
| 111 DeletePageResult result); |
| 112 |
| 74 // Gets offline IDs of all expired pages and return in |offline_ids|. | 113 // Gets offline IDs of all expired pages and return in |offline_ids|. |
| 75 void GetExpiredPageIds(const MultipleOfflinePageItemResult& pages, | 114 void GetExpiredPageIds(const MultipleOfflinePageItemResult& pages, |
| 115 const ArchiveManager::StorageStats& stats, |
| 76 std::vector<int64_t>& offline_ids); | 116 std::vector<int64_t>& offline_ids); |
| 77 | 117 |
| 78 // Callback when expired pages has been deleted. | |
| 79 void OnExpiredPagesDeleted(const ClearPageCallback& callback, | |
| 80 int pages_to_clear, | |
| 81 DeletePageResult result); | |
| 82 | |
| 83 // Determine if manager should clear pages. | 118 // Determine if manager should clear pages. |
| 84 bool ShouldClearPages(); | 119 ClearMode ShouldClearPages(const ArchiveManager::StorageStats& storage_stats); |
| 85 | 120 |
| 86 // Return true if |page| is expired comparing to |now|. | 121 // Return true if |page| is expired comparing to |now|. |
| 87 bool ShouldBeExpired(const base::Time& now, const OfflinePageItem& page); | 122 bool ShouldBeExpired(const base::Time& now, const OfflinePageItem& page); |
| 88 | 123 |
| 89 // Not owned. | 124 // Not owned. |
| 90 Client* client_; | 125 Client* client_; |
| 91 | 126 |
| 92 // Not owned. | 127 // Not owned. |
| 93 ClientPolicyController* policy_controller_; | 128 ClientPolicyController* policy_controller_; |
| 94 | 129 |
| 130 // Not owned. |
| 131 ArchiveManager* archive_manager_; |
| 132 |
| 95 bool in_progress_; | 133 bool in_progress_; |
| 96 | 134 |
| 135 base::Time last_clear_time_; |
| 136 |
| 97 // Clock for getting time. | 137 // Clock for getting time. |
| 98 std::unique_ptr<base::Clock> clock_; | 138 std::unique_ptr<base::Clock> clock_; |
| 99 | 139 |
| 100 base::WeakPtrFactory<OfflinePageStorageManager> weak_ptr_factory_; | 140 base::WeakPtrFactory<OfflinePageStorageManager> weak_ptr_factory_; |
| 101 | 141 |
| 102 DISALLOW_COPY_AND_ASSIGN(OfflinePageStorageManager); | 142 DISALLOW_COPY_AND_ASSIGN(OfflinePageStorageManager); |
| 103 }; | 143 }; |
| 104 | 144 |
| 105 } // namespace offline_pages | 145 } // namespace offline_pages |
| 106 | 146 |
| 107 #endif // COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_STORAGE_MANAGER_H_ | 147 #endif // COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_STORAGE_MANAGER_H_ |
| OLD | NEW |