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/thread_task_runner_handle.h" |
14 #include "base/time/time.h" | 14 #include "base/time/time.h" |
15 #include "base/win/object_watcher.h" | 15 #include "base/win/object_watcher.h" |
16 | 16 |
17 namespace base { | 17 namespace base { |
18 | 18 |
19 namespace { | 19 namespace { |
20 | 20 |
21 class FilePathWatcherImpl : public FilePathWatcher::PlatformDelegate, | 21 class FilePathWatcherImpl : public FilePathWatcher::PlatformDelegate, |
22 public base::win::ObjectWatcher::Delegate, | 22 public base::win::ObjectWatcher::Delegate, |
23 public MessageLoop::DestructionObserver { | 23 public MessageLoop::DestructionObserver { |
(...skipping 26 matching lines...) Expand all Loading... |
50 static bool SetupWatchHandle(const FilePath& dir, | 50 static bool SetupWatchHandle(const FilePath& dir, |
51 bool recursive, | 51 bool recursive, |
52 HANDLE* handle) WARN_UNUSED_RESULT; | 52 HANDLE* handle) WARN_UNUSED_RESULT; |
53 | 53 |
54 // (Re-)Initialize the watch handle. | 54 // (Re-)Initialize the watch handle. |
55 bool UpdateWatch() WARN_UNUSED_RESULT; | 55 bool UpdateWatch() WARN_UNUSED_RESULT; |
56 | 56 |
57 // Destroy the watch handle. | 57 // Destroy the watch handle. |
58 void DestroyWatch(); | 58 void DestroyWatch(); |
59 | 59 |
60 // Cleans up and stops observing the |message_loop_| thread. | 60 // Cleans up and stops observing the |task_runner_| thread. |
61 void CancelOnMessageLoopThread() override; | 61 void CancelOnMessageLoopThread() override; |
62 | 62 |
63 // Callback to notify upon changes. | 63 // Callback to notify upon changes. |
64 FilePathWatcher::Callback callback_; | 64 FilePathWatcher::Callback callback_; |
65 | 65 |
66 // Path we're supposed to watch (passed to callback). | 66 // Path we're supposed to watch (passed to callback). |
67 FilePath target_; | 67 FilePath target_; |
68 | 68 |
69 // Handle for FindFirstChangeNotification. | 69 // Handle for FindFirstChangeNotification. |
70 HANDLE handle_; | 70 HANDLE handle_; |
(...skipping 13 matching lines...) Expand all Loading... |
84 Time first_notification_; | 84 Time first_notification_; |
85 | 85 |
86 DISALLOW_COPY_AND_ASSIGN(FilePathWatcherImpl); | 86 DISALLOW_COPY_AND_ASSIGN(FilePathWatcherImpl); |
87 }; | 87 }; |
88 | 88 |
89 bool FilePathWatcherImpl::Watch(const FilePath& path, | 89 bool FilePathWatcherImpl::Watch(const FilePath& path, |
90 bool recursive, | 90 bool recursive, |
91 const FilePathWatcher::Callback& callback) { | 91 const FilePathWatcher::Callback& callback) { |
92 DCHECK(target_.value().empty()); // Can only watch one path. | 92 DCHECK(target_.value().empty()); // Can only watch one path. |
93 | 93 |
94 set_message_loop(MessageLoopProxy::current()); | 94 set_task_runner(ThreadTaskRunnerHandle::Get()); |
95 callback_ = callback; | 95 callback_ = callback; |
96 target_ = path; | 96 target_ = path; |
97 recursive_watch_ = recursive; | 97 recursive_watch_ = recursive; |
98 MessageLoop::current()->AddDestructionObserver(this); | 98 MessageLoop::current()->AddDestructionObserver(this); |
99 | 99 |
100 File::Info file_info; | 100 File::Info file_info; |
101 if (GetFileInfo(target_, &file_info)) { | 101 if (GetFileInfo(target_, &file_info)) { |
102 last_modified_ = file_info.last_modified; | 102 last_modified_ = file_info.last_modified; |
103 first_notification_ = Time::Now(); | 103 first_notification_ = Time::Now(); |
104 } | 104 } |
105 | 105 |
106 if (!UpdateWatch()) | 106 if (!UpdateWatch()) |
107 return false; | 107 return false; |
108 | 108 |
109 watcher_.StartWatching(handle_, this); | 109 watcher_.StartWatching(handle_, this); |
110 | 110 |
111 return true; | 111 return true; |
112 } | 112 } |
113 | 113 |
114 void FilePathWatcherImpl::Cancel() { | 114 void FilePathWatcherImpl::Cancel() { |
115 if (callback_.is_null()) { | 115 if (callback_.is_null()) { |
116 // Watch was never called, or the |message_loop_| has already quit. | 116 // Watch was never called, or the |task_runner_| has already quit. |
117 set_cancelled(); | 117 set_cancelled(); |
118 return; | 118 return; |
119 } | 119 } |
120 | 120 |
121 // Switch to the file thread if necessary so we can stop |watcher_|. | 121 // Switch to the file thread if necessary so we can stop |watcher_|. |
122 if (!message_loop()->BelongsToCurrentThread()) { | 122 if (!task_runner()->BelongsToCurrentThread()) { |
123 message_loop()->PostTask(FROM_HERE, | 123 task_runner()->PostTask(FROM_HERE, Bind(&FilePathWatcher::CancelWatch, |
124 Bind(&FilePathWatcher::CancelWatch, | 124 make_scoped_refptr(this))); |
125 make_scoped_refptr(this))); | |
126 } else { | 125 } else { |
127 CancelOnMessageLoopThread(); | 126 CancelOnMessageLoopThread(); |
128 } | 127 } |
129 } | 128 } |
130 | 129 |
131 void FilePathWatcherImpl::CancelOnMessageLoopThread() { | 130 void FilePathWatcherImpl::CancelOnMessageLoopThread() { |
132 DCHECK(message_loop()->BelongsToCurrentThread()); | 131 DCHECK(task_runner()->BelongsToCurrentThread()); |
133 set_cancelled(); | 132 set_cancelled(); |
134 | 133 |
135 if (handle_ != INVALID_HANDLE_VALUE) | 134 if (handle_ != INVALID_HANDLE_VALUE) |
136 DestroyWatch(); | 135 DestroyWatch(); |
137 | 136 |
138 if (!callback_.is_null()) { | 137 if (!callback_.is_null()) { |
139 MessageLoop::current()->RemoveDestructionObserver(this); | 138 MessageLoop::current()->RemoveDestructionObserver(this); |
140 callback_.Reset(); | 139 callback_.Reset(); |
141 } | 140 } |
142 } | 141 } |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
292 handle_ = INVALID_HANDLE_VALUE; | 291 handle_ = INVALID_HANDLE_VALUE; |
293 } | 292 } |
294 | 293 |
295 } // namespace | 294 } // namespace |
296 | 295 |
297 FilePathWatcher::FilePathWatcher() { | 296 FilePathWatcher::FilePathWatcher() { |
298 impl_ = new FilePathWatcherImpl(); | 297 impl_ = new FilePathWatcherImpl(); |
299 } | 298 } |
300 | 299 |
301 } // namespace base | 300 } // namespace base |
OLD | NEW |