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_
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"
25 class SequencedTaskRunner
;
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.
40 // class ArchiverImpl : public OfflinePageArchiver {
41 // // This is a class that knows how to create archiver
42 // void CreateArchiver(...) override;
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
{
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
{
64 ARCHIVE_CREATION_FAILED
,
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.
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
{
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.
88 // Result of loading all pages.
89 enum class LoadResult
{
95 // Observer of the OfflinePageModel.
98 // Invoked when the model has finished loading.
99 virtual void OfflinePageModelLoaded(OfflinePageModel
* model
) = 0;
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|.
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
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
128 void SavePage(const GURL
& url
,
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
148 const std::vector
<OfflinePageItem
> GetPagesToCleanUp() const;
150 // Gets an offline page associated with a specified |bookmark_id|. Returns
151 // true if a matching offline page exists, and |offline_page| will be updated
152 // with corresponding value, or false, if no offline page was found.
153 bool GetPageByBookmarkId(int64 bookmark_id
,
154 OfflinePageItem
* offline_page
) const;
156 // Returns an offline page that is stored as |offline_url|. nullptr is
157 // returned if not found.
158 const OfflinePageItem
* GetPageByOfflineURL(const GURL
& offline_url
) const;
160 // Methods for testing only:
161 OfflinePageMetadataStore
* GetStoreForTesting();
163 bool is_loaded() const { return is_loaded_
; }
166 typedef ScopedVector
<OfflinePageArchiver
> PendingArchivers
;
168 // BaseBookmarkModelObserver:
169 void BookmarkModelChanged() override
;
170 void BookmarkNodeRemoved(bookmarks::BookmarkModel
* model
,
171 const bookmarks::BookmarkNode
* parent
,
173 const bookmarks::BookmarkNode
* node
,
174 const std::set
<GURL
>& removed_urls
) override
;
176 // Callback for loading pages from the offline page metadata store.
177 void OnLoadDone(bool success
,
178 const std::vector
<OfflinePageItem
>& offline_pages
);
180 // Steps for saving a page offline.
181 void OnCreateArchiveDone(const GURL
& requested_url
,
183 const SavePageCallback
& callback
,
184 OfflinePageArchiver
* archiver
,
185 OfflinePageArchiver::ArchiverResult result
,
187 const base::FilePath
& file_path
,
189 void OnAddOfflinePageDone(OfflinePageArchiver
* archiver
,
190 const SavePageCallback
& callback
,
191 const OfflinePageItem
& offline_page
,
193 void InformSavePageDone(const SavePageCallback
& callback
,
194 SavePageResult result
);
195 void DeletePendingArchiver(OfflinePageArchiver
* archiver
);
197 // Steps for deleting files and data for an offline page.
198 void OnDeleteArchiveFilesDone(
199 const std::vector
<int64
>& bookmark_ids
,
200 const DeletePageCallback
& callback
,
201 const bool* success
);
202 void OnRemoveOfflinePagesDone(const std::vector
<int64
>& bookmark_ids
,
203 const DeletePageCallback
& callback
,
205 void InformDeletePageDone(const DeletePageCallback
& callback
,
206 DeletePageResult result
);
208 // Persistent store for offline page metadata.
209 scoped_ptr
<OfflinePageMetadataStore
> store_
;
212 base::ObserverList
<Observer
> observers_
;
216 // In memory copy of the offline page metadata, keyed by bookmark IDs.
217 std::map
<int64
, OfflinePageItem
> offline_pages_
;
219 scoped_refptr
<base::SequencedTaskRunner
> task_runner_
;
221 // Pending archivers owned by this model.
222 PendingArchivers pending_archivers_
;
224 // Delayed tasks that should be invoked after the loading is done.
225 std::vector
<base::Closure
> delayed_tasks_
;
227 ScopedObserver
<bookmarks::BookmarkModel
, bookmarks::BookmarkModelObserver
>
230 base::WeakPtrFactory
<OfflinePageModel
> weak_ptr_factory_
;
232 DISALLOW_COPY_AND_ASSIGN(OfflinePageModel
);
235 } // namespace offline_pages
237 #endif // COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_MODEL_H_