[email protected] | 6d2d92a | 2014-06-11 07:15:24 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef BASE_FILES_FILE_PATH_WATCHER_FSEVENTS_H_ |
| 6 | #define BASE_FILES_FILE_PATH_WATCHER_FSEVENTS_H_ |
| 7 | |
| 8 | #include <CoreServices/CoreServices.h> |
avi | 543540e | 2015-12-24 05:15:32 | [diff] [blame^] | 9 | #include <stddef.h> |
[email protected] | 6d2d92a | 2014-06-11 07:15:24 | [diff] [blame] | 10 | |
| 11 | #include <vector> |
| 12 | |
| 13 | #include "base/files/file_path.h" |
| 14 | #include "base/files/file_path_watcher.h" |
avi | 543540e | 2015-12-24 05:15:32 | [diff] [blame^] | 15 | #include "base/macros.h" |
[email protected] | 6d2d92a | 2014-06-11 07:15:24 | [diff] [blame] | 16 | |
| 17 | namespace base { |
| 18 | |
| 19 | // Mac-specific file watcher implementation based on FSEvents. |
| 20 | // There are trade-offs between the FSEvents implementation and a kqueue |
| 21 | // implementation. The biggest issues are that FSEvents on 10.6 sometimes drops |
| 22 | // events and kqueue does not trigger for modifications to a file in a watched |
| 23 | // directory. See file_path_watcher_mac.cc for the code that decides when to |
| 24 | // use which one. |
| 25 | class FilePathWatcherFSEvents : public FilePathWatcher::PlatformDelegate { |
| 26 | public: |
| 27 | FilePathWatcherFSEvents(); |
| 28 | |
reillyg | 6422ac6 | 2015-04-10 23:07:27 | [diff] [blame] | 29 | // FilePathWatcher::PlatformDelegate overrides. |
| 30 | bool Watch(const FilePath& path, |
| 31 | bool recursive, |
| 32 | const FilePathWatcher::Callback& callback) override; |
| 33 | void Cancel() override; |
| 34 | |
| 35 | private: |
| 36 | static void FSEventsCallback(ConstFSEventStreamRef stream, |
| 37 | void* event_watcher, |
| 38 | size_t num_events, |
| 39 | void* event_paths, |
| 40 | const FSEventStreamEventFlags flags[], |
| 41 | const FSEventStreamEventId event_ids[]); |
| 42 | |
| 43 | ~FilePathWatcherFSEvents() override; |
| 44 | |
| 45 | // Called from FSEventsCallback whenever there is a change to the paths. |
[email protected] | 6d2d92a | 2014-06-11 07:15:24 | [diff] [blame] | 46 | void OnFilePathsChanged(const std::vector<FilePath>& paths); |
| 47 | |
reillyg | 6422ac6 | 2015-04-10 23:07:27 | [diff] [blame] | 48 | // Called on the message_loop() thread to dispatch path events. Can't access |
| 49 | // target_ and resolved_target_ directly as those are modified on the |
| 50 | // libdispatch thread. |
| 51 | void DispatchEvents(const std::vector<FilePath>& paths, |
| 52 | const FilePath& target, |
| 53 | const FilePath& resolved_target); |
| 54 | |
| 55 | // Cleans up and stops the event stream. |
| 56 | void CancelOnMessageLoopThread() override; |
| 57 | |
[email protected] | 6d2d92a | 2014-06-11 07:15:24 | [diff] [blame] | 58 | // (Re-)Initialize the event stream to start reporting events from |
| 59 | // |start_event|. |
| 60 | void UpdateEventStream(FSEventStreamEventId start_event); |
| 61 | |
| 62 | // Returns true if resolving the target path got a different result than |
| 63 | // last time it was done. |
| 64 | bool ResolveTargetPath(); |
| 65 | |
reillyg | 6422ac6 | 2015-04-10 23:07:27 | [diff] [blame] | 66 | // Report an error watching the given target. |
| 67 | void ReportError(const FilePath& target); |
[email protected] | 6d2d92a | 2014-06-11 07:15:24 | [diff] [blame] | 68 | |
| 69 | // Destroy the event stream. |
| 70 | void DestroyEventStream(); |
| 71 | |
| 72 | // Start watching the FSEventStream. |
reillyg | 6422ac6 | 2015-04-10 23:07:27 | [diff] [blame] | 73 | void StartEventStream(FSEventStreamEventId start_event, const FilePath& path); |
[email protected] | 6d2d92a | 2014-06-11 07:15:24 | [diff] [blame] | 74 | |
| 75 | // Callback to notify upon changes. |
reillyg | 6422ac6 | 2015-04-10 23:07:27 | [diff] [blame] | 76 | // (Only accessed from the message_loop() thread.) |
[email protected] | 6d2d92a | 2014-06-11 07:15:24 | [diff] [blame] | 77 | FilePathWatcher::Callback callback_; |
| 78 | |
| 79 | // Target path to watch (passed to callback). |
reillyg | 6422ac6 | 2015-04-10 23:07:27 | [diff] [blame] | 80 | // (Only accessed from the libdispatch thread.) |
[email protected] | 6d2d92a | 2014-06-11 07:15:24 | [diff] [blame] | 81 | FilePath target_; |
| 82 | |
| 83 | // Target path with all symbolic links resolved. |
reillyg | 6422ac6 | 2015-04-10 23:07:27 | [diff] [blame] | 84 | // (Only accessed from the libdispatch thread.) |
[email protected] | 6d2d92a | 2014-06-11 07:15:24 | [diff] [blame] | 85 | FilePath resolved_target_; |
| 86 | |
| 87 | // Backend stream we receive event callbacks from (strong reference). |
reillyg | 6422ac6 | 2015-04-10 23:07:27 | [diff] [blame] | 88 | // (Only accessed from the libdispatch thread.) |
[email protected] | 6d2d92a | 2014-06-11 07:15:24 | [diff] [blame] | 89 | FSEventStreamRef fsevent_stream_; |
| 90 | |
| 91 | DISALLOW_COPY_AND_ASSIGN(FilePathWatcherFSEvents); |
| 92 | }; |
| 93 | |
| 94 | } // namespace base |
| 95 | |
| 96 | #endif // BASE_FILES_FILE_PATH_WATCHER_FSEVENTS_H_ |