file_path_watcher_fsevents.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. #ifndef BUTIL_FILES_FILE_PATH_WATCHER_FSEVENTS_H_
  5. #define BUTIL_FILES_FILE_PATH_WATCHER_FSEVENTS_H_
  6. #include <CoreServices/CoreServices.h>
  7. #include <vector>
  8. #include "butil/files/file_path.h"
  9. #include "butil/files/file_path_watcher.h"
  10. namespace butil {
  11. // Mac-specific file watcher implementation based on FSEvents.
  12. // There are trade-offs between the FSEvents implementation and a kqueue
  13. // implementation. The biggest issues are that FSEvents on 10.6 sometimes drops
  14. // events and kqueue does not trigger for modifications to a file in a watched
  15. // directory. See file_path_watcher_mac.cc for the code that decides when to
  16. // use which one.
  17. class FilePathWatcherFSEvents : public FilePathWatcher::PlatformDelegate {
  18. public:
  19. FilePathWatcherFSEvents();
  20. // Called from the FSEvents callback whenever there is a change to the paths.
  21. void OnFilePathsChanged(const std::vector<FilePath>& paths);
  22. // (Re-)Initialize the event stream to start reporting events from
  23. // |start_event|.
  24. void UpdateEventStream(FSEventStreamEventId start_event);
  25. // Returns true if resolving the target path got a different result than
  26. // last time it was done.
  27. bool ResolveTargetPath();
  28. // FilePathWatcher::PlatformDelegate overrides.
  29. virtual bool Watch(const FilePath& path,
  30. bool recursive,
  31. const FilePathWatcher::Callback& callback) OVERRIDE;
  32. virtual void Cancel() OVERRIDE;
  33. private:
  34. virtual ~FilePathWatcherFSEvents();
  35. // Destroy the event stream.
  36. void DestroyEventStream();
  37. // Start watching the FSEventStream.
  38. void StartEventStream(FSEventStreamEventId start_event);
  39. // Cleans up and stops the event stream.
  40. virtual void CancelOnMessageLoopThread() OVERRIDE;
  41. // Callback to notify upon changes.
  42. FilePathWatcher::Callback callback_;
  43. // Target path to watch (passed to callback).
  44. FilePath target_;
  45. // Target path with all symbolic links resolved.
  46. FilePath resolved_target_;
  47. // Backend stream we receive event callbacks from (strong reference).
  48. FSEventStreamRef fsevent_stream_;
  49. DISALLOW_COPY_AND_ASSIGN(FilePathWatcherFSEvents);
  50. };
  51. } // namespace butil
  52. #endif // BUTIL_FILES_FILE_PATH_WATCHER_FSEVENTS_H_