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

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

Issue 2011763005: Splits the OfflinePageModel into and interface and and implementation class. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: 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 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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_MODEL_H_ 5 #ifndef COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_MODEL_H_
6 #define COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_MODEL_H_ 6 #define COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_MODEL_H_
7 7
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <map>
11 #include <memory> 10 #include <memory>
12 #include <set> 11 #include <set>
12 #include <string>
13 #include <vector> 13 #include <vector>
14 14
15 #include "base/callback.h" 15 #include "base/callback.h"
16 #include "base/files/file_path.h"
17 #include "base/gtest_prod_util.h" 16 #include "base/gtest_prod_util.h"
18 #include "base/macros.h" 17 #include "base/macros.h"
19 #include "base/memory/ref_counted.h" 18 #include "base/memory/ref_counted.h"
20 #include "base/memory/scoped_vector.h" 19 #include "base/memory/scoped_vector.h"
21 #include "base/memory/weak_ptr.h"
22 #include "base/observer_list.h"
23 #include "base/optional.h" 20 #include "base/optional.h"
romax 2016/05/26 01:26:48 this seems not being used in header anymore.
dougarnett 2016/05/26 16:03:22 Done.
24 #include "base/supports_user_data.h" 21 #include "base/supports_user_data.h"
25 #include "components/keyed_service/core/keyed_service.h" 22 #include "components/keyed_service/core/keyed_service.h"
26 #include "components/offline_pages/offline_page_archiver.h" 23 #include "components/offline_pages/offline_page_archiver.h"
27 #include "components/offline_pages/offline_page_metadata_store.h"
28 #include "components/offline_pages/offline_page_storage_manager.h" 24 #include "components/offline_pages/offline_page_storage_manager.h"
29 #include "components/offline_pages/offline_page_types.h" 25 #include "components/offline_pages/offline_page_types.h"
30 26
31 class GURL; 27 class GURL;
32 namespace base { 28 namespace base {
33 class SequencedTaskRunner;
34 class Time; 29 class Time;
35 class TimeDelta;
36 class TimeTicks;
37 } // namespace base 30 } // namespace base
38 31
39 namespace offline_pages { 32 namespace offline_pages {
40 33
41 static const char* const kBookmarkNamespace = "bookmark"; 34 static const char* const kBookmarkNamespace = "bookmark";
42 static const int64_t kInvalidOfflineId = 0; 35 static const int64_t kInvalidOfflineId = 0;
43 36
44 struct ClientId; 37 struct ClientId;
45 struct OfflinePageItem; 38 struct OfflinePageItem;
46 39
47 class ArchiveManager;
48 class ClientPolicyController; 40 class ClientPolicyController;
romax 2016/05/26 01:26:48 can be removed?
dougarnett 2016/05/26 16:03:22 Done.
49 class OfflinePageMetadataStore;
50 class OfflinePageStorageManager; 41 class OfflinePageStorageManager;
romax 2016/05/26 01:26:48 can be removed.
dougarnett 2016/05/26 16:03:22 Done.
51 42
52 // Service for saving pages offline, storing the offline copy and metadata, and 43 // Service for saving pages offline, storing the offline copy and metadata, and
53 // retrieving them upon request. 44 // retrieving them upon request.
54 // 45 //
55 // Example usage: 46 // Example usage:
56 // class ArchiverImpl : public OfflinePageArchiver { 47 // class ArchiverImpl : public OfflinePageArchiver {
57 // // This is a class that knows how to create archiver 48 // // This is a class that knows how to create archiver
58 // void CreateArchiver(...) override; 49 // void CreateArchiver(...) override;
59 // ... 50 // ...
60 // } 51 // }
(...skipping 28 matching lines...) Expand all
89 protected: 80 protected:
90 virtual ~Observer() {} 81 virtual ~Observer() {}
91 }; 82 };
92 83
93 using CheckPagesExistOfflineResult = 84 using CheckPagesExistOfflineResult =
94 offline_pages::CheckPagesExistOfflineResult; 85 offline_pages::CheckPagesExistOfflineResult;
95 using MultipleOfflinePageItemResult = 86 using MultipleOfflinePageItemResult =
96 offline_pages::MultipleOfflinePageItemResult; 87 offline_pages::MultipleOfflinePageItemResult;
97 using SingleOfflinePageItemResult = 88 using SingleOfflinePageItemResult =
98 offline_pages::SingleOfflinePageItemResult; 89 offline_pages::SingleOfflinePageItemResult;
99
100 //using DeletePageCallback = offline_pages::DeletePageCallback;
101 using DeletePageResult = offline_pages::DeletePageResult; 90 using DeletePageResult = offline_pages::DeletePageResult;
102 using SavePageResult = offline_pages::SavePageResult; 91 using SavePageResult = offline_pages::SavePageResult;
103 92
104 // Generates a new offline id
105 static int64_t GenerateOfflineId();
106
107 // Returns true if an offline copy can be saved for the given URL. 93 // Returns true if an offline copy can be saved for the given URL.
108 static bool CanSavePage(const GURL& url); 94 static bool CanSavePage(const GURL& url);
109 95
110 static base::TimeDelta GetFinalDeletionDelayForTesting(); 96 OfflinePageModel();
111
112 // All blocking calls/disk access will happen on the provided |task_runner|.
113 OfflinePageModel(std::unique_ptr<OfflinePageMetadataStore> store,
114 const base::FilePath& archives_dir,
115 const scoped_refptr<base::SequencedTaskRunner>& task_runner);
116 ~OfflinePageModel() override; 97 ~OfflinePageModel() override;
117 98
118 void AddObserver(Observer* observer); 99 virtual void AddObserver(Observer* observer) = 0;
119 void RemoveObserver(Observer* observer); 100 virtual void RemoveObserver(Observer* observer) = 0;
120 101
121 // Attempts to save a page addressed by |url| offline. Requires that the model 102 // Attempts to save a page addressed by |url| offline. Requires that the model
122 // is loaded. Generates a new offline id and returns it. 103 // is loaded. Generates a new offline id and returns it.
123 void SavePage(const GURL& url, 104 virtual void SavePage(const GURL& url,
124 const ClientId& client_id, 105 const ClientId& client_id,
125 std::unique_ptr<OfflinePageArchiver> archiver, 106 std::unique_ptr<OfflinePageArchiver> archiver,
126 const SavePageCallback& callback); 107 const SavePageCallback& callback) = 0;
127 108
128 // Marks that the offline page related to the passed |offline_id| has been 109 // Marks that the offline page related to the passed |offline_id| has been
129 // accessed. Its access info, including last access time and access count, 110 // accessed. Its access info, including last access time and access count,
130 // will be updated. Requires that the model is loaded. 111 // will be updated. Requires that the model is loaded.
131 void MarkPageAccessed(int64_t offline_id); 112 virtual void MarkPageAccessed(int64_t offline_id) = 0;
132
133 // Deletes an offline page related to the passed |offline_id|.
134 void DeletePageByOfflineId(int64_t offline_id,
135 const DeletePageCallback& callback);
136
137 // Deletes offline pages related to the passed |offline_ids|.
138 void DeletePagesByOfflineId(const std::vector<int64_t>& offline_ids,
romax 2016/05/26 01:26:48 I think eventually we want this method in this int
dougarnett 2016/05/26 16:03:22 Added TODO on you to pull this up.
139 const DeletePageCallback& callback) override;
140 113
141 // Wipes out all the data by deleting all saved files and clearing the store. 114 // Wipes out all the data by deleting all saved files and clearing the store.
142 void ClearAll(const base::Closure& callback); 115 virtual void ClearAll(const base::Closure& callback) = 0;
143 116
144 // Deletes offline pages matching the URL predicate. 117 // Deletes offline pages matching the URL predicate.
145 void DeletePagesByURLPredicate(const UrlPredicate& predicate, 118 virtual void DeletePagesByURLPredicate(
146 const DeletePageCallback& callback); 119 const UrlPredicate& predicate,
120 const DeletePageCallback& callback) = 0;
147 121
148 // Returns true via callback if there are offline pages in the given 122 // Returns true via callback if there are offline pages in the given
149 // |name_space|. 123 // |name_space|.
150 void HasPages(const std::string& name_space, 124 virtual void HasPages(const std::string& name_space,
151 const HasPagesCallback& callback); 125 const HasPagesCallback& callback) = 0;
152 126
153 // Returns via callback all GURLs in |urls| that are equal to the online URL 127 // Returns via callback all GURLs in |urls| that are equal to the online URL
154 // of any offline page. 128 // of any offline page.
155 void CheckPagesExistOffline(const std::set<GURL>& urls, 129 virtual void CheckPagesExistOffline(
156 const CheckPagesExistOfflineCallback& callback); 130 const std::set<GURL>& urls,
157 131 const CheckPagesExistOfflineCallback& callback) = 0;
158 // Gets all available offline pages.
159 void GetAllPages(const MultipleOfflinePageItemCallback& callback) override;
160 132
161 // Gets all offline ids where the offline page has the matching client id. 133 // Gets all offline ids where the offline page has the matching client id.
162 void GetOfflineIdsForClientId(const ClientId& client_id, 134 virtual void GetOfflineIdsForClientId(
163 const MultipleOfflineIdCallback& callback); 135 const ClientId& client_id,
136 const MultipleOfflineIdCallback& callback) = 0;
164 137
165 // Gets all offline ids where the offline page has the matching client id. 138 // Gets all offline ids where the offline page has the matching client id.
166 // Requires that the model is loaded. May not return matching IDs depending 139 // Requires that the model is loaded. May not return matching IDs depending
167 // on the internal state of the model. 140 // on the internal state of the model.#include <algorithm>
168 // 141 //
169 // This function is deprecated. Use |GetOfflineIdsForClientId| instead. 142 // This function is deprecated. Use |GetOfflineIdsForClientId| instead.
170 const std::vector<int64_t> MaybeGetOfflineIdsForClientId( 143 virtual const std::vector<int64_t> MaybeGetOfflineIdsForClientId(
171 const ClientId& client_id) const; 144 const ClientId& client_id) const = 0;
172 145
173 // Returns zero or one offline pages associated with a specified |offline_id|. 146 // Returns zero or one offline pages associated with a specified |offline_id|.
174 void GetPageByOfflineId(int64_t offline_id, 147 virtual void GetPageByOfflineId(
175 const SingleOfflinePageItemCallback& callback); 148 int64_t offline_id,
149 const SingleOfflinePageItemCallback& callback) = 0;
176 150
177 // Returns an offline page associated with a specified |offline_id|. nullptr 151 // Returns an offline page associated with a specified |offline_id|. nullptr
178 // is returned if not found. 152 // is returned if not found.
179 const OfflinePageItem* MaybeGetPageByOfflineId(int64_t offline_id) const; 153 virtual const OfflinePageItem* MaybeGetPageByOfflineId(
154 int64_t offline_id) const = 0;
180 155
181 // Returns the offline page that is stored under |offline_url|, if any. 156 // Returns the offline page that is stored under |offline_url|, if any.
182 void GetPageByOfflineURL(const GURL& offline_url, 157 virtual void GetPageByOfflineURL(
183 const SingleOfflinePageItemCallback& callback); 158 const GURL& offline_url,
159 const SingleOfflinePageItemCallback& callback) = 0;
184 160
185 // Returns an offline page that is stored as |offline_url|. A nullptr is 161 // Returns an offline page that is stored as |offline_url|. A nullptr is
186 // returned if not found. 162 // returned if not found.
187 // 163 //
188 // This function is deprecated, and may return |nullptr| even if a page 164 // This function is deprecated, and may return |nullptr| even if a page
189 // exists, depending on the implementation details of OfflinePageModel. 165 // exists, depending on the implementation details of OfflinePageModel.
190 // Use |GetPageByOfflineURL| instead. 166 // Use |GetPageByOfflineURL| instead.
191 const OfflinePageItem* MaybeGetPageByOfflineURL( 167 virtual const OfflinePageItem* MaybeGetPageByOfflineURL(
192 const GURL& offline_url) const; 168 const GURL& offline_url) const = 0;
193 169
194 // Returns the offline pages that are stored under |online_url|. 170 // Returns the offline pages that are stored under |online_url|.
195 void GetPagesByOnlineURL(const GURL& online_url, 171 virtual void GetPagesByOnlineURL(
196 const MultipleOfflinePageItemCallback& callback); 172 const GURL& online_url,
173 const MultipleOfflinePageItemCallback& callback) = 0;
197 174
198 // Returns via callback an offline page saved for |online_url|, if any. The 175 // Returns via callback an offline page saved for |online_url|, if any. The
199 // best page is chosen based on creation date; a more recently created offline 176 // best page is chosen based on creation date; a more recently created offline
200 // page will be preferred over an older one. This API function does not 177 // page will be preferred over an older one. This API function does not
201 // respect namespaces, as it is used to choose which page is rendered in a 178 // respect namespaces, as it is used to choose which page is rendered in a
202 // tab. Today all namespaces are treated equally for the purposes of this 179 // tab. Today all namespaces are treated equally for the purposes of this
203 // selection. 180 // selection.
204 void GetBestPageForOnlineURL(const GURL& online_url, 181 virtual void GetBestPageForOnlineURL(
205 const SingleOfflinePageItemCallback callback); 182 const GURL& online_url,
183 const SingleOfflinePageItemCallback callback) = 0;
206 184
207 // Returns an offline page saved for |online_url|. A nullptr is returned if 185 // Returns an offline page saved for |online_url|. A nullptr is returned if
208 // not found. See |GetBestPageForOnlineURL| for selection criteria. 186 // not found. See |GetBestPageForOnlineURL| for selection criteria.
209 const OfflinePageItem* MaybeGetBestPageForOnlineURL( 187 virtual const OfflinePageItem* MaybeGetBestPageForOnlineURL(
210 const GURL& online_url) const; 188 const GURL& online_url) const = 0;
211 189
212 // Checks that all of the offline pages have corresponding offline copies. 190 // Checks that all of the offline pages have corresponding offline copies.
213 // If a page is discovered to be missing an offline copy, its offline page 191 // If a page is discovered to be missing an offline copy, its offline page
214 // metadata will be removed and |OfflinePageDeleted| will be sent to model 192 // metadata will be removed and |OfflinePageDeleted| will be sent to model
215 // observers. 193 // observers.
216 void CheckForExternalFileDeletion(); 194 virtual void CheckForExternalFileDeletion() = 0;
217 195
218 // Marks pages as expired and removes their respective files from the archive 196 // Marks pages as expired and removes their respective files from the archive
219 // directory. 197 // directory.
220 void ExpirePages(const std::vector<int64_t>& offline_ids, 198 virtual void ExpirePages(const std::vector<int64_t>& offline_ids,
221 const base::Time& expiration_time); 199 const base::Time& expiration_time) = 0;
222 200
223 // Returns the policy controller. 201 // Returns the policy controller.
224 ClientPolicyController* GetPolicyController(); 202 virtual ClientPolicyController* GetPolicyController() = 0;
romax 2016/05/26 01:26:48 I dont think anyone is using this on the interface
dougarnett 2016/05/26 16:03:22 I don't see any current uses. Can I get a "2nd" fr
225 203
226 // Methods for testing only: 204 // Method for testing only:
227 OfflinePageMetadataStore* GetStoreForTesting(); 205 virtual bool is_loaded() const = 0;
romax 2016/05/26 01:26:48 same with above^
dougarnett 2016/05/26 16:03:22 Did find tests using this. Would like to get it ou
228
229 OfflinePageStorageManager* GetStorageManager();
230
231 bool is_loaded() const;
232
233 protected:
234 // Adding a protected constructor for testing-only purposes in
235 // offline_page_storage_manager_unittest.cc
236 OfflinePageModel();
237 206
238 private: 207 private:
239 FRIEND_TEST_ALL_PREFIXES(OfflinePageModelTest, MarkPageForDeletion);
240
241 typedef ScopedVector<OfflinePageArchiver> PendingArchivers;
242
243 // Callback for ensuring archive directory is created.
244 void OnEnsureArchivesDirCreatedDone(const base::TimeTicks& start_time);
245
246 void GetAllPagesAfterLoadDone(
247 const MultipleOfflinePageItemCallback& callback);
248 void CheckPagesExistOfflineAfterLoadDone(
249 const std::set<GURL>& urls,
250 const CheckPagesExistOfflineCallback& callback);
251 void GetOfflineIdsForClientIdWhenLoadDone(
252 const ClientId& client_id,
253 const MultipleOfflineIdCallback& callback) const;
254 void GetPageByOfflineIdWhenLoadDone(
255 int64_t offline_id,
256 const SingleOfflinePageItemCallback& callback) const;
257 void GetPagesByOnlineURLWhenLoadDone(
258 const GURL& offline_url,
259 const MultipleOfflinePageItemCallback& callback) const;
260 void GetPageByOfflineURLWhenLoadDone(
261 const GURL& offline_url,
262 const SingleOfflinePageItemCallback& callback) const;
263 void GetBestPageForOnlineURLWhenLoadDone(
264 const GURL& online_url,
265 const SingleOfflinePageItemCallback& callback) const;
266
267 // Callback for checking whether we have offline pages.
268 void HasPagesAfterLoadDone(const std::string& name_space,
269 const HasPagesCallback& callback) const;
270
271 // Callback for loading pages from the offline page metadata store.
272 void OnLoadDone(const base::TimeTicks& start_time,
273 OfflinePageMetadataStore::LoadStatus load_status,
274 const std::vector<OfflinePageItem>& offline_pages);
275
276 // Steps for saving a page offline.
277 void OnCreateArchiveDone(const GURL& requested_url,
278 int64_t offline_id,
279 const ClientId& client_id,
280 const base::Time& start_time,
281 const SavePageCallback& callback,
282 OfflinePageArchiver* archiver,
283 OfflinePageArchiver::ArchiverResult result,
284 const GURL& url,
285 const base::FilePath& file_path,
286 int64_t file_size);
287 void OnAddOfflinePageDone(OfflinePageArchiver* archiver,
288 const SavePageCallback& callback,
289 const OfflinePageItem& offline_page,
290 bool success);
291 void InformSavePageDone(const SavePageCallback& callback,
292 SavePageResult result,
293 const ClientId& client_id,
294 int64_t offline_id);
295 void DeletePendingArchiver(OfflinePageArchiver* archiver);
296
297 // Steps for deleting files and data for an offline page.
298 void OnDeleteArchiveFilesDone(const std::vector<int64_t>& offline_ids,
299 const DeletePageCallback& callback,
300 bool success);
301 void OnRemoveOfflinePagesDone(const std::vector<int64_t>& offline_ids,
302 const DeletePageCallback& callback,
303 bool success);
304 void InformDeletePageDone(const DeletePageCallback& callback,
305 DeletePageResult result);
306
307 void OnMarkPageAccesseDone(const OfflinePageItem& offline_page_item,
308 bool success);
309
310 // Callbacks for checking if offline pages are missing archive files.
311 void ScanForMissingArchiveFiles(
312 const std::set<base::FilePath>& archive_paths);
313 void OnRemoveOfflinePagesMissingArchiveFileDone(
314 const std::vector<std::pair<int64_t, ClientId>>& offline_client_id_pairs,
315 DeletePageResult result);
316
317 // Steps for clearing all.
318 void OnRemoveAllFilesDoneForClearAll(const base::Closure& callback,
319 DeletePageResult result);
320 void OnResetStoreDoneForClearAll(const base::Closure& callback, bool success);
321 void OnReloadStoreDoneForClearAll(
322 const base::Closure& callback,
323 OfflinePageMetadataStore::LoadStatus load_status,
324 const std::vector<OfflinePageItem>& offline_pages);
325
326 void CacheLoadedData(const std::vector<OfflinePageItem>& offline_pages);
327
328 // Actually does the work of deleting, requires the model is loaded.
329 void DoDeletePagesByOfflineId(const std::vector<int64_t>& offline_ids,
330 const DeletePageCallback& callback);
331
332 // Similar to DoDeletePagesByOfflineId, does actual work of deleting, and
333 // requires that the model is loaded.
334 void DoDeletePagesByURLPredicate(const UrlPredicate& predicate,
335 const DeletePageCallback& callback);
336
337 // Callback completing page expiration.
338 void OnExpirePageDone(int64_t offline_id,
339 const base::Time& expiration_time,
340 bool success);
341
342 void RunWhenLoaded(const base::Closure& job);
343
344 // Persistent store for offline page metadata.
345 std::unique_ptr<OfflinePageMetadataStore> store_;
346
347 // Location where all of the archive files will be stored.
348 base::FilePath archives_dir_;
349
350 // The observers.
351 base::ObserverList<Observer> observers_;
352
353 bool is_loaded_;
354
355 // In memory copy of the offline page metadata, keyed by bookmark IDs.
356 std::map<int64_t, OfflinePageItem> offline_pages_;
357
358 // Pending archivers owned by this model.
359 PendingArchivers pending_archivers_;
360
361 // Delayed tasks that should be invoked after the loading is done.
362 std::vector<base::Closure> delayed_tasks_;
363
364 // Controller of the client policies.
365 std::unique_ptr<ClientPolicyController> policy_controller_;
366
367 // Manager for the storage consumed by archives and responsible for
368 // automatic page clearing.
369 std::unique_ptr<OfflinePageStorageManager> storage_manager_;
370
371 // Manager for the offline archive files and directory.
372 std::unique_ptr<ArchiveManager> archive_manager_;
373
374 base::WeakPtrFactory<OfflinePageModel> weak_ptr_factory_;
375
376 DISALLOW_COPY_AND_ASSIGN(OfflinePageModel); 208 DISALLOW_COPY_AND_ASSIGN(OfflinePageModel);
377 }; 209 };
378 210
379 } // namespace offline_pages 211 } // namespace offline_pages
380 212
381 #endif // COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_MODEL_H_ 213 #endif // COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_MODEL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698