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

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

Issue 2889663004: [DO NOT COMMIT][Offline Pages] Moving Offline Page Cache to cache directory.
Patch Set: again? Created 3 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
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 "components/offline_pages/core/archive_manager.h"
6
7 #include <set>
8 #include <vector>
9
5 #include "base/bind.h" 10 #include "base/bind.h"
6 #include "base/callback.h" 11 #include "base/callback.h"
7 #include "base/files/file_enumerator.h" 12 #include "base/files/file_enumerator.h"
8 #include "base/files/file_util.h" 13 #include "base/files/file_util.h"
9 #include "base/location.h" 14 #include "base/location.h"
10 #include "base/logging.h" 15 #include "base/logging.h"
11 #include "base/memory/ref_counted.h" 16 #include "base/memory/ref_counted.h"
12 #include "base/sequenced_task_runner.h" 17 #include "base/sequenced_task_runner.h"
13 #include "base/sys_info.h" 18 #include "base/sys_info.h"
14 #include "base/threading/thread_task_runner_handle.h" 19 #include "base/threading/thread_task_runner_handle.h"
15 #include "components/offline_pages/core/archive_manager.h"
16 20
17 namespace offline_pages { 21 namespace offline_pages {
18 22
23 using LifetimeType = LifetimePolicy::LifetimeType;
24
19 namespace { 25 namespace {
20 26
21 using StorageStatsCallback = 27 using StorageStatsCallback =
22 base::Callback<void(const ArchiveManager::StorageStats& storage_stats)>; 28 base::Callback<void(const ArchiveManager::StorageStats& storage_stats)>;
23 29
24 void EnsureArchivesDirCreatedImpl(const base::FilePath& archives_dir) { 30 void EnsureArchivesDirCreatedImpl(const ArchiveDirectories& archive_dirs) {
25 CHECK(base::CreateDirectory(archives_dir)); 31 for (const auto& archive_dir : archive_dirs) {
32 CHECK(base::CreateDirectory(archive_dir.second));
33 }
26 } 34 }
27 35
28 void ExistsArchiveImpl(const base::FilePath& file_path, 36 void ExistsArchiveImpl(const base::FilePath& file_path,
29 scoped_refptr<base::SequencedTaskRunner> task_runner, 37 scoped_refptr<base::SequencedTaskRunner> task_runner,
30 const base::Callback<void(bool)>& callback) { 38 const base::Callback<void(bool)>& callback) {
31 task_runner->PostTask(FROM_HERE, 39 task_runner->PostTask(FROM_HERE,
32 base::Bind(callback, base::PathExists(file_path))); 40 base::Bind(callback, base::PathExists(file_path)));
33 } 41 }
34 42
35 void DeleteArchivesImpl(const std::vector<base::FilePath>& file_paths, 43 void DeleteArchivesImpl(const std::vector<base::FilePath>& file_paths,
36 scoped_refptr<base::SequencedTaskRunner> task_runner, 44 scoped_refptr<base::SequencedTaskRunner> task_runner,
37 const base::Callback<void(bool)>& callback) { 45 const base::Callback<void(bool)>& callback) {
38 bool result = false; 46 bool result = false;
39 for (const auto& file_path : file_paths) { 47 for (const auto& file_path : file_paths) {
40 // Make sure delete happens on the left of || so that it is always executed. 48 // Make sure delete happens on the left of || so that it is always executed.
41 result = base::DeleteFile(file_path, false) || result; 49 result = base::DeleteFile(file_path, false) || result;
42 } 50 }
43 task_runner->PostTask(FROM_HERE, base::Bind(callback, result)); 51 task_runner->PostTask(FROM_HERE, base::Bind(callback, result));
44 } 52 }
45 53
46 void GetAllArchivesImpl( 54 void GetAllArchivesImpl(
47 const base::FilePath& archive_dir, 55 const std::vector<base::FilePath>& archive_dirs,
48 scoped_refptr<base::SequencedTaskRunner> task_runner, 56 scoped_refptr<base::SequencedTaskRunner> task_runner,
49 const base::Callback<void(const std::set<base::FilePath>&)>& callback) { 57 const base::Callback<void(const std::set<base::FilePath>&)>& callback) {
50 std::set<base::FilePath> archive_paths; 58 std::set<base::FilePath> archive_paths;
51 base::FileEnumerator file_enumerator(archive_dir, false, 59 // Iterate through all the archive directories.
52 base::FileEnumerator::FILES); 60 for (const auto& archive_dir : archive_dirs) {
53 for (base::FilePath archive_path = file_enumerator.Next(); 61 base::FileEnumerator file_enumerator(archive_dir, false,
54 !archive_path.empty(); archive_path = file_enumerator.Next()) { 62 base::FileEnumerator::FILES);
55 archive_paths.insert(archive_path); 63 for (base::FilePath archive_path = file_enumerator.Next();
64 !archive_path.empty(); archive_path = file_enumerator.Next()) {
65 archive_paths.insert(archive_path);
66 }
56 } 67 }
57 task_runner->PostTask(FROM_HERE, base::Bind(callback, archive_paths)); 68 task_runner->PostTask(FROM_HERE, base::Bind(callback, archive_paths));
58 } 69 }
59 70
60 void GetStorageStatsImpl(const base::FilePath& archive_dir, 71 void GetStorageStatsImpl(const std::vector<base::FilePath>& archive_dirs,
61 scoped_refptr<base::SequencedTaskRunner> task_runner, 72 scoped_refptr<base::SequencedTaskRunner> task_runner,
62 const StorageStatsCallback& callback) { 73 const StorageStatsCallback& callback) {
63 ArchiveManager::StorageStats storage_stats; 74 ArchiveManager::StorageStats storage_stats;
64 storage_stats.free_disk_space = 75 storage_stats.free_disk_space = 0;
65 base::SysInfo::AmountOfFreeDiskSpace(archive_dir); 76 storage_stats.total_archives_size = 0;
66 storage_stats.total_archives_size = base::ComputeDirectorySize(archive_dir); 77 // Since currently both types of pages are still saved on the same disk, we
78 // should divide the free disk space by the number of places we save pages.
79 for (const auto& archive_dir : archive_dirs) {
80 storage_stats.free_disk_space +=
81 base::SysInfo::AmountOfFreeDiskSpace(archive_dir);
82 storage_stats.total_archives_size +=
83 base::ComputeDirectorySize(archive_dir);
84 }
85 storage_stats.free_disk_space /= archive_dirs.size();
67 task_runner->PostTask(FROM_HERE, base::Bind(callback, storage_stats)); 86 task_runner->PostTask(FROM_HERE, base::Bind(callback, storage_stats));
68 } 87 }
69 88
70 } // namespace 89 } // namespace
71 90
72 // protected and used for testing. 91 // protected and used for testing.
73 ArchiveManager::ArchiveManager() {} 92 ArchiveManager::ArchiveManager() {}
74 93
75 ArchiveManager::ArchiveManager( 94 ArchiveManager::ArchiveManager(
76 const base::FilePath& archives_dir, 95 const ArchiveDirectories& archive_dirs,
77 const scoped_refptr<base::SequencedTaskRunner>& task_runner) 96 const scoped_refptr<base::SequencedTaskRunner>& task_runner)
78 : archives_dir_(archives_dir), task_runner_(task_runner) {} 97 : archive_dirs_(archive_dirs), task_runner_(task_runner) {}
79 98
80 ArchiveManager::~ArchiveManager() {} 99 ArchiveManager::~ArchiveManager() {}
81 100
82 void ArchiveManager::EnsureArchivesDirCreated(const base::Closure& callback) { 101 void ArchiveManager::EnsureArchivesDirCreated(const base::Closure& callback) {
83 task_runner_->PostTaskAndReply( 102 task_runner_->PostTaskAndReply(
84 FROM_HERE, base::Bind(EnsureArchivesDirCreatedImpl, archives_dir_), 103 FROM_HERE, base::Bind(EnsureArchivesDirCreatedImpl, archive_dirs_),
85 callback); 104 callback);
86 } 105 }
87 106
88 void ArchiveManager::ExistsArchive(const base::FilePath& archive_path, 107 void ArchiveManager::ExistsArchive(const base::FilePath& archive_path,
89 const base::Callback<void(bool)>& callback) { 108 const base::Callback<void(bool)>& callback) {
90 task_runner_->PostTask( 109 task_runner_->PostTask(
91 FROM_HERE, base::Bind(ExistsArchiveImpl, archive_path, 110 FROM_HERE, base::Bind(ExistsArchiveImpl, archive_path,
92 base::ThreadTaskRunnerHandle::Get(), callback)); 111 base::ThreadTaskRunnerHandle::Get(), callback));
93 } 112 }
94 113
95 void ArchiveManager::DeleteArchive(const base::FilePath& archive_path, 114 void ArchiveManager::DeleteArchive(const base::FilePath& archive_path,
96 const base::Callback<void(bool)>& callback) { 115 const base::Callback<void(bool)>& callback) {
97 std::vector<base::FilePath> archive_paths = {archive_path}; 116 std::vector<base::FilePath> archive_paths = {archive_path};
98 DeleteMultipleArchives(archive_paths, callback); 117 DeleteMultipleArchives(archive_paths, callback);
99 } 118 }
100 119
101 void ArchiveManager::DeleteMultipleArchives( 120 void ArchiveManager::DeleteMultipleArchives(
102 const std::vector<base::FilePath>& archive_paths, 121 const std::vector<base::FilePath>& archive_paths,
103 const base::Callback<void(bool)>& callback) { 122 const base::Callback<void(bool)>& callback) {
104 task_runner_->PostTask( 123 task_runner_->PostTask(
105 FROM_HERE, base::Bind(DeleteArchivesImpl, archive_paths, 124 FROM_HERE, base::Bind(DeleteArchivesImpl, archive_paths,
106 base::ThreadTaskRunnerHandle::Get(), callback)); 125 base::ThreadTaskRunnerHandle::Get(), callback));
107 } 126 }
108 127
109 void ArchiveManager::GetAllArchives( 128 void ArchiveManager::GetAllArchives(
110 const base::Callback<void(const std::set<base::FilePath>&)>& callback) 129 const base::Callback<void(const std::set<base::FilePath>&)>& callback)
111 const { 130 const {
131 std::vector<base::FilePath> archive_dirs;
132 for (const auto& iter : archive_dirs_)
133 archive_dirs.push_back(iter.second);
112 task_runner_->PostTask( 134 task_runner_->PostTask(
113 FROM_HERE, base::Bind(GetAllArchivesImpl, archives_dir_, 135 FROM_HERE, base::Bind(GetAllArchivesImpl, archive_dirs,
114 base::ThreadTaskRunnerHandle::Get(), callback)); 136 base::ThreadTaskRunnerHandle::Get(), callback));
115 } 137 }
116 138
117 void ArchiveManager::GetStorageStats( 139 void ArchiveManager::GetStorageStats(
140 const std::set<LifetimeType>& lifetime_types,
118 const StorageStatsCallback& callback) const { 141 const StorageStatsCallback& callback) const {
142 std::vector<base::FilePath> archive_dirs;
143 for (const auto& type : lifetime_types)
144 archive_dirs.push_back(archive_dirs_.at(type));
145 DCHECK(archive_dirs.size());
119 task_runner_->PostTask( 146 task_runner_->PostTask(
120 FROM_HERE, base::Bind(GetStorageStatsImpl, archives_dir_, 147 FROM_HERE, base::Bind(GetStorageStatsImpl, archive_dirs,
121 base::ThreadTaskRunnerHandle::Get(), callback)); 148 base::ThreadTaskRunnerHandle::Get(), callback));
122 } 149 }
123 150
124 } // namespace offline_pages 151 } // namespace offline_pages
OLDNEW
« no previous file with comments | « components/offline_pages/core/archive_manager.h ('k') | components/offline_pages/core/archive_manager_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698