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/ptr_util.h" |
13 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
14 #include "base/threading/sequenced_task_runner_handle.h" | 15 #include "base/threading/sequenced_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: | 25 public: |
25 FilePathWatcherImpl() | 26 FilePathWatcherImpl() |
26 : handle_(INVALID_HANDLE_VALUE), | 27 : handle_(INVALID_HANDLE_VALUE), |
27 recursive_watch_(false) {} | 28 recursive_watch_(false) {} |
| 29 ~FilePathWatcherImpl() override; |
28 | 30 |
29 // FilePathWatcher::PlatformDelegate: | 31 // FilePathWatcher::PlatformDelegate: |
30 bool Watch(const FilePath& path, | 32 bool Watch(const FilePath& path, |
31 bool recursive, | 33 bool recursive, |
32 const FilePathWatcher::Callback& callback) override; | 34 const FilePathWatcher::Callback& callback) override; |
33 void Cancel() override; | 35 void Cancel() override; |
34 | 36 |
35 // base::win::ObjectWatcher::Delegate: | 37 // base::win::ObjectWatcher::Delegate: |
36 void OnObjectSignaled(HANDLE object) override; | 38 void OnObjectSignaled(HANDLE object) override; |
37 | 39 |
38 private: | 40 private: |
39 ~FilePathWatcherImpl() override {} | |
40 | |
41 // Setup a watch handle for directory |dir|. Set |recursive| to true to watch | 41 // Setup a watch handle for directory |dir|. Set |recursive| to true to watch |
42 // the directory sub trees. Returns true if no fatal error occurs. |handle| | 42 // the directory sub trees. Returns true if no fatal error occurs. |handle| |
43 // will receive the handle value if |dir| is watchable, otherwise | 43 // will receive the handle value if |dir| is watchable, otherwise |
44 // INVALID_HANDLE_VALUE. | 44 // INVALID_HANDLE_VALUE. |
45 static bool SetupWatchHandle(const FilePath& dir, | 45 static bool SetupWatchHandle(const FilePath& dir, |
46 bool recursive, | 46 bool recursive, |
47 HANDLE* handle) WARN_UNUSED_RESULT; | 47 HANDLE* handle) WARN_UNUSED_RESULT; |
48 | 48 |
49 // (Re-)Initialize the watch handle. | 49 // (Re-)Initialize the watch handle. |
50 bool UpdateWatch() WARN_UNUSED_RESULT; | 50 bool UpdateWatch() WARN_UNUSED_RESULT; |
(...skipping 20 matching lines...) Expand all Loading... |
71 // to represent the file not existing. | 71 // to represent the file not existing. |
72 Time last_modified_; | 72 Time last_modified_; |
73 | 73 |
74 // The time at which we processed the first notification with the | 74 // The time at which we processed the first notification with the |
75 // |last_modified_| time stamp. | 75 // |last_modified_| time stamp. |
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 FilePathWatcherImpl::~FilePathWatcherImpl() { |
| 82 DCHECK(!task_runner() || task_runner()->RunsTasksOnCurrentThread()); |
| 83 } |
| 84 |
81 bool FilePathWatcherImpl::Watch(const FilePath& path, | 85 bool FilePathWatcherImpl::Watch(const FilePath& path, |
82 bool recursive, | 86 bool recursive, |
83 const FilePathWatcher::Callback& callback) { | 87 const FilePathWatcher::Callback& callback) { |
84 DCHECK(target_.value().empty()); // Can only watch one path. | 88 DCHECK(target_.value().empty()); // Can only watch one path. |
85 | 89 |
86 set_task_runner(SequencedTaskRunnerHandle::Get()); | 90 set_task_runner(SequencedTaskRunnerHandle::Get()); |
87 callback_ = callback; | 91 callback_ = callback; |
88 target_ = path; | 92 target_ = path; |
89 recursive_watch_ = recursive; | 93 recursive_watch_ = recursive; |
90 | 94 |
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
262 void FilePathWatcherImpl::DestroyWatch() { | 266 void FilePathWatcherImpl::DestroyWatch() { |
263 watcher_.StopWatching(); | 267 watcher_.StopWatching(); |
264 FindCloseChangeNotification(handle_); | 268 FindCloseChangeNotification(handle_); |
265 handle_ = INVALID_HANDLE_VALUE; | 269 handle_ = INVALID_HANDLE_VALUE; |
266 } | 270 } |
267 | 271 |
268 } // namespace | 272 } // namespace |
269 | 273 |
270 FilePathWatcher::FilePathWatcher() { | 274 FilePathWatcher::FilePathWatcher() { |
271 sequence_checker_.DetachFromSequence(); | 275 sequence_checker_.DetachFromSequence(); |
272 impl_ = new FilePathWatcherImpl(); | 276 impl_ = MakeUnique<FilePathWatcherImpl>(); |
273 } | 277 } |
274 | 278 |
275 } // namespace base | 279 } // namespace base |
OLD | NEW |