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