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_path.h" | 8 #include "base/file_path.h" |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
12 #include "base/message_loop_proxy.h" | 12 #include "base/message_loop_proxy.h" |
13 #include "base/time.h" | 13 #include "base/time.h" |
14 #include "base/win/object_watcher.h" | 14 #include "base/win/object_watcher.h" |
15 | 15 |
16 namespace base { | 16 namespace base { |
17 namespace files { | 17 namespace files { |
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 { |
24 public: | 24 public: |
25 FilePathWatcherImpl() | 25 FilePathWatcherImpl() |
26 : delegate_(NULL), | 26 : handle_(INVALID_HANDLE_VALUE), |
27 handle_(INVALID_HANDLE_VALUE), | |
28 recursive_watch_(false) {} | 27 recursive_watch_(false) {} |
29 | 28 |
30 // FilePathWatcher::PlatformDelegate overrides. | 29 // FilePathWatcher::PlatformDelegate overrides. |
31 virtual bool Watch(const FilePath& path, | 30 virtual bool Watch(const FilePath& path, |
32 bool recursive, | 31 bool recursive, |
33 FilePathWatcher::Delegate* delegate) OVERRIDE; | 32 const FilePathWatcher::Callback& callback) OVERRIDE; |
34 virtual void Cancel() OVERRIDE; | 33 virtual void Cancel() OVERRIDE; |
35 | 34 |
36 // Deletion of the FilePathWatcher will call Cancel() to dispose of this | 35 // Deletion of the FilePathWatcher will call Cancel() to dispose of this |
37 // object in the right thread. This also observes destruction of the required | 36 // object in the right thread. This also observes destruction of the required |
38 // cleanup thread, in case it quits before Cancel() is called. | 37 // cleanup thread, in case it quits before Cancel() is called. |
39 virtual void WillDestroyCurrentMessageLoop() OVERRIDE; | 38 virtual void WillDestroyCurrentMessageLoop() OVERRIDE; |
40 | 39 |
41 // Callback from MessageLoopForIO. | 40 // Callback from MessageLoopForIO. |
42 virtual void OnObjectSignaled(HANDLE object); | 41 virtual void OnObjectSignaled(HANDLE object); |
43 | 42 |
(...skipping 10 matching lines...) Expand all Loading... |
54 | 53 |
55 // (Re-)Initialize the watch handle. | 54 // (Re-)Initialize the watch handle. |
56 bool UpdateWatch() WARN_UNUSED_RESULT; | 55 bool UpdateWatch() WARN_UNUSED_RESULT; |
57 | 56 |
58 // Destroy the watch handle. | 57 // Destroy the watch handle. |
59 void DestroyWatch(); | 58 void DestroyWatch(); |
60 | 59 |
61 // Cleans up and stops observing the |message_loop_| thread. | 60 // Cleans up and stops observing the |message_loop_| thread. |
62 void CancelOnMessageLoopThread() OVERRIDE; | 61 void CancelOnMessageLoopThread() OVERRIDE; |
63 | 62 |
64 // Delegate to notify upon changes. | 63 // Callback to notify upon changes. |
65 scoped_refptr<FilePathWatcher::Delegate> delegate_; | 64 FilePathWatcher::Callback callback_; |
66 | 65 |
67 // Path we're supposed to watch (passed to delegate). | 66 // Path we're supposed to watch (passed to callback). |
68 FilePath target_; | 67 FilePath target_; |
69 | 68 |
70 // Handle for FindFirstChangeNotification. | 69 // Handle for FindFirstChangeNotification. |
71 HANDLE handle_; | 70 HANDLE handle_; |
72 | 71 |
73 // ObjectWatcher to watch handle_ for events. | 72 // ObjectWatcher to watch handle_ for events. |
74 base::win::ObjectWatcher watcher_; | 73 base::win::ObjectWatcher watcher_; |
75 | 74 |
76 // Set to true to watch the sub trees of the specified directory file path. | 75 // Set to true to watch the sub trees of the specified directory file path. |
77 bool recursive_watch_; | 76 bool recursive_watch_; |
78 | 77 |
79 // Keep track of the last modified time of the file. We use nulltime | 78 // Keep track of the last modified time of the file. We use nulltime |
80 // to represent the file not existing. | 79 // to represent the file not existing. |
81 base::Time last_modified_; | 80 base::Time last_modified_; |
82 | 81 |
83 // The time at which we processed the first notification with the | 82 // The time at which we processed the first notification with the |
84 // |last_modified_| time stamp. | 83 // |last_modified_| time stamp. |
85 base::Time first_notification_; | 84 base::Time first_notification_; |
86 | 85 |
87 DISALLOW_COPY_AND_ASSIGN(FilePathWatcherImpl); | 86 DISALLOW_COPY_AND_ASSIGN(FilePathWatcherImpl); |
88 }; | 87 }; |
89 | 88 |
90 bool FilePathWatcherImpl::Watch(const FilePath& path, | 89 bool FilePathWatcherImpl::Watch(const FilePath& path, |
91 bool recursive, | 90 bool recursive, |
92 FilePathWatcher::Delegate* delegate) { | 91 const FilePathWatcher::Callback& callback) { |
93 DCHECK(target_.value().empty()); // Can only watch one path. | 92 DCHECK(target_.value().empty()); // Can only watch one path. |
94 | 93 |
95 set_message_loop(base::MessageLoopProxy::current()); | 94 set_message_loop(base::MessageLoopProxy::current()); |
96 delegate_ = delegate; | 95 callback_ = callback; |
97 target_ = path; | 96 target_ = path; |
98 recursive_watch_ = recursive; | 97 recursive_watch_ = recursive; |
99 MessageLoop::current()->AddDestructionObserver(this); | 98 MessageLoop::current()->AddDestructionObserver(this); |
100 | 99 |
101 if (!UpdateWatch()) | 100 if (!UpdateWatch()) |
102 return false; | 101 return false; |
103 | 102 |
104 watcher_.StartWatching(handle_, this); | 103 watcher_.StartWatching(handle_, this); |
105 | 104 |
106 return true; | 105 return true; |
107 } | 106 } |
108 | 107 |
109 void FilePathWatcherImpl::Cancel() { | 108 void FilePathWatcherImpl::Cancel() { |
110 if (!delegate_) { | 109 if (callback_.is_null()) { |
111 // Watch was never called, or the |message_loop_| has already quit. | 110 // Watch was never called, or the |message_loop_| has already quit. |
112 set_cancelled(); | 111 set_cancelled(); |
113 return; | 112 return; |
114 } | 113 } |
115 | 114 |
116 // Switch to the file thread if necessary so we can stop |watcher_|. | 115 // Switch to the file thread if necessary so we can stop |watcher_|. |
117 if (!message_loop()->BelongsToCurrentThread()) { | 116 if (!message_loop()->BelongsToCurrentThread()) { |
118 message_loop()->PostTask(FROM_HERE, | 117 message_loop()->PostTask(FROM_HERE, |
119 base::Bind(&FilePathWatcher::CancelWatch, | 118 base::Bind(&FilePathWatcher::CancelWatch, |
120 make_scoped_refptr(this))); | 119 make_scoped_refptr(this))); |
121 } else { | 120 } else { |
122 CancelOnMessageLoopThread(); | 121 CancelOnMessageLoopThread(); |
123 } | 122 } |
124 } | 123 } |
125 | 124 |
126 void FilePathWatcherImpl::CancelOnMessageLoopThread() { | 125 void FilePathWatcherImpl::CancelOnMessageLoopThread() { |
127 set_cancelled(); | 126 set_cancelled(); |
128 | 127 |
129 if (handle_ != INVALID_HANDLE_VALUE) | 128 if (handle_ != INVALID_HANDLE_VALUE) |
130 DestroyWatch(); | 129 DestroyWatch(); |
131 | 130 |
132 if (delegate_) { | 131 if (!callback_.is_null()) { |
133 MessageLoop::current()->RemoveDestructionObserver(this); | 132 MessageLoop::current()->RemoveDestructionObserver(this); |
134 delegate_ = NULL; | 133 callback_.Reset(); |
135 } | 134 } |
136 } | 135 } |
137 | 136 |
138 void FilePathWatcherImpl::WillDestroyCurrentMessageLoop() { | 137 void FilePathWatcherImpl::WillDestroyCurrentMessageLoop() { |
139 CancelOnMessageLoopThread(); | 138 CancelOnMessageLoopThread(); |
140 } | 139 } |
141 | 140 |
142 void FilePathWatcherImpl::OnObjectSignaled(HANDLE object) { | 141 void FilePathWatcherImpl::OnObjectSignaled(HANDLE object) { |
143 DCHECK(object == handle_); | 142 DCHECK(object == handle_); |
144 // Make sure we stay alive through the body of this function. | 143 // Make sure we stay alive through the body of this function. |
145 scoped_refptr<FilePathWatcherImpl> keep_alive(this); | 144 scoped_refptr<FilePathWatcherImpl> keep_alive(this); |
146 | 145 |
147 if (!UpdateWatch()) { | 146 if (!UpdateWatch()) { |
148 delegate_->OnFilePathError(target_); | 147 callback_.Run(target_, true /* error */); |
149 return; | 148 return; |
150 } | 149 } |
151 | 150 |
152 // Check whether the event applies to |target_| and notify the delegate. | 151 // Check whether the event applies to |target_| and notify the callback. |
153 base::PlatformFileInfo file_info; | 152 base::PlatformFileInfo file_info; |
154 bool file_exists = file_util::GetFileInfo(target_, &file_info); | 153 bool file_exists = file_util::GetFileInfo(target_, &file_info); |
155 if (file_exists && (last_modified_.is_null() || | 154 if (file_exists && (last_modified_.is_null() || |
156 last_modified_ != file_info.last_modified)) { | 155 last_modified_ != file_info.last_modified)) { |
157 last_modified_ = file_info.last_modified; | 156 last_modified_ = file_info.last_modified; |
158 first_notification_ = base::Time::Now(); | 157 first_notification_ = base::Time::Now(); |
159 delegate_->OnFilePathChanged(target_); | 158 callback_.Run(target_, false); |
160 } else if (file_exists && !first_notification_.is_null()) { | 159 } else if (file_exists && !first_notification_.is_null()) { |
161 // The target's last modification time is equal to what's on record. This | 160 // The target's last modification time is equal to what's on record. This |
162 // means that either an unrelated event occurred, or the target changed | 161 // means that either an unrelated event occurred, or the target changed |
163 // again (file modification times only have a resolution of 1s). Comparing | 162 // again (file modification times only have a resolution of 1s). Comparing |
164 // file modification times against the wall clock is not reliable to find | 163 // file modification times against the wall clock is not reliable to find |
165 // out whether the change is recent, since this code might just run too | 164 // out whether the change is recent, since this code might just run too |
166 // late. Moreover, there's no guarantee that file modification time and wall | 165 // late. Moreover, there's no guarantee that file modification time and wall |
167 // clock times come from the same source. | 166 // clock times come from the same source. |
168 // | 167 // |
169 // Instead, the time at which the first notification carrying the current | 168 // Instead, the time at which the first notification carrying the current |
170 // |last_notified_| time stamp is recorded. Later notifications that find | 169 // |last_notified_| time stamp is recorded. Later notifications that find |
171 // the same file modification time only need to be forwarded until wall | 170 // the same file modification time only need to be forwarded until wall |
172 // clock has advanced one second from the initial notification. After that | 171 // clock has advanced one second from the initial notification. After that |
173 // interval, client code is guaranteed to having seen the current revision | 172 // interval, client code is guaranteed to having seen the current revision |
174 // of the file. | 173 // of the file. |
175 if (base::Time::Now() - first_notification_ > | 174 if (base::Time::Now() - first_notification_ > |
176 base::TimeDelta::FromSeconds(1)) { | 175 base::TimeDelta::FromSeconds(1)) { |
177 // Stop further notifications for this |last_modification_| time stamp. | 176 // Stop further notifications for this |last_modification_| time stamp. |
178 first_notification_ = base::Time(); | 177 first_notification_ = base::Time(); |
179 } | 178 } |
180 delegate_->OnFilePathChanged(target_); | 179 callback_.Run(target_, false); |
181 } else if (!file_exists && !last_modified_.is_null()) { | 180 } else if (!file_exists && !last_modified_.is_null()) { |
182 last_modified_ = base::Time(); | 181 last_modified_ = base::Time(); |
183 delegate_->OnFilePathChanged(target_); | 182 callback_.Run(target_, false); |
184 } | 183 } |
185 | 184 |
186 // The watch may have been cancelled by the callback. | 185 // The watch may have been cancelled by the callback. |
187 if (handle_ != INVALID_HANDLE_VALUE) | 186 if (handle_ != INVALID_HANDLE_VALUE) |
188 watcher_.StartWatching(handle_, this); | 187 watcher_.StartWatching(handle_, this); |
189 } | 188 } |
190 | 189 |
191 // static | 190 // static |
192 bool FilePathWatcherImpl::SetupWatchHandle(const FilePath& dir, | 191 bool FilePathWatcherImpl::SetupWatchHandle(const FilePath& dir, |
193 bool recursive, | 192 bool recursive, |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
286 } | 285 } |
287 | 286 |
288 } // namespace | 287 } // namespace |
289 | 288 |
290 FilePathWatcher::FilePathWatcher() { | 289 FilePathWatcher::FilePathWatcher() { |
291 impl_ = new FilePathWatcherImpl(); | 290 impl_ = new FilePathWatcherImpl(); |
292 } | 291 } |
293 | 292 |
294 } // namespace files | 293 } // namespace files |
295 } // namespace base | 294 } // namespace base |
OLD | NEW |