Vectorize website settings icons in omnibox
[chromium-blink-merge.git] / components / offline_pages / offline_page_model.cc
blob56516d282199758967983310d77aaef95b5c9af1
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/files/file_util.h"
11 #include "base/location.h"
12 #include "base/logging.h"
13 #include "base/metrics/histogram_macros.h"
14 #include "base/sequenced_task_runner.h"
15 #include "base/time/time.h"
16 #include "components/bookmarks/browser/bookmark_model.h"
17 #include "components/bookmarks/browser/bookmark_node.h"
18 #include "components/offline_pages/offline_page_item.h"
19 #include "components/offline_pages/offline_page_metadata_store.h"
20 #include "url/gurl.h"
22 using ArchiverResult = offline_pages::OfflinePageArchiver::ArchiverResult;
23 using SavePageResult = offline_pages::OfflinePageModel::SavePageResult;
25 namespace offline_pages {
27 namespace {
29 // Threshold for how old offline copy of a page should be before we offer to
30 // delete it to free up space.
31 const base::TimeDelta kPageCleanUpThreshold = base::TimeDelta::FromDays(30);
33 SavePageResult ToSavePageResult(ArchiverResult archiver_result) {
34 SavePageResult result;
35 switch (archiver_result) {
36 case ArchiverResult::SUCCESSFULLY_CREATED:
37 result = SavePageResult::SUCCESS;
38 break;
39 case ArchiverResult::ERROR_DEVICE_FULL:
40 result = SavePageResult::DEVICE_FULL;
41 break;
42 case ArchiverResult::ERROR_CONTENT_UNAVAILABLE:
43 result = SavePageResult::CONTENT_UNAVAILABLE;
44 break;
45 case ArchiverResult::ERROR_ARCHIVE_CREATION_FAILED:
46 result = SavePageResult::ARCHIVE_CREATION_FAILED;
47 break;
48 case ArchiverResult::ERROR_CANCELED:
49 result = SavePageResult::CANCELLED;
50 break;
51 default:
52 NOTREACHED();
53 result = SavePageResult::CONTENT_UNAVAILABLE;
55 return result;
58 void DeleteArchiveFiles(const std::vector<base::FilePath>& paths_to_delete,
59 bool* success) {
60 DCHECK(success);
61 for (const auto& file_path : paths_to_delete) {
62 // Make sure delete happens on the left of || so that it is always executed.
63 *success = base::DeleteFile(file_path, false) || *success;
67 void EmptyDeleteCallback(OfflinePageModel::DeletePageResult /* result */) {
70 } // namespace
72 OfflinePageModel::OfflinePageModel(
73 scoped_ptr<OfflinePageMetadataStore> store,
74 const scoped_refptr<base::SequencedTaskRunner>& task_runner)
75 : store_(store.Pass()),
76 is_loaded_(false),
77 task_runner_(task_runner),
78 scoped_observer_(this),
79 weak_ptr_factory_(this) {
80 store_->Load(base::Bind(&OfflinePageModel::OnLoadDone,
81 weak_ptr_factory_.GetWeakPtr()));
84 OfflinePageModel::~OfflinePageModel() {
87 void OfflinePageModel::Start(bookmarks::BookmarkModel* model) {
88 scoped_observer_.Add(model);
91 void OfflinePageModel::Shutdown() {
92 scoped_observer_.RemoveAll();
95 void OfflinePageModel::AddObserver(Observer* observer) {
96 observers_.AddObserver(observer);
99 void OfflinePageModel::RemoveObserver(Observer* observer) {
100 observers_.RemoveObserver(observer);
103 void OfflinePageModel::SavePage(const GURL& url,
104 int64 bookmark_id,
105 scoped_ptr<OfflinePageArchiver> archiver,
106 const SavePageCallback& callback) {
107 DCHECK(is_loaded_);
108 DCHECK(archiver.get());
109 archiver->CreateArchive(base::Bind(&OfflinePageModel::OnCreateArchiveDone,
110 weak_ptr_factory_.GetWeakPtr(), url,
111 bookmark_id, callback));
112 pending_archivers_.push_back(archiver.Pass());
115 void OfflinePageModel::DeletePageByBookmarkId(
116 int64 bookmark_id,
117 const DeletePageCallback& callback) {
118 DCHECK(is_loaded_);
119 std::vector<int64> bookmark_ids_to_delete;
120 bookmark_ids_to_delete.push_back(bookmark_id);
121 DeletePagesByBookmarkId(bookmark_ids_to_delete, callback);
124 void OfflinePageModel::DeletePagesByBookmarkId(
125 const std::vector<int64>& bookmark_ids,
126 const DeletePageCallback& callback) {
127 DCHECK(is_loaded_);
129 std::vector<base::FilePath> paths_to_delete;
130 for (const auto& bookmark_id : bookmark_ids) {
131 auto iter = offline_pages_.find(bookmark_id);
132 if (iter != offline_pages_.end()) {
133 paths_to_delete.push_back(iter->second.file_path);
137 if (paths_to_delete.empty()) {
138 callback.Run(DeletePageResult::NOT_FOUND);
139 return;
142 bool* success = new bool(false);
143 task_runner_->PostTaskAndReply(
144 FROM_HERE,
145 base::Bind(&DeleteArchiveFiles, paths_to_delete, success),
146 base::Bind(&OfflinePageModel::OnDeleteArchiveFilesDone,
147 weak_ptr_factory_.GetWeakPtr(),
148 bookmark_ids,
149 callback,
150 base::Owned(success)));
153 const std::vector<OfflinePageItem> OfflinePageModel::GetAllPages() const {
154 DCHECK(is_loaded_);
155 std::vector<OfflinePageItem> offline_pages;
156 for (const auto& id_page_pair : offline_pages_)
157 offline_pages.push_back(id_page_pair.second);
158 return offline_pages;
161 const std::vector<OfflinePageItem> OfflinePageModel::GetPagesToCleanUp() const {
162 DCHECK(is_loaded_);
163 std::vector<OfflinePageItem> offline_pages;
164 base::Time now = base::Time::Now();
165 for (const auto& id_page_pair : offline_pages_) {
166 if (now - id_page_pair.second.creation_time > kPageCleanUpThreshold)
167 offline_pages.push_back(id_page_pair.second);
169 return offline_pages;
172 bool OfflinePageModel::GetPageByBookmarkId(
173 int64 bookmark_id,
174 OfflinePageItem* offline_page) const {
175 DCHECK(offline_page);
177 const auto iter = offline_pages_.find(bookmark_id);
178 if (iter != offline_pages_.end()) {
179 *offline_page = iter->second;
180 return true;
183 return false;
186 const OfflinePageItem* OfflinePageModel::GetPageByOfflineURL(
187 const GURL& offline_url) const {
188 for (auto iter = offline_pages_.begin();
189 iter != offline_pages_.end();
190 ++iter) {
191 if (iter->second.GetOfflineURL() == offline_url)
192 return &(iter->second);
194 return nullptr;
197 OfflinePageMetadataStore* OfflinePageModel::GetStoreForTesting() {
198 return store_.get();
201 void OfflinePageModel::OnCreateArchiveDone(const GURL& requested_url,
202 int64 bookmark_id,
203 const SavePageCallback& callback,
204 OfflinePageArchiver* archiver,
205 ArchiverResult archiver_result,
206 const GURL& url,
207 const base::FilePath& file_path,
208 int64 file_size) {
209 if (requested_url != url) {
210 DVLOG(1) << "Saved URL does not match requested URL.";
211 // TODO(fgorski): We have created an archive for a wrong URL. It should be
212 // deleted from here, once archiver has the right functionality.
213 InformSavePageDone(callback, SavePageResult::ARCHIVE_CREATION_FAILED);
214 DeletePendingArchiver(archiver);
215 return;
218 if (archiver_result != ArchiverResult::SUCCESSFULLY_CREATED) {
219 SavePageResult result = ToSavePageResult(archiver_result);
220 InformSavePageDone(callback, result);
221 DeletePendingArchiver(archiver);
222 return;
225 OfflinePageItem offline_page_item(url, bookmark_id, file_path, file_size,
226 base::Time::Now());
227 store_->AddOfflinePage(
228 offline_page_item,
229 base::Bind(&OfflinePageModel::OnAddOfflinePageDone,
230 weak_ptr_factory_.GetWeakPtr(), archiver, callback,
231 offline_page_item));
234 void OfflinePageModel::OnAddOfflinePageDone(OfflinePageArchiver* archiver,
235 const SavePageCallback& callback,
236 const OfflinePageItem& offline_page,
237 bool success) {
238 SavePageResult result;
239 if (success) {
240 offline_pages_[offline_page.bookmark_id] = offline_page;
241 result = SavePageResult::SUCCESS;
242 } else {
243 result = SavePageResult::STORE_FAILURE;
245 InformSavePageDone(callback, result);
246 DeletePendingArchiver(archiver);
249 void OfflinePageModel::BookmarkModelChanged() {
252 void OfflinePageModel::BookmarkNodeRemoved(
253 bookmarks::BookmarkModel* model,
254 const bookmarks::BookmarkNode* parent,
255 int old_index,
256 const bookmarks::BookmarkNode* node,
257 const std::set<GURL>& removed_urls) {
258 if (!is_loaded_) {
259 delayed_tasks_.push_back(
260 base::Bind(&OfflinePageModel::DeletePageByBookmarkId,
261 weak_ptr_factory_.GetWeakPtr(),
262 node->id(),
263 base::Bind(&EmptyDeleteCallback)));
264 return;
266 DeletePageByBookmarkId(node->id(), base::Bind(&EmptyDeleteCallback));
269 void OfflinePageModel::OnLoadDone(
270 bool success,
271 const std::vector<OfflinePageItem>& offline_pages) {
272 DCHECK(!is_loaded_);
273 is_loaded_ = true;
275 if (success) {
276 for (const auto& offline_page : offline_pages)
277 offline_pages_[offline_page.bookmark_id] = offline_page;
280 // Run all the delayed tasks.
281 for (const auto& delayed_task : delayed_tasks_)
282 delayed_task.Run();
283 delayed_tasks_.clear();
285 FOR_EACH_OBSERVER(Observer, observers_, OfflinePageModelLoaded(this));
288 void OfflinePageModel::InformSavePageDone(const SavePageCallback& callback,
289 SavePageResult result) {
290 callback.Run(result);
293 void OfflinePageModel::DeletePendingArchiver(OfflinePageArchiver* archiver) {
294 pending_archivers_.erase(std::find(
295 pending_archivers_.begin(), pending_archivers_.end(), archiver));
298 void OfflinePageModel::OnDeleteArchiveFilesDone(
299 const std::vector<int64>& bookmark_ids,
300 const DeletePageCallback& callback,
301 const bool* success) {
302 DCHECK(success);
304 if (!*success) {
305 callback.Run(DeletePageResult::DEVICE_FAILURE);
306 return;
309 store_->RemoveOfflinePages(
310 bookmark_ids,
311 base::Bind(&OfflinePageModel::OnRemoveOfflinePagesDone,
312 weak_ptr_factory_.GetWeakPtr(), bookmark_ids, callback));
315 void OfflinePageModel::OnRemoveOfflinePagesDone(
316 const std::vector<int64>& bookmark_ids,
317 const DeletePageCallback& callback,
318 bool success) {
319 // Delete the offline page from the in memory cache regardless of success in
320 // store.
321 base::Time now = base::Time::Now();
322 for (int64 bookmark_id : bookmark_ids) {
323 auto iter = offline_pages_.find(bookmark_id);
324 if (iter == offline_pages_.end())
325 continue;
326 UMA_HISTOGRAM_CUSTOM_COUNTS("OfflinePages.PageLifetime",
327 (now - iter->second.creation_time).InMinutes(),
329 base::TimeDelta::FromDays(365).InMinutes(),
330 100);
331 offline_pages_.erase(iter);
333 // Deleting multiple pages always succeeds when it gets to this point.
334 if (success || bookmark_ids.size() > 1)
335 callback.Run(DeletePageResult::SUCCESS);
336 else
337 callback.Run(DeletePageResult::STORE_FAILURE);
340 } // namespace offline_pages