OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 22 matching lines...) Expand all Loading... |
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() {} | 36 virtual ~Delegate() {} |
37 virtual void OnFilePathChanged(const FilePath& path) = 0; | 37 virtual void OnFilePathChanged(const FilePath& path) = 0; |
38 // Called when platform specific code detected an error. The watcher will | 38 // Called when platform specific code detected an error. The watcher will |
39 // not call OnFilePathChanged for future changes. | 39 // not call OnFilePathChanged for future changes. |
40 virtual void OnFilePathError(const FilePath& path) {} | 40 virtual void OnFilePathError(const FilePath& path) {} |
41 }; | 41 }; |
42 | 42 |
43 FilePathWatcher(); | |
44 ~FilePathWatcher(); | |
45 | |
46 // Register interest in any changes on |path|. OnPathChanged will be called | |
47 // back for each change. Returns true on success. | |
48 // OnFilePathChanged() will be called on the same thread as Watch() is called, | |
49 // which should have a MessageLoop of TYPE_IO. | |
50 bool Watch(const FilePath& path, Delegate* delegate) WARN_UNUSED_RESULT; | |
51 | |
52 class PlatformDelegate; | |
53 | |
54 // A custom Task that always cleans up the PlatformDelegate, either when | |
55 // executed or when deleted without having been executed at all, as can | |
56 // happen during shutdown. | |
57 class CancelTask : public Task { | |
58 public: | |
59 CancelTask(PlatformDelegate* delegate): delegate_(delegate) {} | |
60 virtual ~CancelTask() { | |
61 delegate_->CancelOnMessageLoopThread(); | |
62 } | |
63 | |
64 virtual void Run() OVERRIDE { | |
65 delegate_->CancelOnMessageLoopThread(); | |
66 } | |
67 private: | |
68 scoped_refptr<PlatformDelegate> delegate_; | |
69 | |
70 DISALLOW_COPY_AND_ASSIGN(CancelTask); | |
71 }; | |
72 | |
73 // Used internally to encapsulate different members on different platforms. | 43 // Used internally to encapsulate different members on different platforms. |
| 44 // TODO(jhawkins): Move this into its own file. Also fix the confusing naming |
| 45 // wrt Delegate vs PlatformDelegate. |
74 class PlatformDelegate : public base::RefCountedThreadSafe<PlatformDelegate> { | 46 class PlatformDelegate : public base::RefCountedThreadSafe<PlatformDelegate> { |
75 public: | 47 public: |
76 PlatformDelegate(); | 48 PlatformDelegate(); |
77 | 49 |
78 // Start watching for the given |path| and notify |delegate| about changes. | 50 // Start watching for the given |path| and notify |delegate| about changes. |
79 virtual bool Watch(const FilePath& path, | 51 virtual bool Watch(const FilePath& path, |
80 Delegate* delegate) WARN_UNUSED_RESULT = 0; | 52 Delegate* delegate) WARN_UNUSED_RESULT = 0; |
81 | 53 |
82 // Stop watching. This is called from FilePathWatcher's dtor in order to | 54 // Stop watching. This is called from FilePathWatcher's dtor in order to |
83 // allow to shut down properly while the object is still alive. | 55 // allow to shut down properly while the object is still alive. |
84 // It can be called from any thread. | 56 // It can be called from any thread. |
85 virtual void Cancel() = 0; | 57 virtual void Cancel() = 0; |
86 | 58 |
87 protected: | 59 protected: |
| 60 friend class FilePathWatcher; |
| 61 |
88 virtual ~PlatformDelegate(); | 62 virtual ~PlatformDelegate(); |
89 | 63 |
90 // Stop watching. This is only called on the thread of the appropriate | 64 // Stop watching. This is only called on the thread of the appropriate |
91 // message loop. Since it can also be called more than once, it should | 65 // message loop. Since it can also be called more than once, it should |
92 // check |is_cancelled()| to avoid duplicate work. | 66 // check |is_cancelled()| to avoid duplicate work. |
93 virtual void CancelOnMessageLoopThread() = 0; | 67 virtual void CancelOnMessageLoopThread() = 0; |
94 | 68 |
95 scoped_refptr<base::MessageLoopProxy> message_loop() const { | 69 scoped_refptr<base::MessageLoopProxy> message_loop() const { |
96 return message_loop_; | 70 return message_loop_; |
97 } | 71 } |
98 | 72 |
99 void set_message_loop(base::MessageLoopProxy* loop) { | 73 void set_message_loop(base::MessageLoopProxy* loop) { |
100 message_loop_ = loop; | 74 message_loop_ = loop; |
101 } | 75 } |
102 | 76 |
103 // Must be called before the PlatformDelegate is deleted. | 77 // Must be called before the PlatformDelegate is deleted. |
104 void set_cancelled() { | 78 void set_cancelled() { |
105 cancelled_ = true; | 79 cancelled_ = true; |
106 } | 80 } |
107 | 81 |
108 bool is_cancelled() const { | 82 bool is_cancelled() const { |
109 return cancelled_; | 83 return cancelled_; |
110 } | 84 } |
111 | 85 |
112 private: | 86 private: |
113 friend class base::RefCountedThreadSafe<PlatformDelegate>; | 87 friend class base::RefCountedThreadSafe<PlatformDelegate>; |
114 friend class CancelTask; | |
115 | 88 |
116 scoped_refptr<base::MessageLoopProxy> message_loop_; | 89 scoped_refptr<base::MessageLoopProxy> message_loop_; |
117 bool cancelled_; | 90 bool cancelled_; |
118 }; | 91 }; |
119 | 92 |
| 93 FilePathWatcher(); |
| 94 ~FilePathWatcher(); |
| 95 |
| 96 // 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 |
| 98 // shutdown. |
| 99 static void CancelWatch(const scoped_refptr<PlatformDelegate>& delegate); |
| 100 |
| 101 // Register interest in any changes on |path|. OnPathChanged will be called |
| 102 // back for each change. Returns true on success. |
| 103 // OnFilePathChanged() will be called on the same thread as Watch() is called, |
| 104 // which should have a MessageLoop of TYPE_IO. |
| 105 bool Watch(const FilePath& path, Delegate* delegate) WARN_UNUSED_RESULT; |
| 106 |
120 private: | 107 private: |
121 scoped_refptr<PlatformDelegate> impl_; | 108 scoped_refptr<PlatformDelegate> impl_; |
122 | 109 |
123 DISALLOW_COPY_AND_ASSIGN(FilePathWatcher); | 110 DISALLOW_COPY_AND_ASSIGN(FilePathWatcher); |
124 }; | 111 }; |
125 | 112 |
126 } // namespace files | 113 } // namespace files |
127 } // namespace base | 114 } // namespace base |
128 | 115 |
129 #endif // BASE_FILES_FILE_PATH_WATCHER_H_ | 116 #endif // BASE_FILES_FILE_PATH_WATCHER_H_ |
OLD | NEW |