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