Vectorize website settings icons in omnibox
[chromium-blink-merge.git] / components / offline_pages / offline_page_model.h
blob650627ef0d66ad4e8c80ab18aef88540824e161b
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,
69 // Result of deleting an offline page.
70 // A Java counterpart will be generated for this enum.
71 // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.components.offlinepages
72 enum class DeletePageResult {
73 SUCCESS,
74 CANCELLED,
75 STORE_FAILURE,
76 DEVICE_FAILURE,
77 NOT_FOUND,
80 // Result of loading all pages.
81 enum class LoadResult {
82 SUCCESS,
83 CANCELLED,
84 STORE_FAILURE,
87 // Observer of the OfflinePageModel.
88 class Observer {
89 public:
90 // Invoked when the model has finished loading.
91 virtual void OfflinePageModelLoaded(OfflinePageModel* model) = 0;
93 protected:
94 virtual ~Observer() {}
97 typedef base::Callback<void(SavePageResult)> SavePageCallback;
98 typedef base::Callback<void(DeletePageResult)> DeletePageCallback;
100 // All blocking calls/disk access will happen on the provided |task_runner|.
101 OfflinePageModel(
102 scoped_ptr<OfflinePageMetadataStore> store,
103 const scoped_refptr<base::SequencedTaskRunner>& task_runner);
104 ~OfflinePageModel() override;
106 // Starts the OfflinePageModel and registers it as a BookmarkModelObserver.
107 // Calling this method is optional, but offline pages will not be deleted
108 // when the bookmark is deleted, i.e. due to sync, until this method is
109 // called.
110 void Start(bookmarks::BookmarkModel* model);
112 // KeyedService implementation.
113 void Shutdown() override;
115 void AddObserver(Observer* observer);
116 void RemoveObserver(Observer* observer);
118 // Attempts to save a page addressed by |url| offline. Requires that the model
119 // is loaded.
120 void SavePage(const GURL& url,
121 int64 bookmark_id,
122 scoped_ptr<OfflinePageArchiver> archiver,
123 const SavePageCallback& callback);
125 // Deletes an offline page related to the passed |bookmark_id|. Requires that
126 // the model is loaded.
127 void DeletePageByBookmarkId(int64 bookmark_id,
128 const DeletePageCallback& callback);
130 // Deletes offline pages related to the passed |bookmark_ids|. Requires that
131 // the model is loaded.
132 void DeletePagesByBookmarkId(const std::vector<int64>& bookmark_ids,
133 const DeletePageCallback& callback);
135 // Gets all available offline pages. Requires that the model is loaded.
136 const std::vector<OfflinePageItem> GetAllPages() const;
138 // Gets pages that should be removed to clean up storage. Requires that the
139 // model is loaded.
140 const std::vector<OfflinePageItem> GetPagesToCleanUp() const;
142 // Gets an offline page associated with a specified |bookmark_id|. Returns
143 // true if a matching offline page exists, and |offline_page| will be updated
144 // with corresponding value, or false, if no offline page was found.
145 bool GetPageByBookmarkId(int64 bookmark_id,
146 OfflinePageItem* offline_page) const;
148 // Returns an offline page that is stored as |offline_url|. nullptr is
149 // returned if not found.
150 const OfflinePageItem* GetPageByOfflineURL(const GURL& offline_url) const;
152 // Methods for testing only:
153 OfflinePageMetadataStore* GetStoreForTesting();
155 bool is_loaded() const { return is_loaded_; }
157 private:
158 typedef ScopedVector<OfflinePageArchiver> PendingArchivers;
160 // BaseBookmarkModelObserver:
161 void BookmarkModelChanged() override;
162 void BookmarkNodeRemoved(bookmarks::BookmarkModel* model,
163 const bookmarks::BookmarkNode* parent,
164 int old_index,
165 const bookmarks::BookmarkNode* node,
166 const std::set<GURL>& removed_urls) override;
168 // Callback for loading pages from the offline page metadata store.
169 void OnLoadDone(bool success,
170 const std::vector<OfflinePageItem>& offline_pages);
172 // Steps for saving a page offline.
173 void OnCreateArchiveDone(const GURL& requested_url,
174 int64 bookmark_id,
175 const SavePageCallback& callback,
176 OfflinePageArchiver* archiver,
177 OfflinePageArchiver::ArchiverResult result,
178 const GURL& url,
179 const base::FilePath& file_path,
180 int64 file_size);
181 void OnAddOfflinePageDone(OfflinePageArchiver* archiver,
182 const SavePageCallback& callback,
183 const OfflinePageItem& offline_page,
184 bool success);
185 void InformSavePageDone(const SavePageCallback& callback,
186 SavePageResult result);
187 void DeletePendingArchiver(OfflinePageArchiver* archiver);
189 // Steps for deleting files and data for an offline page.
190 void OnDeleteArchiveFilesDone(
191 const std::vector<int64>& bookmark_ids,
192 const DeletePageCallback& callback,
193 const bool* success);
194 void OnRemoveOfflinePagesDone(const std::vector<int64>& bookmark_ids,
195 const DeletePageCallback& callback,
196 bool success);
198 // Persistent store for offline page metadata.
199 scoped_ptr<OfflinePageMetadataStore> store_;
201 // The observers.
202 base::ObserverList<Observer> observers_;
204 bool is_loaded_;
206 // In memory copy of the offline page metadata, keyed by bookmark IDs.
207 std::map<int64, OfflinePageItem> offline_pages_;
209 scoped_refptr<base::SequencedTaskRunner> task_runner_;
211 // Pending archivers owned by this model.
212 PendingArchivers pending_archivers_;
214 // Delayed tasks that should be invoked after the loading is done.
215 std::vector<base::Closure> delayed_tasks_;
217 ScopedObserver<bookmarks::BookmarkModel, bookmarks::BookmarkModelObserver>
218 scoped_observer_;
220 base::WeakPtrFactory<OfflinePageModel> weak_ptr_factory_;
222 DISALLOW_COPY_AND_ASSIGN(OfflinePageModel);
225 } // namespace offline_pages
227 #endif // COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_MODEL_H_