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 #include "base/files/file_path_watcher_fsevents.h" | 5 #include "base/files/file_path_watcher_fsevents.h" |
6 | 6 |
7 #include <list> | 7 #include <list> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 } else { | 68 } else { |
69 result = current; | 69 result = current; |
70 } | 70 } |
71 } | 71 } |
72 | 72 |
73 if (resolve_count >= kMaxLinksToResolve) | 73 if (resolve_count >= kMaxLinksToResolve) |
74 result.clear(); | 74 result.clear(); |
75 return result; | 75 return result; |
76 } | 76 } |
77 | 77 |
78 // The callback passed to FSEventStreamCreate(). | 78 } // namespace |
79 void FSEventsCallback(ConstFSEventStreamRef stream, | 79 |
80 void* event_watcher, size_t num_events, | 80 FilePathWatcherFSEvents::FilePathWatcherFSEvents() : fsevent_stream_(NULL) { |
81 void* event_paths, const FSEventStreamEventFlags flags[], | 81 } |
82 const FSEventStreamEventId event_ids[]) { | 82 |
| 83 bool FilePathWatcherFSEvents::Watch(const FilePath& path, |
| 84 bool recursive, |
| 85 const FilePathWatcher::Callback& callback) { |
| 86 DCHECK(MessageLoopForIO::current()); |
| 87 DCHECK(!callback.is_null()); |
| 88 DCHECK(callback_.is_null()); |
| 89 |
| 90 // This class could support non-recursive watches, but that is currently |
| 91 // left to FilePathWatcherKQueue. |
| 92 if (!recursive) |
| 93 return false; |
| 94 |
| 95 set_message_loop(MessageLoopProxy::current()); |
| 96 callback_ = callback; |
| 97 |
| 98 FSEventStreamEventId start_event = FSEventsGetCurrentEventId(); |
| 99 g_task_runner.Get().PostTask( |
| 100 FROM_HERE, Bind(&FilePathWatcherFSEvents::StartEventStream, this, |
| 101 start_event, path)); |
| 102 return true; |
| 103 } |
| 104 |
| 105 void FilePathWatcherFSEvents::Cancel() { |
| 106 set_cancelled(); |
| 107 callback_.Reset(); |
| 108 |
| 109 // Switch to the dispatch queue thread to tear down the event stream. |
| 110 g_task_runner.Get().PostTask( |
| 111 FROM_HERE, |
| 112 Bind(&FilePathWatcherFSEvents::CancelOnMessageLoopThread, this)); |
| 113 } |
| 114 |
| 115 // static |
| 116 void FilePathWatcherFSEvents::FSEventsCallback( |
| 117 ConstFSEventStreamRef stream, |
| 118 void* event_watcher, |
| 119 size_t num_events, |
| 120 void* event_paths, |
| 121 const FSEventStreamEventFlags flags[], |
| 122 const FSEventStreamEventId event_ids[]) { |
83 FilePathWatcherFSEvents* watcher = | 123 FilePathWatcherFSEvents* watcher = |
84 reinterpret_cast<FilePathWatcherFSEvents*>(event_watcher); | 124 reinterpret_cast<FilePathWatcherFSEvents*>(event_watcher); |
85 DCHECK(g_task_runner.Get().RunsTasksOnCurrentThread()); | 125 DCHECK(g_task_runner.Get().RunsTasksOnCurrentThread()); |
86 | 126 |
87 bool root_changed = watcher->ResolveTargetPath(); | 127 bool root_changed = watcher->ResolveTargetPath(); |
88 std::vector<FilePath> paths; | 128 std::vector<FilePath> paths; |
89 FSEventStreamEventId root_change_at = FSEventStreamGetLatestEventId(stream); | 129 FSEventStreamEventId root_change_at = FSEventStreamGetLatestEventId(stream); |
90 for (size_t i = 0; i < num_events; i++) { | 130 for (size_t i = 0; i < num_events; i++) { |
91 if (flags[i] & kFSEventStreamEventFlagRootChanged) | 131 if (flags[i] & kFSEventStreamEventFlagRootChanged) |
92 root_changed = true; | 132 root_changed = true; |
(...skipping 11 matching lines...) Expand all Loading... |
104 // bad file descriptor errors), so post a task to do the reset. | 144 // bad file descriptor errors), so post a task to do the reset. |
105 g_task_runner.Get().PostTask( | 145 g_task_runner.Get().PostTask( |
106 FROM_HERE, | 146 FROM_HERE, |
107 Bind(&FilePathWatcherFSEvents::UpdateEventStream, watcher, | 147 Bind(&FilePathWatcherFSEvents::UpdateEventStream, watcher, |
108 root_change_at)); | 148 root_change_at)); |
109 } | 149 } |
110 | 150 |
111 watcher->OnFilePathsChanged(paths); | 151 watcher->OnFilePathsChanged(paths); |
112 } | 152 } |
113 | 153 |
114 } // namespace | 154 FilePathWatcherFSEvents::~FilePathWatcherFSEvents() { |
115 | 155 // This method may be called on either the libdispatch or message_loop() |
116 FilePathWatcherFSEvents::FilePathWatcherFSEvents() : fsevent_stream_(NULL) { | 156 // thread. Checking callback_ on the libdispatch thread here is safe because |
| 157 // it is executing in a task posted by Cancel() which first reset callback_. |
| 158 // PostTask forms a sufficient memory barrier to ensure that the value is |
| 159 // consistent on the target thread. |
| 160 DCHECK(callback_.is_null()) |
| 161 << "Cancel() must be called before FilePathWatcher is destroyed."; |
117 } | 162 } |
118 | 163 |
119 void FilePathWatcherFSEvents::OnFilePathsChanged( | 164 void FilePathWatcherFSEvents::OnFilePathsChanged( |
120 const std::vector<FilePath>& paths) { | 165 const std::vector<FilePath>& paths) { |
121 if (!message_loop()->BelongsToCurrentThread()) { | 166 DCHECK(g_task_runner.Get().RunsTasksOnCurrentThread()); |
122 message_loop()->PostTask( | 167 DCHECK(!resolved_target_.empty()); |
123 FROM_HERE, | 168 message_loop()->PostTask( |
124 Bind(&FilePathWatcherFSEvents::OnFilePathsChanged, this, paths)); | 169 FROM_HERE, Bind(&FilePathWatcherFSEvents::DispatchEvents, this, paths, |
| 170 target_, resolved_target_)); |
| 171 } |
| 172 |
| 173 void FilePathWatcherFSEvents::DispatchEvents(const std::vector<FilePath>& paths, |
| 174 const FilePath& target, |
| 175 const FilePath& resolved_target) { |
| 176 DCHECK(message_loop()->RunsTasksOnCurrentThread()); |
| 177 |
| 178 // Don't issue callbacks after Cancel() has been called. |
| 179 if (is_cancelled() || callback_.is_null()) { |
125 return; | 180 return; |
126 } | 181 } |
127 | 182 |
128 DCHECK(message_loop()->BelongsToCurrentThread()); | 183 for (const FilePath& path : paths) { |
129 if (resolved_target_.empty()) | 184 if (resolved_target.IsParent(path) || resolved_target == path) { |
130 return; | 185 callback_.Run(target, false); |
131 | |
132 for (size_t i = 0; i < paths.size(); i++) { | |
133 if (resolved_target_.IsParent(paths[i]) || resolved_target_ == paths[i]) { | |
134 callback_.Run(target_, false); | |
135 return; | 186 return; |
136 } | 187 } |
137 } | 188 } |
138 } | 189 } |
139 | 190 |
140 bool FilePathWatcherFSEvents::Watch(const FilePath& path, | |
141 bool recursive, | |
142 const FilePathWatcher::Callback& callback) { | |
143 DCHECK(resolved_target_.empty()); | |
144 DCHECK(MessageLoopForIO::current()); | |
145 DCHECK(!callback.is_null()); | |
146 | |
147 // This class could support non-recursive watches, but that is currently | |
148 // left to FilePathWatcherKQueue. | |
149 if (!recursive) | |
150 return false; | |
151 | |
152 set_message_loop(MessageLoopProxy::current()); | |
153 callback_ = callback; | |
154 target_ = path; | |
155 | |
156 FSEventStreamEventId start_event = FSEventsGetCurrentEventId(); | |
157 g_task_runner.Get().PostTask( | |
158 FROM_HERE, | |
159 Bind(&FilePathWatcherFSEvents::StartEventStream, this, start_event)); | |
160 return true; | |
161 } | |
162 | |
163 void FilePathWatcherFSEvents::Cancel() { | |
164 if (callback_.is_null()) { | |
165 // Watch was never called, so exit. | |
166 set_cancelled(); | |
167 return; | |
168 } | |
169 | |
170 // Switch to the dispatch queue thread if necessary, so we can tear down | |
171 // the event stream. | |
172 if (!g_task_runner.Get().RunsTasksOnCurrentThread()) { | |
173 g_task_runner.Get().PostTask( | |
174 FROM_HERE, | |
175 Bind(&FilePathWatcherFSEvents::CancelOnMessageLoopThread, this)); | |
176 } else { | |
177 CancelOnMessageLoopThread(); | |
178 } | |
179 } | |
180 | |
181 void FilePathWatcherFSEvents::CancelOnMessageLoopThread() { | 191 void FilePathWatcherFSEvents::CancelOnMessageLoopThread() { |
182 // For all other implementations, the "message loop thread" is the IO thread, | 192 // For all other implementations, the "message loop thread" is the IO thread, |
183 // as returned by message_loop(). This implementation, however, needs to | 193 // as returned by message_loop(). This implementation, however, needs to |
184 // cancel pending work on the Dipatch Queue thread. | 194 // cancel pending work on the Dispatch Queue thread. |
185 DCHECK(g_task_runner.Get().RunsTasksOnCurrentThread()); | 195 DCHECK(g_task_runner.Get().RunsTasksOnCurrentThread()); |
186 | 196 |
187 set_cancelled(); | |
188 if (fsevent_stream_) { | 197 if (fsevent_stream_) { |
189 DestroyEventStream(); | 198 DestroyEventStream(); |
190 callback_.Reset(); | |
191 target_.clear(); | 199 target_.clear(); |
192 resolved_target_.clear(); | 200 resolved_target_.clear(); |
193 } | 201 } |
194 } | 202 } |
195 | 203 |
196 void FilePathWatcherFSEvents::UpdateEventStream( | 204 void FilePathWatcherFSEvents::UpdateEventStream( |
197 FSEventStreamEventId start_event) { | 205 FSEventStreamEventId start_event) { |
198 DCHECK(g_task_runner.Get().RunsTasksOnCurrentThread()); | 206 DCHECK(g_task_runner.Get().RunsTasksOnCurrentThread()); |
199 | 207 |
200 // It can happen that the watcher gets canceled while tasks that call this | 208 // It can happen that the watcher gets canceled while tasks that call this |
201 // function are still in flight, so abort if this situation is detected. | 209 // function are still in flight, so abort if this situation is detected. |
202 if (is_cancelled() || resolved_target_.empty()) | 210 if (resolved_target_.empty()) |
203 return; | 211 return; |
204 | 212 |
205 if (fsevent_stream_) | 213 if (fsevent_stream_) |
206 DestroyEventStream(); | 214 DestroyEventStream(); |
207 | 215 |
208 ScopedCFTypeRef<CFStringRef> cf_path(CFStringCreateWithCString( | 216 ScopedCFTypeRef<CFStringRef> cf_path(CFStringCreateWithCString( |
209 NULL, resolved_target_.value().c_str(), kCFStringEncodingMacHFS)); | 217 NULL, resolved_target_.value().c_str(), kCFStringEncodingMacHFS)); |
210 ScopedCFTypeRef<CFStringRef> cf_dir_path(CFStringCreateWithCString( | 218 ScopedCFTypeRef<CFStringRef> cf_dir_path(CFStringCreateWithCString( |
211 NULL, resolved_target_.DirName().value().c_str(), | 219 NULL, resolved_target_.DirName().value().c_str(), |
212 kCFStringEncodingMacHFS)); | 220 kCFStringEncodingMacHFS)); |
(...skipping 10 matching lines...) Expand all Loading... |
223 context.copyDescription = NULL; | 231 context.copyDescription = NULL; |
224 | 232 |
225 fsevent_stream_ = FSEventStreamCreate(NULL, &FSEventsCallback, &context, | 233 fsevent_stream_ = FSEventStreamCreate(NULL, &FSEventsCallback, &context, |
226 watched_paths, | 234 watched_paths, |
227 start_event, | 235 start_event, |
228 kEventLatencySeconds, | 236 kEventLatencySeconds, |
229 kFSEventStreamCreateFlagWatchRoot); | 237 kFSEventStreamCreateFlagWatchRoot); |
230 FSEventStreamSetDispatchQueue(fsevent_stream_, | 238 FSEventStreamSetDispatchQueue(fsevent_stream_, |
231 g_task_runner.Get().GetDispatchQueue()); | 239 g_task_runner.Get().GetDispatchQueue()); |
232 | 240 |
233 if (!FSEventStreamStart(fsevent_stream_)) | 241 if (!FSEventStreamStart(fsevent_stream_)) { |
234 message_loop()->PostTask(FROM_HERE, Bind(callback_, target_, true)); | 242 message_loop()->PostTask( |
| 243 FROM_HERE, Bind(&FilePathWatcherFSEvents::ReportError, this, target_)); |
| 244 } |
235 } | 245 } |
236 | 246 |
237 bool FilePathWatcherFSEvents::ResolveTargetPath() { | 247 bool FilePathWatcherFSEvents::ResolveTargetPath() { |
238 DCHECK(g_task_runner.Get().RunsTasksOnCurrentThread()); | 248 DCHECK(g_task_runner.Get().RunsTasksOnCurrentThread()); |
239 FilePath resolved = ResolvePath(target_).StripTrailingSeparators(); | 249 FilePath resolved = ResolvePath(target_).StripTrailingSeparators(); |
240 bool changed = resolved != resolved_target_; | 250 bool changed = resolved != resolved_target_; |
241 resolved_target_ = resolved; | 251 resolved_target_ = resolved; |
242 if (resolved_target_.empty()) | 252 if (resolved_target_.empty()) { |
243 message_loop()->PostTask(FROM_HERE, Bind(callback_, target_, true)); | 253 message_loop()->PostTask( |
| 254 FROM_HERE, Bind(&FilePathWatcherFSEvents::ReportError, this, target_)); |
| 255 } |
244 return changed; | 256 return changed; |
245 } | 257 } |
246 | 258 |
| 259 void FilePathWatcherFSEvents::ReportError(const FilePath& target) { |
| 260 DCHECK(message_loop()->RunsTasksOnCurrentThread()); |
| 261 if (!callback_.is_null()) { |
| 262 callback_.Run(target, true); |
| 263 } |
| 264 } |
| 265 |
247 void FilePathWatcherFSEvents::DestroyEventStream() { | 266 void FilePathWatcherFSEvents::DestroyEventStream() { |
248 FSEventStreamStop(fsevent_stream_); | 267 FSEventStreamStop(fsevent_stream_); |
249 FSEventStreamInvalidate(fsevent_stream_); | 268 FSEventStreamInvalidate(fsevent_stream_); |
250 FSEventStreamRelease(fsevent_stream_); | 269 FSEventStreamRelease(fsevent_stream_); |
251 fsevent_stream_ = NULL; | 270 fsevent_stream_ = NULL; |
252 } | 271 } |
253 | 272 |
254 void FilePathWatcherFSEvents::StartEventStream( | 273 void FilePathWatcherFSEvents::StartEventStream(FSEventStreamEventId start_event, |
255 FSEventStreamEventId start_event) { | 274 const FilePath& path) { |
256 DCHECK(g_task_runner.Get().RunsTasksOnCurrentThread()); | 275 DCHECK(g_task_runner.Get().RunsTasksOnCurrentThread()); |
| 276 DCHECK(resolved_target_.empty()); |
| 277 |
| 278 target_ = path; |
257 ResolveTargetPath(); | 279 ResolveTargetPath(); |
258 UpdateEventStream(start_event); | 280 UpdateEventStream(start_event); |
259 } | 281 } |
260 | 282 |
261 FilePathWatcherFSEvents::~FilePathWatcherFSEvents() { | |
262 DCHECK(!fsevent_stream_) | |
263 << "File path watcher destroyed before event stream."; | |
264 } | |
265 | |
266 } // namespace base | 283 } // namespace base |
OLD | NEW |