file_path_watcher_fsevents.h revision 0d205d712abd16eeed2f5d5b1052a367d23a223f
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> 9#include <stddef.h> 10 11#include <vector> 12 13#include "base/files/file_path.h" 14#include "base/files/file_path_watcher.h" 15#include "base/macros.h" 16 17namespace 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. 25class FilePathWatcherFSEvents : public FilePathWatcher::PlatformDelegate { 26 public: 27 FilePathWatcherFSEvents(); 28 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. 46 void OnFilePathsChanged(const std::vector<FilePath>& paths); 47 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 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 66 // Report an error watching the given target. 67 void ReportError(const FilePath& target); 68 69 // Destroy the event stream. 70 void DestroyEventStream(); 71 72 // Start watching the FSEventStream. 73 void StartEventStream(FSEventStreamEventId start_event, const FilePath& path); 74 75 // Callback to notify upon changes. 76 // (Only accessed from the message_loop() thread.) 77 FilePathWatcher::Callback callback_; 78 79 // Target path to watch (passed to callback). 80 // (Only accessed from the libdispatch thread.) 81 FilePath target_; 82 83 // Target path with all symbolic links resolved. 84 // (Only accessed from the libdispatch thread.) 85 FilePath resolved_target_; 86 87 // Backend stream we receive event callbacks from (strong reference). 88 // (Only accessed from the libdispatch thread.) 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_ 97