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