Chromium Code Reviews| Index: components/offline_pages/archive_manager.cc |
| diff --git a/components/offline_pages/archive_manager.cc b/components/offline_pages/archive_manager.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..8c44ac94cf49d16812aa910fa47085b67876b38f |
| --- /dev/null |
| +++ b/components/offline_pages/archive_manager.cc |
| @@ -0,0 +1,73 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "base/bind.h" |
| +#include "base/callback.h" |
| +#include "base/files/file_util.h" |
| +#include "base/location.h" |
| +#include "components/offline_pages/archive_manager.h" |
| + |
| +namespace offline_pages { |
|
jianli
2016/05/13 23:50:48
nit: add empty line
fgorski
2016/05/16 17:46:51
Done.
|
| +namespace { |
|
jianli
2016/05/13 23:50:48
nit: add empty line
fgorski
2016/05/16 17:46:51
Done.
|
| +void EnsureArchivesDirCreatedImpl(const base::FilePath& archives_dir) { |
| + CHECK(base::CreateDirectory(archives_dir)); |
| +} |
| + |
| +void ArchiveExistsImpl(const base::FilePath& file_path, bool* exists) { |
| + DCHECK(exists); |
|
jianli
2016/05/13 23:50:48
Please include logging.h
fgorski
2016/05/16 17:46:51
Done.
|
| + *exists = base::PathExists(file_path); |
| +} |
| + |
| +void DeleteArchivesImpl(const std::vector<base::FilePath>& file_paths, |
| + bool* result) { |
| + DCHECK(result); |
| + for (const auto& file_path : file_paths) { |
| + // Make sure delete happens on the left of || so that it is always executed. |
| + *result = base::DeleteFile(file_path, false) || *result; |
| + } |
| +} |
| + |
| +void CompleteBooleanCallback(const base::Callback<void(bool)>& callback, |
| + bool* exists) { |
| + callback.Run(*exists); |
| +} |
| +} // namespace |
| + |
| +ArchiveManager::ArchiveManager( |
| + const base::FilePath& archives_dir, |
| + const scoped_refptr<base::SequencedTaskRunner>& task_runner) |
| + : archives_dir_(archives_dir), task_runner_(task_runner) {} |
| + |
| +ArchiveManager::~ArchiveManager() {} |
| + |
| +void ArchiveManager::EnsureArchivesDirCreated(const base::Closure& callback) { |
| + task_runner_->PostTaskAndReply( |
| + FROM_HERE, base::Bind(EnsureArchivesDirCreatedImpl, archives_dir_), |
| + callback); |
| +} |
| + |
| +void ArchiveManager::ArchiveExists(const base::FilePath& archive_path, |
| + const base::Callback<void(bool)>& callback) { |
| + bool* result = new bool(false); |
| + task_runner_->PostTaskAndReply( |
| + FROM_HERE, base::Bind(ArchiveExistsImpl, archive_path, result), |
| + base::Bind(CompleteBooleanCallback, callback, base::Owned(result))); |
| +} |
| + |
| +void ArchiveManager::DeleteArchive(const base::FilePath& archive_path, |
| + const base::Callback<void(bool)>& callback) { |
| + std::vector<base::FilePath> archive_paths = {archive_path}; |
| + DeleteArchives(archive_paths, callback); |
| +} |
| + |
| +void ArchiveManager::DeleteArchives( |
| + const std::vector<base::FilePath>& archive_paths, |
| + const base::Callback<void(bool)>& callback) { |
| + bool* result = new bool(false); |
| + task_runner_->PostTaskAndReply( |
| + FROM_HERE, base::Bind(DeleteArchivesImpl, archive_paths, result), |
| + base::Bind(CompleteBooleanCallback, callback, base::Owned(result))); |
| +} |
| + |
| +} // namespace offline_pages |