| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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.h" | 5 #include "base/files/file_path_watcher.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/files/file.h" | 9 #include "base/files/file.h" |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/message_loop/message_loop_proxy.h" | 13 #include "base/message_loop/message_loop_proxy.h" |
| 14 #include "base/thread_task_runner_handle.h" |
| 14 #include "base/time/time.h" | 15 #include "base/time/time.h" |
| 15 #include "base/win/object_watcher.h" | 16 #include "base/win/object_watcher.h" |
| 16 | 17 |
| 17 namespace base { | 18 namespace base { |
| 18 | 19 |
| 19 namespace { | 20 namespace { |
| 20 | 21 |
| 21 class FilePathWatcherImpl : public FilePathWatcher::PlatformDelegate, | 22 class FilePathWatcherImpl : public FilePathWatcher::PlatformDelegate, |
| 22 public base::win::ObjectWatcher::Delegate, | 23 public base::win::ObjectWatcher::Delegate, |
| 23 public MessageLoop::DestructionObserver { | 24 public MessageLoop::DestructionObserver { |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 Time first_notification_; | 85 Time first_notification_; |
| 85 | 86 |
| 86 DISALLOW_COPY_AND_ASSIGN(FilePathWatcherImpl); | 87 DISALLOW_COPY_AND_ASSIGN(FilePathWatcherImpl); |
| 87 }; | 88 }; |
| 88 | 89 |
| 89 bool FilePathWatcherImpl::Watch(const FilePath& path, | 90 bool FilePathWatcherImpl::Watch(const FilePath& path, |
| 90 bool recursive, | 91 bool recursive, |
| 91 const FilePathWatcher::Callback& callback) { | 92 const FilePathWatcher::Callback& callback) { |
| 92 DCHECK(target_.value().empty()); // Can only watch one path. | 93 DCHECK(target_.value().empty()); // Can only watch one path. |
| 93 | 94 |
| 94 set_message_loop(MessageLoopProxy::current()); | 95 set_task_runner(ThreadTaskRunnerHandle::Get()); |
| 95 callback_ = callback; | 96 callback_ = callback; |
| 96 target_ = path; | 97 target_ = path; |
| 97 recursive_watch_ = recursive; | 98 recursive_watch_ = recursive; |
| 98 MessageLoop::current()->AddDestructionObserver(this); | 99 MessageLoop::current()->AddDestructionObserver(this); |
| 99 | 100 |
| 100 File::Info file_info; | 101 File::Info file_info; |
| 101 if (GetFileInfo(target_, &file_info)) { | 102 if (GetFileInfo(target_, &file_info)) { |
| 102 last_modified_ = file_info.last_modified; | 103 last_modified_ = file_info.last_modified; |
| 103 first_notification_ = Time::Now(); | 104 first_notification_ = Time::Now(); |
| 104 } | 105 } |
| 105 | 106 |
| 106 if (!UpdateWatch()) | 107 if (!UpdateWatch()) |
| 107 return false; | 108 return false; |
| 108 | 109 |
| 109 watcher_.StartWatching(handle_, this); | 110 watcher_.StartWatching(handle_, this); |
| 110 | 111 |
| 111 return true; | 112 return true; |
| 112 } | 113 } |
| 113 | 114 |
| 114 void FilePathWatcherImpl::Cancel() { | 115 void FilePathWatcherImpl::Cancel() { |
| 115 if (callback_.is_null()) { | 116 if (callback_.is_null()) { |
| 116 // Watch was never called, or the |message_loop_| has already quit. | 117 // Watch was never called, or the |message_loop_| has already quit. |
| 117 set_cancelled(); | 118 set_cancelled(); |
| 118 return; | 119 return; |
| 119 } | 120 } |
| 120 | 121 |
| 121 // Switch to the file thread if necessary so we can stop |watcher_|. | 122 // Switch to the file thread if necessary so we can stop |watcher_|. |
| 122 if (!message_loop()->BelongsToCurrentThread()) { | 123 if (!task_runner()->RunsTasksOnCurrentThread()) { |
| 123 message_loop()->PostTask(FROM_HERE, | 124 task_runner()->PostTask( |
| 124 Bind(&FilePathWatcher::CancelWatch, | 125 FROM_HERE, |
| 125 make_scoped_refptr(this))); | 126 Bind(&FilePathWatcher::CancelWatch, make_scoped_refptr(this))); |
| 126 } else { | 127 } else { |
| 127 CancelOnMessageLoopThread(); | 128 CancelOnMessageLoopThread(); |
| 128 } | 129 } |
| 129 } | 130 } |
| 130 | 131 |
| 131 void FilePathWatcherImpl::CancelOnMessageLoopThread() { | 132 void FilePathWatcherImpl::CancelOnMessageLoopThread() { |
| 132 DCHECK(message_loop()->BelongsToCurrentThread()); | 133 DCHECK(task_runner()->RunsTasksOnCurrentThread()); |
| 133 set_cancelled(); | 134 set_cancelled(); |
| 134 | 135 |
| 135 if (handle_ != INVALID_HANDLE_VALUE) | 136 if (handle_ != INVALID_HANDLE_VALUE) |
| 136 DestroyWatch(); | 137 DestroyWatch(); |
| 137 | 138 |
| 138 if (!callback_.is_null()) { | 139 if (!callback_.is_null()) { |
| 139 MessageLoop::current()->RemoveDestructionObserver(this); | 140 MessageLoop::current()->RemoveDestructionObserver(this); |
| 140 callback_.Reset(); | 141 callback_.Reset(); |
| 141 } | 142 } |
| 142 } | 143 } |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 293 handle_ = INVALID_HANDLE_VALUE; | 294 handle_ = INVALID_HANDLE_VALUE; |
| 294 } | 295 } |
| 295 | 296 |
| 296 } // namespace | 297 } // namespace |
| 297 | 298 |
| 298 FilePathWatcher::FilePathWatcher() { | 299 FilePathWatcher::FilePathWatcher() { |
| 299 impl_ = new FilePathWatcherImpl(); | 300 impl_ = new FilePathWatcherImpl(); |
| 300 } | 301 } |
| 301 | 302 |
| 302 } // namespace base | 303 } // namespace base |
| OLD | NEW |