| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/callback.h" | 6 #include "base/callback.h" |
| 7 #include "base/files/file_enumerator.h" |
| 7 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
| 8 #include "base/location.h" | 9 #include "base/location.h" |
| 9 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/sequenced_task_runner.h" |
| 13 #include "base/threading/thread_task_runner_handle.h" |
| 10 #include "components/offline_pages/archive_manager.h" | 14 #include "components/offline_pages/archive_manager.h" |
| 11 | 15 |
| 12 namespace offline_pages { | 16 namespace offline_pages { |
| 13 | 17 |
| 14 namespace { | 18 namespace { |
| 15 | 19 |
| 16 void EnsureArchivesDirCreatedImpl(const base::FilePath& archives_dir) { | 20 void EnsureArchivesDirCreatedImpl(const base::FilePath& archives_dir) { |
| 17 CHECK(base::CreateDirectory(archives_dir)); | 21 CHECK(base::CreateDirectory(archives_dir)); |
| 18 } | 22 } |
| 19 | 23 |
| 20 void ExistsArchiveImpl(const base::FilePath& file_path, bool* exists) { | 24 void ExistsArchiveImpl(const base::FilePath& file_path, |
| 21 DCHECK(exists); | 25 scoped_refptr<base::SequencedTaskRunner> task_runner, |
| 22 *exists = base::PathExists(file_path); | 26 const base::Callback<void(bool)>& callback) { |
| 27 task_runner->PostTask(FROM_HERE, |
| 28 base::Bind(callback, base::PathExists(file_path))); |
| 23 } | 29 } |
| 24 | 30 |
| 25 void DeleteArchivesImpl(const std::vector<base::FilePath>& file_paths, | 31 void DeleteArchivesImpl(const std::vector<base::FilePath>& file_paths, |
| 26 bool* result) { | 32 scoped_refptr<base::SequencedTaskRunner> task_runner, |
| 27 DCHECK(result); | 33 const base::Callback<void(bool)>& callback) { |
| 34 bool result = false; |
| 28 for (const auto& file_path : file_paths) { | 35 for (const auto& file_path : file_paths) { |
| 29 // Make sure delete happens on the left of || so that it is always executed. | 36 // Make sure delete happens on the left of || so that it is always executed. |
| 30 *result = base::DeleteFile(file_path, false) || *result; | 37 result = base::DeleteFile(file_path, false) || result; |
| 31 } | 38 } |
| 39 task_runner->PostTask(FROM_HERE, base::Bind(callback, result)); |
| 32 } | 40 } |
| 33 | 41 |
| 34 void CompleteBooleanCallback(const base::Callback<void(bool)>& callback, | 42 void GetAllArchivesImpl( |
| 35 bool* exists) { | 43 const base::FilePath& archive_dir, |
| 36 callback.Run(*exists); | 44 scoped_refptr<base::SequencedTaskRunner> task_runner, |
| 45 const base::Callback<void(const std::set<base::FilePath>&)>& callback) { |
| 46 std::set<base::FilePath> archive_paths; |
| 47 base::FileEnumerator file_enumerator(archive_dir, false, |
| 48 base::FileEnumerator::FILES); |
| 49 for (base::FilePath archive_path = file_enumerator.Next(); |
| 50 !archive_path.empty(); archive_path = file_enumerator.Next()) { |
| 51 archive_paths.insert(archive_path); |
| 52 } |
| 53 task_runner->PostTask(FROM_HERE, base::Bind(callback, archive_paths)); |
| 37 } | 54 } |
| 55 |
| 38 } // namespace | 56 } // namespace |
| 39 | 57 |
| 40 ArchiveManager::ArchiveManager( | 58 ArchiveManager::ArchiveManager( |
| 41 const base::FilePath& archives_dir, | 59 const base::FilePath& archives_dir, |
| 42 const scoped_refptr<base::SequencedTaskRunner>& task_runner) | 60 const scoped_refptr<base::SequencedTaskRunner>& task_runner) |
| 43 : archives_dir_(archives_dir), task_runner_(task_runner) {} | 61 : archives_dir_(archives_dir), task_runner_(task_runner) {} |
| 44 | 62 |
| 45 ArchiveManager::~ArchiveManager() {} | 63 ArchiveManager::~ArchiveManager() {} |
| 46 | 64 |
| 47 void ArchiveManager::EnsureArchivesDirCreated(const base::Closure& callback) { | 65 void ArchiveManager::EnsureArchivesDirCreated(const base::Closure& callback) { |
| 48 task_runner_->PostTaskAndReply( | 66 task_runner_->PostTaskAndReply( |
| 49 FROM_HERE, base::Bind(EnsureArchivesDirCreatedImpl, archives_dir_), | 67 FROM_HERE, base::Bind(EnsureArchivesDirCreatedImpl, archives_dir_), |
| 50 callback); | 68 callback); |
| 51 } | 69 } |
| 52 | 70 |
| 53 void ArchiveManager::ExistsArchive(const base::FilePath& archive_path, | 71 void ArchiveManager::ExistsArchive(const base::FilePath& archive_path, |
| 54 const base::Callback<void(bool)>& callback) { | 72 const base::Callback<void(bool)>& callback) { |
| 55 bool* result = new bool(false); | 73 task_runner_->PostTask( |
| 56 task_runner_->PostTaskAndReply( | 74 FROM_HERE, base::Bind(ExistsArchiveImpl, archive_path, |
| 57 FROM_HERE, base::Bind(ExistsArchiveImpl, archive_path, result), | 75 base::ThreadTaskRunnerHandle::Get(), callback)); |
| 58 base::Bind(CompleteBooleanCallback, callback, base::Owned(result))); | |
| 59 } | 76 } |
| 60 | 77 |
| 61 void ArchiveManager::DeleteArchive(const base::FilePath& archive_path, | 78 void ArchiveManager::DeleteArchive(const base::FilePath& archive_path, |
| 62 const base::Callback<void(bool)>& callback) { | 79 const base::Callback<void(bool)>& callback) { |
| 63 std::vector<base::FilePath> archive_paths = {archive_path}; | 80 std::vector<base::FilePath> archive_paths = {archive_path}; |
| 64 DeleteMultipleArchives(archive_paths, callback); | 81 DeleteMultipleArchives(archive_paths, callback); |
| 65 } | 82 } |
| 66 | 83 |
| 67 void ArchiveManager::DeleteMultipleArchives( | 84 void ArchiveManager::DeleteMultipleArchives( |
| 68 const std::vector<base::FilePath>& archive_paths, | 85 const std::vector<base::FilePath>& archive_paths, |
| 69 const base::Callback<void(bool)>& callback) { | 86 const base::Callback<void(bool)>& callback) { |
| 70 bool* result = new bool(false); | 87 task_runner_->PostTask( |
| 71 task_runner_->PostTaskAndReply( | 88 FROM_HERE, base::Bind(DeleteArchivesImpl, archive_paths, |
| 72 FROM_HERE, base::Bind(DeleteArchivesImpl, archive_paths, result), | 89 base::ThreadTaskRunnerHandle::Get(), callback)); |
| 73 base::Bind(CompleteBooleanCallback, callback, base::Owned(result))); | 90 } |
| 91 |
| 92 void ArchiveManager::GetAllArchives( |
| 93 const base::Callback<void(const std::set<base::FilePath>&)>& callback) |
| 94 const { |
| 95 task_runner_->PostTask( |
| 96 FROM_HERE, base::Bind(GetAllArchivesImpl, archives_dir_, |
| 97 base::ThreadTaskRunnerHandle::Get(), callback)); |
| 74 } | 98 } |
| 75 | 99 |
| 76 } // namespace offline_pages | 100 } // namespace offline_pages |
| OLD | NEW |