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 "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 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 if (!message_loop().get()) { |
81 if (!BrowserThread::CurrentlyOn(BrowserThread::FILE)) { | 86 // Watch was never called, so exit. |
82 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 87 return; |
88 } | |
89 // Switch to the file thread if necessary so we can stop |watcher_|. | |
Mattias Nissler (ping if slow)
2011/03/17 18:14:27
indentation
| |
90 if (!message_loop()->BelongsToCurrentThread()) { | |
91 message_loop()->PostTask(FROM_HERE, | |
83 NewRunnableMethod(this, &FilePathWatcherImpl::Cancel)); | 92 NewRunnableMethod(this, &FilePathWatcherImpl::Cancel)); |
84 return; | 93 return; |
85 } | 94 } |
86 | 95 |
87 if (handle_ != INVALID_HANDLE_VALUE) | 96 if (handle_ != INVALID_HANDLE_VALUE) |
88 DestroyWatch(); | 97 DestroyWatch(); |
89 } | 98 } |
90 | 99 |
91 void FilePathWatcherImpl::OnObjectSignaled(HANDLE object) { | 100 void FilePathWatcherImpl::OnObjectSignaled(HANDLE object) { |
92 DCHECK(object == handle_); | 101 DCHECK(object == handle_); |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
235 watcher_.StopWatching(); | 244 watcher_.StopWatching(); |
236 FindCloseChangeNotification(handle_); | 245 FindCloseChangeNotification(handle_); |
237 handle_ = INVALID_HANDLE_VALUE; | 246 handle_ = INVALID_HANDLE_VALUE; |
238 } | 247 } |
239 | 248 |
240 } // namespace | 249 } // namespace |
241 | 250 |
242 FilePathWatcher::FilePathWatcher() { | 251 FilePathWatcher::FilePathWatcher() { |
243 impl_ = new FilePathWatcherImpl(); | 252 impl_ = new FilePathWatcherImpl(); |
244 } | 253 } |
OLD | NEW |