Rewrite HpackRoundTripTest::RandomizedExamples.
[chromium-blink-merge.git] / components / offline_pages / offline_page_model.h
blob4901914011a68314464725c4cf4b67cb91bd0966
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
3 // found in the LICENSE file.
5 #ifndef COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_MODEL_H_
6 #define COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_MODEL_H_
8 #include <vector>
10 #include "base/callback.h"
11 #include "base/macros.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/scoped_vector.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/observer_list.h"
17 #include "components/keyed_service/core/keyed_service.h"
18 #include "components/offline_pages/offline_page_archiver.h"
20 class GURL;
21 namespace base {
22 class SequencedTaskRunner;
25 namespace offline_pages {
27 struct OfflinePageItem;
28 class OfflinePageMetadataStore;
30 // Service for saving pages offline, storing the offline copy and metadata, and
31 // retrieving them upon request.
33 // Example usage:
34 // class ArchiverImpl : public OfflinePageArchiver {
35 // // This is a class that knows how to create archiver
36 // void CreateArchiver(...) override;
37 // ...
38 // }
40 // // In code using the OfflinePagesModel to save a page:
41 // scoped_ptr<ArchiverImpl> archiver(new ArchiverImpl());
42 // // Callback is of type SavePageCallback.
43 // model->SavePage(url, archiver.Pass(), callback);
45 // TODO(fgorski): Things to describe:
46 // * how to cancel requests and what to expect
47 class OfflinePageModel : public KeyedService {
48 public:
49 // Result of saving a page offline.
50 // A Java counterpart will be generated for this enum.
51 // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.components.offline_pages
52 enum class SavePageResult {
53 SUCCESS,
54 CANCELLED,
55 DEVICE_FULL,
56 CONTENT_UNAVAILABLE,
57 ARCHIVE_CREATION_FAILED,
58 STORE_FAILURE,
59 ALREADY_EXISTS,
62 // Result of deleting an offline page.
63 // A Java counterpart will be generated for this enum.
64 // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.components.offline_pages
65 enum class DeletePageResult {
66 SUCCESS,
67 CANCELLED,
68 STORE_FAILURE,
69 DEVICE_FAILURE,
70 NOT_FOUND,
73 // Result of loading all pages.
74 // A Java counterpart will be generated for this enum.
75 // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.components.offline_pages
76 enum class LoadResult {
77 SUCCESS,
78 CANCELLED,
79 STORE_FAILURE,
82 // Observer of the OfflinePageModel.
83 class Observer {
84 public:
85 // Invoked when the model has finished loading.
86 virtual void OfflinePageModelLoaded(OfflinePageModel* model) = 0;
88 protected:
89 virtual ~Observer() {}
92 typedef base::Callback<void(SavePageResult)> SavePageCallback;
93 typedef base::Callback<void(DeletePageResult)> DeletePageCallback;
95 // All blocking calls/disk access will happen on the provided |task_runner|.
96 OfflinePageModel(
97 scoped_ptr<OfflinePageMetadataStore> store,
98 const scoped_refptr<base::SequencedTaskRunner>& task_runner);
99 ~OfflinePageModel() override;
101 // KeyedService implementation.
102 void Shutdown() override;
104 void AddObserver(Observer* observer);
105 void RemoveObserver(Observer* observer);
107 // Attempts to save a page addressed by |url| offline. Requires that the model
108 // is loaded.
109 void SavePage(const GURL& url,
110 int64 bookmark_id,
111 scoped_ptr<OfflinePageArchiver> archiver,
112 const SavePageCallback& callback);
114 // Deletes an offline page related to the passed |bookmark_id|. Requires that
115 // the model is loaded.
116 void DeletePageByBookmarkId(int64 bookmark_id,
117 const DeletePageCallback& callback);
119 // Gets all available offline pages. Requires that the model is loaded.
120 const std::vector<OfflinePageItem>& GetAllPages() const;
122 // Gets an offline page associated with a specified |bookmark_id|. Returns
123 // true if a matching offline page exists, and |offline_page| will be updated
124 // with corresponding value, or false, if no offline page was found.
125 bool GetPageByBookmarkId(int64 bookmark_id,
126 OfflinePageItem* offline_page) const;
128 // Methods for testing only:
129 OfflinePageMetadataStore* GetStoreForTesting();
131 bool is_loaded() const { return is_loaded_; }
133 private:
134 typedef ScopedVector<OfflinePageArchiver> PendingArchivers;
136 void DeletePage(const OfflinePageItem& offline_page,
137 const DeletePageCallback& callback);
139 // Callback for loading pages from the offline page metadata store.
140 void OnLoadDone(bool success,
141 const std::vector<OfflinePageItem>& offline_pages);
143 // Steps for saving a page offline.
144 void OnCreateArchiveDone(const GURL& requested_url,
145 int64 bookmark_id,
146 const SavePageCallback& callback,
147 OfflinePageArchiver* archiver,
148 OfflinePageArchiver::ArchiverResult result,
149 const GURL& url,
150 const base::FilePath& file_path,
151 int64 file_size);
152 void OnAddOfflinePageDone(OfflinePageArchiver* archiver,
153 const SavePageCallback& callback,
154 const OfflinePageItem& offline_page,
155 bool success);
156 void InformSavePageDone(const SavePageCallback& callback,
157 SavePageResult result);
158 void DeletePendingArchiver(OfflinePageArchiver* archiver);
160 // Steps for deleting files and data for an offline page.
161 void OnDeleteArchiverFileDone(
162 const GURL& url,
163 const DeletePageCallback& callback,
164 const bool* success);
165 void OnRemoveOfflinePageDone(const GURL& url,
166 const DeletePageCallback& callback,
167 bool success);
169 // Persistent store for offline page metadata.
170 scoped_ptr<OfflinePageMetadataStore> store_;
172 // The observers.
173 base::ObserverList<Observer> observers_;
175 bool is_loaded_;
177 // In memory copy of the offline page metadata.
178 std::vector<OfflinePageItem> offline_pages_;
180 scoped_refptr<base::SequencedTaskRunner> task_runner_;
182 // Pending archivers owned by this model.
183 PendingArchivers pending_archivers_;
185 base::WeakPtrFactory<OfflinePageModel> weak_ptr_factory_;
187 DISALLOW_COPY_AND_ASSIGN(OfflinePageModel);
190 } // namespace offline_pages
192 #endif // COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_MODEL_H_