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" | |
| 15 #include "components/offline_pages/offline_page_types.h" | 16 #include "components/offline_pages/offline_page_types.h" |
| 16 | 17 |
| 18 class PrefService; | |
| 17 namespace base { | 19 namespace base { |
| 18 class Clock; | 20 class Clock; |
| 19 } | 21 } |
| 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 minimal | |
| 28 // of (50 MB, 10% of available storage space). | |
|
fgorski
2016/05/17 05:33:29
where did the second number come from?
romax
2016/05/18 02:36:19
it's from the Offline Page Cache prd. second point
| |
| 29 static const int64_t kOfflinePageStorageLimit = 50 * (1 << 20); | |
| 30 static const double kOfflinePageStorageLimitPercentage = 0.1; | |
| 31 static const base::TimeDelta kClearStorageTimeGap = | |
|
fgorski
2016/05/17 05:33:29
kClearStorageInterval
romax
2016/05/18 02:36:19
Done.
| |
| 32 base::TimeDelta::FromHours(6); | |
| 33 static const char kOfflinePageStorageLastClearedTime[] = | |
|
fgorski
2016/05/17 05:33:29
isn't this defined already?
If unaccessible, this
romax
2016/05/18 02:36:20
yes it was unaccessible. removing since we no long
| |
| 34 "offline_page.storage.last_cleared_time"; | |
| 35 | |
| 25 // This class is used for storage management of offline pages. It provides | 36 // This class is used for storage management of offline pages. It provides |
| 26 // a ClearPagesIfNeeded method which is used to clear expired offline pages | 37 // a ClearPagesIfNeeded method which is used to clear expired offline pages |
| 27 // based on last_access_time and lifetime policy of its namespace. | 38 // 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 | 39 // It has its own throttle mechanism so calling the method would not be |
| 29 // guaranteed to clear the pages immediately. | 40 // guaranteed to clear the pages immediately. |
| 30 // | 41 // |
| 31 // OfflinePageModel should own and control the lifecycle of this manager. | 42 // OfflinePageModel should own and control the lifecycle of this manager. |
| 32 // And this manager would use OfflinePageModel to get/remove pages. | 43 // And this manager would use OfflinePageModel to get/remove pages. |
| 33 class OfflinePageStorageManager { | 44 class OfflinePageStorageManager { |
| 34 public: | 45 public: |
| 35 // This interface should have no knowledge of offline page model. | 46 // This interface should have no knowledge of offline page model. |
| 36 // This interface should be implemented by clients managed by storage manager. | 47 // This interface should be implemented by clients managed by storage manager. |
| 37 class Client { | 48 class Client { |
| 38 public: | 49 public: |
| 39 // Asks the client to get all offline pages and invoke |callback|. | 50 // Asks the client to get all offline pages and invoke |callback|. |
| 40 virtual void GetAllPages( | 51 virtual void GetAllPages( |
| 41 const MultipleOfflinePageItemCallback& callback) = 0; | 52 const MultipleOfflinePageItemCallback& callback) = 0; |
| 42 | 53 |
| 43 // Asks the client to delete pages based on |offline_ids| and invoke | 54 // Asks the client to delete pages based on |offline_ids| and invoke |
| 44 // |callback|. | 55 // |callback|. |
| 45 virtual void DeletePagesByOfflineId(const std::vector<int64_t>& offline_ids, | 56 virtual void DeletePagesByOfflineId(const std::vector<int64_t>& offline_ids, |
| 46 const DeletePageCallback& callback) = 0; | 57 const DeletePageCallback& callback) = 0; |
| 47 }; | 58 }; |
| 48 | 59 |
| 60 enum class StorageClearResult { | |
| 61 SUCCESS, // Cleared successfully. | |
| 62 UNNECESSARY, // No expired pages. | |
| 63 DELETE_FAIL, // Deletion failed. | |
|
fgorski
2016/05/17 05:33:29
FAILURE?
romax
2016/05/18 02:36:20
Done.
| |
| 64 }; | |
| 65 | |
| 49 // Callback used when calling ClearPagesIfNeeded. | 66 // Callback used when calling ClearPagesIfNeeded. |
| 50 // int: the number of deleted pages. | 67 // int: the number of deleted pages. |
| 51 // DeletePageResult: result of deleting pages. | 68 // DeletePageResult: result of deleting pages. |
| 52 typedef base::Callback<void(int, DeletePageResult)> ClearPageCallback; | 69 typedef base::Callback<void(int, StorageClearResult)> ClearPageCallback; |
| 53 | 70 |
| 54 explicit OfflinePageStorageManager(Client* client, | 71 explicit OfflinePageStorageManager(Client* client, |
| 55 ClientPolicyController* policy_controller); | 72 ClientPolicyController* policy_controller, |
| 73 PrefService* prefs); | |
| 56 | 74 |
| 57 ~OfflinePageStorageManager(); | 75 ~OfflinePageStorageManager(); |
| 58 | 76 |
| 59 // The manager would *try* to clear pages when called. It may not delete any | 77 // The manager would *try* to clear pages when called. It may not delete any |
| 60 // pages (if clearing condition wasn't satisfied). | 78 // pages (if clearing condition wasn't satisfied). |
| 61 void ClearPagesIfNeeded(const ClearPageCallback& callback); | 79 void ClearPagesIfNeeded(const ClearPageCallback& callback); |
| 62 | 80 |
| 63 // Sets the clock for testing. | 81 // Sets the clock for testing. |
| 64 void SetClockForTesting(std::unique_ptr<base::Clock> clock); | 82 void SetClockForTesting(std::unique_ptr<base::Clock> clock); |
| 65 | 83 |
| 66 private: | 84 private: |
| 85 // Enum indicating how to clear the pages. | |
| 86 enum class ClearMode { | |
| 87 DEFAULT, // Using normal expiration logic. | |
| 88 CLEAR_ALL, // Remove all offline pages. | |
| 89 NO_NEED, // No need to remove any page. | |
|
fgorski
2016/05/17 05:33:29
Please explain that a bit better.
romax
2016/05/18 02:36:20
Done.
| |
| 90 }; | |
| 91 | |
| 67 // Selects and removes pages that need to be expired. Triggered as a callback | 92 // Selects and removes pages that need to be expired. Triggered as a callback |
| 68 // to |GetAllPages|. | 93 // to |GetAllPages|. |
| 69 void ClearExpiredPages(const ClearPageCallback& callback, | 94 void ClearExpiredPages(const ClearPageCallback& callback, |
| 70 const MultipleOfflinePageItemResult& pages); | 95 const MultipleOfflinePageItemResult& pages); |
| 71 | 96 |
| 72 // Gets offline IDs of all expired pages and return in |offline_ids|. | 97 // Gets offline IDs of all expired pages and return in |offline_ids|. |
| 73 void GetExpiredPageIds(const MultipleOfflinePageItemResult& pages, | 98 void GetExpiredPageIds(const MultipleOfflinePageItemResult& pages, |
| 74 std::vector<int64_t>& offline_ids); | 99 std::vector<int64_t>& offline_ids); |
| 75 | 100 |
| 76 // Callback when expired pages has been deleted. | 101 // Callback when expired pages has been deleted. |
| 77 void OnExpiredPagesDeleted(const ClearPageCallback& callback, | 102 void OnExpiredPagesDeleted(const ClearPageCallback& callback, |
| 78 int pages_to_clear, | 103 int pages_to_clear, |
| 79 DeletePageResult result); | 104 DeletePageResult result); |
| 80 | 105 |
| 81 // Determine if manager should clear pages. | 106 // Determine if manager should clear pages. |
| 82 bool ShouldClearPages(); | 107 ClearMode ShouldClearPages(const MultipleOfflinePageItemResult& pages); |
| 83 | 108 |
| 84 // Return true if |page| is expired comparing to |now|. | 109 // Return true if |page| is expired comparing to |now|. |
| 85 bool ShouldBeExpired(const base::Time& now, const OfflinePageItem& page); | 110 bool ShouldBeExpired(const base::Time& now, const OfflinePageItem& page); |
| 86 | 111 |
| 87 // Not owned. | 112 // Not owned. |
| 88 Client* client_; | 113 Client* client_; |
| 89 | 114 |
| 90 // Not owned. | 115 // Not owned. |
| 91 ClientPolicyController* policy_controller_; | 116 ClientPolicyController* policy_controller_; |
| 92 | 117 |
| 118 // Not owned, PrefService which is used to store last clear time. | |
| 119 PrefService* prefs_; | |
| 120 | |
| 93 bool in_progress_; | 121 bool in_progress_; |
| 94 | 122 |
| 95 // Clock for getting time. | 123 // Clock for getting time. |
| 96 std::unique_ptr<base::Clock> clock_; | 124 std::unique_ptr<base::Clock> clock_; |
| 97 | 125 |
| 98 base::WeakPtrFactory<OfflinePageStorageManager> weak_ptr_factory_; | 126 base::WeakPtrFactory<OfflinePageStorageManager> weak_ptr_factory_; |
| 99 | 127 |
| 100 DISALLOW_COPY_AND_ASSIGN(OfflinePageStorageManager); | 128 DISALLOW_COPY_AND_ASSIGN(OfflinePageStorageManager); |
| 101 }; | 129 }; |
| 102 | 130 |
| 103 } // namespace offline_pages | 131 } // namespace offline_pages |
| 104 | 132 |
| 105 #endif // COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_STORAGE_MANAGER_H_ | 133 #endif // COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_STORAGE_MANAGER_H_ |
| OLD | NEW |