Cleanup: Pass std::string as const reference from pdf/
[chromium-blink-merge.git] / components / offline_pages / offline_page_model.h
blobdd55a7e623af4c897380b82904d4cfcc84583e05
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 <map>
9 #include <vector>
11 #include "base/callback.h"
12 #include "base/macros.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/scoped_vector.h"
16 #include "base/memory/weak_ptr.h"
17 #include "base/observer_list.h"
18 #include "base/scoped_observer.h"
19 #include "components/bookmarks/browser/base_bookmark_model_observer.h"
20 #include "components/keyed_service/core/keyed_service.h"
21 #include "components/offline_pages/offline_page_archiver.h"
23 class GURL;
24 namespace base {
25 class SequencedTaskRunner;
27 namespace bookmarks {
28 class BookmarkModel;
31 namespace offline_pages {
33 struct OfflinePageItem;
34 class OfflinePageMetadataStore;
36 // Service for saving pages offline, storing the offline copy and metadata, and
37 // retrieving them upon request.
39 // Example usage:
40 // class ArchiverImpl : public OfflinePageArchiver {
41 // // This is a class that knows how to create archiver
42 // void CreateArchiver(...) override;
43 // ...
44 // }
46 // // In code using the OfflinePagesModel to save a page:
47 // scoped_ptr<ArchiverImpl> archiver(new ArchiverImpl());
48 // // Callback is of type SavePageCallback.
49 // model->SavePage(url, archiver.Pass(), callback);
51 // TODO(fgorski): Things to describe:
52 // * how to cancel requests and what to expect
53 class OfflinePageModel : public KeyedService,
54 public bookmarks::BaseBookmarkModelObserver {
55 public:
56 // Result of saving a page offline.
57 // A Java counterpart will be generated for this enum.
58 // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.components.offlinepages
59 enum class SavePageResult {
60 SUCCESS,
61 CANCELLED,
62 DEVICE_FULL,
63 CONTENT_UNAVAILABLE,
64 ARCHIVE_CREATION_FAILED,
65 STORE_FAILURE,
66 ALREADY_EXISTS,
67 // NOTE: always keep this entry at the end. Add new result types only
68 // immediately above this line. Make sure to update the corresponding
69 // histogram enum accordingly.
70 RESULT_COUNT,
73 // Result of deleting an offline page.
74 // A Java counterpart will be generated for this enum.
75 // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.components.offlinepages
76 enum class DeletePageResult {
77 SUCCESS,
78 CANCELLED,
79 STORE_FAILURE,
80 DEVICE_FAILURE,
81 NOT_FOUND,
82 // NOTE: always keep this entry at the end. Add new result types only
83 // immediately above this line. Make sure to update the corresponding
84 // histogram enum accordingly.
85 RESULT_COUNT,
88 // Result of loading all pages.
89 enum class LoadResult {
90 SUCCESS,
91 CANCELLED,
92 STORE_FAILURE,
95 // Observer of the OfflinePageModel.
96 class Observer {
97 public:
98 // Invoked when the model has finished loading.
99 virtual void OfflinePageModelLoaded(OfflinePageModel* model) = 0;
101 protected:
102 virtual ~Observer() {}
105 typedef base::Callback<void(SavePageResult)> SavePageCallback;
106 typedef base::Callback<void(DeletePageResult)> DeletePageCallback;
108 // All blocking calls/disk access will happen on the provided |task_runner|.
109 OfflinePageModel(
110 scoped_ptr<OfflinePageMetadataStore> store,
111 const scoped_refptr<base::SequencedTaskRunner>& task_runner);
112 ~OfflinePageModel() override;
114 // Starts the OfflinePageModel and registers it as a BookmarkModelObserver.
115 // Calling this method is optional, but offline pages will not be deleted
116 // when the bookmark is deleted, i.e. due to sync, until this method is
117 // called.
118 void Start(bookmarks::BookmarkModel* model);
120 // KeyedService implementation.
121 void Shutdown() override;
123 void AddObserver(Observer* observer);
124 void RemoveObserver(Observer* observer);
126 // Attempts to save a page addressed by |url| offline. Requires that the model
127 // is loaded.
128 void SavePage(const GURL& url,
129 int64 bookmark_id,
130 scoped_ptr<OfflinePageArchiver> archiver,
131 const SavePageCallback& callback);
133 // Deletes an offline page related to the passed |bookmark_id|. Requires that
134 // the model is loaded.
135 void DeletePageByBookmarkId(int64 bookmark_id,
136 const DeletePageCallback& callback);
138 // Deletes offline pages related to the passed |bookmark_ids|. Requires that
139 // the model is loaded.
140 void DeletePagesByBookmarkId(const std::vector<int64>& bookmark_ids,
141 const DeletePageCallback& callback);
143 // Gets all available offline pages. Requires that the model is loaded.
144 const std::vector<OfflinePageItem> GetAllPages() const;
146 // Gets pages that should be removed to clean up storage. Requires that the
147 // model is loaded.
148 const std::vector<OfflinePageItem> GetPagesToCleanUp() const;
150 // Returns an offline page associated with a specified |bookmark_id|. nullptr
151 // is returned if not found.
152 const OfflinePageItem* GetPageByBookmarkId(int64 bookmark_id) const;
154 // Returns an offline page that is stored as |offline_url|. nullptr is
155 // returned if not found.
156 const OfflinePageItem* GetPageByOfflineURL(const GURL& offline_url) const;
158 // Methods for testing only:
159 OfflinePageMetadataStore* GetStoreForTesting();
161 bool is_loaded() const { return is_loaded_; }
163 private:
164 typedef ScopedVector<OfflinePageArchiver> PendingArchivers;
166 // BaseBookmarkModelObserver:
167 void BookmarkModelChanged() override;
168 void BookmarkNodeRemoved(bookmarks::BookmarkModel* model,
169 const bookmarks::BookmarkNode* parent,
170 int old_index,
171 const bookmarks::BookmarkNode* node,
172 const std::set<GURL>& removed_urls) override;
174 // Callback for loading pages from the offline page metadata store.
175 void OnLoadDone(bool success,
176 const std::vector<OfflinePageItem>& offline_pages);
178 // Steps for saving a page offline.
179 void OnCreateArchiveDone(const GURL& requested_url,
180 int64 bookmark_id,
181 const SavePageCallback& callback,
182 OfflinePageArchiver* archiver,
183 OfflinePageArchiver::ArchiverResult result,
184 const GURL& url,
185 const base::FilePath& file_path,
186 int64 file_size);
187 void OnAddOfflinePageDone(OfflinePageArchiver* archiver,
188 const SavePageCallback& callback,
189 const OfflinePageItem& offline_page,
190 bool success);
191 void InformSavePageDone(const SavePageCallback& callback,
192 SavePageResult result);
193 void DeletePendingArchiver(OfflinePageArchiver* archiver);
195 // Steps for deleting files and data for an offline page.
196 void OnDeleteArchiveFilesDone(
197 const std::vector<int64>& bookmark_ids,
198 const DeletePageCallback& callback,
199 const bool* success);
200 void OnRemoveOfflinePagesDone(const std::vector<int64>& bookmark_ids,
201 const DeletePageCallback& callback,
202 bool success);
203 void InformDeletePageDone(const DeletePageCallback& callback,
204 DeletePageResult result);
206 // Persistent store for offline page metadata.
207 scoped_ptr<OfflinePageMetadataStore> store_;
209 // The observers.
210 base::ObserverList<Observer> observers_;
212 bool is_loaded_;
214 // In memory copy of the offline page metadata, keyed by bookmark IDs.
215 std::map<int64, OfflinePageItem> offline_pages_;
217 scoped_refptr<base::SequencedTaskRunner> task_runner_;
219 // Pending archivers owned by this model.
220 PendingArchivers pending_archivers_;
222 // Delayed tasks that should be invoked after the loading is done.
223 std::vector<base::Closure> delayed_tasks_;
225 ScopedObserver<bookmarks::BookmarkModel, bookmarks::BookmarkModelObserver>
226 scoped_observer_;
228 base::WeakPtrFactory<OfflinePageModel> weak_ptr_factory_;
230 DISALLOW_COPY_AND_ASSIGN(OfflinePageModel);
233 } // namespace offline_pages
235 #endif // COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_MODEL_H_