OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 file or directory for changes. | 5 // This module provides a way to monitor a file or directory for changes. |
6 | 6 |
7 #ifndef BASE_FILES_FILE_PATH_WATCHER_H_ | 7 #ifndef BASE_FILES_FILE_PATH_WATCHER_H_ |
8 #define BASE_FILES_FILE_PATH_WATCHER_H_ | 8 #define BASE_FILES_FILE_PATH_WATCHER_H_ |
9 #pragma once | 9 #pragma once |
10 | 10 |
(...skipping 15 matching lines...) Expand all Loading... |
26 // detect the creation and deletion of files in a watched directory, but will | 26 // detect the creation and deletion of files in a watched directory, but will |
27 // not detect modifications to those files. See file_path_watcher_mac.cc for | 27 // not detect modifications to those files. See file_path_watcher_mac.cc for |
28 // details. | 28 // details. |
29 class BASE_EXPORT FilePathWatcher { | 29 class BASE_EXPORT FilePathWatcher { |
30 public: | 30 public: |
31 // Declares the callback client code implements to receive notifications. Note | 31 // Declares the callback client code implements to receive notifications. Note |
32 // that implementations of this interface should not keep a reference to the | 32 // that implementations of this interface should not keep a reference to the |
33 // corresponding FileWatcher object to prevent a reference cycle. | 33 // corresponding FileWatcher object to prevent a reference cycle. |
34 class Delegate : public base::RefCountedThreadSafe<Delegate> { | 34 class Delegate : public base::RefCountedThreadSafe<Delegate> { |
35 public: | 35 public: |
36 virtual ~Delegate() {} | |
37 virtual void OnFilePathChanged(const FilePath& path) = 0; | 36 virtual void OnFilePathChanged(const FilePath& path) = 0; |
38 // Called when platform specific code detected an error. The watcher will | 37 // Called when platform specific code detected an error. The watcher will |
39 // not call OnFilePathChanged for future changes. | 38 // not call OnFilePathChanged for future changes. |
40 virtual void OnFilePathError(const FilePath& path) {} | 39 virtual void OnFilePathError(const FilePath& path) {} |
| 40 |
| 41 protected: |
| 42 friend class base::RefCountedThreadSafe<Delegate>; |
| 43 virtual ~Delegate() {} |
41 }; | 44 }; |
42 | 45 |
43 // Used internally to encapsulate different members on different platforms. | 46 // Used internally to encapsulate different members on different platforms. |
44 // TODO(jhawkins): Move this into its own file. Also fix the confusing naming | 47 // TODO(jhawkins): Move this into its own file. Also fix the confusing naming |
45 // wrt Delegate vs PlatformDelegate. | 48 // wrt Delegate vs PlatformDelegate. |
46 class PlatformDelegate : public base::RefCountedThreadSafe<PlatformDelegate> { | 49 class PlatformDelegate : public base::RefCountedThreadSafe<PlatformDelegate> { |
47 public: | 50 public: |
48 PlatformDelegate(); | 51 PlatformDelegate(); |
49 | 52 |
50 // Start watching for the given |path| and notify |delegate| about changes. | 53 // Start watching for the given |path| and notify |delegate| about changes. |
51 virtual bool Watch(const FilePath& path, | 54 virtual bool Watch(const FilePath& path, |
52 Delegate* delegate) WARN_UNUSED_RESULT = 0; | 55 Delegate* delegate) WARN_UNUSED_RESULT = 0; |
53 | 56 |
54 // Stop watching. This is called from FilePathWatcher's dtor in order to | 57 // Stop watching. This is called from FilePathWatcher's dtor in order to |
55 // allow to shut down properly while the object is still alive. | 58 // allow to shut down properly while the object is still alive. |
56 // It can be called from any thread. | 59 // It can be called from any thread. |
57 virtual void Cancel() = 0; | 60 virtual void Cancel() = 0; |
58 | 61 |
59 protected: | 62 protected: |
| 63 friend class base::RefCountedThreadSafe<PlatformDelegate>; |
60 friend class FilePathWatcher; | 64 friend class FilePathWatcher; |
61 | 65 |
62 virtual ~PlatformDelegate(); | 66 virtual ~PlatformDelegate(); |
63 | 67 |
64 // Stop watching. This is only called on the thread of the appropriate | 68 // Stop watching. This is only called on the thread of the appropriate |
65 // message loop. Since it can also be called more than once, it should | 69 // message loop. Since it can also be called more than once, it should |
66 // check |is_cancelled()| to avoid duplicate work. | 70 // check |is_cancelled()| to avoid duplicate work. |
67 virtual void CancelOnMessageLoopThread() = 0; | 71 virtual void CancelOnMessageLoopThread() = 0; |
68 | 72 |
69 scoped_refptr<base::MessageLoopProxy> message_loop() const { | 73 scoped_refptr<base::MessageLoopProxy> message_loop() const { |
70 return message_loop_; | 74 return message_loop_; |
71 } | 75 } |
72 | 76 |
73 void set_message_loop(base::MessageLoopProxy* loop) { | 77 void set_message_loop(base::MessageLoopProxy* loop) { |
74 message_loop_ = loop; | 78 message_loop_ = loop; |
75 } | 79 } |
76 | 80 |
77 // Must be called before the PlatformDelegate is deleted. | 81 // Must be called before the PlatformDelegate is deleted. |
78 void set_cancelled() { | 82 void set_cancelled() { |
79 cancelled_ = true; | 83 cancelled_ = true; |
80 } | 84 } |
81 | 85 |
82 bool is_cancelled() const { | 86 bool is_cancelled() const { |
83 return cancelled_; | 87 return cancelled_; |
84 } | 88 } |
85 | 89 |
86 private: | 90 private: |
87 friend class base::RefCountedThreadSafe<PlatformDelegate>; | |
88 | |
89 scoped_refptr<base::MessageLoopProxy> message_loop_; | 91 scoped_refptr<base::MessageLoopProxy> message_loop_; |
90 bool cancelled_; | 92 bool cancelled_; |
91 }; | 93 }; |
92 | 94 |
93 FilePathWatcher(); | 95 FilePathWatcher(); |
94 virtual ~FilePathWatcher(); | 96 virtual ~FilePathWatcher(); |
95 | 97 |
96 // A callback that always cleans up the PlatformDelegate, either when executed | 98 // A callback that always cleans up the PlatformDelegate, either when executed |
97 // or when deleted without having been executed at all, as can happen during | 99 // or when deleted without having been executed at all, as can happen during |
98 // shutdown. | 100 // shutdown. |
99 static void CancelWatch(const scoped_refptr<PlatformDelegate>& delegate); | 101 static void CancelWatch(const scoped_refptr<PlatformDelegate>& delegate); |
100 | 102 |
101 // Register interest in any changes on |path|. OnPathChanged will be called | 103 // Register interest in any changes on |path|. OnPathChanged will be called |
102 // back for each change. Returns true on success. | 104 // back for each change. Returns true on success. |
103 // OnFilePathChanged() will be called on the same thread as Watch() is called, | 105 // OnFilePathChanged() will be called on the same thread as Watch() is called, |
104 // which should have a MessageLoop of TYPE_IO. | 106 // which should have a MessageLoop of TYPE_IO. |
105 virtual bool Watch(const FilePath& path, Delegate* delegate) | 107 virtual bool Watch(const FilePath& path, Delegate* delegate) |
106 WARN_UNUSED_RESULT; | 108 WARN_UNUSED_RESULT; |
107 | 109 |
108 private: | 110 private: |
109 scoped_refptr<PlatformDelegate> impl_; | 111 scoped_refptr<PlatformDelegate> impl_; |
110 | 112 |
111 DISALLOW_COPY_AND_ASSIGN(FilePathWatcher); | 113 DISALLOW_COPY_AND_ASSIGN(FilePathWatcher); |
112 }; | 114 }; |
113 | 115 |
114 } // namespace files | 116 } // namespace files |
115 } // namespace base | 117 } // namespace base |
116 | 118 |
117 #endif // BASE_FILES_FILE_PATH_WATCHER_H_ | 119 #endif // BASE_FILES_FILE_PATH_WATCHER_H_ |
OLD | NEW |