OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_FILES_FILE_PATH_WATCHER_FSEVENTS_H_ | 5 #ifndef BASE_FILES_FILE_PATH_WATCHER_FSEVENTS_H_ |
6 #define BASE_FILES_FILE_PATH_WATCHER_FSEVENTS_H_ | 6 #define BASE_FILES_FILE_PATH_WATCHER_FSEVENTS_H_ |
7 | 7 |
8 #include <CoreServices/CoreServices.h> | 8 #include <CoreServices/CoreServices.h> |
9 | 9 |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
13 #include "base/files/file_path_watcher.h" | 13 #include "base/files/file_path_watcher.h" |
14 | 14 |
15 namespace base { | 15 namespace base { |
16 | 16 |
17 // Mac-specific file watcher implementation based on FSEvents. | 17 // Mac-specific file watcher implementation based on FSEvents. |
18 // There are trade-offs between the FSEvents implementation and a kqueue | 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 | 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 | 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 | 21 // directory. See file_path_watcher_mac.cc for the code that decides when to |
22 // use which one. | 22 // use which one. |
23 class FilePathWatcherFSEvents : public FilePathWatcher::PlatformDelegate { | 23 class FilePathWatcherFSEvents : public FilePathWatcher::PlatformDelegate { |
24 public: | 24 public: |
25 FilePathWatcherFSEvents(); | 25 FilePathWatcherFSEvents(); |
26 | 26 |
27 // Called from the FSEvents callback whenever there is a change to the paths. | 27 // FilePathWatcher::PlatformDelegate overrides. |
| 28 bool Watch(const FilePath& path, |
| 29 bool recursive, |
| 30 const FilePathWatcher::Callback& callback) override; |
| 31 void Cancel() override; |
| 32 |
| 33 private: |
| 34 static void FSEventsCallback(ConstFSEventStreamRef stream, |
| 35 void* event_watcher, |
| 36 size_t num_events, |
| 37 void* event_paths, |
| 38 const FSEventStreamEventFlags flags[], |
| 39 const FSEventStreamEventId event_ids[]); |
| 40 |
| 41 ~FilePathWatcherFSEvents() override; |
| 42 |
| 43 // Called from FSEventsCallback whenever there is a change to the paths. |
28 void OnFilePathsChanged(const std::vector<FilePath>& paths); | 44 void OnFilePathsChanged(const std::vector<FilePath>& paths); |
29 | 45 |
| 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); |
| 52 |
| 53 // Cleans up and stops the event stream. |
| 54 void CancelOnMessageLoopThread() override; |
| 55 |
30 // (Re-)Initialize the event stream to start reporting events from | 56 // (Re-)Initialize the event stream to start reporting events from |
31 // |start_event|. | 57 // |start_event|. |
32 void UpdateEventStream(FSEventStreamEventId start_event); | 58 void UpdateEventStream(FSEventStreamEventId start_event); |
33 | 59 |
34 // Returns true if resolving the target path got a different result than | 60 // Returns true if resolving the target path got a different result than |
35 // last time it was done. | 61 // last time it was done. |
36 bool ResolveTargetPath(); | 62 bool ResolveTargetPath(); |
37 | 63 |
38 // FilePathWatcher::PlatformDelegate overrides. | 64 // Report an error watching the given target. |
39 bool Watch(const FilePath& path, | 65 void ReportError(const FilePath& target); |
40 bool recursive, | |
41 const FilePathWatcher::Callback& callback) override; | |
42 void Cancel() override; | |
43 | |
44 private: | |
45 ~FilePathWatcherFSEvents() override; | |
46 | 66 |
47 // Destroy the event stream. | 67 // Destroy the event stream. |
48 void DestroyEventStream(); | 68 void DestroyEventStream(); |
49 | 69 |
50 // Start watching the FSEventStream. | 70 // Start watching the FSEventStream. |
51 void StartEventStream(FSEventStreamEventId start_event); | 71 void StartEventStream(FSEventStreamEventId start_event, const FilePath& path); |
52 | |
53 // Cleans up and stops the event stream. | |
54 void CancelOnMessageLoopThread() override; | |
55 | 72 |
56 // Callback to notify upon changes. | 73 // Callback to notify upon changes. |
| 74 // (Only accessed from the message_loop() thread.) |
57 FilePathWatcher::Callback callback_; | 75 FilePathWatcher::Callback callback_; |
58 | 76 |
59 // Target path to watch (passed to callback). | 77 // Target path to watch (passed to callback). |
| 78 // (Only accessed from the libdispatch thread.) |
60 FilePath target_; | 79 FilePath target_; |
61 | 80 |
62 // Target path with all symbolic links resolved. | 81 // Target path with all symbolic links resolved. |
| 82 // (Only accessed from the libdispatch thread.) |
63 FilePath resolved_target_; | 83 FilePath resolved_target_; |
64 | 84 |
65 // Backend stream we receive event callbacks from (strong reference). | 85 // Backend stream we receive event callbacks from (strong reference). |
| 86 // (Only accessed from the libdispatch thread.) |
66 FSEventStreamRef fsevent_stream_; | 87 FSEventStreamRef fsevent_stream_; |
67 | 88 |
68 DISALLOW_COPY_AND_ASSIGN(FilePathWatcherFSEvents); | 89 DISALLOW_COPY_AND_ASSIGN(FilePathWatcherFSEvents); |
69 }; | 90 }; |
70 | 91 |
71 } // namespace base | 92 } // namespace base |
72 | 93 |
73 #endif // BASE_FILES_FILE_PATH_WATCHER_FSEVENTS_H_ | 94 #endif // BASE_FILES_FILE_PATH_WATCHER_FSEVENTS_H_ |
OLD | NEW |