OLD | NEW |
| (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_enumerator.h" | |
8 #include "base/files/file_util.h" | |
9 #include "base/location.h" | |
10 #include "base/logging.h" | |
11 #include "base/memory/ref_counted.h" | |
12 #include "base/sequenced_task_runner.h" | |
13 #include "base/sys_info.h" | |
14 #include "base/threading/thread_task_runner_handle.h" | |
15 #include "components/offline_pages/archive_manager.h" | |
16 | |
17 namespace offline_pages { | |
18 | |
19 namespace { | |
20 | |
21 using StorageStatsCallback = | |
22 base::Callback<void(const ArchiveManager::StorageStats& storage_stats)>; | |
23 | |
24 void EnsureArchivesDirCreatedImpl(const base::FilePath& archives_dir) { | |
25 CHECK(base::CreateDirectory(archives_dir)); | |
26 } | |
27 | |
28 void ExistsArchiveImpl(const base::FilePath& file_path, | |
29 scoped_refptr<base::SequencedTaskRunner> task_runner, | |
30 const base::Callback<void(bool)>& callback) { | |
31 task_runner->PostTask(FROM_HERE, | |
32 base::Bind(callback, base::PathExists(file_path))); | |
33 } | |
34 | |
35 void DeleteArchivesImpl(const std::vector<base::FilePath>& file_paths, | |
36 scoped_refptr<base::SequencedTaskRunner> task_runner, | |
37 const base::Callback<void(bool)>& callback) { | |
38 bool result = false; | |
39 for (const auto& file_path : file_paths) { | |
40 // Make sure delete happens on the left of || so that it is always executed. | |
41 result = base::DeleteFile(file_path, false) || result; | |
42 } | |
43 task_runner->PostTask(FROM_HERE, base::Bind(callback, result)); | |
44 } | |
45 | |
46 void GetAllArchivesImpl( | |
47 const base::FilePath& archive_dir, | |
48 scoped_refptr<base::SequencedTaskRunner> task_runner, | |
49 const base::Callback<void(const std::set<base::FilePath>&)>& callback) { | |
50 std::set<base::FilePath> archive_paths; | |
51 base::FileEnumerator file_enumerator(archive_dir, false, | |
52 base::FileEnumerator::FILES); | |
53 for (base::FilePath archive_path = file_enumerator.Next(); | |
54 !archive_path.empty(); archive_path = file_enumerator.Next()) { | |
55 archive_paths.insert(archive_path); | |
56 } | |
57 task_runner->PostTask(FROM_HERE, base::Bind(callback, archive_paths)); | |
58 } | |
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 | |
70 } // namespace | |
71 | |
72 // protected and used for testing. | |
73 ArchiveManager::ArchiveManager() {} | |
74 | |
75 ArchiveManager::ArchiveManager( | |
76 const base::FilePath& archives_dir, | |
77 const scoped_refptr<base::SequencedTaskRunner>& task_runner) | |
78 : archives_dir_(archives_dir), task_runner_(task_runner) {} | |
79 | |
80 ArchiveManager::~ArchiveManager() {} | |
81 | |
82 void ArchiveManager::EnsureArchivesDirCreated(const base::Closure& callback) { | |
83 task_runner_->PostTaskAndReply( | |
84 FROM_HERE, base::Bind(EnsureArchivesDirCreatedImpl, archives_dir_), | |
85 callback); | |
86 } | |
87 | |
88 void ArchiveManager::ExistsArchive(const base::FilePath& archive_path, | |
89 const base::Callback<void(bool)>& callback) { | |
90 task_runner_->PostTask( | |
91 FROM_HERE, base::Bind(ExistsArchiveImpl, archive_path, | |
92 base::ThreadTaskRunnerHandle::Get(), callback)); | |
93 } | |
94 | |
95 void ArchiveManager::DeleteArchive(const base::FilePath& archive_path, | |
96 const base::Callback<void(bool)>& callback) { | |
97 std::vector<base::FilePath> archive_paths = {archive_path}; | |
98 DeleteMultipleArchives(archive_paths, callback); | |
99 } | |
100 | |
101 void ArchiveManager::DeleteMultipleArchives( | |
102 const std::vector<base::FilePath>& archive_paths, | |
103 const base::Callback<void(bool)>& callback) { | |
104 task_runner_->PostTask( | |
105 FROM_HERE, base::Bind(DeleteArchivesImpl, archive_paths, | |
106 base::ThreadTaskRunnerHandle::Get(), callback)); | |
107 } | |
108 | |
109 void ArchiveManager::GetAllArchives( | |
110 const base::Callback<void(const std::set<base::FilePath>&)>& callback) | |
111 const { | |
112 task_runner_->PostTask( | |
113 FROM_HERE, base::Bind(GetAllArchivesImpl, archives_dir_, | |
114 base::ThreadTaskRunnerHandle::Get(), callback)); | |
115 } | |
116 | |
117 void ArchiveManager::GetStorageStats( | |
118 const StorageStatsCallback& callback) const { | |
119 task_runner_->PostTask( | |
120 FROM_HERE, base::Bind(GetStorageStatsImpl, archives_dir_, | |
121 base::ThreadTaskRunnerHandle::Get(), callback)); | |
122 } | |
123 | |
124 } // namespace offline_pages | |
OLD | NEW |