Gray horizontal keyboard accessory
[chromium-blink-merge.git] / components / offline_pages / offline_page_model.h
blobc42feb409fdc0b487aaf9e549542b8ff20bd871f
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_
8 #include <vector>
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"
19 class GURL;
20 namespace base {
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
34 // respective calls.
36 // Example usage:
37 // class ArchiverImpl : public OfflinePageArchiver {
38 // // This is a class that knows how to create archiver
39 // void CreateArchiver(...) override;
40 // ...
41 // }
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 {
51 public:
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 {
56 SUCCESS,
57 CANCELLED,
58 DEVICE_FULL,
59 CONTENT_UNAVAILABLE,
60 ARCHIVE_CREATION_FAILED,
61 STORE_FAILURE,
62 ALREADY_EXISTS,
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 {
69 SUCCESS,
70 CANCELLED,
71 STORE_FAILURE,
72 DEVICE_FAILURE,
73 NOT_FOUND,
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 {
80 SUCCESS,
81 CANCELLED,
82 STORE_FAILURE,
85 typedef base::Callback<void(SavePageResult)> SavePageCallback;
86 typedef base::Callback<void(DeletePageResult)> DeletePageCallback;
87 typedef base::Callback<void(LoadResult, const std::vector<OfflinePageItem>&)>
88 LoadAllPagesCallback;
90 // All blocking calls/disk access will happen on the provided |task_runner|.
91 OfflinePageModel(
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();
113 private:
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,
121 const GURL& url,
122 const base::string16& title,
123 const base::FilePath& file_path,
124 int64 file_size);
126 // OfflinePageMetadataStore callbacks.
127 void OnAddOfflinePageDone(OfflinePageArchiver* archiver,
128 const SavePageCallback& callback,
129 bool success);
130 void OnLoadDone(const LoadAllPagesCallback& callback,
131 bool success,
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,
142 bool success,
143 const std::vector<OfflinePageItem>& offline_pages);
144 void DeleteArchiverFile(const base::FilePath& file_path, bool* success);
145 void OnDeleteArchiverFileDone(
146 const GURL& url,
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_