| 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/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 14 #include "base/threading/thread_task_runner_handle.h" | 14 #include "base/threading/sequenced_task_runner_handle.h" |
| 15 #include "base/time/time.h" | 15 #include "base/time/time.h" |
| 16 #include "base/win/object_watcher.h" | 16 #include "base/win/object_watcher.h" |
| 17 | 17 |
| 18 namespace base { | 18 namespace base { |
| 19 | 19 |
| 20 namespace { | 20 namespace { |
| 21 | 21 |
| 22 class FilePathWatcherImpl : public FilePathWatcher::PlatformDelegate, | 22 class FilePathWatcherImpl : public FilePathWatcher::PlatformDelegate, |
| 23 public base::win::ObjectWatcher::Delegate { | 23 public base::win::ObjectWatcher::Delegate { |
| 24 public: | 24 public: |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 Time first_notification_; | 76 Time first_notification_; |
| 77 | 77 |
| 78 DISALLOW_COPY_AND_ASSIGN(FilePathWatcherImpl); | 78 DISALLOW_COPY_AND_ASSIGN(FilePathWatcherImpl); |
| 79 }; | 79 }; |
| 80 | 80 |
| 81 bool FilePathWatcherImpl::Watch(const FilePath& path, | 81 bool FilePathWatcherImpl::Watch(const FilePath& path, |
| 82 bool recursive, | 82 bool recursive, |
| 83 const FilePathWatcher::Callback& callback) { | 83 const FilePathWatcher::Callback& callback) { |
| 84 DCHECK(target_.value().empty()); // Can only watch one path. | 84 DCHECK(target_.value().empty()); // Can only watch one path. |
| 85 | 85 |
| 86 set_task_runner(ThreadTaskRunnerHandle::Get()); | 86 set_task_runner(SequencedTaskRunnerHandle::Get()); |
| 87 callback_ = callback; | 87 callback_ = callback; |
| 88 target_ = path; | 88 target_ = path; |
| 89 recursive_watch_ = recursive; | 89 recursive_watch_ = recursive; |
| 90 | 90 |
| 91 File::Info file_info; | 91 File::Info file_info; |
| 92 if (GetFileInfo(target_, &file_info)) { | 92 if (GetFileInfo(target_, &file_info)) { |
| 93 last_modified_ = file_info.last_modified; | 93 last_modified_ = file_info.last_modified; |
| 94 first_notification_ = Time::Now(); | 94 first_notification_ = Time::Now(); |
| 95 } | 95 } |
| 96 | 96 |
| 97 if (!UpdateWatch()) | 97 if (!UpdateWatch()) |
| 98 return false; | 98 return false; |
| 99 | 99 |
| 100 watcher_.StartWatchingOnce(handle_, this); | 100 watcher_.StartWatchingOnce(handle_, this); |
| 101 | 101 |
| 102 return true; | 102 return true; |
| 103 } | 103 } |
| 104 | 104 |
| 105 void FilePathWatcherImpl::Cancel() { | 105 void FilePathWatcherImpl::Cancel() { |
| 106 if (callback_.is_null()) { | 106 if (callback_.is_null()) { |
| 107 // Watch was never called, or the |task_runner_| has already quit. | 107 // Watch was never called, or the |task_runner_| has already quit. |
| 108 set_cancelled(); | 108 set_cancelled(); |
| 109 return; | 109 return; |
| 110 } | 110 } |
| 111 | 111 |
| 112 DCHECK(task_runner()->BelongsToCurrentThread()); | 112 DCHECK(task_runner()->RunsTasksOnCurrentThread()); |
| 113 set_cancelled(); | 113 set_cancelled(); |
| 114 | 114 |
| 115 if (handle_ != INVALID_HANDLE_VALUE) | 115 if (handle_ != INVALID_HANDLE_VALUE) |
| 116 DestroyWatch(); | 116 DestroyWatch(); |
| 117 | 117 |
| 118 callback_.Reset(); | 118 callback_.Reset(); |
| 119 } | 119 } |
| 120 | 120 |
| 121 void FilePathWatcherImpl::OnObjectSignaled(HANDLE object) { | 121 void FilePathWatcherImpl::OnObjectSignaled(HANDLE object) { |
| 122 DCHECK(object == handle_); | 122 DCHECK(object == handle_); |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 266 } | 266 } |
| 267 | 267 |
| 268 } // namespace | 268 } // namespace |
| 269 | 269 |
| 270 FilePathWatcher::FilePathWatcher() { | 270 FilePathWatcher::FilePathWatcher() { |
| 271 sequence_checker_.DetachFromSequence(); | 271 sequence_checker_.DetachFromSequence(); |
| 272 impl_ = new FilePathWatcherImpl(); | 272 impl_ = new FilePathWatcherImpl(); |
| 273 } | 273 } |
| 274 | 274 |
| 275 } // namespace base | 275 } // namespace base |
| OLD | NEW |