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