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 #include "testing/gtest/include/gtest/gtest_prod.h" |
12 | 13 |
13 class FilePath; | 14 class FilePath; |
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 // If |recursive| is true, the delegate will be notified for each change |
31 // within the directory tree starting at |path|. Returns false on error. | 33 // within the directory tree starting at |path|. Returns false on error. |
32 // | 34 // |
33 // Note: on Windows you may got more notifications for non-recursive watch | 35 // Note: on Windows you may got more notifications for non-recursive watch |
34 // than you expect, especially on versions earlier than Vista. The behavior | 36 // than you expect, especially on versions earlier than Vista. The behavior |
35 // is consistent on any particular version of Windows, but not across | 37 // is consistent on any particular version of Windows, but not across |
36 // different versions. | 38 // different versions. |
37 bool Watch(const FilePath& path, Delegate* delegate, bool recursive) { | 39 bool Watch(const FilePath& path, Delegate* delegate, bool recursive) { |
38 return impl_->Watch(path, delegate, recursive); | 40 return impl_->Watch(path, delegate, recursive); |
39 } | 41 } |
40 | 42 |
41 // Used internally to encapsulate different members on different platforms. | 43 // Used internally to encapsulate different members on different platforms. |
42 class PlatformDelegate : public base::RefCounted<PlatformDelegate> { | 44 class PlatformDelegate : public base::RefCounted<PlatformDelegate> { |
43 public: | 45 public: |
44 virtual ~PlatformDelegate() {} | 46 virtual ~PlatformDelegate() {} |
45 virtual bool Watch(const FilePath& path, Delegate* delegate, | 47 virtual bool Watch(const FilePath& path, Delegate* delegate, |
46 bool recursive) = 0; | 48 bool recursive) = 0; |
| 49 |
| 50 // This operation will block until all required OS native watches are |
| 51 // set up. |
| 52 virtual void EnsureSetupFinished() {} |
47 }; | 53 }; |
48 | 54 |
49 private: | 55 private: |
| 56 FRIEND_TEST(DirectoryWatcherTest, RecursiveWatchDeletedSubdirectory); |
| 57 FRIEND_TEST(DirectoryWatcherTest, SubDirRecursive); |
| 58 |
| 59 void EnsureSetupFinished() { |
| 60 impl_->EnsureSetupFinished(); |
| 61 } |
| 62 |
50 scoped_refptr<PlatformDelegate> impl_; | 63 scoped_refptr<PlatformDelegate> impl_; |
51 | 64 |
52 DISALLOW_COPY_AND_ASSIGN(DirectoryWatcher); | 65 DISALLOW_COPY_AND_ASSIGN(DirectoryWatcher); |
53 }; | 66 }; |
54 | 67 |
55 #endif // BASE_DIRECTORY_WATCHER_H_ | 68 #endif // BASE_DIRECTORY_WATCHER_H_ |
| 69 |
OLD | NEW |