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 #include "components/offline_pages/offline_page_model.h"
10 #include "base/location.h"
11 #include "base/logging.h"
12 #include "components/offline_pages/offline_page_item.h"
13 #include "components/offline_pages/offline_page_metadata_store.h"
16 using ArchiverResult
= offline_pages::OfflinePageArchiver::ArchiverResult
;
17 using SavePageResult
= offline_pages::OfflinePageModel::SavePageResult
;
19 namespace offline_pages
{
23 SavePageResult
ToSavePageResult(ArchiverResult archiver_result
) {
24 SavePageResult result
;
25 switch (archiver_result
) {
26 case ArchiverResult::SUCCESSFULLY_CREATED
:
27 result
= SavePageResult::SUCCESS
;
29 case ArchiverResult::ERROR_DEVICE_FULL
:
30 result
= SavePageResult::DEVICE_FULL
;
32 case ArchiverResult::ERROR_CONTENT_UNAVAILABLE
:
33 result
= SavePageResult::CONTENT_UNAVAILABLE
;
35 case ArchiverResult::ERROR_ARCHIVE_CREATION_FAILED
:
36 result
= SavePageResult::ARCHIVE_CREATION_FAILED
;
38 case ArchiverResult::ERROR_CANCELED
:
39 result
= SavePageResult::CANCELLED
;
43 result
= SavePageResult::CONTENT_UNAVAILABLE
;
50 OfflinePageModel::OfflinePageModel(scoped_ptr
<OfflinePageMetadataStore
> store
)
51 : store_(store
.Pass()),
52 weak_ptr_factory_(this) {
55 OfflinePageModel::~OfflinePageModel() {
58 void OfflinePageModel::Shutdown() {
61 void OfflinePageModel::SavePage(
63 scoped_ptr
<OfflinePageArchiver
> archiver
,
64 const SavePageCallback
& callback
) {
65 DCHECK(archiver
.get());
66 archiver
->CreateArchive(base::Bind(&OfflinePageModel::OnCreateArchiveDone
,
67 weak_ptr_factory_
.GetWeakPtr(), url
,
69 pending_archivers_
.push_back(archiver
.Pass());
72 void OfflinePageModel::DeletePage(const GURL
& url
,
73 const DeletePageCallback
& callback
) {
77 void OfflinePageModel::LoadAllPages(const LoadAllPagesCallback
& callback
) {
78 store_
->Load(base::Bind(&OfflinePageModel::OnLoadDone
,
79 weak_ptr_factory_
.GetWeakPtr(), callback
));
82 OfflinePageMetadataStore
* OfflinePageModel::GetStoreForTesting() {
86 void OfflinePageModel::OnCreateArchiveDone(
87 const GURL
& requested_url
,
88 const SavePageCallback
& callback
,
89 OfflinePageArchiver
* archiver
,
90 ArchiverResult archiver_result
,
92 const base::string16
& title
,
93 const base::FilePath
& file_path
,
95 if (requested_url
!= url
) {
96 DVLOG(1) << "Saved URL does not match requested URL.";
97 // TODO(fgorski): We have created an archive for a wrong URL. It should be
98 // deleted from here, once archiver has the right functionality.
99 InformSavePageDone(callback
, SavePageResult::ARCHIVE_CREATION_FAILED
);
100 DeletePendingArchiver(archiver
);
104 if (archiver_result
!= ArchiverResult::SUCCESSFULLY_CREATED
) {
105 SavePageResult result
= ToSavePageResult(archiver_result
);
106 InformSavePageDone(callback
, result
);
107 DeletePendingArchiver(archiver
);
111 OfflinePageItem
offline_page_item(url
, title
, file_path
, file_size
,
113 store_
->AddOfflinePage(
115 base::Bind(&OfflinePageModel::OnAddOfflinePageDone
,
116 weak_ptr_factory_
.GetWeakPtr(), archiver
, callback
));
119 void OfflinePageModel::OnAddOfflinePageDone(OfflinePageArchiver
* archiver
,
120 const SavePageCallback
& callback
,
122 SavePageResult result
=
123 success
? SavePageResult::SUCCESS
: SavePageResult::STORE_FAILURE
;
124 InformSavePageDone(callback
, result
);
125 DeletePendingArchiver(archiver
);
128 void OfflinePageModel::OnLoadDone(
129 const LoadAllPagesCallback
& callback
,
131 const std::vector
<OfflinePageItem
>& offline_pages
) {
132 // TODO(fgorski): Cache the values here, if we are comfortable with that
133 // model. This will require extra handling of parallel loads.
135 success
? LoadResult::SUCCESS
: LoadResult::STORE_FAILURE
;
136 callback
.Run(result
, offline_pages
);
139 void OfflinePageModel::InformSavePageDone(const SavePageCallback
& callback
,
140 SavePageResult result
) {
141 callback
.Run(result
);
144 void OfflinePageModel::DeletePendingArchiver(OfflinePageArchiver
* archiver
) {
145 pending_archivers_
.erase(std::find(
146 pending_archivers_
.begin(), pending_archivers_
.end(), archiver
));
149 } // namespace offline_pages