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 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
104 // bad file descriptor errors), so post a task to do the reset. | 104 // bad file descriptor errors), so post a task to do the reset. |
105 g_task_runner.Get().PostTask( | 105 g_task_runner.Get().PostTask( |
106 FROM_HERE, | 106 FROM_HERE, |
107 Bind(&FilePathWatcherFSEvents::UpdateEventStream, watcher, | 107 Bind(&FilePathWatcherFSEvents::UpdateEventStream, watcher, |
108 root_change_at)); | 108 root_change_at)); |
109 } | 109 } |
110 | 110 |
111 watcher->OnFilePathsChanged(paths); | 111 watcher->OnFilePathsChanged(paths); |
112 } | 112 } |
113 | 113 |
114 void FilterAndDispatchEvents(const std::vector<FilePath>& paths, | |
115 const FilePath& target, | |
116 const FilePath& resolved_target, | |
117 const FilePathWatcher::Callback& callback) { | |
118 for (const FilePath& path : paths) { | |
119 if (resolved_target.IsParent(path) || resolved_target == path) { | |
120 callback.Run(target, false); | |
Mattias Nissler (ping if slow)
2015/04/02 17:35:45
I think this may result in callbacks firing even a
Reilly Grant (use Gerrit)
2015/04/02 17:53:30
That's true. If it's a contract violation then I'l
| |
121 return; | |
122 } | |
123 } | |
124 } | |
125 | |
114 } // namespace | 126 } // namespace |
115 | 127 |
116 FilePathWatcherFSEvents::FilePathWatcherFSEvents() : fsevent_stream_(NULL) { | 128 FilePathWatcherFSEvents::FilePathWatcherFSEvents() : fsevent_stream_(NULL) { |
117 } | 129 } |
118 | 130 |
119 void FilePathWatcherFSEvents::OnFilePathsChanged( | 131 void FilePathWatcherFSEvents::OnFilePathsChanged( |
120 const std::vector<FilePath>& paths) { | 132 const std::vector<FilePath>& paths) { |
121 if (!message_loop()->BelongsToCurrentThread()) { | 133 DCHECK(!resolved_target_.empty()); |
122 message_loop()->PostTask( | 134 message_loop()->PostTask(FROM_HERE, |
123 FROM_HERE, | 135 Bind(&FilterAndDispatchEvents, paths, target_, |
124 Bind(&FilePathWatcherFSEvents::OnFilePathsChanged, this, paths)); | 136 resolved_target_, callback_)); |
125 return; | |
126 } | |
127 | |
128 DCHECK(message_loop()->BelongsToCurrentThread()); | |
129 if (resolved_target_.empty()) | |
130 return; | |
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; | |
136 } | |
137 } | |
138 } | 137 } |
139 | 138 |
140 bool FilePathWatcherFSEvents::Watch(const FilePath& path, | 139 bool FilePathWatcherFSEvents::Watch(const FilePath& path, |
141 bool recursive, | 140 bool recursive, |
142 const FilePathWatcher::Callback& callback) { | 141 const FilePathWatcher::Callback& callback) { |
143 DCHECK(resolved_target_.empty()); | |
144 DCHECK(MessageLoopForIO::current()); | 142 DCHECK(MessageLoopForIO::current()); |
145 DCHECK(!callback.is_null()); | 143 DCHECK(!callback.is_null()); |
146 | 144 |
147 // This class could support non-recursive watches, but that is currently | 145 // This class could support non-recursive watches, but that is currently |
148 // left to FilePathWatcherKQueue. | 146 // left to FilePathWatcherKQueue. |
149 if (!recursive) | 147 if (!recursive) |
150 return false; | 148 return false; |
151 | 149 |
152 set_message_loop(MessageLoopProxy::current()); | 150 set_message_loop(MessageLoopProxy::current()); |
153 callback_ = callback; | |
154 target_ = path; | |
155 | 151 |
156 FSEventStreamEventId start_event = FSEventsGetCurrentEventId(); | 152 FSEventStreamEventId start_event = FSEventsGetCurrentEventId(); |
157 g_task_runner.Get().PostTask( | 153 g_task_runner.Get().PostTask( |
158 FROM_HERE, | 154 FROM_HERE, Bind(&FilePathWatcherFSEvents::StartEventStream, this, |
159 Bind(&FilePathWatcherFSEvents::StartEventStream, this, start_event)); | 155 start_event, path, callback)); |
160 return true; | 156 return true; |
161 } | 157 } |
162 | 158 |
163 void FilePathWatcherFSEvents::Cancel() { | 159 void FilePathWatcherFSEvents::Cancel() { |
164 if (callback_.is_null()) { | 160 // Switch to the dispatch queue thread to tear down the event stream. |
165 // Watch was never called, so exit. | 161 g_task_runner.Get().PostTask( |
166 set_cancelled(); | 162 FROM_HERE, |
167 return; | 163 Bind(&FilePathWatcherFSEvents::CancelOnMessageLoopThread, this)); |
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 } | 164 } |
180 | 165 |
181 void FilePathWatcherFSEvents::CancelOnMessageLoopThread() { | 166 void FilePathWatcherFSEvents::CancelOnMessageLoopThread() { |
182 // For all other implementations, the "message loop thread" is the IO thread, | 167 // For all other implementations, the "message loop thread" is the IO thread, |
183 // as returned by message_loop(). This implementation, however, needs to | 168 // as returned by message_loop(). This implementation, however, needs to |
184 // cancel pending work on the Dipatch Queue thread. | 169 // cancel pending work on the Dispatch Queue thread. |
185 DCHECK(g_task_runner.Get().RunsTasksOnCurrentThread()); | 170 DCHECK(g_task_runner.Get().RunsTasksOnCurrentThread()); |
186 | 171 |
187 set_cancelled(); | 172 set_cancelled(); |
188 if (fsevent_stream_) { | 173 if (fsevent_stream_) { |
189 DestroyEventStream(); | 174 DestroyEventStream(); |
190 callback_.Reset(); | 175 callback_.Reset(); |
191 target_.clear(); | 176 target_.clear(); |
192 resolved_target_.clear(); | 177 resolved_target_.clear(); |
193 } | 178 } |
194 } | 179 } |
195 | 180 |
196 void FilePathWatcherFSEvents::UpdateEventStream( | 181 void FilePathWatcherFSEvents::UpdateEventStream( |
197 FSEventStreamEventId start_event) { | 182 FSEventStreamEventId start_event) { |
198 DCHECK(g_task_runner.Get().RunsTasksOnCurrentThread()); | 183 DCHECK(g_task_runner.Get().RunsTasksOnCurrentThread()); |
199 | 184 |
200 // It can happen that the watcher gets canceled while tasks that call this | |
Mattias Nissler (ping if slow)
2015/04/02 17:35:45
I don't see why we no longer need a safeguard here
Reilly Grant (use Gerrit)
2015/04/02 17:53:30
With the cancel operation serialized onto the libd
| |
201 // function are still in flight, so abort if this situation is detected. | |
202 if (is_cancelled() || resolved_target_.empty()) | |
203 return; | |
204 | |
205 if (fsevent_stream_) | 185 if (fsevent_stream_) |
206 DestroyEventStream(); | 186 DestroyEventStream(); |
207 | 187 |
208 ScopedCFTypeRef<CFStringRef> cf_path(CFStringCreateWithCString( | 188 ScopedCFTypeRef<CFStringRef> cf_path(CFStringCreateWithCString( |
209 NULL, resolved_target_.value().c_str(), kCFStringEncodingMacHFS)); | 189 NULL, resolved_target_.value().c_str(), kCFStringEncodingMacHFS)); |
210 ScopedCFTypeRef<CFStringRef> cf_dir_path(CFStringCreateWithCString( | 190 ScopedCFTypeRef<CFStringRef> cf_dir_path(CFStringCreateWithCString( |
211 NULL, resolved_target_.DirName().value().c_str(), | 191 NULL, resolved_target_.DirName().value().c_str(), |
212 kCFStringEncodingMacHFS)); | 192 kCFStringEncodingMacHFS)); |
213 CFStringRef paths_array[] = { cf_path.get(), cf_dir_path.get() }; | 193 CFStringRef paths_array[] = { cf_path.get(), cf_dir_path.get() }; |
214 ScopedCFTypeRef<CFArrayRef> watched_paths(CFArrayCreate( | 194 ScopedCFTypeRef<CFArrayRef> watched_paths(CFArrayCreate( |
(...skipping 30 matching lines...) Expand all Loading... | |
245 } | 225 } |
246 | 226 |
247 void FilePathWatcherFSEvents::DestroyEventStream() { | 227 void FilePathWatcherFSEvents::DestroyEventStream() { |
248 FSEventStreamStop(fsevent_stream_); | 228 FSEventStreamStop(fsevent_stream_); |
249 FSEventStreamInvalidate(fsevent_stream_); | 229 FSEventStreamInvalidate(fsevent_stream_); |
250 FSEventStreamRelease(fsevent_stream_); | 230 FSEventStreamRelease(fsevent_stream_); |
251 fsevent_stream_ = NULL; | 231 fsevent_stream_ = NULL; |
252 } | 232 } |
253 | 233 |
254 void FilePathWatcherFSEvents::StartEventStream( | 234 void FilePathWatcherFSEvents::StartEventStream( |
255 FSEventStreamEventId start_event) { | 235 FSEventStreamEventId start_event, |
236 const FilePath& path, | |
237 const FilePathWatcher::Callback& callback) { | |
256 DCHECK(g_task_runner.Get().RunsTasksOnCurrentThread()); | 238 DCHECK(g_task_runner.Get().RunsTasksOnCurrentThread()); |
239 DCHECK(resolved_target_.empty()); | |
240 | |
241 callback_ = callback; | |
Mattias Nissler (ping if slow)
2015/04/02 17:35:45
I think you want to keep callback_ on the message_
Reilly Grant (use Gerrit)
2015/04/02 17:53:31
I don't think base::Callback is thread safe. I'll
| |
242 target_ = path; | |
257 ResolveTargetPath(); | 243 ResolveTargetPath(); |
258 UpdateEventStream(start_event); | 244 UpdateEventStream(start_event); |
259 } | 245 } |
260 | 246 |
261 FilePathWatcherFSEvents::~FilePathWatcherFSEvents() { | 247 FilePathWatcherFSEvents::~FilePathWatcherFSEvents() { |
262 DCHECK(!fsevent_stream_) | 248 DCHECK(!fsevent_stream_) |
263 << "File path watcher destroyed before event stream."; | 249 << "File path watcher destroyed before event stream."; |
264 } | 250 } |
265 | 251 |
266 } // namespace base | 252 } // namespace base |
OLD | NEW |