OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/file_path_watcher/file_path_watcher.h" | 5 #include "chrome/browser/file_path_watcher/file_path_watcher.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <string.h> | 8 #include <string.h> |
9 #include <sys/inotify.h> | 9 #include <sys/inotify.h> |
10 #include <sys/ioctl.h> | 10 #include <sys/ioctl.h> |
11 #include <sys/select.h> | 11 #include <sys/select.h> |
12 #include <unistd.h> | 12 #include <unistd.h> |
13 | 13 |
14 #include <algorithm> | 14 #include <algorithm> |
15 #include <set> | 15 #include <set> |
16 #include <utility> | 16 #include <utility> |
17 #include <vector> | 17 #include <vector> |
18 | 18 |
19 #include "base/eintr_wrapper.h" | 19 #include "base/eintr_wrapper.h" |
20 #include "base/file_path.h" | 20 #include "base/file_path.h" |
21 #include "base/file_util.h" | 21 #include "base/file_util.h" |
22 #include "base/hash_tables.h" | 22 #include "base/hash_tables.h" |
23 #include "base/lazy_instance.h" | 23 #include "base/lazy_instance.h" |
24 #include "base/lock.h" | |
25 #include "base/logging.h" | 24 #include "base/logging.h" |
26 #include "base/message_loop.h" | 25 #include "base/message_loop.h" |
27 #include "base/scoped_ptr.h" | 26 #include "base/scoped_ptr.h" |
| 27 #include "base/synchronization/lock.h" |
28 #include "base/task.h" | 28 #include "base/task.h" |
29 #include "base/threading/thread.h" | 29 #include "base/threading/thread.h" |
30 | 30 |
31 namespace { | 31 namespace { |
32 | 32 |
33 class FilePathWatcherImpl; | 33 class FilePathWatcherImpl; |
34 | 34 |
35 // Singleton to manage all inotify watches. | 35 // Singleton to manage all inotify watches. |
36 // TODO(tony): It would be nice if this wasn't a singleton. | 36 // TODO(tony): It would be nice if this wasn't a singleton. |
37 // http://crbug.com/38174 | 37 // http://crbug.com/38174 |
(...skipping 17 matching lines...) Expand all Loading... |
55 | 55 |
56 typedef std::set<FilePathWatcherImpl*> WatcherSet; | 56 typedef std::set<FilePathWatcherImpl*> WatcherSet; |
57 | 57 |
58 InotifyReader(); | 58 InotifyReader(); |
59 ~InotifyReader(); | 59 ~InotifyReader(); |
60 | 60 |
61 // We keep track of which delegates want to be notified on which watches. | 61 // We keep track of which delegates want to be notified on which watches. |
62 base::hash_map<Watch, WatcherSet> watchers_; | 62 base::hash_map<Watch, WatcherSet> watchers_; |
63 | 63 |
64 // Lock to protect watchers_. | 64 // Lock to protect watchers_. |
65 Lock lock_; | 65 base::Lock lock_; |
66 | 66 |
67 // Separate thread on which we run blocking read for inotify events. | 67 // Separate thread on which we run blocking read for inotify events. |
68 base::Thread thread_; | 68 base::Thread thread_; |
69 | 69 |
70 // File descriptor returned by inotify_init. | 70 // File descriptor returned by inotify_init. |
71 const int inotify_fd_; | 71 const int inotify_fd_; |
72 | 72 |
73 // Use self-pipe trick to unblock select during shutdown. | 73 // Use self-pipe trick to unblock select during shutdown. |
74 int shutdown_pipe_[2]; | 74 int shutdown_pipe_[2]; |
75 | 75 |
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
230 close(shutdown_pipe_[0]); | 230 close(shutdown_pipe_[0]); |
231 if (shutdown_pipe_[1] >= 0) | 231 if (shutdown_pipe_[1] >= 0) |
232 close(shutdown_pipe_[1]); | 232 close(shutdown_pipe_[1]); |
233 } | 233 } |
234 | 234 |
235 InotifyReader::Watch InotifyReader::AddWatch( | 235 InotifyReader::Watch InotifyReader::AddWatch( |
236 const FilePath& path, FilePathWatcherImpl* watcher) { | 236 const FilePath& path, FilePathWatcherImpl* watcher) { |
237 if (!valid_) | 237 if (!valid_) |
238 return kInvalidWatch; | 238 return kInvalidWatch; |
239 | 239 |
240 AutoLock auto_lock(lock_); | 240 base::AutoLock auto_lock(lock_); |
241 | 241 |
242 Watch watch = inotify_add_watch(inotify_fd_, path.value().c_str(), | 242 Watch watch = inotify_add_watch(inotify_fd_, path.value().c_str(), |
243 IN_CREATE | IN_DELETE | | 243 IN_CREATE | IN_DELETE | |
244 IN_CLOSE_WRITE | IN_MOVE | | 244 IN_CLOSE_WRITE | IN_MOVE | |
245 IN_ONLYDIR); | 245 IN_ONLYDIR); |
246 | 246 |
247 if (watch == kInvalidWatch) | 247 if (watch == kInvalidWatch) |
248 return kInvalidWatch; | 248 return kInvalidWatch; |
249 | 249 |
250 watchers_[watch].insert(watcher); | 250 watchers_[watch].insert(watcher); |
251 | 251 |
252 return watch; | 252 return watch; |
253 } | 253 } |
254 | 254 |
255 bool InotifyReader::RemoveWatch(Watch watch, | 255 bool InotifyReader::RemoveWatch(Watch watch, |
256 FilePathWatcherImpl* watcher) { | 256 FilePathWatcherImpl* watcher) { |
257 if (!valid_) | 257 if (!valid_) |
258 return false; | 258 return false; |
259 | 259 |
260 AutoLock auto_lock(lock_); | 260 base::AutoLock auto_lock(lock_); |
261 | 261 |
262 watchers_[watch].erase(watcher); | 262 watchers_[watch].erase(watcher); |
263 | 263 |
264 if (watchers_[watch].empty()) { | 264 if (watchers_[watch].empty()) { |
265 watchers_.erase(watch); | 265 watchers_.erase(watch); |
266 return (inotify_rm_watch(inotify_fd_, watch) == 0); | 266 return (inotify_rm_watch(inotify_fd_, watch) == 0); |
267 } | 267 } |
268 | 268 |
269 return true; | 269 return true; |
270 } | 270 } |
271 | 271 |
272 void InotifyReader::OnInotifyEvent(const inotify_event* event) { | 272 void InotifyReader::OnInotifyEvent(const inotify_event* event) { |
273 if (event->mask & IN_IGNORED) | 273 if (event->mask & IN_IGNORED) |
274 return; | 274 return; |
275 | 275 |
276 FilePath::StringType child(event->len ? event->name : FILE_PATH_LITERAL("")); | 276 FilePath::StringType child(event->len ? event->name : FILE_PATH_LITERAL("")); |
277 AutoLock auto_lock(lock_); | 277 base::AutoLock auto_lock(lock_); |
278 | 278 |
279 for (WatcherSet::iterator watcher = watchers_[event->wd].begin(); | 279 for (WatcherSet::iterator watcher = watchers_[event->wd].begin(); |
280 watcher != watchers_[event->wd].end(); | 280 watcher != watchers_[event->wd].end(); |
281 ++watcher) { | 281 ++watcher) { |
282 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 282 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
283 NewRunnableMethod(*watcher, | 283 NewRunnableMethod(*watcher, |
284 &FilePathWatcherImpl::OnFilePathChanged, | 284 &FilePathWatcherImpl::OnFilePathChanged, |
285 event->wd, | 285 event->wd, |
286 child, | 286 child, |
287 event->mask & (IN_CREATE | IN_MOVED_TO), | 287 event->mask & (IN_CREATE | IN_MOVED_TO), |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
403 } | 403 } |
404 | 404 |
405 return true; | 405 return true; |
406 } | 406 } |
407 | 407 |
408 } // namespace | 408 } // namespace |
409 | 409 |
410 FilePathWatcher::FilePathWatcher() { | 410 FilePathWatcher::FilePathWatcher() { |
411 impl_ = new FilePathWatcherImpl(); | 411 impl_ = new FilePathWatcherImpl(); |
412 } | 412 } |
OLD | NEW |