[email protected] | b8ca91f | 2011-03-18 04:11:49 | [diff] [blame] | 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 | ||||
3 | // found in the LICENSE file. | ||||
4 | |||||
5 | // This module provides a way to monitor a file or directory for changes. | ||||
6 | |||||
[email protected] | 493c800 | 2011-04-14 16:56:01 | [diff] [blame] | 7 | #ifndef BASE_FILES_FILE_PATH_WATCHER_H_ |
8 | #define BASE_FILES_FILE_PATH_WATCHER_H_ | ||||
[email protected] | b8ca91f | 2011-03-18 04:11:49 | [diff] [blame] | 9 | #pragma once |
10 | |||||
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 11 | #include "base/base_export.h" |
[email protected] | b8ca91f | 2011-03-18 04:11:49 | [diff] [blame] | 12 | #include "base/basictypes.h" |
13 | #include "base/file_path.h" | ||||
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 14 | #include "base/memory/ref_counted.h" |
[email protected] | b8ca91f | 2011-03-18 04:11:49 | [diff] [blame] | 15 | #include "base/message_loop_proxy.h" |
[email protected] | b8ca91f | 2011-03-18 04:11:49 | [diff] [blame] | 16 | |
[email protected] | 493c800 | 2011-04-14 16:56:01 | [diff] [blame] | 17 | namespace base { |
18 | namespace files { | ||||
19 | |||||
[email protected] | b8ca91f | 2011-03-18 04:11:49 | [diff] [blame] | 20 | // This class lets you register interest in changes on a FilePath. |
21 | // The delegate will get called whenever the file or directory referenced by the | ||||
22 | // FilePath is changed, including created or deleted. Due to limitations in the | ||||
[email protected] | 7b3ee8b | 2011-04-01 18:48:19 | [diff] [blame] | 23 | // underlying OS APIs, FilePathWatcher has slightly different semantics on OS X |
24 | // than on Windows or Linux. FilePathWatcher on Linux and Windows will detect | ||||
25 | // modifications to files in a watched directory. FilePathWatcher on Mac 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 | ||||
28 | // details. | ||||
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 29 | class BASE_EXPORT FilePathWatcher { |
[email protected] | b8ca91f | 2011-03-18 04:11:49 | [diff] [blame] | 30 | public: |
31 | // Declares the callback client code implements to receive notifications. Note | ||||
32 | // that implementations of this interface should not keep a reference to the | ||||
33 | // corresponding FileWatcher object to prevent a reference cycle. | ||||
34 | class Delegate : public base::RefCountedThreadSafe<Delegate> { | ||||
35 | public: | ||||
36 | virtual ~Delegate() {} | ||||
37 | virtual void OnFilePathChanged(const FilePath& path) = 0; | ||||
38 | // Called when platform specific code detected an error. The watcher will | ||||
39 | // not call OnFilePathChanged for future changes. | ||||
[email protected] | 7b3ee8b | 2011-04-01 18:48:19 | [diff] [blame] | 40 | virtual void OnFilePathError(const FilePath& path) {} |
[email protected] | b8ca91f | 2011-03-18 04:11:49 | [diff] [blame] | 41 | }; |
42 | |||||
[email protected] | b8ca91f | 2011-03-18 04:11:49 | [diff] [blame] | 43 | // Used internally to encapsulate different members on different platforms. |
[email protected] | 29ec5c1 | 2011-11-23 20:44:00 | [diff] [blame^] | 44 | // TODO(jhawkins): Move this into its own file. Also fix the confusing naming |
45 | // wrt Delegate vs PlatformDelegate. | ||||
[email protected] | d64abe1 | 2011-03-22 20:14:06 | [diff] [blame] | 46 | class PlatformDelegate : public base::RefCountedThreadSafe<PlatformDelegate> { |
[email protected] | b8ca91f | 2011-03-18 04:11:49 | [diff] [blame] | 47 | public: |
[email protected] | 55f2c516 | 2011-03-18 04:28:24 | [diff] [blame] | 48 | PlatformDelegate(); |
49 | |||||
[email protected] | b8ca91f | 2011-03-18 04:11:49 | [diff] [blame] | 50 | // Start watching for the given |path| and notify |delegate| about changes. |
[email protected] | 7b3ee8b | 2011-04-01 18:48:19 | [diff] [blame] | 51 | virtual bool Watch(const FilePath& path, |
52 | Delegate* delegate) WARN_UNUSED_RESULT = 0; | ||||
[email protected] | b8ca91f | 2011-03-18 04:11:49 | [diff] [blame] | 53 | |
54 | // Stop watching. This is called from FilePathWatcher's dtor in order to | ||||
55 | // allow to shut down properly while the object is still alive. | ||||
[email protected] | d64abe1 | 2011-03-22 20:14:06 | [diff] [blame] | 56 | // It can be called from any thread. |
[email protected] | b8ca91f | 2011-03-18 04:11:49 | [diff] [blame] | 57 | virtual void Cancel() = 0; |
58 | |||||
[email protected] | b8ca91f | 2011-03-18 04:11:49 | [diff] [blame] | 59 | protected: |
[email protected] | 29ec5c1 | 2011-11-23 20:44:00 | [diff] [blame^] | 60 | friend class FilePathWatcher; |
61 | |||||
[email protected] | b8ca91f | 2011-03-18 04:11:49 | [diff] [blame] | 62 | virtual ~PlatformDelegate(); |
63 | |||||
[email protected] | d64abe1 | 2011-03-22 20:14:06 | [diff] [blame] | 64 | // 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 | ||||
66 | // check |is_cancelled()| to avoid duplicate work. | ||||
67 | virtual void CancelOnMessageLoopThread() = 0; | ||||
68 | |||||
[email protected] | b8ca91f | 2011-03-18 04:11:49 | [diff] [blame] | 69 | scoped_refptr<base::MessageLoopProxy> message_loop() const { |
70 | return message_loop_; | ||||
71 | } | ||||
72 | |||||
73 | void set_message_loop(base::MessageLoopProxy* loop) { | ||||
74 | message_loop_ = loop; | ||||
75 | } | ||||
76 | |||||
[email protected] | d64abe1 | 2011-03-22 20:14:06 | [diff] [blame] | 77 | // Must be called before the PlatformDelegate is deleted. |
78 | void set_cancelled() { | ||||
79 | cancelled_ = true; | ||||
80 | } | ||||
81 | |||||
82 | bool is_cancelled() const { | ||||
83 | return cancelled_; | ||||
84 | } | ||||
85 | |||||
[email protected] | b8ca91f | 2011-03-18 04:11:49 | [diff] [blame] | 86 | private: |
[email protected] | d64abe1 | 2011-03-22 20:14:06 | [diff] [blame] | 87 | friend class base::RefCountedThreadSafe<PlatformDelegate>; |
[email protected] | d64abe1 | 2011-03-22 20:14:06 | [diff] [blame] | 88 | |
[email protected] | b8ca91f | 2011-03-18 04:11:49 | [diff] [blame] | 89 | scoped_refptr<base::MessageLoopProxy> message_loop_; |
[email protected] | d64abe1 | 2011-03-22 20:14:06 | [diff] [blame] | 90 | bool cancelled_; |
[email protected] | b8ca91f | 2011-03-18 04:11:49 | [diff] [blame] | 91 | }; |
92 | |||||
[email protected] | 29ec5c1 | 2011-11-23 20:44:00 | [diff] [blame^] | 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 | |||||
[email protected] | b8ca91f | 2011-03-18 04:11:49 | [diff] [blame] | 107 | private: |
108 | scoped_refptr<PlatformDelegate> impl_; | ||||
109 | |||||
110 | DISALLOW_COPY_AND_ASSIGN(FilePathWatcher); | ||||
111 | }; | ||||
112 | |||||
[email protected] | 493c800 | 2011-04-14 16:56:01 | [diff] [blame] | 113 | } // namespace files |
114 | } // namespace base | ||||
115 | |||||
116 | #endif // BASE_FILES_FILE_PATH_WATCHER_H_ |