• 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 #include "base/memory/weak_ptr.h"
18 
19 namespace base {
20 
21 // Mac-specific file watcher implementation based on FSEvents.
22 // There are trade-offs between the FSEvents implementation and a kqueue
23 // implementation. The biggest issues are that FSEvents on 10.6 sometimes drops
24 // events and kqueue does not trigger for modifications to a file in a watched
25 // directory. See file_path_watcher_mac.cc for the code that decides when to
26 // use which one.
27 class FilePathWatcherFSEvents : public FilePathWatcher::PlatformDelegate {
28  public:
29   FilePathWatcherFSEvents();
30   ~FilePathWatcherFSEvents() override;
31 
32   // FilePathWatcher::PlatformDelegate overrides.
33   bool Watch(const FilePath& path,
34              bool recursive,
35              const FilePathWatcher::Callback& callback) override;
36   void Cancel() override;
37 
38  private:
39   static void FSEventsCallback(ConstFSEventStreamRef stream,
40                                void* event_watcher,
41                                size_t num_events,
42                                void* event_paths,
43                                const FSEventStreamEventFlags flags[],
44                                const FSEventStreamEventId event_ids[]);
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   // (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   // The dispatch queue on which the the event stream is scheduled.
78   ScopedDispatchObject<dispatch_queue_t> queue_;
79 
80   // Target path to watch (passed to callback).
81   // (Only accessed from the libdispatch queue.)
82   FilePath target_;
83 
84   // Target path with all symbolic links resolved.
85   // (Only accessed from the libdispatch queue.)
86   FilePath resolved_target_;
87 
88   // Backend stream we receive event callbacks from (strong reference).
89   // (Only accessed from the libdispatch queue.)
90   FSEventStreamRef fsevent_stream_;
91 
92   WeakPtrFactory<FilePathWatcherFSEvents> weak_factory_;
93 
94   DISALLOW_COPY_AND_ASSIGN(FilePathWatcherFSEvents);
95 };
96 
97 }  // namespace base
98 
99 #endif  // BASE_FILES_FILE_PATH_WATCHER_FSEVENTS_H_
100