• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/mac/scoped_dispatch_object.h"
16 #include "base/macros.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 
30   // FilePathWatcher::PlatformDelegate overrides.
31   bool Watch(const FilePath& path,
32              bool recursive,
33              const FilePathWatcher::Callback& callback) override;
34   void Cancel() override;
35 
36  private:
37   static void FSEventsCallback(ConstFSEventStreamRef stream,
38                                void* event_watcher,
39                                size_t num_events,
40                                void* event_paths,
41                                const FSEventStreamEventFlags flags[],
42                                const FSEventStreamEventId event_ids[]);
43 
44   ~FilePathWatcherFSEvents() override;
45 
46   // Called from FSEventsCallback whenever there is a change to the paths.
47   void OnFilePathsChanged(const std::vector<FilePath>& paths);
48 
49   // Called on the message_loop() thread to dispatch path events. Can't access
50   // target_ and resolved_target_ directly as those are modified on the
51   // libdispatch thread.
52   void DispatchEvents(const std::vector<FilePath>& paths,
53                       const FilePath& target,
54                       const FilePath& resolved_target);
55 
56   // Cleans up and stops the event stream.
57   void CancelOnMessageLoopThread() override;
58 
59   // (Re-)Initialize the event stream to start reporting events from
60   // |start_event|.
61   void UpdateEventStream(FSEventStreamEventId start_event);
62 
63   // Returns true if resolving the target path got a different result than
64   // last time it was done.
65   bool ResolveTargetPath();
66 
67   // Report an error watching the given target.
68   void ReportError(const FilePath& target);
69 
70   // Destroy the event stream.
71   void DestroyEventStream();
72 
73   // Start watching the FSEventStream.
74   void StartEventStream(FSEventStreamEventId start_event, const FilePath& path);
75 
76   // Callback to notify upon changes.
77   // (Only accessed from the message_loop() thread.)
78   FilePathWatcher::Callback callback_;
79 
80   // The dispatch queue on which the the event stream is scheduled.
81   ScopedDispatchObject<dispatch_queue_t> queue_;
82 
83   // Target path to watch (passed to callback).
84   // (Only accessed from the libdispatch queue.)
85   FilePath target_;
86 
87   // Target path with all symbolic links resolved.
88   // (Only accessed from the libdispatch queue.)
89   FilePath resolved_target_;
90 
91   // Backend stream we receive event callbacks from (strong reference).
92   // (Only accessed from the libdispatch queue.)
93   FSEventStreamRef fsevent_stream_;
94 
95   DISALLOW_COPY_AND_ASSIGN(FilePathWatcherFSEvents);
96 };
97 
98 }  // namespace base
99 
100 #endif  // BASE_FILES_FILE_PATH_WATCHER_FSEVENTS_H_
101