[email protected] | 799d53bf | 2013-08-07 06:59:56 | [diff] [blame] | 1 | // Copyright 2013 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 | |
lukasza | 3fb2262 | 2015-08-27 21:04:34 | [diff] [blame] | 5 | #include "components/drive/file_write_watcher.h" |
[email protected] | 799d53bf | 2013-08-07 06:59:56 | [diff] [blame] | 6 | |
avi | bc5337b | 2015-12-25 23:16:33 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | #include <stdint.h> |
| 9 | |
[email protected] | 799d53bf | 2013-08-07 06:59:56 | [diff] [blame] | 10 | #include <map> |
[email protected] | 8df572cc | 2013-08-22 12:51:09 | [diff] [blame] | 11 | #include <vector> |
[email protected] | 799d53bf | 2013-08-07 06:59:56 | [diff] [blame] | 12 | |
| 13 | #include "base/bind.h" |
| 14 | #include "base/callback.h" |
| 15 | #include "base/files/file_path_watcher.h" |
avi | bc5337b | 2015-12-25 23:16:33 | [diff] [blame] | 16 | #include "base/macros.h" |
avi | 92a3e96 | 2016-09-21 02:50:21 | [diff] [blame] | 17 | #include "base/memory/ptr_util.h" |
gab | 10ae436 | 2016-11-02 23:34:53 | [diff] [blame] | 18 | #include "base/memory/ref_counted.h" |
| 19 | #include "base/single_thread_task_runner.h" |
[email protected] | 799d53bf | 2013-08-07 06:59:56 | [diff] [blame] | 20 | #include "base/timer/timer.h" |
[email protected] | e196bef3 | 2013-12-03 05:33:13 | [diff] [blame] | 21 | #include "google_apis/drive/task_util.h" |
[email protected] | 799d53bf | 2013-08-07 06:59:56 | [diff] [blame] | 22 | |
[email protected] | 799d53bf | 2013-08-07 06:59:56 | [diff] [blame] | 23 | namespace drive { |
| 24 | namespace internal { |
| 25 | |
| 26 | namespace { |
avi | bc5337b | 2015-12-25 23:16:33 | [diff] [blame] | 27 | const int64_t kWriteEventDelayInSeconds = 5; |
[email protected] | 799d53bf | 2013-08-07 06:59:56 | [diff] [blame] | 28 | } // namespace |
| 29 | |
| 30 | // base::FileWatcher needs to live in a thread that is allowed to do File IO |
| 31 | // and has a TYPE_IO message loop: that is, FILE thread. This class bridges the |
| 32 | // UI thread and FILE thread, and does all the main tasks in the FILE thread. |
| 33 | class FileWriteWatcher::FileWriteWatcherImpl { |
| 34 | public: |
lukasza | 2ff73e64 | 2015-06-11 03:52:43 | [diff] [blame] | 35 | explicit FileWriteWatcherImpl(base::SingleThreadTaskRunner* file_task_runner); |
[email protected] | 799d53bf | 2013-08-07 06:59:56 | [diff] [blame] | 36 | |
| 37 | // Forwards the call to DestoryOnFileThread(). This method must be used to |
| 38 | // destruct the instance. |
| 39 | void Destroy(); |
| 40 | |
[email protected] | 8df572cc | 2013-08-22 12:51:09 | [diff] [blame] | 41 | // Forwards the call to StartWatchOnFileThread(). |on_start_callback| is |
| 42 | // called back on the caller (UI) thread when the watch has started. |
| 43 | // |on_write_callback| is called when a write has happened to the path. |
[email protected] | 799d53bf | 2013-08-07 06:59:56 | [diff] [blame] | 44 | void StartWatch(const base::FilePath& path, |
[email protected] | 8df572cc | 2013-08-22 12:51:09 | [diff] [blame] | 45 | const StartWatchCallback& on_start_callback, |
| 46 | const base::Closure& on_write_callback); |
[email protected] | 799d53bf | 2013-08-07 06:59:56 | [diff] [blame] | 47 | |
| 48 | void set_delay(base::TimeDelta delay) { delay_ = delay; } |
| 49 | |
| 50 | private: |
| 51 | ~FileWriteWatcherImpl(); |
| 52 | |
| 53 | void DestroyOnFileThread(); |
| 54 | |
| 55 | void StartWatchOnFileThread(const base::FilePath& path, |
[email protected] | 8df572cc | 2013-08-22 12:51:09 | [diff] [blame] | 56 | const StartWatchCallback& on_start_callback, |
| 57 | const base::Closure& on_write_callback); |
[email protected] | 799d53bf | 2013-08-07 06:59:56 | [diff] [blame] | 58 | |
| 59 | void OnWriteEvent(const base::FilePath& path, bool error); |
| 60 | |
| 61 | void InvokeCallback(const base::FilePath& path); |
| 62 | |
| 63 | struct PathWatchInfo { |
[email protected] | 8df572cc | 2013-08-22 12:51:09 | [diff] [blame] | 64 | std::vector<base::Closure> on_write_callbacks; |
[email protected] | 799d53bf | 2013-08-07 06:59:56 | [diff] [blame] | 65 | base::FilePathWatcher watcher; |
| 66 | base::Timer timer; |
| 67 | |
[email protected] | 8df572cc | 2013-08-22 12:51:09 | [diff] [blame] | 68 | explicit PathWatchInfo(const base::Closure& on_write_callback) |
| 69 | : on_write_callbacks(1, on_write_callback), |
[email protected] | 799d53bf | 2013-08-07 06:59:56 | [diff] [blame] | 70 | timer(false /* retain_closure_on_reset */, false /* is_repeating */) { |
| 71 | } |
| 72 | }; |
| 73 | |
| 74 | base::TimeDelta delay_; |
avi | 92a3e96 | 2016-09-21 02:50:21 | [diff] [blame] | 75 | std::map<base::FilePath, std::unique_ptr<PathWatchInfo>> watchers_; |
lukasza | 2ff73e64 | 2015-06-11 03:52:43 | [diff] [blame] | 76 | scoped_refptr<base::SingleThreadTaskRunner> file_task_runner_; |
[email protected] | 799d53bf | 2013-08-07 06:59:56 | [diff] [blame] | 77 | |
lukasza | 037c10b1 | 2015-06-12 04:21:25 | [diff] [blame] | 78 | base::ThreadChecker thread_checker_; |
| 79 | |
[email protected] | 799d53bf | 2013-08-07 06:59:56 | [diff] [blame] | 80 | // Note: This should remain the last member so it'll be destroyed and |
| 81 | // invalidate its weak pointers before any other members are destroyed. |
| 82 | base::WeakPtrFactory<FileWriteWatcherImpl> weak_ptr_factory_; |
| 83 | DISALLOW_COPY_AND_ASSIGN(FileWriteWatcherImpl); |
| 84 | }; |
| 85 | |
lukasza | 2ff73e64 | 2015-06-11 03:52:43 | [diff] [blame] | 86 | FileWriteWatcher::FileWriteWatcherImpl::FileWriteWatcherImpl( |
| 87 | base::SingleThreadTaskRunner* file_task_runner) |
[email protected] | 799d53bf | 2013-08-07 06:59:56 | [diff] [blame] | 88 | : delay_(base::TimeDelta::FromSeconds(kWriteEventDelayInSeconds)), |
lukasza | 2ff73e64 | 2015-06-11 03:52:43 | [diff] [blame] | 89 | file_task_runner_(file_task_runner), |
[email protected] | 799d53bf | 2013-08-07 06:59:56 | [diff] [blame] | 90 | weak_ptr_factory_(this) { |
[email protected] | 799d53bf | 2013-08-07 06:59:56 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | void FileWriteWatcher::FileWriteWatcherImpl::Destroy() { |
lukasza | 037c10b1 | 2015-06-12 04:21:25 | [diff] [blame] | 94 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | 799d53bf | 2013-08-07 06:59:56 | [diff] [blame] | 95 | |
| 96 | // Just forwarding the call to FILE thread. |
lukasza | 2ff73e64 | 2015-06-11 03:52:43 | [diff] [blame] | 97 | file_task_runner_->PostTask( |
| 98 | FROM_HERE, base::Bind(&FileWriteWatcherImpl::DestroyOnFileThread, |
| 99 | base::Unretained(this))); |
[email protected] | 799d53bf | 2013-08-07 06:59:56 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | void FileWriteWatcher::FileWriteWatcherImpl::StartWatch( |
| 103 | const base::FilePath& path, |
[email protected] | 8df572cc | 2013-08-22 12:51:09 | [diff] [blame] | 104 | const StartWatchCallback& on_start_callback, |
| 105 | const base::Closure& on_write_callback) { |
lukasza | 037c10b1 | 2015-06-12 04:21:25 | [diff] [blame] | 106 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | 799d53bf | 2013-08-07 06:59:56 | [diff] [blame] | 107 | |
| 108 | // Forwarding the call to FILE thread and relaying the |callback|. |
lukasza | 2ff73e64 | 2015-06-11 03:52:43 | [diff] [blame] | 109 | file_task_runner_->PostTask( |
[email protected] | 799d53bf | 2013-08-07 06:59:56 | [diff] [blame] | 110 | FROM_HERE, |
| 111 | base::Bind(&FileWriteWatcherImpl::StartWatchOnFileThread, |
lukasza | 2ff73e64 | 2015-06-11 03:52:43 | [diff] [blame] | 112 | base::Unretained(this), path, |
[email protected] | 8df572cc | 2013-08-22 12:51:09 | [diff] [blame] | 113 | google_apis::CreateRelayCallback(on_start_callback), |
| 114 | google_apis::CreateRelayCallback(on_write_callback))); |
[email protected] | 799d53bf | 2013-08-07 06:59:56 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | FileWriteWatcher::FileWriteWatcherImpl::~FileWriteWatcherImpl() { |
lukasza | 2ff73e64 | 2015-06-11 03:52:43 | [diff] [blame] | 118 | DCHECK(file_task_runner_->BelongsToCurrentThread()); |
[email protected] | 799d53bf | 2013-08-07 06:59:56 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | void FileWriteWatcher::FileWriteWatcherImpl::DestroyOnFileThread() { |
lukasza | 2ff73e64 | 2015-06-11 03:52:43 | [diff] [blame] | 122 | DCHECK(file_task_runner_->BelongsToCurrentThread()); |
[email protected] | 799d53bf | 2013-08-07 06:59:56 | [diff] [blame] | 123 | |
| 124 | delete this; |
| 125 | } |
| 126 | |
| 127 | void FileWriteWatcher::FileWriteWatcherImpl::StartWatchOnFileThread( |
| 128 | const base::FilePath& path, |
[email protected] | 8df572cc | 2013-08-22 12:51:09 | [diff] [blame] | 129 | const StartWatchCallback& on_start_callback, |
| 130 | const base::Closure& on_write_callback) { |
lukasza | 2ff73e64 | 2015-06-11 03:52:43 | [diff] [blame] | 131 | DCHECK(file_task_runner_->BelongsToCurrentThread()); |
[email protected] | 799d53bf | 2013-08-07 06:59:56 | [diff] [blame] | 132 | |
avi | 92a3e96 | 2016-09-21 02:50:21 | [diff] [blame] | 133 | auto it = watchers_.find(path); |
[email protected] | 799d53bf | 2013-08-07 06:59:56 | [diff] [blame] | 134 | if (it != watchers_.end()) { |
[email protected] | 8df572cc | 2013-08-22 12:51:09 | [diff] [blame] | 135 | // We are already watching the path. |
| 136 | on_start_callback.Run(true); |
| 137 | it->second->on_write_callbacks.push_back(on_write_callback); |
[email protected] | 799d53bf | 2013-08-07 06:59:56 | [diff] [blame] | 138 | return; |
| 139 | } |
| 140 | |
| 141 | // Start watching |path|. |
avi | 92a3e96 | 2016-09-21 02:50:21 | [diff] [blame] | 142 | std::unique_ptr<PathWatchInfo> info = |
| 143 | base::MakeUnique<PathWatchInfo>(on_write_callback); |
[email protected] | 799d53bf | 2013-08-07 06:59:56 | [diff] [blame] | 144 | bool ok = info->watcher.Watch( |
| 145 | path, |
| 146 | false, // recursive |
| 147 | base::Bind(&FileWriteWatcherImpl::OnWriteEvent, |
| 148 | weak_ptr_factory_.GetWeakPtr())); |
avi | 92a3e96 | 2016-09-21 02:50:21 | [diff] [blame] | 149 | watchers_[path] = std::move(info); |
[email protected] | 8df572cc | 2013-08-22 12:51:09 | [diff] [blame] | 150 | on_start_callback.Run(ok); |
[email protected] | 799d53bf | 2013-08-07 06:59:56 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | void FileWriteWatcher::FileWriteWatcherImpl::OnWriteEvent( |
| 154 | const base::FilePath& path, |
| 155 | bool error) { |
lukasza | 2ff73e64 | 2015-06-11 03:52:43 | [diff] [blame] | 156 | DCHECK(file_task_runner_->BelongsToCurrentThread()); |
[email protected] | 799d53bf | 2013-08-07 06:59:56 | [diff] [blame] | 157 | |
| 158 | if (error) |
| 159 | return; |
| 160 | |
avi | 92a3e96 | 2016-09-21 02:50:21 | [diff] [blame] | 161 | auto it = watchers_.find(path); |
[email protected] | 799d53bf | 2013-08-07 06:59:56 | [diff] [blame] | 162 | DCHECK(it != watchers_.end()); |
| 163 | |
| 164 | // Heuristics for detecting the end of successive write operations. |
| 165 | // Delay running on_write_event_callback by |delay_| time, and if OnWriteEvent |
| 166 | // is called again in the period, the timer is reset. In other words, we |
| 167 | // invoke callback when |delay_| has passed after the last OnWriteEvent(). |
| 168 | it->second->timer.Start(FROM_HERE, |
| 169 | delay_, |
| 170 | base::Bind(&FileWriteWatcherImpl::InvokeCallback, |
| 171 | weak_ptr_factory_.GetWeakPtr(), |
| 172 | path)); |
| 173 | } |
| 174 | |
| 175 | void FileWriteWatcher::FileWriteWatcherImpl::InvokeCallback( |
| 176 | const base::FilePath& path) { |
lukasza | 2ff73e64 | 2015-06-11 03:52:43 | [diff] [blame] | 177 | DCHECK(file_task_runner_->BelongsToCurrentThread()); |
[email protected] | 799d53bf | 2013-08-07 06:59:56 | [diff] [blame] | 178 | |
avi | 92a3e96 | 2016-09-21 02:50:21 | [diff] [blame] | 179 | auto it = watchers_.find(path); |
[email protected] | 799d53bf | 2013-08-07 06:59:56 | [diff] [blame] | 180 | DCHECK(it != watchers_.end()); |
| 181 | |
[email protected] | 8df572cc | 2013-08-22 12:51:09 | [diff] [blame] | 182 | std::vector<base::Closure> callbacks; |
| 183 | callbacks.swap(it->second->on_write_callbacks); |
[email protected] | 799d53bf | 2013-08-07 06:59:56 | [diff] [blame] | 184 | watchers_.erase(it); |
| 185 | |
avi | 92a3e96 | 2016-09-21 02:50:21 | [diff] [blame] | 186 | for (const auto& callback : callbacks) |
| 187 | callback.Run(); |
[email protected] | 799d53bf | 2013-08-07 06:59:56 | [diff] [blame] | 188 | } |
| 189 | |
lukasza | 2ff73e64 | 2015-06-11 03:52:43 | [diff] [blame] | 190 | FileWriteWatcher::FileWriteWatcher( |
| 191 | base::SingleThreadTaskRunner* file_task_runner) |
| 192 | : watcher_impl_(new FileWriteWatcherImpl(file_task_runner)) { |
[email protected] | 799d53bf | 2013-08-07 06:59:56 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | FileWriteWatcher::~FileWriteWatcher() { |
lukasza | 037c10b1 | 2015-06-12 04:21:25 | [diff] [blame] | 196 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | 799d53bf | 2013-08-07 06:59:56 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | void FileWriteWatcher::StartWatch(const base::FilePath& file_path, |
[email protected] | 8df572cc | 2013-08-22 12:51:09 | [diff] [blame] | 200 | const StartWatchCallback& on_start_callback, |
| 201 | const base::Closure& on_write_callback) { |
lukasza | 037c10b1 | 2015-06-12 04:21:25 | [diff] [blame] | 202 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | 8df572cc | 2013-08-22 12:51:09 | [diff] [blame] | 203 | watcher_impl_->StartWatch(file_path, on_start_callback, on_write_callback); |
[email protected] | 799d53bf | 2013-08-07 06:59:56 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | void FileWriteWatcher::DisableDelayForTesting() { |
| 207 | watcher_impl_->set_delay(base::TimeDelta()); |
| 208 | } |
| 209 | |
[email protected] | 799d53bf | 2013-08-07 06:59:56 | [diff] [blame] | 210 | } // namespace internal |
| 211 | } // namespace drive |