Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_kqueue.h" | 5 #include "base/files/file_path_watcher_kqueue.h" |
| 6 | 6 |
| 7 #include <fcntl.h> | 7 #include <fcntl.h> |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 #include <sys/param.h> | 9 #include <sys/param.h> |
| 10 | 10 |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 29 FilePathWatcherKQueue::~FilePathWatcherKQueue() {} | 29 FilePathWatcherKQueue::~FilePathWatcherKQueue() {} |
| 30 | 30 |
| 31 void FilePathWatcherKQueue::ReleaseEvent(struct kevent& event) { | 31 void FilePathWatcherKQueue::ReleaseEvent(struct kevent& event) { |
| 32 CloseFileDescriptor(&event.ident); | 32 CloseFileDescriptor(&event.ident); |
| 33 EventData* entry = EventDataForKevent(event); | 33 EventData* entry = EventDataForKevent(event); |
| 34 delete entry; | 34 delete entry; |
| 35 event.udata = NULL; | 35 event.udata = NULL; |
| 36 } | 36 } |
| 37 | 37 |
| 38 int FilePathWatcherKQueue::EventsForPath(FilePath path, EventVector* events) { | 38 int FilePathWatcherKQueue::EventsForPath(FilePath path, EventVector* events) { |
| 39 DCHECK(MessageLoopForIO::current()); | |
| 40 // Make sure that we are working with a clean slate. | 39 // Make sure that we are working with a clean slate. |
| 41 DCHECK(events->empty()); | 40 DCHECK(events->empty()); |
| 42 | 41 |
| 43 std::vector<FilePath::StringType> components; | 42 std::vector<FilePath::StringType> components; |
| 44 path.GetComponents(&components); | 43 path.GetComponents(&components); |
| 45 | 44 |
| 46 if (components.size() < 1) { | 45 if (components.size() < 1) { |
| 47 return -1; | 46 return -1; |
| 48 } | 47 } |
| 49 | 48 |
| (...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 223 *target_file_affected = true; | 222 *target_file_affected = true; |
| 224 } | 223 } |
| 225 } else { | 224 } else { |
| 226 break; | 225 break; |
| 227 } | 226 } |
| 228 } | 227 } |
| 229 } | 228 } |
| 230 return true; | 229 return true; |
| 231 } | 230 } |
| 232 | 231 |
| 233 void FilePathWatcherKQueue::OnFileCanReadWithoutBlocking(int fd) { | 232 void FilePathWatcherKQueue::WillDestroyCurrentMessageLoop() { |
| 234 DCHECK(MessageLoopForIO::current()); | 233 CancelOnMessageLoopThread(); |
| 235 DCHECK_EQ(fd, kqueue_); | 234 } |
| 235 | |
| 236 bool FilePathWatcherKQueue::Watch(const FilePath& path, | |
| 237 bool recursive, | |
| 238 const FilePathWatcher::Callback& callback) { | |
| 239 DCHECK(target_.value().empty()); // Can only watch one path. | |
| 240 DCHECK(!callback.is_null()); | |
| 241 DCHECK_EQ(kqueue_, -1); | |
| 242 // Recursive watch is not supported using kqueue. | |
| 243 DCHECK(!recursive); | |
| 244 | |
| 245 callback_ = callback; | |
| 246 target_ = path; | |
| 247 | |
| 248 MessageLoop::current()->AddDestructionObserver(this); | |
| 249 set_task_runner(ThreadTaskRunnerHandle::Get()); | |
| 250 | |
| 251 kqueue_ = kqueue(); | |
| 252 if (kqueue_ == -1) { | |
| 253 DPLOG(ERROR) << "kqueue"; | |
| 254 return false; | |
| 255 } | |
| 256 | |
| 257 int last_entry = EventsForPath(target_, &events_); | |
| 258 DCHECK_NE(last_entry, 0); | |
| 259 | |
| 260 EventVector responses(last_entry); | |
| 261 | |
| 262 int count = HANDLE_EINTR(kevent(kqueue_, &events_[0], last_entry, | |
| 263 &responses[0], last_entry, NULL)); | |
| 264 if (!AreKeventValuesValid(&responses[0], count)) { | |
| 265 // Calling Cancel() here to close any file descriptors that were opened. | |
| 266 // This would happen in the destructor anyways, but FilePathWatchers tend to | |
| 267 // be long lived, and if an error has occurred, there is no reason to waste | |
| 268 // the file descriptors. | |
| 269 Cancel(); | |
| 270 return false; | |
| 271 } | |
| 272 | |
| 273 kqueue_watch_controller_ = FileDescriptorWatcher::WatchReadable( | |
| 274 kqueue_, | |
| 275 Bind(&FilePathWatcherKQueue::OnKQueueReadable, Unretained(this))); | |
|
dcheng
2016/09/20 23:22:04
|this| is refcounted, so maybe just use this (and
fdoray
2016/09/26 14:32:02
Done.
Note that this creates an ownership cycle (
dcheng
2016/09/29 18:06:53
I'm OK with it either way, I guess. It's just what
| |
| 276 return true; | |
| 277 } | |
| 278 | |
| 279 void FilePathWatcherKQueue::Cancel() { | |
| 280 if (!task_runner()) { | |
| 281 set_cancelled(); | |
| 282 return; | |
| 283 } | |
| 284 if (!task_runner()->BelongsToCurrentThread()) { | |
| 285 task_runner()->PostTask(FROM_HERE, | |
| 286 base::Bind(&FilePathWatcherKQueue::Cancel, this)); | |
| 287 return; | |
| 288 } | |
| 289 CancelOnMessageLoopThread(); | |
| 290 } | |
| 291 | |
| 292 void FilePathWatcherKQueue::OnKQueueReadable() { | |
| 293 DCHECK(task_runner()->BelongsToCurrentThread()); | |
| 236 DCHECK(events_.size()); | 294 DCHECK(events_.size()); |
| 237 | 295 |
| 238 // Request the file system update notifications that have occurred and return | 296 // Request the file system update notifications that have occurred and return |
| 239 // them in |updates|. |count| will contain the number of updates that have | 297 // them in |updates|. |count| will contain the number of updates that have |
| 240 // occurred. | 298 // occurred. |
| 241 EventVector updates(events_.size()); | 299 EventVector updates(events_.size()); |
| 242 struct timespec timeout = {0, 0}; | 300 struct timespec timeout = {0, 0}; |
| 243 int count = HANDLE_EINTR(kevent(kqueue_, NULL, 0, &updates[0], updates.size(), | 301 int count = HANDLE_EINTR(kevent(kqueue_, NULL, 0, &updates[0], updates.size(), |
| 244 &timeout)); | 302 &timeout)); |
| 245 | 303 |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 296 callback_.Run(target_, true /* error */); | 354 callback_.Run(target_, true /* error */); |
| 297 Cancel(); | 355 Cancel(); |
| 298 } | 356 } |
| 299 } | 357 } |
| 300 | 358 |
| 301 if (send_notification) { | 359 if (send_notification) { |
| 302 callback_.Run(target_, false); | 360 callback_.Run(target_, false); |
| 303 } | 361 } |
| 304 } | 362 } |
| 305 | 363 |
| 306 void FilePathWatcherKQueue::OnFileCanWriteWithoutBlocking(int fd) { | |
| 307 NOTREACHED(); | |
| 308 } | |
| 309 | |
| 310 void FilePathWatcherKQueue::WillDestroyCurrentMessageLoop() { | |
| 311 CancelOnMessageLoopThread(); | |
| 312 } | |
| 313 | |
| 314 bool FilePathWatcherKQueue::Watch(const FilePath& path, | |
| 315 bool recursive, | |
| 316 const FilePathWatcher::Callback& callback) { | |
| 317 DCHECK(MessageLoopForIO::current()); | |
| 318 DCHECK(target_.value().empty()); // Can only watch one path. | |
| 319 DCHECK(!callback.is_null()); | |
| 320 DCHECK_EQ(kqueue_, -1); | |
| 321 | |
| 322 if (recursive) { | |
| 323 // Recursive watch is not supported using kqueue. | |
| 324 NOTIMPLEMENTED(); | |
| 325 return false; | |
| 326 } | |
| 327 | |
| 328 callback_ = callback; | |
| 329 target_ = path; | |
| 330 | |
| 331 MessageLoop::current()->AddDestructionObserver(this); | |
| 332 io_task_runner_ = ThreadTaskRunnerHandle::Get(); | |
| 333 | |
| 334 kqueue_ = kqueue(); | |
| 335 if (kqueue_ == -1) { | |
| 336 DPLOG(ERROR) << "kqueue"; | |
| 337 return false; | |
| 338 } | |
| 339 | |
| 340 int last_entry = EventsForPath(target_, &events_); | |
| 341 DCHECK_NE(last_entry, 0); | |
| 342 | |
| 343 EventVector responses(last_entry); | |
| 344 | |
| 345 int count = HANDLE_EINTR(kevent(kqueue_, &events_[0], last_entry, | |
| 346 &responses[0], last_entry, NULL)); | |
| 347 if (!AreKeventValuesValid(&responses[0], count)) { | |
| 348 // Calling Cancel() here to close any file descriptors that were opened. | |
| 349 // This would happen in the destructor anyways, but FilePathWatchers tend to | |
| 350 // be long lived, and if an error has occurred, there is no reason to waste | |
| 351 // the file descriptors. | |
| 352 Cancel(); | |
| 353 return false; | |
| 354 } | |
| 355 | |
| 356 return MessageLoopForIO::current()->WatchFileDescriptor( | |
| 357 kqueue_, true, MessageLoopForIO::WATCH_READ, &kqueue_watcher_, this); | |
| 358 } | |
| 359 | |
| 360 void FilePathWatcherKQueue::Cancel() { | |
| 361 SingleThreadTaskRunner* task_runner = io_task_runner_.get(); | |
| 362 if (!task_runner) { | |
| 363 set_cancelled(); | |
| 364 return; | |
| 365 } | |
| 366 if (!task_runner->BelongsToCurrentThread()) { | |
| 367 task_runner->PostTask(FROM_HERE, | |
| 368 base::Bind(&FilePathWatcherKQueue::Cancel, this)); | |
| 369 return; | |
| 370 } | |
| 371 CancelOnMessageLoopThread(); | |
| 372 } | |
| 373 | |
| 374 void FilePathWatcherKQueue::CancelOnMessageLoopThread() { | 364 void FilePathWatcherKQueue::CancelOnMessageLoopThread() { |
| 375 DCHECK(MessageLoopForIO::current()); | 365 DCHECK(!task_runner() || task_runner()->BelongsToCurrentThread()); |
| 376 if (!is_cancelled()) { | 366 if (!is_cancelled()) { |
| 377 set_cancelled(); | 367 set_cancelled(); |
| 378 kqueue_watcher_.StopWatchingFileDescriptor(); | 368 kqueue_watch_controller_.reset(); |
| 379 if (IGNORE_EINTR(close(kqueue_)) != 0) { | 369 if (IGNORE_EINTR(close(kqueue_)) != 0) { |
| 380 DPLOG(ERROR) << "close kqueue"; | 370 DPLOG(ERROR) << "close kqueue"; |
| 381 } | 371 } |
| 382 kqueue_ = -1; | 372 kqueue_ = -1; |
| 383 std::for_each(events_.begin(), events_.end(), ReleaseEvent); | 373 std::for_each(events_.begin(), events_.end(), ReleaseEvent); |
| 384 events_.clear(); | 374 events_.clear(); |
| 385 io_task_runner_ = NULL; | |
| 386 MessageLoop::current()->RemoveDestructionObserver(this); | 375 MessageLoop::current()->RemoveDestructionObserver(this); |
| 387 callback_.Reset(); | 376 callback_.Reset(); |
| 388 } | 377 } |
| 389 } | 378 } |
| 390 | 379 |
| 391 } // namespace base | 380 } // namespace base |
| OLD | NEW |