OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_STORAGE_MANAGER_H_ | |
6 #define COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_STORAGE_MANAGER_H_ | |
7 | |
8 #include <stdint.h> | |
9 | |
10 #include <vector> | |
11 | |
12 #include "base/callback.h" | |
13 #include "base/macros.h" | |
14 #include "base/memory/weak_ptr.h" | |
15 #include "base/time/time.h" | |
16 #include "components/offline_pages/archive_manager.h" | |
17 #include "components/offline_pages/offline_page_types.h" | |
18 | |
19 namespace base { | |
20 class Clock; | |
21 } // namespace base | |
22 | |
23 namespace offline_pages { | |
24 | |
25 // Maximum % of total available storage that will be occupied by offline pages | |
26 // before a storage clearup. | |
27 struct constants { | |
28 static constexpr double kOfflinePageStorageLimit = 0.3; | |
29 // The target % of storage usage we try to reach below when expiring pages. | |
30 static constexpr double kOfflinePageStorageClearThreshold = 0.1; | |
31 // The time that the storage cleanup will be triggered again since the last | |
32 // one. | |
33 static constexpr base::TimeDelta kClearStorageInterval = | |
34 base::TimeDelta::FromMinutes(10); | |
35 // The time that the page record will be removed from the store since the page | |
36 // has been expired. | |
37 static constexpr base::TimeDelta kRemovePageItemInterval = | |
38 base::TimeDelta::FromDays(21); | |
39 }; | |
40 | |
41 class ClientPolicyController; | |
42 class OfflinePageModel; | |
43 | |
44 // This class is used for storage management of offline pages. It provides | |
45 // a ClearPagesIfNeeded method which is used to clear outdated offline pages | |
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 | |
48 // guaranteed to clear the pages immediately. | |
49 // | |
50 // OfflinePageModel should own and control the lifecycle of this manager. | |
51 // And this manager would use OfflinePageModel to get/remove pages. | |
52 class OfflinePageStorageManager { | |
53 public: | |
54 enum class ClearStorageResult { | |
55 SUCCESS, // Cleared successfully. | |
56 UNNECESSARY, // No expired pages. | |
57 DEPRECATED_EXPIRE_FAILURE, // Expiration failed. (DEPRECATED) | |
58 DELETE_FAILURE, // Deletion failed. | |
59 DEPRECATED_EXPIRE_AND_DELETE_FAILURES, // Both expiration and deletion | |
60 // failed. (DEPRECATED) | |
61 // NOTE: always keep this entry at the end. Add new result types only | |
62 // immediately above this line. Make sure to update the corresponding | |
63 // histogram enum accordingly. | |
64 RESULT_COUNT, | |
65 }; | |
66 | |
67 // Callback used when calling ClearPagesIfNeeded. | |
68 // size_t: the number of cleared pages. | |
69 // ClearStorageResult: result of clearing pages in storage. | |
70 typedef base::Callback<void(size_t, ClearStorageResult)> ClearStorageCallback; | |
71 | |
72 explicit OfflinePageStorageManager(OfflinePageModel* model, | |
73 ClientPolicyController* policy_controller, | |
74 ArchiveManager* archive_manager); | |
75 | |
76 ~OfflinePageStorageManager(); | |
77 | |
78 // The manager would *try* to clear pages when called. It may not delete any | |
79 // pages (if clearing condition wasn't satisfied). | |
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 ClearStorageCallback& callback); | |
84 | |
85 // Sets the clock for testing. | |
86 void SetClockForTesting(std::unique_ptr<base::Clock> clock); | |
87 | |
88 private: | |
89 // Enum indicating how to clear the pages. | |
90 enum class ClearMode { | |
91 // Using normal expiration logic to clear pages. Will reduce the storage | |
92 // usage down below the threshold. | |
93 DEFAULT, | |
94 // No need to clear any page (no pages in the model or no expired pages and | |
95 // we're not exceeding the storage limit.) | |
96 NOT_NEEDED, | |
97 }; | |
98 | |
99 // Callback called after getting storage stats from archive manager. | |
100 void OnGetStorageStatsDoneForClearingPages( | |
101 const ClearStorageCallback& callback, | |
102 const ArchiveManager::StorageStats& pages); | |
103 | |
104 // Callback called after getting all pages from model. | |
105 void OnGetAllPagesDoneForClearingPages( | |
106 const ClearStorageCallback& callback, | |
107 const ArchiveManager::StorageStats& storage_stats, | |
108 const MultipleOfflinePageItemResult& pages); | |
109 | |
110 // Callback called after clearing expired pages from model. | |
111 void OnExpiredPagesCleared(const ClearStorageCallback& callback, | |
112 size_t pages_cleared, | |
113 DeletePageResult result); | |
114 | |
115 // Gets offline IDs of pages that should be cleared based on current |stats| | |
116 // and return the IDs in |page_ids_to_clear|. | |
117 void GetPageIdsToClear(const MultipleOfflinePageItemResult& pages, | |
118 const ArchiveManager::StorageStats& stats, | |
119 std::vector<int64_t>* page_ids_to_clear); | |
120 | |
121 // Determines if manager should clear pages. | |
122 ClearMode ShouldClearPages(const ArchiveManager::StorageStats& storage_stats); | |
123 | |
124 // Returns true if |page| is should be cleared based on |clear_time_|. | |
125 bool IsExpired(const OfflinePageItem& page) const; | |
126 | |
127 // Returns true if we're currently doing a cleanup. | |
128 bool IsInProgress() const; | |
129 | |
130 // Not owned. | |
131 OfflinePageModel* model_; | |
132 | |
133 // Not owned. | |
134 ClientPolicyController* policy_controller_; | |
135 | |
136 // Not owned. | |
137 ArchiveManager* archive_manager_; | |
138 | |
139 // Starting time of the current storage cleanup. If this time is later than | |
140 // |last_clear_time_| it means we're doing a cleanup. | |
141 base::Time clear_time_; | |
142 | |
143 // Timestamp of last storage cleanup. | |
144 base::Time last_clear_time_; | |
145 | |
146 // Clock for getting time. | |
147 std::unique_ptr<base::Clock> clock_; | |
148 | |
149 base::WeakPtrFactory<OfflinePageStorageManager> weak_ptr_factory_; | |
150 | |
151 DISALLOW_COPY_AND_ASSIGN(OfflinePageStorageManager); | |
152 }; | |
153 | |
154 } // namespace offline_pages | |
155 | |
156 #endif // COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_STORAGE_MANAGER_H_ | |
OLD | NEW |