OLD | NEW |
---|---|
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "chrome/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 scoped_refptr<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 scoped_refptr<base::MessageLoopProxy>) { | |
67 DCHECK(target_.value().empty()); // Can only watch one path. | 72 DCHECK(target_.value().empty()); // Can only watch one path. |
68 delegate_ = delegate; | 73 delegate_ = delegate; |
69 target_ = path; | 74 target_ = path; |
70 | 75 |
71 if (!UpdateWatch()) | 76 if (!UpdateWatch()) |
72 return false; | 77 return false; |
73 | 78 |
74 watcher_.StartWatching(handle_, this); | 79 watcher_.StartWatching(handle_, this); |
75 | 80 |
76 return true; | 81 return true; |
77 } | 82 } |
78 | 83 |
79 void FilePathWatcherImpl::Cancel() { | 84 void FilePathWatcherImpl::Cancel() { |
80 // Switch to the file thread if necessary so we can stop |watcher_|. | 85 // Switch to the file thread if necessary so we can stop |watcher_|. |
81 if (!BrowserThread::CurrentlyOn(BrowserThread::FILE)) { | 86 if (message_loop().get() && |
Mattias Nissler (ping if slow)
2011/03/17 10:37:56
Same comment as for Mac.
dmac
2011/03/17 17:16:48
Done.
| |
82 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 87 !message_loop()->BelongsToCurrentThread()) { |
88 message_loop()->PostTask(FROM_HERE, | |
83 NewRunnableMethod(this, &FilePathWatcherImpl::Cancel)); | 89 NewRunnableMethod(this, &FilePathWatcherImpl::Cancel)); |
84 return; | 90 return; |
85 } | 91 } |
86 | 92 |
87 if (handle_ != INVALID_HANDLE_VALUE) | 93 if (handle_ != INVALID_HANDLE_VALUE) |
88 DestroyWatch(); | 94 DestroyWatch(); |
89 } | 95 } |
90 | 96 |
91 void FilePathWatcherImpl::OnObjectSignaled(HANDLE object) { | 97 void FilePathWatcherImpl::OnObjectSignaled(HANDLE object) { |
92 DCHECK(object == handle_); | 98 DCHECK(object == handle_); |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
235 watcher_.StopWatching(); | 241 watcher_.StopWatching(); |
236 FindCloseChangeNotification(handle_); | 242 FindCloseChangeNotification(handle_); |
237 handle_ = INVALID_HANDLE_VALUE; | 243 handle_ = INVALID_HANDLE_VALUE; |
238 } | 244 } |
239 | 245 |
240 } // namespace | 246 } // namespace |
241 | 247 |
242 FilePathWatcher::FilePathWatcher() { | 248 FilePathWatcher::FilePathWatcher() { |
243 impl_ = new FilePathWatcherImpl(); | 249 impl_ = new FilePathWatcherImpl(); |
244 } | 250 } |
OLD | NEW |