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. |
Mattias Nissler (ping if slow)
2015/04/10 09:15:51
3rd attempt: Please document in the comments which
Reilly Grant (use Gerrit)
2015/04/10 18:32:47
My mistake. I put in comments but must not have sa
| |
57 FilePathWatcher::Callback callback_; | 74 FilePathWatcher::Callback callback_; |
58 | 75 |
59 // Target path to watch (passed to callback). | 76 // Target path to watch (passed to callback). |
60 FilePath target_; | 77 FilePath target_; |
61 | 78 |
62 // Target path with all symbolic links resolved. | 79 // Target path with all symbolic links resolved. |
63 FilePath resolved_target_; | 80 FilePath resolved_target_; |
64 | 81 |
65 // Backend stream we receive event callbacks from (strong reference). | 82 // Backend stream we receive event callbacks from (strong reference). |
66 FSEventStreamRef fsevent_stream_; | 83 FSEventStreamRef fsevent_stream_; |
67 | 84 |
68 DISALLOW_COPY_AND_ASSIGN(FilePathWatcherFSEvents); | 85 DISALLOW_COPY_AND_ASSIGN(FilePathWatcherFSEvents); |
69 }; | 86 }; |
70 | 87 |
71 } // namespace base | 88 } // namespace base |
72 | 89 |
73 #endif // BASE_FILES_FILE_PATH_WATCHER_FSEVENTS_H_ | 90 #endif // BASE_FILES_FILE_PATH_WATCHER_FSEVENTS_H_ |
OLD | NEW |