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 "components/keyed_service/core/keyed_service.h"
17 #include "components/offline_pages/offline_page_archiver.h"
21 class SequencedTaskRunner
;
24 namespace offline_pages
{
26 struct OfflinePageItem
;
27 class OfflinePageMetadataStore
;
29 // Service for saving pages offline, storing the offline copy and metadata, and
30 // retrieving them upon request.
32 // Caller of |SavePage|, |DeletePage| and |LoadAllPages| should provide
33 // implementation of |Client|, which will be then used to return a result of
37 // class ArchiverImpl : public OfflinePageArchiver {
38 // // This is a class that knows how to create archiver
39 // void CreateArchiver(...) override;
43 // // In code using the OfflinePagesModel to save a page:
44 // scoped_ptr<ArchiverImpl> archiver(new ArchiverImpl());
45 // // Callback is of type SavePageCallback.
46 // model->SavePage(url, archiver.Pass(), callback);
48 // TODO(fgorski): Things to describe:
49 // * how to cancel requests and what to expect
50 class OfflinePageModel
: public KeyedService
{
52 // Result of saving a page offline.
53 // A Java counterpart will be generated for this enum.
54 // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.components.offline_pages
55 enum class SavePageResult
{
60 ARCHIVE_CREATION_FAILED
,
65 // Result of deleting an offline page.
66 // A Java counterpart will be generated for this enum.
67 // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.components.offline_pages
68 enum class DeletePageResult
{
76 // Result of loading all pages.
77 // A Java counterpart will be generated for this enum.
78 // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.components.offline_pages
79 enum class LoadResult
{
85 typedef base::Callback
<void(SavePageResult
)> SavePageCallback
;
86 typedef base::Callback
<void(DeletePageResult
)> DeletePageCallback
;
87 typedef base::Callback
<void(LoadResult
, const std::vector
<OfflinePageItem
>&)>
90 // All blocking calls/disk access will happen on the provided |task_runner|.
92 scoped_ptr
<OfflinePageMetadataStore
> store
,
93 const scoped_refptr
<base::SequencedTaskRunner
>& task_runner
);
94 ~OfflinePageModel() override
;
96 // KeyedService implementation.
97 void Shutdown() override
;
99 // Attempts to save a page addressed by |url| offline.
100 void SavePage(const GURL
& url
,
101 scoped_ptr
<OfflinePageArchiver
> archiver
,
102 const SavePageCallback
& callback
);
104 // Deletes an offline page related to the passed |url|.
105 void DeletePage(const GURL
& url
, const DeletePageCallback
& callback
);
107 // Loads all of the available offline pages.
108 void LoadAllPages(const LoadAllPagesCallback
& callback
);
110 // Methods for testing only:
111 OfflinePageMetadataStore
* GetStoreForTesting();
114 typedef ScopedVector
<OfflinePageArchiver
> PendingArchivers
;
116 // OfflinePageArchiver callback.
117 void OnCreateArchiveDone(const GURL
& requested_url
,
118 const SavePageCallback
& callback
,
119 OfflinePageArchiver
* archiver
,
120 OfflinePageArchiver::ArchiverResult result
,
122 const base::string16
& title
,
123 const base::FilePath
& file_path
,
126 // OfflinePageMetadataStore callbacks.
127 void OnAddOfflinePageDone(OfflinePageArchiver
* archiver
,
128 const SavePageCallback
& callback
,
130 void OnLoadDone(const LoadAllPagesCallback
& callback
,
132 const std::vector
<OfflinePageItem
>& offline_pages
);
134 void InformSavePageDone(const SavePageCallback
& callback
,
135 SavePageResult result
);
137 void DeletePendingArchiver(OfflinePageArchiver
* archiver
);
139 // Serialized steps of deleting files and data for an offline page.
140 void OnLoadDoneForDeletion(const GURL
& url
,
141 const DeletePageCallback
& callback
,
143 const std::vector
<OfflinePageItem
>& offline_pages
);
144 void DeleteArchiverFile(const base::FilePath
& file_path
, bool* success
);
145 void OnDeleteArchiverFileDone(
147 const DeletePageCallback
& callback
,
148 const bool* success
);
149 void OnRemoveOfflinePageDone(
150 const DeletePageCallback
& callback
, bool success
);
152 // Persistent store for offline page metadata.
153 scoped_ptr
<OfflinePageMetadataStore
> store_
;
155 scoped_refptr
<base::SequencedTaskRunner
> task_runner_
;
157 // Pending archivers owned by this model.
158 PendingArchivers pending_archivers_
;
160 base::WeakPtrFactory
<OfflinePageModel
> weak_ptr_factory_
;
162 DISALLOW_COPY_AND_ASSIGN(OfflinePageModel
);
165 } // namespace offline_pages
167 #endif // COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_MODEL_H_