Chromium Code Reviews| 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 DCHECK(!fsevent_stream_) |
|
Mattias Nissler (ping if slow)
2015/04/07 11:36:20
Is the dtor guaranteed to run on the dispatch queu
Reilly Grant (use Gerrit)
2015/04/07 20:42:14
There are a couple of cases:
1) If Cancel has not
Mattias Nissler (ping if slow)
2015/04/08 07:57:09
I don't see why you would be guaranteed that fseve
| |
| 116 FilePathWatcherFSEvents::FilePathWatcherFSEvents() : fsevent_stream_(NULL) { | 156 << "File path watcher destroyed before event stream."; |
| 117 } | 157 } |
| 118 | 158 |
| 119 void FilePathWatcherFSEvents::OnFilePathsChanged( | 159 void FilePathWatcherFSEvents::OnFilePathsChanged( |
| 120 const std::vector<FilePath>& paths) { | 160 const std::vector<FilePath>& paths) { |
| 121 if (!message_loop()->BelongsToCurrentThread()) { | 161 DCHECK(!resolved_target_.empty()); |
| 122 message_loop()->PostTask( | 162 message_loop()->PostTask( |
| 123 FROM_HERE, | 163 FROM_HERE, Bind(&FilePathWatcherFSEvents::DispatchEvents, this, paths, |
| 124 Bind(&FilePathWatcherFSEvents::OnFilePathsChanged, this, paths)); | 164 target_, resolved_target_)); |
| 165 } | |
| 166 | |
| 167 void FilePathWatcherFSEvents::DispatchEvents(const std::vector<FilePath>& paths, | |
| 168 const FilePath& target, | |
| 169 const FilePath& resolved_target) { | |
| 170 DCHECK(message_loop()->RunsTasksOnCurrentThread()); | |
| 171 | |
| 172 // Don't issue callbacks after Cancel() has been called. | |
| 173 if (is_cancelled() || callback_.is_null()) { | |
| 125 return; | 174 return; |
| 126 } | 175 } |
| 127 | 176 |
| 128 DCHECK(message_loop()->BelongsToCurrentThread()); | 177 for (const FilePath& path : paths) { |
| 129 if (resolved_target_.empty()) | 178 if (resolved_target.IsParent(path) || resolved_target == path) { |
| 130 return; | 179 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; | 180 return; |
| 136 } | 181 } |
| 137 } | 182 } |
| 138 } | 183 } |
| 139 | 184 |
| 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() { | 185 void FilePathWatcherFSEvents::CancelOnMessageLoopThread() { |
| 182 // For all other implementations, the "message loop thread" is the IO thread, | 186 // For all other implementations, the "message loop thread" is the IO thread, |
| 183 // as returned by message_loop(). This implementation, however, needs to | 187 // as returned by message_loop(). This implementation, however, needs to |
| 184 // cancel pending work on the Dipatch Queue thread. | 188 // cancel pending work on the Dispatch Queue thread. |
| 185 DCHECK(g_task_runner.Get().RunsTasksOnCurrentThread()); | 189 DCHECK(g_task_runner.Get().RunsTasksOnCurrentThread()); |
| 186 | 190 |
| 187 set_cancelled(); | |
| 188 if (fsevent_stream_) { | 191 if (fsevent_stream_) { |
| 189 DestroyEventStream(); | 192 DestroyEventStream(); |
| 190 callback_.Reset(); | |
| 191 target_.clear(); | 193 target_.clear(); |
| 192 resolved_target_.clear(); | 194 resolved_target_.clear(); |
| 193 } | 195 } |
| 194 } | 196 } |
| 195 | 197 |
| 196 void FilePathWatcherFSEvents::UpdateEventStream( | 198 void FilePathWatcherFSEvents::UpdateEventStream( |
| 197 FSEventStreamEventId start_event) { | 199 FSEventStreamEventId start_event) { |
| 198 DCHECK(g_task_runner.Get().RunsTasksOnCurrentThread()); | 200 DCHECK(g_task_runner.Get().RunsTasksOnCurrentThread()); |
| 199 | 201 |
| 200 // It can happen that the watcher gets canceled while tasks that call this | 202 // 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. | 203 // function are still in flight, so abort if this situation is detected. |
| 202 if (is_cancelled() || resolved_target_.empty()) | 204 if (resolved_target_.empty()) |
| 203 return; | 205 return; |
| 204 | 206 |
| 205 if (fsevent_stream_) | 207 if (fsevent_stream_) |
| 206 DestroyEventStream(); | 208 DestroyEventStream(); |
| 207 | 209 |
| 208 ScopedCFTypeRef<CFStringRef> cf_path(CFStringCreateWithCString( | 210 ScopedCFTypeRef<CFStringRef> cf_path(CFStringCreateWithCString( |
| 209 NULL, resolved_target_.value().c_str(), kCFStringEncodingMacHFS)); | 211 NULL, resolved_target_.value().c_str(), kCFStringEncodingMacHFS)); |
| 210 ScopedCFTypeRef<CFStringRef> cf_dir_path(CFStringCreateWithCString( | 212 ScopedCFTypeRef<CFStringRef> cf_dir_path(CFStringCreateWithCString( |
| 211 NULL, resolved_target_.DirName().value().c_str(), | 213 NULL, resolved_target_.DirName().value().c_str(), |
| 212 kCFStringEncodingMacHFS)); | 214 kCFStringEncodingMacHFS)); |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 224 | 226 |
| 225 fsevent_stream_ = FSEventStreamCreate(NULL, &FSEventsCallback, &context, | 227 fsevent_stream_ = FSEventStreamCreate(NULL, &FSEventsCallback, &context, |
| 226 watched_paths, | 228 watched_paths, |
| 227 start_event, | 229 start_event, |
| 228 kEventLatencySeconds, | 230 kEventLatencySeconds, |
| 229 kFSEventStreamCreateFlagWatchRoot); | 231 kFSEventStreamCreateFlagWatchRoot); |
| 230 FSEventStreamSetDispatchQueue(fsevent_stream_, | 232 FSEventStreamSetDispatchQueue(fsevent_stream_, |
| 231 g_task_runner.Get().GetDispatchQueue()); | 233 g_task_runner.Get().GetDispatchQueue()); |
| 232 | 234 |
| 233 if (!FSEventStreamStart(fsevent_stream_)) | 235 if (!FSEventStreamStart(fsevent_stream_)) |
| 234 message_loop()->PostTask(FROM_HERE, Bind(callback_, target_, true)); | 236 message_loop()->PostTask(FROM_HERE, Bind(callback_, target_, true)); |
|
Mattias Nissler (ping if slow)
2015/04/07 11:36:20
Strictly speaking, this is still referencing callb
Reilly Grant (use Gerrit)
2015/04/07 20:42:14
Done.
| |
| 235 } | 237 } |
| 236 | 238 |
| 237 bool FilePathWatcherFSEvents::ResolveTargetPath() { | 239 bool FilePathWatcherFSEvents::ResolveTargetPath() { |
| 238 DCHECK(g_task_runner.Get().RunsTasksOnCurrentThread()); | 240 DCHECK(g_task_runner.Get().RunsTasksOnCurrentThread()); |
| 239 FilePath resolved = ResolvePath(target_).StripTrailingSeparators(); | 241 FilePath resolved = ResolvePath(target_).StripTrailingSeparators(); |
| 240 bool changed = resolved != resolved_target_; | 242 bool changed = resolved != resolved_target_; |
| 241 resolved_target_ = resolved; | 243 resolved_target_ = resolved; |
| 242 if (resolved_target_.empty()) | 244 if (resolved_target_.empty()) |
| 243 message_loop()->PostTask(FROM_HERE, Bind(callback_, target_, true)); | 245 message_loop()->PostTask(FROM_HERE, Bind(callback_, target_, true)); |
| 244 return changed; | 246 return changed; |
| 245 } | 247 } |
| 246 | 248 |
| 247 void FilePathWatcherFSEvents::DestroyEventStream() { | 249 void FilePathWatcherFSEvents::DestroyEventStream() { |
| 248 FSEventStreamStop(fsevent_stream_); | 250 FSEventStreamStop(fsevent_stream_); |
| 249 FSEventStreamInvalidate(fsevent_stream_); | 251 FSEventStreamInvalidate(fsevent_stream_); |
| 250 FSEventStreamRelease(fsevent_stream_); | 252 FSEventStreamRelease(fsevent_stream_); |
| 251 fsevent_stream_ = NULL; | 253 fsevent_stream_ = NULL; |
| 252 } | 254 } |
| 253 | 255 |
| 254 void FilePathWatcherFSEvents::StartEventStream( | 256 void FilePathWatcherFSEvents::StartEventStream(FSEventStreamEventId start_event, |
| 255 FSEventStreamEventId start_event) { | 257 const FilePath& path) { |
| 256 DCHECK(g_task_runner.Get().RunsTasksOnCurrentThread()); | 258 DCHECK(g_task_runner.Get().RunsTasksOnCurrentThread()); |
| 259 DCHECK(resolved_target_.empty()); | |
| 260 | |
| 261 target_ = path; | |
| 257 ResolveTargetPath(); | 262 ResolveTargetPath(); |
| 258 UpdateEventStream(start_event); | 263 UpdateEventStream(start_event); |
| 259 } | 264 } |
| 260 | 265 |
| 261 FilePathWatcherFSEvents::~FilePathWatcherFSEvents() { | |
| 262 DCHECK(!fsevent_stream_) | |
| 263 << "File path watcher destroyed before event stream."; | |
| 264 } | |
| 265 | |
| 266 } // namespace base | 266 } // namespace base |
| OLD | NEW |