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
,
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
{
80 // Result of loading all pages.
81 enum class LoadResult
{
87 // Observer of the OfflinePageModel.
90 // Invoked when the model has finished loading.
91 virtual void OfflinePageModelLoaded(OfflinePageModel
* model
) = 0;
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|.
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
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
120 void SavePage(const GURL
& url
,
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
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_
; }
158 typedef ScopedVector
<OfflinePageArchiver
> PendingArchivers
;
160 // BaseBookmarkModelObserver:
161 void BookmarkModelChanged() override
;
162 void BookmarkNodeRemoved(bookmarks::BookmarkModel
* model
,
163 const bookmarks::BookmarkNode
* parent
,
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
,
175 const SavePageCallback
& callback
,
176 OfflinePageArchiver
* archiver
,
177 OfflinePageArchiver::ArchiverResult result
,
179 const base::FilePath
& file_path
,
181 void OnAddOfflinePageDone(OfflinePageArchiver
* archiver
,
182 const SavePageCallback
& callback
,
183 const OfflinePageItem
& offline_page
,
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
,
198 // Persistent store for offline page metadata.
199 scoped_ptr
<OfflinePageMetadataStore
> store_
;
202 base::ObserverList
<Observer
> observers_
;
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
>
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_