| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "content/common/file_path_watcher/file_path_watcher.h" | 5 #include "base/files/file_path_watcher.h" |
| 6 | 6 |
| 7 #include <fcntl.h> | 7 #include <fcntl.h> |
| 8 #include <sys/event.h> | 8 #include <sys/event.h> |
| 9 #include <sys/param.h> | 9 #include <sys/param.h> |
| 10 | 10 |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/file_util.h" | 13 #include "base/file_util.h" |
| 14 #include "base/message_loop.h" | 14 #include "base/message_loop.h" |
| 15 #include "base/message_loop_proxy.h" | 15 #include "base/message_loop_proxy.h" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 30 // send notifications. See | 30 // send notifications. See |
| 31 // http://code.google.com/p/chromium/issues/detail?id=54822#c31 for source that | 31 // http://code.google.com/p/chromium/issues/detail?id=54822#c31 for source that |
| 32 // will reproduce the problem. FSEvents also required having a CFRunLoop | 32 // will reproduce the problem. FSEvents also required having a CFRunLoop |
| 33 // backing the thread that it was running on, that caused added complexity | 33 // backing the thread that it was running on, that caused added complexity |
| 34 // in the interfaces. | 34 // in the interfaces. |
| 35 // The kqueue implementation will handle all of the items in the list above | 35 // The kqueue implementation will handle all of the items in the list above |
| 36 // except for detecting modifications to files in a watched directory. It will | 36 // except for detecting modifications to files in a watched directory. It will |
| 37 // detect the creation and deletion of files, just not the modification of | 37 // detect the creation and deletion of files, just not the modification of |
| 38 // files. It does however detect the attribute changes that the FSEvents impl | 38 // files. It does however detect the attribute changes that the FSEvents impl |
| 39 // would miss. | 39 // would miss. |
| 40 class FilePathWatcherImpl : public FilePathWatcher::PlatformDelegate, | 40 class FilePathWatcherImpl |
| 41 public MessageLoopForIO::Watcher, | 41 : public base::files::FilePathWatcher::PlatformDelegate, |
| 42 public MessageLoop::DestructionObserver { | 42 public MessageLoopForIO::Watcher, |
| 43 public MessageLoop::DestructionObserver { |
| 43 public: | 44 public: |
| 44 FilePathWatcherImpl() : kqueue_(-1) {} | 45 FilePathWatcherImpl() : kqueue_(-1) {} |
| 45 virtual ~FilePathWatcherImpl() {} | 46 virtual ~FilePathWatcherImpl() {} |
| 46 | 47 |
| 47 // MessageLoopForIO::Watcher overrides. | 48 // MessageLoopForIO::Watcher overrides. |
| 48 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE; | 49 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE; |
| 49 virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE; | 50 virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE; |
| 50 | 51 |
| 51 // MessageLoop::DestructionObserver overrides. | 52 // MessageLoop::DestructionObserver overrides. |
| 52 virtual void WillDestroyCurrentMessageLoop() OVERRIDE; | 53 virtual void WillDestroyCurrentMessageLoop() OVERRIDE; |
| 53 | 54 |
| 54 // FilePathWatcher::PlatformDelegate overrides. | 55 // FilePathWatcher::PlatformDelegate overrides. |
| 55 virtual bool Watch(const FilePath& path, | 56 virtual bool Watch(const FilePath& path, |
| 56 FilePathWatcher::Delegate* delegate) OVERRIDE; | 57 base::files::FilePathWatcher::Delegate* delegate) OVERRIDE; |
| 57 virtual void Cancel() OVERRIDE; | 58 virtual void Cancel() OVERRIDE; |
| 58 | 59 |
| 59 private: | 60 private: |
| 60 class EventData { | 61 class EventData { |
| 61 public: | 62 public: |
| 62 EventData(const FilePath& path, const FilePath::StringType& subdir) | 63 EventData(const FilePath& path, const FilePath::StringType& subdir) |
| 63 : path_(path), subdir_(subdir) { } | 64 : path_(path), subdir_(subdir) { } |
| 64 FilePath path_; // Full path to this item. | 65 FilePath path_; // Full path to this item. |
| 65 FilePath::StringType subdir_; // Path to any sub item. | 66 FilePath::StringType subdir_; // Path to any sub item. |
| 66 }; | 67 }; |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 118 return event.ident != static_cast<uintptr_t>(-1); | 119 return event.ident != static_cast<uintptr_t>(-1); |
| 119 } | 120 } |
| 120 | 121 |
| 121 static EventData* EventDataForKevent(const struct kevent& event) { | 122 static EventData* EventDataForKevent(const struct kevent& event) { |
| 122 return reinterpret_cast<EventData*>(event.udata); | 123 return reinterpret_cast<EventData*>(event.udata); |
| 123 } | 124 } |
| 124 | 125 |
| 125 EventVector events_; | 126 EventVector events_; |
| 126 scoped_refptr<base::MessageLoopProxy> io_message_loop_; | 127 scoped_refptr<base::MessageLoopProxy> io_message_loop_; |
| 127 MessageLoopForIO::FileDescriptorWatcher kqueue_watcher_; | 128 MessageLoopForIO::FileDescriptorWatcher kqueue_watcher_; |
| 128 scoped_refptr<FilePathWatcher::Delegate> delegate_; | 129 scoped_refptr<base::files::FilePathWatcher::Delegate> delegate_; |
| 129 FilePath target_; | 130 FilePath target_; |
| 130 int kqueue_; | 131 int kqueue_; |
| 131 | 132 |
| 132 DISALLOW_COPY_AND_ASSIGN(FilePathWatcherImpl); | 133 DISALLOW_COPY_AND_ASSIGN(FilePathWatcherImpl); |
| 133 }; | 134 }; |
| 134 | 135 |
| 135 void FilePathWatcherImpl::ReleaseEvent(struct kevent& event) { | 136 void FilePathWatcherImpl::ReleaseEvent(struct kevent& event) { |
| 136 CloseFileDescriptor(reinterpret_cast<int*>(&event.ident)); | 137 CloseFileDescriptor(reinterpret_cast<int*>(&event.ident)); |
| 137 EventData* entry = EventDataForKevent(event); | 138 EventData* entry = EventDataForKevent(event); |
| 138 delete entry; | 139 delete entry; |
| (...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 406 } | 407 } |
| 407 | 408 |
| 408 void FilePathWatcherImpl::OnFileCanWriteWithoutBlocking(int fd) { | 409 void FilePathWatcherImpl::OnFileCanWriteWithoutBlocking(int fd) { |
| 409 NOTREACHED(); | 410 NOTREACHED(); |
| 410 } | 411 } |
| 411 | 412 |
| 412 void FilePathWatcherImpl::WillDestroyCurrentMessageLoop() { | 413 void FilePathWatcherImpl::WillDestroyCurrentMessageLoop() { |
| 413 CancelOnMessageLoopThread(); | 414 CancelOnMessageLoopThread(); |
| 414 } | 415 } |
| 415 | 416 |
| 416 bool FilePathWatcherImpl::Watch(const FilePath& path, | 417 bool FilePathWatcherImpl::Watch( |
| 417 FilePathWatcher::Delegate* delegate) { | 418 const FilePath& path, |
| 419 base::files::FilePathWatcher::Delegate* delegate) { |
| 418 DCHECK(MessageLoopForIO::current()); | 420 DCHECK(MessageLoopForIO::current()); |
| 419 DCHECK(target_.value().empty()); // Can only watch one path. | 421 DCHECK(target_.value().empty()); // Can only watch one path. |
| 420 DCHECK(delegate); | 422 DCHECK(delegate); |
| 421 DCHECK_EQ(kqueue_, -1); | 423 DCHECK_EQ(kqueue_, -1); |
| 422 | 424 |
| 423 delegate_ = delegate; | 425 delegate_ = delegate; |
| 424 target_ = path; | 426 target_ = path; |
| 425 | 427 |
| 426 MessageLoop::current()->AddDestructionObserver(this); | 428 MessageLoop::current()->AddDestructionObserver(this); |
| 427 io_message_loop_ = base::MessageLoopProxy::CreateForCurrentThread(); | 429 io_message_loop_ = base::MessageLoopProxy::CreateForCurrentThread(); |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 475 std::for_each(events_.begin(), events_.end(), ReleaseEvent); | 477 std::for_each(events_.begin(), events_.end(), ReleaseEvent); |
| 476 events_.clear(); | 478 events_.clear(); |
| 477 io_message_loop_ = NULL; | 479 io_message_loop_ = NULL; |
| 478 MessageLoop::current()->RemoveDestructionObserver(this); | 480 MessageLoop::current()->RemoveDestructionObserver(this); |
| 479 delegate_ = NULL; | 481 delegate_ = NULL; |
| 480 } | 482 } |
| 481 } | 483 } |
| 482 | 484 |
| 483 } // namespace | 485 } // namespace |
| 484 | 486 |
| 485 FilePathWatcher::FilePathWatcher() { | 487 base::files::FilePathWatcher::FilePathWatcher() { |
| 486 impl_ = new FilePathWatcherImpl(); | 488 impl_ = new FilePathWatcherImpl(); |
| 487 } | 489 } |
| OLD | NEW |