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/macros.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "components/keyed_service/core/keyed_service.h"
13 #include "components/offline_pages/offline_page_archiver.h"
17 namespace offline_pages
{
19 struct OfflinePageItem
;
20 class OfflinePageMetadataStore
;
22 // Service for saving pages offline, storing the offline copy and metadata, and
23 // retrieving them upon request.
25 // Caller of |SavePage|, |DeletePage| and |LoadAllPages| should provide
26 // implementation of |Client|, which will be then used to return a result of
30 // class ModelClient : public OfflinePageModel::Client {
32 // void OnSavePageDone(SavePageResult result) override {
33 // // handles errors or completes the save.
35 // const GURL& url() const { return url_; }
40 // scoped_ptr<ModelClient> client(new ModelClient());
41 // model->SavePage(client->url(), client);
43 // TODO(fgorski): Things to describe:
44 // * how to cancel requests and what to expect
45 class OfflinePageModel
: public KeyedService
,
46 public OfflinePageArchiver::Client
{
48 // Interface for clients of OfflinePageModel. Methods on the model accepting
49 // a Client pointer as a parameter will return their results using one of the
50 // methods on the Client interface.
52 // Result of deleting an offline page.
53 enum DeletePageResult
{
55 DELETE_PAGE_CANCELLED
,
56 DELETE_PAGE_DB_FAILURE
,
57 DELETE_PAGE_DOES_NOT_EXIST
,
60 // Result of loading all pages.
67 // Result of saving a page offline.
72 SAVE_PAGE_ALREADY_EXISTS
,
77 // Callback to SavePage call.
78 // TODO(fgorski): Should we return a copy of the record or depend on the
79 // client to call |LoadAllPages| to see things refreshed?
80 virtual void OnSavePageDone(SavePageResult result
) = 0;
82 // Callback to DeletePage call.
83 virtual void OnDeletePageDone(DeletePageResult result
) = 0;
85 // Callback to LoadAllPages call.
86 virtual void OnLoadAllPagesDone(
88 const std::vector
<OfflinePageItem
>& offline_pages
) = 0;
91 OfflinePageModel(scoped_ptr
<OfflinePageMetadataStore
> store
,
92 OfflinePageArchiver
* archiver
);
93 ~OfflinePageModel() override
;
95 // KeyedService implementation.
96 void Shutdown() override
;
98 // OfflinePageArchiver::Client implementation.
99 void OnCreateArchiveDone(OfflinePageArchiver::Request
* request
,
100 OfflinePageArchiver::ArchiverResult error
,
101 const base::FilePath
& file_path
) override
;
103 // Attempts to save a page addressed by |url| offline.
104 void SavePage(const GURL
& url
, Client
* client
);
106 // Deletes an offline page related to the passed |url|.
107 void DeletePage(const GURL
& url
, Client
* client
);
109 // Loads all of the available offline pages.
110 void LoadAllPages(Client
* client
);
112 // Methods for testing only:
113 OfflinePageMetadataStore
* GetStoreForTesting();
116 // Persistent store for offline page metadata.
117 scoped_ptr
<OfflinePageMetadataStore
> store_
;
119 // Offline page archiver. Outlives the model. Owned by the embedder.
120 OfflinePageArchiver
* archiver_
;
122 DISALLOW_COPY_AND_ASSIGN(OfflinePageModel
);
125 } // namespace offline_pages
127 #endif // COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_MODEL_H_