| 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_enumerator.h" |
| 8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
| 9 #include "base/location.h" | 9 #include "base/location.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/sequenced_task_runner.h" | 12 #include "base/sequenced_task_runner.h" |
| 13 #include "base/sys_info.h" |
| 13 #include "base/threading/thread_task_runner_handle.h" | 14 #include "base/threading/thread_task_runner_handle.h" |
| 14 #include "components/offline_pages/archive_manager.h" | 15 #include "components/offline_pages/archive_manager.h" |
| 15 | 16 |
| 16 namespace offline_pages { | 17 namespace offline_pages { |
| 17 | 18 |
| 18 namespace { | 19 namespace { |
| 19 | 20 |
| 21 using StorageStatsCallback = |
| 22 base::Callback<void(const ArchiveManager::StorageStats& storage_stats)>; |
| 23 |
| 20 void EnsureArchivesDirCreatedImpl(const base::FilePath& archives_dir) { | 24 void EnsureArchivesDirCreatedImpl(const base::FilePath& archives_dir) { |
| 21 CHECK(base::CreateDirectory(archives_dir)); | 25 CHECK(base::CreateDirectory(archives_dir)); |
| 22 } | 26 } |
| 23 | 27 |
| 24 void ExistsArchiveImpl(const base::FilePath& file_path, | 28 void ExistsArchiveImpl(const base::FilePath& file_path, |
| 25 scoped_refptr<base::SequencedTaskRunner> task_runner, | 29 scoped_refptr<base::SequencedTaskRunner> task_runner, |
| 26 const base::Callback<void(bool)>& callback) { | 30 const base::Callback<void(bool)>& callback) { |
| 27 task_runner->PostTask(FROM_HERE, | 31 task_runner->PostTask(FROM_HERE, |
| 28 base::Bind(callback, base::PathExists(file_path))); | 32 base::Bind(callback, base::PathExists(file_path))); |
| 29 } | 33 } |
| (...skipping 16 matching lines...) Expand all Loading... |
| 46 std::set<base::FilePath> archive_paths; | 50 std::set<base::FilePath> archive_paths; |
| 47 base::FileEnumerator file_enumerator(archive_dir, false, | 51 base::FileEnumerator file_enumerator(archive_dir, false, |
| 48 base::FileEnumerator::FILES); | 52 base::FileEnumerator::FILES); |
| 49 for (base::FilePath archive_path = file_enumerator.Next(); | 53 for (base::FilePath archive_path = file_enumerator.Next(); |
| 50 !archive_path.empty(); archive_path = file_enumerator.Next()) { | 54 !archive_path.empty(); archive_path = file_enumerator.Next()) { |
| 51 archive_paths.insert(archive_path); | 55 archive_paths.insert(archive_path); |
| 52 } | 56 } |
| 53 task_runner->PostTask(FROM_HERE, base::Bind(callback, archive_paths)); | 57 task_runner->PostTask(FROM_HERE, base::Bind(callback, archive_paths)); |
| 54 } | 58 } |
| 55 | 59 |
| 60 void GetStorageStatsImpl(const base::FilePath& archive_dir, |
| 61 scoped_refptr<base::SequencedTaskRunner> task_runner, |
| 62 const StorageStatsCallback& callback) { |
| 63 ArchiveManager::StorageStats storage_stats; |
| 64 storage_stats.free_disk_space = |
| 65 base::SysInfo::AmountOfFreeDiskSpace(archive_dir); |
| 66 storage_stats.total_archives_size = base::ComputeDirectorySize(archive_dir); |
| 67 task_runner->PostTask(FROM_HERE, base::Bind(callback, storage_stats)); |
| 68 } |
| 69 |
| 56 } // namespace | 70 } // namespace |
| 57 | 71 |
| 58 ArchiveManager::ArchiveManager( | 72 ArchiveManager::ArchiveManager( |
| 59 const base::FilePath& archives_dir, | 73 const base::FilePath& archives_dir, |
| 60 const scoped_refptr<base::SequencedTaskRunner>& task_runner) | 74 const scoped_refptr<base::SequencedTaskRunner>& task_runner) |
| 61 : archives_dir_(archives_dir), task_runner_(task_runner) {} | 75 : archives_dir_(archives_dir), task_runner_(task_runner) {} |
| 62 | 76 |
| 63 ArchiveManager::~ArchiveManager() {} | 77 ArchiveManager::~ArchiveManager() {} |
| 64 | 78 |
| 65 void ArchiveManager::EnsureArchivesDirCreated(const base::Closure& callback) { | 79 void ArchiveManager::EnsureArchivesDirCreated(const base::Closure& callback) { |
| (...skipping 24 matching lines...) Expand all Loading... |
| 90 } | 104 } |
| 91 | 105 |
| 92 void ArchiveManager::GetAllArchives( | 106 void ArchiveManager::GetAllArchives( |
| 93 const base::Callback<void(const std::set<base::FilePath>&)>& callback) | 107 const base::Callback<void(const std::set<base::FilePath>&)>& callback) |
| 94 const { | 108 const { |
| 95 task_runner_->PostTask( | 109 task_runner_->PostTask( |
| 96 FROM_HERE, base::Bind(GetAllArchivesImpl, archives_dir_, | 110 FROM_HERE, base::Bind(GetAllArchivesImpl, archives_dir_, |
| 97 base::ThreadTaskRunnerHandle::Get(), callback)); | 111 base::ThreadTaskRunnerHandle::Get(), callback)); |
| 98 } | 112 } |
| 99 | 113 |
| 114 void ArchiveManager::GetStorageStats( |
| 115 const StorageStatsCallback& callback) const { |
| 116 task_runner_->PostTask( |
| 117 FROM_HERE, base::Bind(GetStorageStatsImpl, archives_dir_, |
| 118 base::ThreadTaskRunnerHandle::Get(), callback)); |
| 119 } |
| 120 |
| 100 } // namespace offline_pages | 121 } // namespace offline_pages |
| OLD | NEW |