ExtensionInstallDialogView: fix scrolling behavior on Views (Win,Linux)
[chromium-blink-merge.git] / components / offline_pages / offline_page_model.cc
blobbcedcdace2ae70ae59aa2711ba2c885fba755044
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"
7 #include <algorithm>
9 #include "base/bind.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"
14 #include "url/gurl.h"
16 using ArchiverResult = offline_pages::OfflinePageArchiver::ArchiverResult;
17 using SavePageResult = offline_pages::OfflinePageModel::SavePageResult;
19 namespace offline_pages {
21 namespace {
23 SavePageResult ToSavePageResult(ArchiverResult archiver_result) {
24 SavePageResult result;
25 switch (archiver_result) {
26 case ArchiverResult::SUCCESSFULLY_CREATED:
27 result = SavePageResult::SUCCESS;
28 break;
29 case ArchiverResult::ERROR_DEVICE_FULL:
30 result = SavePageResult::DEVICE_FULL;
31 break;
32 case ArchiverResult::ERROR_CONTENT_UNAVAILABLE:
33 result = SavePageResult::CONTENT_UNAVAILABLE;
34 break;
35 case ArchiverResult::ERROR_ARCHIVE_CREATION_FAILED:
36 result = SavePageResult::ARCHIVE_CREATION_FAILED;
37 break;
38 case ArchiverResult::ERROR_CANCELED:
39 result = SavePageResult::CANCELLED;
40 break;
41 default:
42 NOTREACHED();
43 result = SavePageResult::CONTENT_UNAVAILABLE;
45 return result;
48 } // namespace
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(
62 const GURL& url,
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,
68 callback));
69 pending_archivers_.push_back(archiver.Pass());
72 void OfflinePageModel::DeletePage(const GURL& url,
73 const DeletePageCallback& callback) {
74 NOTIMPLEMENTED();
77 void OfflinePageModel::LoadAllPages(const LoadAllPagesCallback& callback) {
78 store_->Load(base::Bind(&OfflinePageModel::OnLoadDone,
79 weak_ptr_factory_.GetWeakPtr(), callback));
82 OfflinePageMetadataStore* OfflinePageModel::GetStoreForTesting() {
83 return store_.get();
86 void OfflinePageModel::OnCreateArchiveDone(
87 const GURL& requested_url,
88 const SavePageCallback& callback,
89 OfflinePageArchiver* archiver,
90 ArchiverResult archiver_result,
91 const GURL& url,
92 const base::string16& title,
93 const base::FilePath& file_path,
94 int64 file_size) {
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);
101 return;
104 if (archiver_result != ArchiverResult::SUCCESSFULLY_CREATED) {
105 SavePageResult result = ToSavePageResult(archiver_result);
106 InformSavePageDone(callback, result);
107 DeletePendingArchiver(archiver);
108 return;
111 OfflinePageItem offline_page_item(url, title, file_path, file_size,
112 base::Time::Now());
113 store_->AddOfflinePage(
114 offline_page_item,
115 base::Bind(&OfflinePageModel::OnAddOfflinePageDone,
116 weak_ptr_factory_.GetWeakPtr(), archiver, callback));
119 void OfflinePageModel::OnAddOfflinePageDone(OfflinePageArchiver* archiver,
120 const SavePageCallback& callback,
121 bool success) {
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,
130 bool success,
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.
134 LoadResult result =
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