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