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_MODEL_IMPL_H_ | |
6 #define COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_MODEL_IMPL_H_ | |
7 | |
8 #include <stdint.h> | |
9 | |
10 #include <map> | |
11 #include <memory> | |
12 #include <set> | |
13 #include <string> | |
14 #include <utility> | |
15 #include <vector> | |
16 | |
17 #include "base/callback.h" | |
18 #include "base/files/file_path.h" | |
19 #include "base/gtest_prod_util.h" | |
20 #include "base/macros.h" | |
21 #include "base/memory/ref_counted.h" | |
22 #include "base/memory/scoped_vector.h" | |
23 #include "base/memory/weak_ptr.h" | |
24 #include "base/observer_list.h" | |
25 #include "base/optional.h" | |
26 #include "base/strings/string16.h" | |
27 #include "base/time/time.h" | |
28 #include "components/keyed_service/core/keyed_service.h" | |
29 #include "components/offline_pages/offline_page_archiver.h" | |
30 #include "components/offline_pages/offline_page_metadata_store.h" | |
31 #include "components/offline_pages/offline_page_model.h" | |
32 #include "components/offline_pages/offline_page_model_event_logger.h" | |
33 #include "components/offline_pages/offline_page_storage_manager.h" | |
34 #include "components/offline_pages/offline_page_types.h" | |
35 #include "components/offline_pages/offline_store_types.h" | |
36 | |
37 class GURL; | |
38 namespace base { | |
39 class Clock; | |
40 class SequencedTaskRunner; | |
41 class TimeDelta; | |
42 class TimeTicks; | |
43 } // namespace base | |
44 | |
45 namespace offline_pages { | |
46 | |
47 struct ClientId; | |
48 struct OfflinePageItem; | |
49 | |
50 class ArchiveManager; | |
51 class ClientPolicyController; | |
52 class OfflinePageModelQuery; | |
53 class OfflinePageStorageManager; | |
54 | |
55 // Implementation of service for saving pages offline, storing the offline | |
56 // copy and metadata, and retrieving them upon request. | |
57 class OfflinePageModelImpl : public OfflinePageModel, public KeyedService { | |
58 public: | |
59 // All blocking calls/disk access will happen on the provided |task_runner|. | |
60 OfflinePageModelImpl( | |
61 std::unique_ptr<OfflinePageMetadataStore> store, | |
62 const base::FilePath& archives_dir, | |
63 const scoped_refptr<base::SequencedTaskRunner>& task_runner); | |
64 ~OfflinePageModelImpl() override; | |
65 | |
66 // Implemented methods: | |
67 void AddObserver(Observer* observer) override; | |
68 void RemoveObserver(Observer* observer) override; | |
69 void SavePage(const SavePageParams& save_page_params, | |
70 std::unique_ptr<OfflinePageArchiver> archiver, | |
71 const SavePageCallback& callback) override; | |
72 void MarkPageAccessed(int64_t offline_id) override; | |
73 void DeletePagesByOfflineId(const std::vector<int64_t>& offline_ids, | |
74 const DeletePageCallback& callback) override; | |
75 void DeletePagesByClientIds(const std::vector<ClientId>& client_ids, | |
76 const DeletePageCallback& callback) override; | |
77 | |
78 void GetPagesMatchingQuery( | |
79 std::unique_ptr<OfflinePageModelQuery> query, | |
80 const MultipleOfflinePageItemCallback& callback) override; | |
81 | |
82 void GetPagesByClientIds( | |
83 const std::vector<ClientId>& client_ids, | |
84 const MultipleOfflinePageItemCallback& callback) override; | |
85 | |
86 void DeleteCachedPagesByURLPredicate( | |
87 const UrlPredicate& predicate, | |
88 const DeletePageCallback& callback) override; | |
89 void CheckPagesExistOffline( | |
90 const std::set<GURL>& urls, | |
91 const CheckPagesExistOfflineCallback& callback) override; | |
92 void GetAllPages(const MultipleOfflinePageItemCallback& callback) override; | |
93 void GetAllPagesWithExpired( | |
94 const MultipleOfflinePageItemCallback& callback) override; | |
95 void GetOfflineIdsForClientId( | |
96 const ClientId& client_id, | |
97 const MultipleOfflineIdCallback& callback) override; | |
98 void GetPageByOfflineId( | |
99 int64_t offline_id, | |
100 const SingleOfflinePageItemCallback& callback) override; | |
101 void GetPagesByOnlineURL( | |
102 const GURL& online_url, | |
103 const MultipleOfflinePageItemCallback& callback) override; | |
104 void ExpirePages(const std::vector<int64_t>& offline_ids, | |
105 const base::Time& expiration_time, | |
106 const base::Callback<void(bool)>& callback) override; | |
107 ClientPolicyController* GetPolicyController() override; | |
108 | |
109 // Methods for testing only: | |
110 OfflinePageMetadataStore* GetStoreForTesting(); | |
111 void set_testing_clock(base::Clock* clock) { testing_clock_ = clock; } | |
112 | |
113 OfflinePageStorageManager* GetStorageManager(); | |
114 | |
115 bool is_loaded() const override; | |
116 | |
117 OfflineEventLogger* GetLogger() override; | |
118 | |
119 protected: | |
120 // Adding a protected constructor for testing-only purposes in | |
121 // offline_page_storage_manager_unittest.cc | |
122 OfflinePageModelImpl(); | |
123 | |
124 private: | |
125 FRIEND_TEST_ALL_PREFIXES(OfflinePageModelImplTest, MarkPageForDeletion); | |
126 | |
127 typedef ScopedVector<OfflinePageArchiver> PendingArchivers; | |
128 | |
129 // Callback for ensuring archive directory is created. | |
130 void OnEnsureArchivesDirCreatedDone(const base::TimeTicks& start_time); | |
131 | |
132 void CheckPagesExistOfflineAfterLoadDone( | |
133 const std::set<GURL>& urls, | |
134 const CheckPagesExistOfflineCallback& callback); | |
135 void GetOfflineIdsForClientIdWhenLoadDone( | |
136 const ClientId& client_id, | |
137 const MultipleOfflineIdCallback& callback) const; | |
138 void GetPageByOfflineIdWhenLoadDone( | |
139 int64_t offline_id, | |
140 const SingleOfflinePageItemCallback& callback) const; | |
141 const std::vector<int64_t> MaybeGetOfflineIdsForClientId( | |
142 const ClientId& client_id) const; | |
143 void GetPagesByOnlineURLWhenLoadDone( | |
144 const GURL& online_url, | |
145 const MultipleOfflinePageItemCallback& callback) const; | |
146 void MarkPageAccessedWhenLoadDone(int64_t offline_id); | |
147 | |
148 void CheckMetadataConsistency(); | |
149 | |
150 // Callback for loading pages from the offline page metadata store. | |
151 void OnLoadDone(const base::TimeTicks& start_time, | |
152 OfflinePageMetadataStore::LoadStatus load_status, | |
153 const std::vector<OfflinePageItem>& offline_pages); | |
154 | |
155 // Steps for saving a page offline. | |
156 void OnCreateArchiveDone(const SavePageParams& save_page_params, | |
157 int64_t offline_id, | |
158 const base::Time& start_time, | |
159 const SavePageCallback& callback, | |
160 OfflinePageArchiver* archiver, | |
161 OfflinePageArchiver::ArchiverResult result, | |
162 const GURL& url, | |
163 const base::FilePath& file_path, | |
164 const base::string16& title, | |
165 int64_t file_size); | |
166 void OnAddOfflinePageDone(OfflinePageArchiver* archiver, | |
167 const SavePageCallback& callback, | |
168 const OfflinePageItem& offline_page, | |
169 ItemActionStatus status); | |
170 void InformSavePageDone(const SavePageCallback& callback, | |
171 SavePageResult result, | |
172 const ClientId& client_id, | |
173 int64_t offline_id); | |
174 void DeletePendingArchiver(OfflinePageArchiver* archiver); | |
175 | |
176 // Steps for deleting files and data for an offline page. | |
177 void OnDeleteArchiveFilesDone(const std::vector<int64_t>& offline_ids, | |
178 const DeletePageCallback& callback, | |
179 bool success); | |
180 void OnRemoveOfflinePagesDone( | |
181 const DeletePageCallback& callback, | |
182 std::unique_ptr<OfflinePagesUpdateResult> result); | |
183 void InformDeletePageDone(const DeletePageCallback& callback, | |
184 DeletePageResult result); | |
185 | |
186 void OnMarkPageAccesseDone(const OfflinePageItem& offline_page_item, | |
187 std::unique_ptr<OfflinePagesUpdateResult> result); | |
188 | |
189 // Callbacks for checking metadata consistency. | |
190 void CheckMetadataConsistencyForArchivePaths( | |
191 const std::set<base::FilePath>& archive_paths); | |
192 // Callback called after headless archives deleted. Orphaned archives are | |
193 // archives files on disk which are not pointed to by any of the page items | |
194 // in metadata store. | |
195 void ExpirePagesMissingArchiveFile( | |
196 const std::set<base::FilePath>& archive_paths); | |
197 void OnExpirePagesMissingArchiveFileDone( | |
198 const std::vector<int64_t>& offline_ids, | |
199 bool success); | |
200 void DeleteOrphanedArchives(const std::set<base::FilePath>& archive_paths); | |
201 void OnDeleteOrphanedArchivesDone(const std::vector<base::FilePath>& archives, | |
202 bool success); | |
203 | |
204 // Callbacks for deleting pages with same URL when saving pages. | |
205 void DeleteExistingPagesWithSameURL(const OfflinePageItem& offline_page); | |
206 void OnPagesFoundWithSameURL(const OfflinePageItem& offline_page, | |
207 size_t pages_allowed, | |
208 const MultipleOfflinePageItemResult& items); | |
209 void OnDeleteOldPagesWithSameURL(DeletePageResult result); | |
210 | |
211 void CacheLoadedData(const std::vector<OfflinePageItem>& offline_pages); | |
212 | |
213 // Actually does the work of deleting, requires the model is loaded. | |
214 void DoDeletePagesByOfflineId(const std::vector<int64_t>& offline_ids, | |
215 const DeletePageCallback& callback); | |
216 | |
217 // Actually does the work of deleting, requires the model is loaded. | |
218 void DeletePages(const DeletePageCallback& callback, | |
219 const MultipleOfflinePageItemResult& items); | |
220 | |
221 void DoGetPagesByClientIds(const std::vector<ClientId>& client_ids, | |
222 const MultipleOfflinePageItemCallback& callback); | |
223 | |
224 // Similar to DoDeletePagesByOfflineId, does actual work of deleting, and | |
225 // requires that the model is loaded. | |
226 void DoDeleteCachedPagesByURLPredicate(const UrlPredicate& predicate, | |
227 const DeletePageCallback& callback); | |
228 | |
229 // Callback completing page expiration. | |
230 void OnExpirePageDone(const base::Time& expiration_time, | |
231 std::unique_ptr<OfflinePagesUpdateResult> result); | |
232 | |
233 // Clears expired pages if there are any. | |
234 void ClearStorageIfNeeded( | |
235 const OfflinePageStorageManager::ClearStorageCallback& callback); | |
236 | |
237 // Callback completing storage clearing. | |
238 void OnStorageCleared(size_t expired_page_count, | |
239 OfflinePageStorageManager::ClearStorageResult result); | |
240 | |
241 // Post task to clear storage. | |
242 void PostClearStorageIfNeededTask(); | |
243 | |
244 // Check if |offline_page| should be removed on cache reset by user. | |
245 bool IsRemovedOnCacheReset(const OfflinePageItem& offline_page) const; | |
246 | |
247 void RunWhenLoaded(const base::Closure& job); | |
248 | |
249 base::Time GetCurrentTime() const; | |
250 | |
251 // Persistent store for offline page metadata. | |
252 std::unique_ptr<OfflinePageMetadataStore> store_; | |
253 | |
254 // Location where all of the archive files will be stored. | |
255 base::FilePath archives_dir_; | |
256 | |
257 // The observers. | |
258 base::ObserverList<Observer> observers_; | |
259 | |
260 bool is_loaded_; | |
261 | |
262 // In memory copy of the offline page metadata, keyed by offline IDs. | |
263 std::map<int64_t, OfflinePageItem> offline_pages_; | |
264 | |
265 // Pending archivers owned by this model. | |
266 PendingArchivers pending_archivers_; | |
267 | |
268 // Delayed tasks that should be invoked after the loading is done. | |
269 std::vector<base::Closure> delayed_tasks_; | |
270 | |
271 // Controller of the client policies. | |
272 std::unique_ptr<ClientPolicyController> policy_controller_; | |
273 | |
274 // Manager for the storage consumed by archives and responsible for | |
275 // automatic page clearing. | |
276 std::unique_ptr<OfflinePageStorageManager> storage_manager_; | |
277 | |
278 // Manager for the offline archive files and directory. | |
279 std::unique_ptr<ArchiveManager> archive_manager_; | |
280 | |
281 // Logger to facilitate recording of events. | |
282 OfflinePageModelEventLogger offline_event_logger_; | |
283 | |
284 // Clock for getting time in testing code. The setter is responsible to reset | |
285 // it once it is not longer needed. | |
286 base::Clock* testing_clock_; | |
287 | |
288 base::WeakPtrFactory<OfflinePageModelImpl> weak_ptr_factory_; | |
289 | |
290 DISALLOW_COPY_AND_ASSIGN(OfflinePageModelImpl); | |
291 }; | |
292 | |
293 } // namespace offline_pages | |
294 | |
295 #endif // COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_MODEL_IMPL_H_ | |
OLD | NEW |