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_
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"
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.
34 // class ArchiverImpl : public OfflinePageArchiver {
35 // // This is a class that knows how to create archiver
36 // void CreateArchiver(...) override;
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
{
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
{
57 ARCHIVE_CREATION_FAILED
,
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
{
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
{
82 // Observer of the OfflinePageModel.
85 // Invoked when the model has finished loading.
86 virtual void OfflinePageModelLoaded(OfflinePageModel
* model
) = 0;
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|.
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
109 void SavePage(const GURL
& url
,
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_
; }
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
,
146 const SavePageCallback
& callback
,
147 OfflinePageArchiver
* archiver
,
148 OfflinePageArchiver::ArchiverResult result
,
150 const base::FilePath
& file_path
,
152 void OnAddOfflinePageDone(OfflinePageArchiver
* archiver
,
153 const SavePageCallback
& callback
,
154 const OfflinePageItem
& offline_page
,
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(
163 const DeletePageCallback
& callback
,
164 const bool* success
);
165 void OnRemoveOfflinePageDone(const GURL
& url
,
166 const DeletePageCallback
& callback
,
169 // Persistent store for offline page metadata.
170 scoped_ptr
<OfflinePageMetadataStore
> store_
;
173 base::ObserverList
<Observer
> observers_
;
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_