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  // Called from the FSEvents callback whenever there is a change to the paths.
28  void OnFilePathsChanged(const std::vector<FilePath>& paths);
29
30  // (Re-)Initialize the event stream to start reporting events from
31  // |start_event|.
32  void UpdateEventStream(FSEventStreamEventId start_event);
33
34  // Returns true if resolving the target path got a different result than
35  // last time it was done.
36  bool ResolveTargetPath();
37
38  // FilePathWatcher::PlatformDelegate overrides.
39  virtual bool Watch(const FilePath& path,
40                     bool recursive,
41                     const FilePathWatcher::Callback& callback) OVERRIDE;
42  virtual void Cancel() OVERRIDE;
43
44 private:
45  virtual ~FilePathWatcherFSEvents();
46
47  // Destroy the event stream.
48  void DestroyEventStream();
49
50  // Start watching the FSEventStream.
51  void StartEventStream(FSEventStreamEventId start_event);
52
53  // Cleans up and stops the event stream.
54  virtual void CancelOnMessageLoopThread() OVERRIDE;
55
56  // Callback to notify upon changes.
57  FilePathWatcher::Callback callback_;
58
59  // Target path to watch (passed to callback).
60  FilePath target_;
61
62  // Target path with all symbolic links resolved.
63  FilePath resolved_target_;
64
65  // Backend stream we receive event callbacks from (strong reference).
66  FSEventStreamRef fsevent_stream_;
67
68  DISALLOW_COPY_AND_ASSIGN(FilePathWatcherFSEvents);
69};
70
71}  // namespace base
72
73#endif  // BASE_FILES_FILE_PATH_WATCHER_FSEVENTS_H_
74