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 namespace offline_pages
{
23 struct OfflinePageItem
;
24 class OfflinePageMetadataStore
;
26 // Service for saving pages offline, storing the offline copy and metadata, and
27 // retrieving them upon request.
29 // Caller of |SavePage|, |DeletePage| and |LoadAllPages| should provide
30 // implementation of |Client|, which will be then used to return a result of
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 enum class SavePageResult
{
55 ARCHIVE_CREATION_FAILED
,
60 // Result of deleting an offline page.
61 enum class DeletePageResult
{
68 // Result of loading all pages.
69 enum class LoadResult
{
75 typedef base::Callback
<void(SavePageResult
)> SavePageCallback
;
76 typedef base::Callback
<void(DeletePageResult
)> DeletePageCallback
;
77 typedef base::Callback
<void(LoadResult
, const std::vector
<OfflinePageItem
>&)>
80 explicit OfflinePageModel(scoped_ptr
<OfflinePageMetadataStore
> store
);
81 ~OfflinePageModel() override
;
83 // KeyedService implementation.
84 void Shutdown() override
;
86 // Attempts to save a page addressed by |url| offline.
87 void SavePage(const GURL
& url
,
88 scoped_ptr
<OfflinePageArchiver
> archiver
,
89 const SavePageCallback
& callback
);
91 // Deletes an offline page related to the passed |url|.
92 void DeletePage(const GURL
& url
, const DeletePageCallback
& callback
);
94 // Loads all of the available offline pages.
95 void LoadAllPages(const LoadAllPagesCallback
& callback
);
97 // Methods for testing only:
98 OfflinePageMetadataStore
* GetStoreForTesting();
101 typedef ScopedVector
<OfflinePageArchiver
> PendingArchivers
;
103 // OfflinePageArchiver callback.
104 void OnCreateArchiveDone(const GURL
& requested_url
,
105 const SavePageCallback
& callback
,
106 OfflinePageArchiver
* archiver
,
107 OfflinePageArchiver::ArchiverResult result
,
109 const base::string16
& title
,
110 const base::FilePath
& file_path
,
113 // OfflinePageMetadataStore callbacks.
114 void OnAddOfflinePageDone(OfflinePageArchiver
* archiver
,
115 const SavePageCallback
& callback
,
117 void OnLoadDone(const LoadAllPagesCallback
& callback
,
119 const std::vector
<OfflinePageItem
>& offline_pages
);
121 void InformSavePageDone(const SavePageCallback
& callback
,
122 SavePageResult result
);
124 void DeletePendingArchiver(OfflinePageArchiver
* archiver
);
126 // Persistent store for offline page metadata.
127 scoped_ptr
<OfflinePageMetadataStore
> store_
;
129 // Pending archivers owned by this model.
130 PendingArchivers pending_archivers_
;
132 base::WeakPtrFactory
<OfflinePageModel
> weak_ptr_factory_
;
134 DISALLOW_COPY_AND_ASSIGN(OfflinePageModel
);
137 } // namespace offline_pages
139 #endif // COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_MODEL_H_