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