blob: 2bd979c80ff3788abccab5b2d024b3fd1c36d8a0 [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"
rsesek74c8f3d2016-07-07 18:43:1415#include "base/mac/scoped_dispatch_object.h"
avi543540e2015-12-24 05:15:3216#include "base/macros.h"
fdoray0ac2f0f82017-01-09 23:38:4217#include "base/memory/weak_ptr.h"
[email protected]6d2d92a2014-06-11 07:15:2418
19namespace 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.
27class FilePathWatcherFSEvents : public FilePathWatcher::PlatformDelegate {
28 public:
29 FilePathWatcherFSEvents();
fdoray0ac2f0f82017-01-09 23:38:4230 ~FilePathWatcherFSEvents() override;
[email protected]6d2d92a2014-06-11 07:15:2431
reillyg6422ac62015-04-10 23:07:2732 // 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
reillyg6422ac62015-04-10 23:07:2746 // Called from FSEventsCallback whenever there is a change to the paths.
[email protected]6d2d92a2014-06-11 07:15:2447 void OnFilePathsChanged(const std::vector<FilePath>& paths);
48
Alexander Timin0439ffe2018-10-30 18:10:0049 // Called on the task_runner() thread to dispatch path events. Can't access
reillyg6422ac62015-04-10 23:07:2750 // 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
[email protected]6d2d92a2014-06-11 07:15:2456 // (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
reillyg6422ac62015-04-10 23:07:2764 // Report an error watching the given target.
65 void ReportError(const FilePath& target);
[email protected]6d2d92a2014-06-11 07:15:2466
67 // Destroy the event stream.
68 void DestroyEventStream();
69
70 // Start watching the FSEventStream.
reillyg6422ac62015-04-10 23:07:2771 void StartEventStream(FSEventStreamEventId start_event, const FilePath& path);
[email protected]6d2d92a2014-06-11 07:15:2472
73 // Callback to notify upon changes.
Alexander Timin0439ffe2018-10-30 18:10:0074 // (Only accessed from the task_runner() thread.)
[email protected]6d2d92a2014-06-11 07:15:2475 FilePathWatcher::Callback callback_;
76
rsesek74c8f3d2016-07-07 18:43:1477 // The dispatch queue on which the the event stream is scheduled.
78 ScopedDispatchObject<dispatch_queue_t> queue_;
79
[email protected]6d2d92a2014-06-11 07:15:2480 // Target path to watch (passed to callback).
rsesek74c8f3d2016-07-07 18:43:1481 // (Only accessed from the libdispatch queue.)
[email protected]6d2d92a2014-06-11 07:15:2482 FilePath target_;
83
84 // Target path with all symbolic links resolved.
rsesek74c8f3d2016-07-07 18:43:1485 // (Only accessed from the libdispatch queue.)
[email protected]6d2d92a2014-06-11 07:15:2486 FilePath resolved_target_;
87
88 // Backend stream we receive event callbacks from (strong reference).
rsesek74c8f3d2016-07-07 18:43:1489 // (Only accessed from the libdispatch queue.)
[email protected]6d2d92a2014-06-11 07:15:2490 FSEventStreamRef fsevent_stream_;
91
fdoray0ac2f0f82017-01-09 23:38:4292 WeakPtrFactory<FilePathWatcherFSEvents> weak_factory_;
93
[email protected]6d2d92a2014-06-11 07:15:2494 DISALLOW_COPY_AND_ASSIGN(FilePathWatcherFSEvents);
95};
96
97} // namespace base
98
99#endif // BASE_FILES_FILE_PATH_WATCHER_FSEVENTS_H_