Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(635)

Side by Side Diff: components/offline_pages/offline_page_storage_manager.h

Issue 1986673002: [Offline Pages] Updated clearing logic in storage manager. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Refactored to short circuit earlier if no need to clear. Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
fgorski 2016/05/19 04:14:22 you can forward declare that one.
romax 2016/05/19 18:34:42 I'm also using ArchiveManager::StorageStats, so I
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 }
fgorski 2016/05/19 04:14:22 } // namespace base
romax 2016/05/19 18:34:42 Done.
20 22
21 namespace offline_pages { 23 namespace offline_pages {
22 24
23 class ClientPolicyController; 25 class ClientPolicyController;
24 26
27 // Limit of the total storage space occupied by offline pages should be 30% of
28 // available storage. And we clear storage when it is over the threshold,
29 // reducing the usage below threshold.
30 static const double kOfflinePageStorageLimit = 0.3;
fgorski 2016/05/19 04:14:22 remove static, you are not inside of a class. Als
romax 2016/05/19 18:34:42 Done. Moved them into an anonymous namespace.
31 static const double kOfflinePageStorageClearThreshold = 0.1;
32 static const base::TimeDelta kClearStorageInterval =
33 base::TimeDelta::FromMinutes(10);
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 StorageClearResult {
fgorski 2016/05/19 04:14:22 ClearStorageResult
romax 2016/05/19 18:34:42 Done.
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 // StorageClearResult: result of expiring pages in storage.
54 typedef base::Callback<void(int, DeletePageResult)> ClearPageCallback; 70 typedef base::Callback<void(int, StorageClearResult)> ClearPageCallback;
fgorski 2016/05/19 04:14:22 ClearPagesCallback
romax 2016/05/19 18:34:42 Done.
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).
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.
63 void ClearPagesIfNeeded(const ClearPageCallback& callback); 83 void ClearPagesIfNeeded(const ClearPageCallback& 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.)
fgorski 2016/05/19 04:14:22 why do we have unnecessary and no need?
romax 2016/05/19 18:34:42 I just want to provide a way to identify that we'r
96 NO_NEED,
fgorski 2016/05/19 04:14:22 NOT_NEEDED
romax 2016/05/19 18:34:42 Done.
97 };
98
99 // Callback called after getting storage stats from archive manager.
100 void OnGetStorageStatsDone(const ClearPageCallback& callback,
101 const ArchiveManager::StorageStats& pages);
102
103 // Callback called after getting all pages from client done.
104 void OnGetAllPagesDone(const ClearPageCallback& callback,
105 const ArchiveManager::StorageStats& storage_stats,
72 const MultipleOfflinePageItemResult& pages); 106 const MultipleOfflinePageItemResult& pages);
73 107
74 // Gets offline IDs of all expired pages and return in |offline_ids|. 108 // Callback called after expired pages have been deleted.
75 void GetExpiredPageIds(const MultipleOfflinePageItemResult& pages,
76 std::vector<int64_t>& offline_ids);
77
78 // Callback when expired pages has been deleted.
79 void OnExpiredPagesDeleted(const ClearPageCallback& callback, 109 void OnExpiredPagesDeleted(const ClearPageCallback& callback,
80 int pages_to_clear, 110 int pages_to_clear,
81 DeletePageResult result); 111 DeletePageResult result);
82 112
113 // Gets offline IDs of all expired pages and return in |offline_ids|.
114 void GetExpiredPageIds(const MultipleOfflinePageItemResult& pages,
115 std::vector<int64_t>& offline_ids,
116 const ArchiveManager::StorageStats& stats);
117
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_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698