| 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 "content/common/file_path_watcher/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 : public FilePathWatcher::PlatformDelegate, |
| 18 public base::win::ObjectWatcher::Delegate, | 18 public base::win::ObjectWatcher::Delegate, |
| 19 public MessageLoop::DestructionObserver { | 19 public MessageLoop::DestructionObserver { |
| 20 public: | 20 public: |
| 21 FilePathWatcherImpl() : delegate_(NULL), handle_(INVALID_HANDLE_VALUE) {} | 21 FilePathWatcherImpl() : delegate_(NULL), handle_(INVALID_HANDLE_VALUE) {} |
| 22 | 22 |
| 23 // FilePathWatcher::PlatformDelegate overrides. | 23 // FilePathWatcher::PlatformDelegate overrides. |
| 24 virtual bool Watch(const FilePath& path, | 24 virtual bool Watch(const FilePath& path, |
| 25 FilePathWatcher::Delegate* delegate, | 25 FilePathWatcher::Delegate* delegate) OVERRIDE; |
| 26 base::MessageLoopProxy* loop) OVERRIDE; | |
| 27 virtual void Cancel() OVERRIDE; | 26 virtual void Cancel() OVERRIDE; |
| 28 | 27 |
| 29 // Deletion of the FilePathWatcher will call Cancel() to dispose of this | 28 // Deletion of the FilePathWatcher will call Cancel() to dispose of this |
| 30 // object in the right thread. This also observes destruction of the required | 29 // object in the right thread. This also observes destruction of the required |
| 31 // cleanup thread, in case it quits before Cancel() is called. | 30 // cleanup thread, in case it quits before Cancel() is called. |
| 32 virtual void WillDestroyCurrentMessageLoop() OVERRIDE; | 31 virtual void WillDestroyCurrentMessageLoop() OVERRIDE; |
| 33 | 32 |
| 34 // Callback from MessageLoopForIO. | 33 // Callback from MessageLoopForIO. |
| 35 virtual void OnObjectSignaled(HANDLE object); | 34 virtual void OnObjectSignaled(HANDLE object); |
| 36 | 35 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 69 base::Time last_modified_; | 68 base::Time last_modified_; |
| 70 | 69 |
| 71 // The time at which we processed the first notification with the | 70 // The time at which we processed the first notification with the |
| 72 // |last_modified_| time stamp. | 71 // |last_modified_| time stamp. |
| 73 base::Time first_notification_; | 72 base::Time first_notification_; |
| 74 | 73 |
| 75 DISALLOW_COPY_AND_ASSIGN(FilePathWatcherImpl); | 74 DISALLOW_COPY_AND_ASSIGN(FilePathWatcherImpl); |
| 76 }; | 75 }; |
| 77 | 76 |
| 78 bool FilePathWatcherImpl::Watch(const FilePath& path, | 77 bool FilePathWatcherImpl::Watch(const FilePath& path, |
| 79 FilePathWatcher::Delegate* delegate, | 78 FilePathWatcher::Delegate* delegate) { |
| 80 base::MessageLoopProxy*) { | |
| 81 DCHECK(target_.value().empty()); // Can only watch one path. | 79 DCHECK(target_.value().empty()); // Can only watch one path. |
| 82 | 80 |
| 83 set_message_loop(base::MessageLoopProxy::CreateForCurrentThread()); | 81 set_message_loop(base::MessageLoopProxy::CreateForCurrentThread()); |
| 84 delegate_ = delegate; | 82 delegate_ = delegate; |
| 85 target_ = path; | 83 target_ = path; |
| 86 MessageLoop::current()->AddDestructionObserver(this); | 84 MessageLoop::current()->AddDestructionObserver(this); |
| 87 | 85 |
| 88 if (!UpdateWatch()) | 86 if (!UpdateWatch()) |
| 89 return false; | 87 return false; |
| 90 | 88 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 void FilePathWatcherImpl::WillDestroyCurrentMessageLoop() { | 122 void FilePathWatcherImpl::WillDestroyCurrentMessageLoop() { |
| 125 CancelOnMessageLoopThread(); | 123 CancelOnMessageLoopThread(); |
| 126 } | 124 } |
| 127 | 125 |
| 128 void FilePathWatcherImpl::OnObjectSignaled(HANDLE object) { | 126 void FilePathWatcherImpl::OnObjectSignaled(HANDLE object) { |
| 129 DCHECK(object == handle_); | 127 DCHECK(object == handle_); |
| 130 // Make sure we stay alive through the body of this function. | 128 // Make sure we stay alive through the body of this function. |
| 131 scoped_refptr<FilePathWatcherImpl> keep_alive(this); | 129 scoped_refptr<FilePathWatcherImpl> keep_alive(this); |
| 132 | 130 |
| 133 if (!UpdateWatch()) { | 131 if (!UpdateWatch()) { |
| 134 delegate_->OnError(); | 132 delegate_->OnFilePathError(target_); |
| 135 return; | 133 return; |
| 136 } | 134 } |
| 137 | 135 |
| 138 // Check whether the event applies to |target_| and notify the delegate. | 136 // Check whether the event applies to |target_| and notify the delegate. |
| 139 base::PlatformFileInfo file_info; | 137 base::PlatformFileInfo file_info; |
| 140 bool file_exists = file_util::GetFileInfo(target_, &file_info); | 138 bool file_exists = file_util::GetFileInfo(target_, &file_info); |
| 141 if (file_exists && (last_modified_.is_null() || | 139 if (file_exists && (last_modified_.is_null() || |
| 142 last_modified_ != file_info.last_modified)) { | 140 last_modified_ != file_info.last_modified)) { |
| 143 last_modified_ = file_info.last_modified; | 141 last_modified_ = file_info.last_modified; |
| 144 first_notification_ = base::Time::Now(); | 142 first_notification_ = base::Time::Now(); |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 267 watcher_.StopWatching(); | 265 watcher_.StopWatching(); |
| 268 FindCloseChangeNotification(handle_); | 266 FindCloseChangeNotification(handle_); |
| 269 handle_ = INVALID_HANDLE_VALUE; | 267 handle_ = INVALID_HANDLE_VALUE; |
| 270 } | 268 } |
| 271 | 269 |
| 272 } // namespace | 270 } // namespace |
| 273 | 271 |
| 274 FilePathWatcher::FilePathWatcher() { | 272 FilePathWatcher::FilePathWatcher() { |
| 275 impl_ = new FilePathWatcherImpl(); | 273 impl_ = new FilePathWatcherImpl(); |
| 276 } | 274 } |
| OLD | NEW |