OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #ifndef BASE_FILES_FILE_PATH_WATCHER_KQUEUE_H_ | 5 #ifndef BASE_FILES_FILE_PATH_WATCHER_KQUEUE_H_ |
6 #define BASE_FILES_FILE_PATH_WATCHER_KQUEUE_H_ | 6 #define BASE_FILES_FILE_PATH_WATCHER_KQUEUE_H_ |
7 | 7 |
8 #include <sys/event.h> | 8 #include <sys/event.h> |
| 9 |
| 10 #include <memory> |
9 #include <vector> | 11 #include <vector> |
10 | 12 |
| 13 #include "base/files/file_descriptor_watcher_posix.h" |
11 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
12 #include "base/files/file_path_watcher.h" | 15 #include "base/files/file_path_watcher.h" |
13 #include "base/macros.h" | 16 #include "base/macros.h" |
14 #include "base/message_loop/message_loop.h" | 17 #include "base/message_loop/message_loop.h" |
15 #include "base/single_thread_task_runner.h" | 18 #include "base/single_thread_task_runner.h" |
16 | 19 |
17 namespace base { | 20 namespace base { |
18 | 21 |
19 // Mac-specific file watcher implementation based on kqueue. | 22 // Mac-specific file watcher implementation based on kqueue. |
20 // The Linux and Windows versions are able to detect: | 23 // The Linux and Windows versions are able to detect: |
21 // - file creation/deletion/modification in a watched directory | 24 // - file creation/deletion/modification in a watched directory |
22 // - file creation/deletion/modification for a watched file | 25 // - file creation/deletion/modification for a watched file |
23 // - modifications to the paths to a watched object that would affect the | 26 // - modifications to the paths to a watched object that would affect the |
24 // object such as renaming/attibute changes etc. | 27 // object such as renaming/attibute changes etc. |
25 // The kqueue implementation will handle all of the items in the list above | 28 // The kqueue implementation will handle all of the items in the list above |
26 // except for detecting modifications to files in a watched directory. It will | 29 // except for detecting modifications to files in a watched directory. It will |
27 // detect the creation and deletion of files, just not the modification of | 30 // detect the creation and deletion of files, just not the modification of |
28 // files. It does however detect the attribute changes that the FSEvents impl | 31 // files. It does however detect the attribute changes that the FSEvents impl |
29 // would miss. | 32 // would miss. |
30 class FilePathWatcherKQueue : public FilePathWatcher::PlatformDelegate, | 33 class FilePathWatcherKQueue : public FilePathWatcher::PlatformDelegate, |
31 public MessageLoopForIO::Watcher, | |
32 public MessageLoop::DestructionObserver { | 34 public MessageLoop::DestructionObserver { |
33 public: | 35 public: |
34 FilePathWatcherKQueue(); | 36 FilePathWatcherKQueue(); |
35 | 37 |
36 // MessageLoopForIO::Watcher overrides. | |
37 void OnFileCanReadWithoutBlocking(int fd) override; | |
38 void OnFileCanWriteWithoutBlocking(int fd) override; | |
39 | |
40 // MessageLoop::DestructionObserver overrides. | 38 // MessageLoop::DestructionObserver overrides. |
41 void WillDestroyCurrentMessageLoop() override; | 39 void WillDestroyCurrentMessageLoop() override; |
42 | 40 |
43 // FilePathWatcher::PlatformDelegate overrides. | 41 // FilePathWatcher::PlatformDelegate overrides. |
44 bool Watch(const FilePath& path, | 42 bool Watch(const FilePath& path, |
45 bool recursive, | 43 bool recursive, |
46 const FilePathWatcher::Callback& callback) override; | 44 const FilePathWatcher::Callback& callback) override; |
47 void Cancel() override; | 45 void Cancel() override; |
48 | 46 |
49 protected: | 47 protected: |
50 ~FilePathWatcherKQueue() override; | 48 ~FilePathWatcherKQueue() override; |
51 | 49 |
52 private: | 50 private: |
53 class EventData { | 51 class EventData { |
54 public: | 52 public: |
55 EventData(const FilePath& path, const FilePath::StringType& subdir) | 53 EventData(const FilePath& path, const FilePath::StringType& subdir) |
56 : path_(path), subdir_(subdir) { } | 54 : path_(path), subdir_(subdir) { } |
57 FilePath path_; // Full path to this item. | 55 FilePath path_; // Full path to this item. |
58 FilePath::StringType subdir_; // Path to any sub item. | 56 FilePath::StringType subdir_; // Path to any sub item. |
59 }; | 57 }; |
60 | 58 |
61 typedef std::vector<struct kevent> EventVector; | 59 typedef std::vector<struct kevent> EventVector; |
62 | 60 |
| 61 // Called when data is available in |kqueue_|. |
| 62 void OnKQueueReadable(); |
| 63 |
63 // Can only be called on |io_task_runner_|'s thread. | 64 // Can only be called on |io_task_runner_|'s thread. |
64 void CancelOnMessageLoopThread(); | 65 void CancelOnMessageLoopThread(); |
65 | 66 |
66 // Returns true if the kevent values are error free. | 67 // Returns true if the kevent values are error free. |
67 bool AreKeventValuesValid(struct kevent* kevents, int count); | 68 bool AreKeventValuesValid(struct kevent* kevents, int count); |
68 | 69 |
69 // Respond to a change of attributes of the path component represented by | 70 // Respond to a change of attributes of the path component represented by |
70 // |event|. Sets |target_file_affected| to true if |target_| is affected. | 71 // |event|. Sets |target_file_affected| to true if |target_| is affected. |
71 // Sets |update_watches| to true if |events_| need to be updated. | 72 // Sets |update_watches| to true if |events_| need to be updated. |
72 void HandleAttributesChange(const EventVector::iterator& event, | 73 void HandleAttributesChange(const EventVector::iterator& event, |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 // Returns true if kevent has open file descriptor. | 113 // Returns true if kevent has open file descriptor. |
113 static bool IsKeventFileDescriptorOpen(const struct kevent& event) { | 114 static bool IsKeventFileDescriptorOpen(const struct kevent& event) { |
114 return event.ident != kNoFileDescriptor; | 115 return event.ident != kNoFileDescriptor; |
115 } | 116 } |
116 | 117 |
117 static EventData* EventDataForKevent(const struct kevent& event) { | 118 static EventData* EventDataForKevent(const struct kevent& event) { |
118 return reinterpret_cast<EventData*>(event.udata); | 119 return reinterpret_cast<EventData*>(event.udata); |
119 } | 120 } |
120 | 121 |
121 EventVector events_; | 122 EventVector events_; |
122 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; | |
123 MessageLoopForIO::FileDescriptorWatcher kqueue_watcher_; | |
124 FilePathWatcher::Callback callback_; | 123 FilePathWatcher::Callback callback_; |
125 FilePath target_; | 124 FilePath target_; |
126 int kqueue_; | 125 int kqueue_; |
127 | 126 |
| 127 // Throughout the lifetime of this, OnKQueueReadable() will be called when |
| 128 // data is available in |kqueue_|. |
| 129 std::unique_ptr<FileDescriptorWatcher::Controller> kqueue_watch_controller_; |
| 130 |
128 DISALLOW_COPY_AND_ASSIGN(FilePathWatcherKQueue); | 131 DISALLOW_COPY_AND_ASSIGN(FilePathWatcherKQueue); |
129 }; | 132 }; |
130 | 133 |
131 } // namespace base | 134 } // namespace base |
132 | 135 |
133 #endif // BASE_FILES_FILE_PATH_WATCHER_KQUEUE_H_ | 136 #endif // BASE_FILES_FILE_PATH_WATCHER_KQUEUE_H_ |
OLD | NEW |