blob: 1ebe4636e4ba19bfa58bc2eec84320ecd1720365 [file] [log] [blame]
[email protected]6d2d92a2014-06-11 07:15:241// 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>
avi543540e2015-12-24 05:15:329#include <stddef.h>
[email protected]6d2d92a2014-06-11 07:15:2410
11#include <vector>
12
13#include "base/files/file_path.h"
14#include "base/files/file_path_watcher.h"
avi543540e2015-12-24 05:15:3215#include "base/macros.h"
[email protected]6d2d92a2014-06-11 07:15:2416
17namespace base {
18
19// Mac-specific file watcher implementation based on FSEvents.
20// There are trade-offs between the FSEvents implementation and a kqueue
21// implementation. The biggest issues are that FSEvents on 10.6 sometimes drops
22// events and kqueue does not trigger for modifications to a file in a watched
23// directory. See file_path_watcher_mac.cc for the code that decides when to
24// use which one.
25class FilePathWatcherFSEvents : public FilePathWatcher::PlatformDelegate {
26 public:
27 FilePathWatcherFSEvents();
28
reillyg6422ac62015-04-10 23:07:2729 // FilePathWatcher::PlatformDelegate overrides.
30 bool Watch(const FilePath& path,
31 bool recursive,
32 const FilePathWatcher::Callback& callback) override;
33 void Cancel() override;
34
35 private:
36 static void FSEventsCallback(ConstFSEventStreamRef stream,
37 void* event_watcher,
38 size_t num_events,
39 void* event_paths,
40 const FSEventStreamEventFlags flags[],
41 const FSEventStreamEventId event_ids[]);
42
43 ~FilePathWatcherFSEvents() override;
44
45 // Called from FSEventsCallback whenever there is a change to the paths.
[email protected]6d2d92a2014-06-11 07:15:2446 void OnFilePathsChanged(const std::vector<FilePath>& paths);
47
reillyg6422ac62015-04-10 23:07:2748 // Called on the message_loop() thread to dispatch path events. Can't access
49 // target_ and resolved_target_ directly as those are modified on the
50 // libdispatch thread.
51 void DispatchEvents(const std::vector<FilePath>& paths,
52 const FilePath& target,
53 const FilePath& resolved_target);
54
55 // Cleans up and stops the event stream.
56 void CancelOnMessageLoopThread() override;
57
[email protected]6d2d92a2014-06-11 07:15:2458 // (Re-)Initialize the event stream to start reporting events from
59 // |start_event|.
60 void UpdateEventStream(FSEventStreamEventId start_event);
61
62 // Returns true if resolving the target path got a different result than
63 // last time it was done.
64 bool ResolveTargetPath();
65
reillyg6422ac62015-04-10 23:07:2766 // Report an error watching the given target.
67 void ReportError(const FilePath& target);
[email protected]6d2d92a2014-06-11 07:15:2468
69 // Destroy the event stream.
70 void DestroyEventStream();
71
72 // Start watching the FSEventStream.
reillyg6422ac62015-04-10 23:07:2773 void StartEventStream(FSEventStreamEventId start_event, const FilePath& path);
[email protected]6d2d92a2014-06-11 07:15:2474
75 // Callback to notify upon changes.
reillyg6422ac62015-04-10 23:07:2776 // (Only accessed from the message_loop() thread.)
[email protected]6d2d92a2014-06-11 07:15:2477 FilePathWatcher::Callback callback_;
78
79 // Target path to watch (passed to callback).
reillyg6422ac62015-04-10 23:07:2780 // (Only accessed from the libdispatch thread.)
[email protected]6d2d92a2014-06-11 07:15:2481 FilePath target_;
82
83 // Target path with all symbolic links resolved.
reillyg6422ac62015-04-10 23:07:2784 // (Only accessed from the libdispatch thread.)
[email protected]6d2d92a2014-06-11 07:15:2485 FilePath resolved_target_;
86
87 // Backend stream we receive event callbacks from (strong reference).
reillyg6422ac62015-04-10 23:07:2788 // (Only accessed from the libdispatch thread.)
[email protected]6d2d92a2014-06-11 07:15:2489 FSEventStreamRef fsevent_stream_;
90
91 DISALLOW_COPY_AND_ASSIGN(FilePathWatcherFSEvents);
92};
93
94} // namespace base
95
96#endif // BASE_FILES_FILE_PATH_WATCHER_FSEVENTS_H_