| 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 "content/common/file_path_watcher/file_path_watcher.h" | 5 #include "base/files/file_path_watcher.h" |
| 6 | 6 |
| 7 #include "base/file_path.h" | 7 #include "base/file_path.h" |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
| 11 #include "base/message_loop_proxy.h" | 11 #include "base/message_loop_proxy.h" |
| 12 #include "base/time.h" | 12 #include "base/time.h" |
| 13 #include "base/win/object_watcher.h" | 13 #include "base/win/object_watcher.h" |
| 14 | 14 |
| 15 namespace { | 15 namespace { |
| 16 | 16 |
| 17 class FilePathWatcherImpl : public FilePathWatcher::PlatformDelegate, | 17 class FilePathWatcherImpl |
| 18 public base::win::ObjectWatcher::Delegate, | 18 : public base::files::FilePathWatcher::PlatformDelegate, |
| 19 public MessageLoop::DestructionObserver { | 19 public base::win::ObjectWatcher::Delegate, |
| 20 public MessageLoop::DestructionObserver { |
| 20 public: | 21 public: |
| 21 FilePathWatcherImpl() : delegate_(NULL), handle_(INVALID_HANDLE_VALUE) {} | 22 FilePathWatcherImpl() : delegate_(NULL), handle_(INVALID_HANDLE_VALUE) {} |
| 22 | 23 |
| 23 // FilePathWatcher::PlatformDelegate overrides. | 24 // FilePathWatcher::PlatformDelegate overrides. |
| 24 virtual bool Watch(const FilePath& path, | 25 virtual bool Watch(const FilePath& path, |
| 25 FilePathWatcher::Delegate* delegate) OVERRIDE; | 26 base::files::FilePathWatcher::Delegate* delegate) OVERRIDE; |
| 26 virtual void Cancel() OVERRIDE; | 27 virtual void Cancel() OVERRIDE; |
| 27 | 28 |
| 28 // Deletion of the FilePathWatcher will call Cancel() to dispose of this | 29 // Deletion of the FilePathWatcher will call Cancel() to dispose of this |
| 29 // object in the right thread. This also observes destruction of the required | 30 // object in the right thread. This also observes destruction of the required |
| 30 // cleanup thread, in case it quits before Cancel() is called. | 31 // cleanup thread, in case it quits before Cancel() is called. |
| 31 virtual void WillDestroyCurrentMessageLoop() OVERRIDE; | 32 virtual void WillDestroyCurrentMessageLoop() OVERRIDE; |
| 32 | 33 |
| 33 // Callback from MessageLoopForIO. | 34 // Callback from MessageLoopForIO. |
| 34 virtual void OnObjectSignaled(HANDLE object); | 35 virtual void OnObjectSignaled(HANDLE object); |
| 35 | 36 |
| 36 private: | 37 private: |
| 37 virtual ~FilePathWatcherImpl() {} | 38 virtual ~FilePathWatcherImpl() {} |
| 38 | 39 |
| 39 // Setup a watch handle for directory |dir|. Returns true if no fatal error | 40 // Setup a watch handle for directory |dir|. Returns true if no fatal error |
| 40 // occurs. |handle| will receive the handle value if |dir| is watchable, | 41 // occurs. |handle| will receive the handle value if |dir| is watchable, |
| 41 // otherwise INVALID_HANDLE_VALUE. | 42 // otherwise INVALID_HANDLE_VALUE. |
| 42 static bool SetupWatchHandle(const FilePath& dir, HANDLE* handle) | 43 static bool SetupWatchHandle(const FilePath& dir, HANDLE* handle) |
| 43 WARN_UNUSED_RESULT; | 44 WARN_UNUSED_RESULT; |
| 44 | 45 |
| 45 // (Re-)Initialize the watch handle. | 46 // (Re-)Initialize the watch handle. |
| 46 bool UpdateWatch() WARN_UNUSED_RESULT; | 47 bool UpdateWatch() WARN_UNUSED_RESULT; |
| 47 | 48 |
| 48 // Destroy the watch handle. | 49 // Destroy the watch handle. |
| 49 void DestroyWatch(); | 50 void DestroyWatch(); |
| 50 | 51 |
| 51 // Cleans up and stops observing the |message_loop_| thread. | 52 // Cleans up and stops observing the |message_loop_| thread. |
| 52 void CancelOnMessageLoopThread() OVERRIDE; | 53 void CancelOnMessageLoopThread() OVERRIDE; |
| 53 | 54 |
| 54 // Delegate to notify upon changes. | 55 // Delegate to notify upon changes. |
| 55 scoped_refptr<FilePathWatcher::Delegate> delegate_; | 56 scoped_refptr<base::files::FilePathWatcher::Delegate> delegate_; |
| 56 | 57 |
| 57 // Path we're supposed to watch (passed to delegate). | 58 // Path we're supposed to watch (passed to delegate). |
| 58 FilePath target_; | 59 FilePath target_; |
| 59 | 60 |
| 60 // Handle for FindFirstChangeNotification. | 61 // Handle for FindFirstChangeNotification. |
| 61 HANDLE handle_; | 62 HANDLE handle_; |
| 62 | 63 |
| 63 // ObjectWatcher to watch handle_ for events. | 64 // ObjectWatcher to watch handle_ for events. |
| 64 base::win::ObjectWatcher watcher_; | 65 base::win::ObjectWatcher watcher_; |
| 65 | 66 |
| 66 // Keep track of the last modified time of the file. We use nulltime | 67 // Keep track of the last modified time of the file. We use nulltime |
| 67 // to represent the file not existing. | 68 // to represent the file not existing. |
| 68 base::Time last_modified_; | 69 base::Time last_modified_; |
| 69 | 70 |
| 70 // The time at which we processed the first notification with the | 71 // The time at which we processed the first notification with the |
| 71 // |last_modified_| time stamp. | 72 // |last_modified_| time stamp. |
| 72 base::Time first_notification_; | 73 base::Time first_notification_; |
| 73 | 74 |
| 74 DISALLOW_COPY_AND_ASSIGN(FilePathWatcherImpl); | 75 DISALLOW_COPY_AND_ASSIGN(FilePathWatcherImpl); |
| 75 }; | 76 }; |
| 76 | 77 |
| 77 bool FilePathWatcherImpl::Watch(const FilePath& path, | 78 bool FilePathWatcherImpl::Watch( |
| 78 FilePathWatcher::Delegate* delegate) { | 79 const FilePath& path, |
| 80 base::files::FilePathWatcher::Delegate* delegate) { |
| 79 DCHECK(target_.value().empty()); // Can only watch one path. | 81 DCHECK(target_.value().empty()); // Can only watch one path. |
| 80 | 82 |
| 81 set_message_loop(base::MessageLoopProxy::CreateForCurrentThread()); | 83 set_message_loop(base::MessageLoopProxy::CreateForCurrentThread()); |
| 82 delegate_ = delegate; | 84 delegate_ = delegate; |
| 83 target_ = path; | 85 target_ = path; |
| 84 MessageLoop::current()->AddDestructionObserver(this); | 86 MessageLoop::current()->AddDestructionObserver(this); |
| 85 | 87 |
| 86 if (!UpdateWatch()) | 88 if (!UpdateWatch()) |
| 87 return false; | 89 return false; |
| 88 | 90 |
| 89 watcher_.StartWatching(handle_, this); | 91 watcher_.StartWatching(handle_, this); |
| 90 | 92 |
| 91 return true; | 93 return true; |
| 92 } | 94 } |
| 93 | 95 |
| 94 void FilePathWatcherImpl::Cancel() { | 96 void FilePathWatcherImpl::Cancel() { |
| 95 if (!delegate_) { | 97 if (!delegate_) { |
| 96 // Watch was never called, or the |message_loop_| has already quit. | 98 // Watch was never called, or the |message_loop_| has already quit. |
| 97 set_cancelled(); | 99 set_cancelled(); |
| 98 return; | 100 return; |
| 99 } | 101 } |
| 100 | 102 |
| 101 // Switch to the file thread if necessary so we can stop |watcher_|. | 103 // Switch to the file thread if necessary so we can stop |watcher_|. |
| 102 if (!message_loop()->BelongsToCurrentThread()) { | 104 if (!message_loop()->BelongsToCurrentThread()) { |
| 103 message_loop()->PostTask(FROM_HERE, | 105 message_loop()->PostTask( |
| 104 new FilePathWatcher::CancelTask(this)); | 106 FROM_HERE, |
| 107 new base::files::FilePathWatcher::CancelTask(this)); |
| 105 } else { | 108 } else { |
| 106 CancelOnMessageLoopThread(); | 109 CancelOnMessageLoopThread(); |
| 107 } | 110 } |
| 108 } | 111 } |
| 109 | 112 |
| 110 void FilePathWatcherImpl::CancelOnMessageLoopThread() { | 113 void FilePathWatcherImpl::CancelOnMessageLoopThread() { |
| 111 set_cancelled(); | 114 set_cancelled(); |
| 112 | 115 |
| 113 if (handle_ != INVALID_HANDLE_VALUE) | 116 if (handle_ != INVALID_HANDLE_VALUE) |
| 114 DestroyWatch(); | 117 DestroyWatch(); |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 262 } | 265 } |
| 263 | 266 |
| 264 void FilePathWatcherImpl::DestroyWatch() { | 267 void FilePathWatcherImpl::DestroyWatch() { |
| 265 watcher_.StopWatching(); | 268 watcher_.StopWatching(); |
| 266 FindCloseChangeNotification(handle_); | 269 FindCloseChangeNotification(handle_); |
| 267 handle_ = INVALID_HANDLE_VALUE; | 270 handle_ = INVALID_HANDLE_VALUE; |
| 268 } | 271 } |
| 269 | 272 |
| 270 } // namespace | 273 } // namespace |
| 271 | 274 |
| 272 FilePathWatcher::FilePathWatcher() { | 275 base::files::FilePathWatcher::FilePathWatcher() { |
| 273 impl_ = new FilePathWatcherImpl(); | 276 impl_ = new FilePathWatcherImpl(); |
| 274 } | 277 } |
| OLD | NEW |