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.
5 #ifndef BASE_FILES_FILE_PATH_WATCHER_FSEVENTS_H_
6 #define BASE_FILES_FILE_PATH_WATCHER_FSEVENTS_H_
8 #include <CoreServices/CoreServices.h>
12 #include "base/files/file_path.h"
13 #include "base/files/file_path_watcher.h"
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
23 class FilePathWatcherFSEvents
: public FilePathWatcher::PlatformDelegate
{
25 FilePathWatcherFSEvents();
27 // FilePathWatcher::PlatformDelegate overrides.
28 bool Watch(const FilePath
& path
,
30 const FilePathWatcher::Callback
& callback
) override
;
31 void Cancel() override
;
34 static void FSEventsCallback(ConstFSEventStreamRef stream
,
38 const FSEventStreamEventFlags flags
[],
39 const FSEventStreamEventId event_ids
[]);
41 ~FilePathWatcherFSEvents() override
;
43 // Called from FSEventsCallback whenever there is a change to the paths.
44 void OnFilePathsChanged(const std::vector
<FilePath
>& paths
);
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
);
53 // Cleans up and stops the event stream.
54 void CancelOnMessageLoopThread() override
;
56 // (Re-)Initialize the event stream to start reporting events from
58 void UpdateEventStream(FSEventStreamEventId start_event
);
60 // Returns true if resolving the target path got a different result than
61 // last time it was done.
62 bool ResolveTargetPath();
64 // Report an error watching the given target.
65 void ReportError(const FilePath
& target
);
67 // Destroy the event stream.
68 void DestroyEventStream();
70 // Start watching the FSEventStream.
71 void StartEventStream(FSEventStreamEventId start_event
, const FilePath
& path
);
73 // Callback to notify upon changes.
74 // (Only accessed from the message_loop() thread.)
75 FilePathWatcher::Callback callback_
;
77 // Target path to watch (passed to callback).
78 // (Only accessed from the libdispatch thread.)
81 // Target path with all symbolic links resolved.
82 // (Only accessed from the libdispatch thread.)
83 FilePath resolved_target_
;
85 // Backend stream we receive event callbacks from (strong reference).
86 // (Only accessed from the libdispatch thread.)
87 FSEventStreamRef fsevent_stream_
;
89 DISALLOW_COPY_AND_ASSIGN(FilePathWatcherFSEvents
);
94 #endif // BASE_FILES_FILE_PATH_WATCHER_FSEVENTS_H_