OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 // This module provides a way to monitor a directory for changes. | 5 // This module provides a way to monitor a directory for changes. |
6 | 6 |
7 #ifndef BASE_DIRECTORY_WATCHER_H_ | 7 #ifndef BASE_DIRECTORY_WATCHER_H_ |
8 #define BASE_DIRECTORY_WATCHER_H_ | 8 #define BASE_DIRECTORY_WATCHER_H_ |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "base/ref_counted.h" | 11 #include "base/ref_counted.h" |
12 | 12 |
13 class FilePath; | 13 class FilePath; |
| 14 class MessageLoop; |
14 | 15 |
15 // This class lets you register interest in changes on a directory. | 16 // This class lets you register interest in changes on a directory. |
16 // The delegate will get called whenever a file is added or changed in the | 17 // The delegate will get called whenever a file is added or changed in the |
17 // directory. | 18 // directory. |
18 class DirectoryWatcher { | 19 class DirectoryWatcher { |
19 public: | 20 public: |
20 class Delegate { | 21 class Delegate { |
21 public: | 22 public: |
| 23 virtual ~Delegate() {} |
22 virtual void OnDirectoryChanged(const FilePath& path) = 0; | 24 virtual void OnDirectoryChanged(const FilePath& path) = 0; |
23 }; | 25 }; |
24 | 26 |
25 DirectoryWatcher(); | 27 DirectoryWatcher(); |
26 ~DirectoryWatcher() {} | 28 ~DirectoryWatcher() {} |
27 | 29 |
28 // Register interest in any changes in the directory |path|. | 30 // Register interest in any changes in the directory |path|. |
29 // OnDirectoryChanged will be called back for each change within the dir. | 31 // OnDirectoryChanged will be called back for each change within the dir. |
30 // If |recursive| is true, the delegate will be notified for each change | 32 // Any background operations will be ran on |backend_loop|, or inside Watch |
31 // within the directory tree starting at |path|. Returns false on error. | 33 // if |backend_loop| is NULL. If |recursive| is true, the delegate will be |
| 34 // notified for each change within the directory tree starting at |path|. |
| 35 // Returns false on error. |
32 // | 36 // |
33 // Note: on Windows you may got more notifications for non-recursive watch | 37 // Note: on Windows you may got more notifications for non-recursive watch |
34 // than you expect, especially on versions earlier than Vista. The behavior | 38 // than you expect, especially on versions earlier than Vista. The behavior |
35 // is consistent on any particular version of Windows, but not across | 39 // is consistent on any particular version of Windows, but not across |
36 // different versions. | 40 // different versions. |
37 bool Watch(const FilePath& path, Delegate* delegate, bool recursive) { | 41 bool Watch(const FilePath& path, Delegate* delegate, |
38 return impl_->Watch(path, delegate, recursive); | 42 MessageLoop* backend_loop, bool recursive) { |
| 43 return impl_->Watch(path, delegate, backend_loop, recursive); |
39 } | 44 } |
40 | 45 |
41 // Used internally to encapsulate different members on different platforms. | 46 // Used internally to encapsulate different members on different platforms. |
42 class PlatformDelegate : public base::RefCounted<PlatformDelegate> { | 47 class PlatformDelegate : public base::RefCounted<PlatformDelegate> { |
43 public: | 48 public: |
44 virtual ~PlatformDelegate() {} | 49 virtual ~PlatformDelegate() {} |
45 virtual bool Watch(const FilePath& path, Delegate* delegate, | 50 virtual bool Watch(const FilePath& path, Delegate* delegate, |
46 bool recursive) = 0; | 51 MessageLoop* backend_loop, bool recursive) = 0; |
47 }; | 52 }; |
48 | 53 |
49 private: | 54 private: |
50 scoped_refptr<PlatformDelegate> impl_; | 55 scoped_refptr<PlatformDelegate> impl_; |
51 | 56 |
52 DISALLOW_COPY_AND_ASSIGN(DirectoryWatcher); | 57 DISALLOW_COPY_AND_ASSIGN(DirectoryWatcher); |
53 }; | 58 }; |
54 | 59 |
55 #endif // BASE_DIRECTORY_WATCHER_H_ | 60 #endif // BASE_DIRECTORY_WATCHER_H_ |
OLD | NEW |