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 "chrome/browser/chromeos/base/file_flusher.h" | 5 #include "chrome/browser/chromeos/base/file_flusher.h" |
6 | 6 |
7 #include <algorithm> | |
7 #include <set> | 8 #include <set> |
8 | 9 |
9 #include "base/bind.h" | 10 #include "base/bind.h" |
10 #include "base/files/file.h" | 11 #include "base/files/file.h" |
11 #include "base/files/file_enumerator.h" | 12 #include "base/files/file_enumerator.h" |
12 #include "base/logging.h" | 13 #include "base/logging.h" |
13 #include "base/synchronization/cancellation_flag.h" | 14 #include "base/synchronization/cancellation_flag.h" |
14 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
15 | 16 |
16 namespace chromeos { | 17 namespace chromeos { |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
61 void FinishOnUIThread(); | 62 void FinishOnUIThread(); |
62 | 63 |
63 base::WeakPtr<FileFlusher> master_; | 64 base::WeakPtr<FileFlusher> master_; |
64 const base::FilePath path_; | 65 const base::FilePath path_; |
65 const std::set<base::FilePath> excludes_; | 66 const std::set<base::FilePath> excludes_; |
66 const FileFlusher::OnFlushCallback on_flush_callback_; | 67 const FileFlusher::OnFlushCallback on_flush_callback_; |
67 const base::Closure callback_; | 68 const base::Closure callback_; |
68 | 69 |
69 bool started_ = false; | 70 bool started_ = false; |
70 base::CancellationFlag cancel_flag_; | 71 base::CancellationFlag cancel_flag_; |
72 bool finish_scheduled_ = false; | |
71 | 73 |
72 DISALLOW_COPY_AND_ASSIGN(Job); | 74 DISALLOW_COPY_AND_ASSIGN(Job); |
73 }; | 75 }; |
74 | 76 |
75 FileFlusher::Job::Job(const base::WeakPtr<FileFlusher>& master, | 77 FileFlusher::Job::Job(const base::WeakPtr<FileFlusher>& master, |
76 const base::FilePath& path, | 78 const base::FilePath& path, |
77 const std::vector<base::FilePath>& excludes, | 79 const std::vector<base::FilePath>& excludes, |
78 const FileFlusher::OnFlushCallback& on_flush_callback, | 80 const FileFlusher::OnFlushCallback& on_flush_callback, |
79 const base::Closure& callback) | 81 const base::Closure& callback) |
80 : master_(master), | 82 : master_(master), |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
136 if (!on_flush_callback_.is_null()) | 138 if (!on_flush_callback_.is_null()) |
137 on_flush_callback_.Run(current); | 139 on_flush_callback_.Run(current); |
138 } | 140 } |
139 } | 141 } |
140 | 142 |
141 bool FileFlusher::Job::ShouldExclude(const base::FilePath& path) const { | 143 bool FileFlusher::Job::ShouldExclude(const base::FilePath& path) const { |
142 return excludes_.find(path) != excludes_.end(); | 144 return excludes_.find(path) != excludes_.end(); |
143 } | 145 } |
144 | 146 |
145 void FileFlusher::Job::ScheduleFinish() { | 147 void FileFlusher::Job::ScheduleFinish() { |
148 if (finish_scheduled_) | |
149 return; | |
150 | |
151 finish_scheduled_ = true; | |
146 content::BrowserThread::PostTask( | 152 content::BrowserThread::PostTask( |
147 content::BrowserThread::UI, FROM_HERE, | 153 content::BrowserThread::UI, FROM_HERE, |
148 base::Bind(&Job::FinishOnUIThread, base::Unretained(this))); | 154 base::Bind(&Job::FinishOnUIThread, base::Unretained(this))); |
149 } | 155 } |
150 | 156 |
151 void FileFlusher::Job::FinishOnUIThread() { | 157 void FileFlusher::Job::FinishOnUIThread() { |
152 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 158 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
153 | 159 |
154 if (!callback_.is_null()) | 160 if (!callback_.is_null()) |
155 callback_.Run(); | 161 callback_.Run(); |
(...skipping 20 matching lines...) Expand all Loading... | |
176 for (auto* job : jobs_) { | 182 for (auto* job : jobs_) { |
177 if (path == job->path() || path.IsParent(job->path())) | 183 if (path == job->path() || path.IsParent(job->path())) |
178 job->Cancel(); | 184 job->Cancel(); |
179 } | 185 } |
180 | 186 |
181 jobs_.push_back(new Job(weak_factory_.GetWeakPtr(), path, excludes, | 187 jobs_.push_back(new Job(weak_factory_.GetWeakPtr(), path, excludes, |
182 on_flush_callback_for_test_, callback)); | 188 on_flush_callback_for_test_, callback)); |
183 ScheduleJob(); | 189 ScheduleJob(); |
184 } | 190 } |
185 | 191 |
192 void FileFlusher::PauseForTest() { | |
193 DCHECK(std::none_of(jobs_.begin(), jobs_.end(), | |
achuithb
2016/09/15 18:36:56
Wow, nice. It's like python.
| |
194 [](const Job* job) { return job->started(); })); | |
195 paused_for_test_ = true; | |
196 } | |
197 | |
198 void FileFlusher::ResumeForTest() { | |
199 paused_for_test_ = false; | |
200 ScheduleJob(); | |
201 } | |
202 | |
186 void FileFlusher::ScheduleJob() { | 203 void FileFlusher::ScheduleJob() { |
187 if (jobs_.empty()) | 204 if (jobs_.empty() || paused_for_test_) |
188 return; | 205 return; |
189 | 206 |
190 auto* job = jobs_.front(); | 207 auto* job = jobs_.front(); |
191 if (!job->started()) | 208 if (!job->started()) |
192 job->Start(); | 209 job->Start(); |
193 } | 210 } |
194 | 211 |
195 void FileFlusher::OnJobDone(FileFlusher::Job* job) { | 212 void FileFlusher::OnJobDone(FileFlusher::Job* job) { |
196 for (auto it = jobs_.begin(); it != jobs_.end(); ++it) { | 213 for (auto it = jobs_.begin(); it != jobs_.end(); ++it) { |
197 if (*it == job) { | 214 if (*it == job) { |
198 jobs_.erase(it); | 215 jobs_.erase(it); |
199 break; | 216 break; |
200 } | 217 } |
201 } | 218 } |
202 | 219 |
203 ScheduleJob(); | 220 ScheduleJob(); |
204 } | 221 } |
205 | 222 |
206 } // namespace chromeos | 223 } // namespace chromeos |
OLD | NEW |