OLD | NEW |
1 // Copyright (c) 2010 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 "chrome/browser/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/message_loop_proxy.h" |
10 #include "base/ref_counted.h" | 11 #include "base/ref_counted.h" |
11 #include "base/time.h" | 12 #include "base/time.h" |
12 #include "base/win/object_watcher.h" | 13 #include "base/win/object_watcher.h" |
13 | 14 |
14 namespace { | 15 namespace { |
15 | 16 |
16 class FilePathWatcherImpl : public FilePathWatcher::PlatformDelegate, | 17 class FilePathWatcherImpl : public FilePathWatcher::PlatformDelegate, |
17 public base::win::ObjectWatcher::Delegate { | 18 public base::win::ObjectWatcher::Delegate { |
18 public: | 19 public: |
19 FilePathWatcherImpl() : delegate_(NULL), handle_(INVALID_HANDLE_VALUE) {} | 20 FilePathWatcherImpl() : delegate_(NULL), handle_(INVALID_HANDLE_VALUE) {} |
20 | 21 |
21 virtual bool Watch(const FilePath& path, FilePathWatcher::Delegate* delegate); | 22 // FilePathWatcher::PlatformDelegate overrides. |
22 virtual void Cancel(); | 23 virtual bool Watch(const FilePath& path, |
| 24 FilePathWatcher::Delegate* delegate, |
| 25 base::MessageLoopProxy* loop) OVERRIDE; |
| 26 virtual void Cancel() OVERRIDE; |
23 | 27 |
24 // Callback from MessageLoopForIO. | 28 // Callback from MessageLoopForIO. |
25 virtual void OnObjectSignaled(HANDLE object); | 29 virtual void OnObjectSignaled(HANDLE object); |
26 | 30 |
27 private: | 31 private: |
28 virtual ~FilePathWatcherImpl(); | 32 virtual ~FilePathWatcherImpl(); |
29 | 33 |
30 // Setup a watch handle for directory |dir|. Returns true if no fatal error | 34 // Setup a watch handle for directory |dir|. Returns true if no fatal error |
31 // occurs. |handle| will receive the handle value if |dir| is watchable, | 35 // occurs. |handle| will receive the handle value if |dir| is watchable, |
32 // otherwise INVALID_HANDLE_VALUE. | 36 // otherwise INVALID_HANDLE_VALUE. |
(...skipping 23 matching lines...) Expand all Loading... |
56 base::Time last_modified_; | 60 base::Time last_modified_; |
57 | 61 |
58 // The time at which we processed the first notification with the | 62 // The time at which we processed the first notification with the |
59 // |last_modified_| time stamp. | 63 // |last_modified_| time stamp. |
60 base::Time first_notification_; | 64 base::Time first_notification_; |
61 | 65 |
62 DISALLOW_COPY_AND_ASSIGN(FilePathWatcherImpl); | 66 DISALLOW_COPY_AND_ASSIGN(FilePathWatcherImpl); |
63 }; | 67 }; |
64 | 68 |
65 bool FilePathWatcherImpl::Watch(const FilePath& path, | 69 bool FilePathWatcherImpl::Watch(const FilePath& path, |
66 FilePathWatcher::Delegate* delegate) { | 70 FilePathWatcher::Delegate* delegate, |
| 71 base::MessageLoopProxy*) { |
67 DCHECK(target_.value().empty()); // Can only watch one path. | 72 DCHECK(target_.value().empty()); // Can only watch one path. |
| 73 |
| 74 set_message_loop(base::MessageLoopProxy::CreateForCurrentThread()); |
68 delegate_ = delegate; | 75 delegate_ = delegate; |
69 target_ = path; | 76 target_ = path; |
70 | 77 |
71 if (!UpdateWatch()) | 78 if (!UpdateWatch()) |
72 return false; | 79 return false; |
73 | 80 |
74 watcher_.StartWatching(handle_, this); | 81 watcher_.StartWatching(handle_, this); |
75 | 82 |
76 return true; | 83 return true; |
77 } | 84 } |
78 | 85 |
79 void FilePathWatcherImpl::Cancel() { | 86 void FilePathWatcherImpl::Cancel() { |
| 87 if (!message_loop().get()) { |
| 88 // Watch was never called, so exit. |
| 89 return; |
| 90 } |
| 91 |
80 // Switch to the file thread if necessary so we can stop |watcher_|. | 92 // Switch to the file thread if necessary so we can stop |watcher_|. |
81 if (!BrowserThread::CurrentlyOn(BrowserThread::FILE)) { | 93 if (!message_loop()->BelongsToCurrentThread()) { |
82 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 94 message_loop()->PostTask(FROM_HERE, |
83 NewRunnableMethod(this, &FilePathWatcherImpl::Cancel)); | 95 NewRunnableMethod(this, &FilePathWatcherImpl::Cancel)); |
84 return; | 96 return; |
85 } | 97 } |
86 | 98 |
87 if (handle_ != INVALID_HANDLE_VALUE) | 99 if (handle_ != INVALID_HANDLE_VALUE) |
88 DestroyWatch(); | 100 DestroyWatch(); |
89 } | 101 } |
90 | 102 |
91 void FilePathWatcherImpl::OnObjectSignaled(HANDLE object) { | 103 void FilePathWatcherImpl::OnObjectSignaled(HANDLE object) { |
92 DCHECK(object == handle_); | 104 DCHECK(object == handle_); |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
235 watcher_.StopWatching(); | 247 watcher_.StopWatching(); |
236 FindCloseChangeNotification(handle_); | 248 FindCloseChangeNotification(handle_); |
237 handle_ = INVALID_HANDLE_VALUE; | 249 handle_ = INVALID_HANDLE_VALUE; |
238 } | 250 } |
239 | 251 |
240 } // namespace | 252 } // namespace |
241 | 253 |
242 FilePathWatcher::FilePathWatcher() { | 254 FilePathWatcher::FilePathWatcher() { |
243 impl_ = new FilePathWatcherImpl(); | 255 impl_ = new FilePathWatcherImpl(); |
244 } | 256 } |
OLD | NEW |