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