| 6 |
| 7 | #ifndef BASE_FILES_FILE_PATH_WATCHER_H_ |
| 8 | #define BASE_FILES_FILE_PATH_WATCHER_H_ |
| 9 |
| 10 | #include <memory> |
| 11 |
| 12 | #include "base/base_export.h" |
| 13 | #include "base/callback.h" |
| 14 | #include "base/files/file_path.h" |
| 15 | #include "base/macros.h" |
| 16 | #include "base/memory/ref_counted.h" |
| 17 | #include "base/sequence_checker.h" |
| 18 | #include "base/sequenced_task_runner.h" |
| 19 |
| 20 | namespace base { |
| 21 |
| 22 | // This class lets you register interest in changes on a FilePath. |
| 23 | // The callback will get called whenever the file or directory referenced by the |
| 24 | // FilePath is changed, including created or deleted. Due to limitations in the |
| 25 | // underlying OS APIs, FilePathWatcher has slightly different semantics on OS X |
| 26 | // than on Windows or Linux. FilePathWatcher on Linux and Windows will detect |
| 27 | // modifications to files in a watched directory. FilePathWatcher on Mac will |
| 28 | // detect the creation and deletion of files in a watched directory, but will |
| 29 | // not detect modifications to those files. See file_path_watcher_kqueue.cc for |
| 30 | // details. |
| 31 | // |
| 32 | // Must be destroyed on the sequence that invokes Watch(). |
| 33 | class BASE_EXPORT FilePathWatcher { |
| 34 | public: |
| 35 | // Callback type for Watch(). |path| points to the file that was updated, |
| 36 | // and |error| is true if the platform specific code detected an error. In |
| 37 | // that case, the callback won't be invoked again. |
| 38 | using Callback = |
| 39 | base::RepeatingCallback<void(const FilePath& path, bool error)>; |
| 40 |
| 41 | // Used internally to encapsulate different members on different platforms. |
| 42 | class PlatformDelegate { |
| 43 | public: |
| 44 | PlatformDelegate(); |
| 45 | virtual ~PlatformDelegate(); |
| 46 |
| 47 | // Start watching for the given |path| and notify |delegate| about changes. |
| 48 | virtual bool Watch(const FilePath& path, |
| 49 | bool recursive, |
| 50 | const Callback& callback) WARN_UNUSED_RESULT = 0; |
| 51 |
| 52 | // Stop watching. This is called from FilePathWatcher's dtor in order to |
| 53 | // allow to shut down properly while the object is still alive. |
| 54 | virtual void Cancel() = 0; |
| 55 |
| 56 | protected: |
| 57 | friend class FilePathWatcher; |
| 58 |
| 59 | scoped_refptr<SequencedTaskRunner> task_runner() const { |
| 60 | return task_runner_; |
| 61 | } |
| 62 |
| 63 | void set_task_runner(scoped_refptr<SequencedTaskRunner> runner) { |
| 64 | task_runner_ = std::move(runner); |
| 65 | } |
| 66 |
| 67 | // Must be called before the PlatformDelegate is deleted. |
| 68 | void set_cancelled() { |
| 69 | cancelled_ = true; |
| 70 | } |
| 71 |
| 72 | bool is_cancelled() const { |
| 73 | return cancelled_; |
| 74 | } |
| 75 |
| 76 | private: |
| 77 | scoped_refptr<SequencedTaskRunner> task_runner_; |
| 78 | bool cancelled_; |
| 79 |
| 80 | DISALLOW_COPY_AND_ASSIGN(PlatformDelegate); |
| 81 | }; |
| 82 |
| 83 | FilePathWatcher(); |
| 84 | ~FilePathWatcher(); |
| 85 |
| 86 | // Returns true if the platform and OS version support recursive watches. |
| 87 | static bool RecursiveWatchAvailable(); |
| 88 |
| 89 | // Invokes |callback| whenever updates to |path| are detected. This should be |
| 90 | // called at most once. Set |recursive| to true to watch |path| and its |
| 91 | // children. The callback will be invoked on the same sequence. Returns true |
| 92 | // on success. |
| 93 | // |
| 94 | // On POSIX, this must be called from a thread that supports |
| 95 | // FileDescriptorWatcher. |
| 96 | // |
| 97 | // Recursive watch is not supported on all platforms and file systems. |
| 98 | // Watch() will return false in the case of failure. |
| 99 | bool Watch(const FilePath& path, bool recursive, const Callback& callback); |
| 100 |
| 101 | private: |
| 102 | std::unique_ptr<PlatformDelegate> impl_; |
| 103 |
| 104 | SequenceChecker sequence_checker_; |
| 105 |
| 106 | DISALLOW_COPY_AND_ASSIGN(FilePathWatcher); |
| 107 | }; |
| 108 |
| 109 | } // namespace base |
| 110 |
| 111 | #endif // BASE_FILES_FILE_PATH_WATCHER_H_ |
| 112 |