Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(426)

Side by Side Diff: components/offline_pages/archive_manager.cc

Issue 1977853004: [Offline pages] Adding ArchiveManager to abstract offline archives management (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Adding a missing destructor for OfflinePageStorageManager::Client Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 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.
4
5 #include "base/bind.h"
6 #include "base/callback.h"
7 #include "base/files/file_util.h"
8 #include "base/location.h"
9 #include "base/logging.h"
10 #include "components/offline_pages/archive_manager.h"
11
12 namespace offline_pages {
13
14 namespace {
15
16 void EnsureArchivesDirCreatedImpl(const base::FilePath& archives_dir) {
17 CHECK(base::CreateDirectory(archives_dir));
18 }
19
20 void ExistsArchiveImpl(const base::FilePath& file_path, bool* exists) {
21 DCHECK(exists);
22 *exists = base::PathExists(file_path);
23 }
24
25 void DeleteArchivesImpl(const std::vector<base::FilePath>& file_paths,
26 bool* result) {
27 DCHECK(result);
28 for (const auto& file_path : file_paths) {
29 // Make sure delete happens on the left of || so that it is always executed.
30 *result = base::DeleteFile(file_path, false) || *result;
31 }
32 }
33
34 void CompleteBooleanCallback(const base::Callback<void(bool)>& callback,
35 bool* exists) {
36 callback.Run(*exists);
37 }
38 } // namespace
39
40 ArchiveManager::ArchiveManager(
41 const base::FilePath& archives_dir,
42 const scoped_refptr<base::SequencedTaskRunner>& task_runner)
43 : archives_dir_(archives_dir), task_runner_(task_runner) {}
44
45 ArchiveManager::~ArchiveManager() {}
46
47 void ArchiveManager::EnsureArchivesDirCreated(const base::Closure& callback) {
48 task_runner_->PostTaskAndReply(
49 FROM_HERE, base::Bind(EnsureArchivesDirCreatedImpl, archives_dir_),
50 callback);
51 }
52
53 void ArchiveManager::ExistsArchive(const base::FilePath& archive_path,
54 const base::Callback<void(bool)>& callback) {
55 bool* result = new bool(false);
56 task_runner_->PostTaskAndReply(
57 FROM_HERE, base::Bind(ExistsArchiveImpl, archive_path, result),
58 base::Bind(CompleteBooleanCallback, callback, base::Owned(result)));
59 }
60
61 void ArchiveManager::DeleteArchive(const base::FilePath& archive_path,
62 const base::Callback<void(bool)>& callback) {
63 std::vector<base::FilePath> archive_paths = {archive_path};
64 DeleteMultipleArchives(archive_paths, callback);
65 }
66
67 void ArchiveManager::DeleteMultipleArchives(
68 const std::vector<base::FilePath>& archive_paths,
69 const base::Callback<void(bool)>& callback) {
70 bool* result = new bool(false);
71 task_runner_->PostTaskAndReply(
72 FROM_HERE, base::Bind(DeleteArchivesImpl, archive_paths, result),
73 base::Bind(CompleteBooleanCallback, callback, base::Owned(result)));
74 }
75
76 } // namespace offline_pages
OLDNEW
« no previous file with comments | « components/offline_pages/archive_manager.h ('k') | components/offline_pages/archive_manager_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698