[email protected] | 85109f5df | 2012-02-24 22:18:52 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | b8ca91f | 2011-03-18 04:11:49 | [diff] [blame] | 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 | |
fdoray | 0ac2f0f8 | 2017-01-09 23:38:42 | [diff] [blame] | 10 | #include <memory> |
| 11 | |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 12 | #include "base/base_export.h" |
[email protected] | 10e38b6 | 2012-06-04 14:21:27 | [diff] [blame] | 13 | #include "base/callback.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 14 | #include "base/files/file_path.h" |
avi | 543540e | 2015-12-24 05:15:32 | [diff] [blame] | 15 | #include "base/macros.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 16 | #include "base/memory/ref_counted.h" |
fdoray | 1ce19c6 | 2016-11-14 22:37:55 | [diff] [blame] | 17 | #include "base/sequence_checker.h" |
fdoray | caa1245b | 2016-12-23 18:56:44 | [diff] [blame] | 18 | #include "base/sequenced_task_runner.h" |
[email protected] | b8ca91f | 2011-03-18 04:11:49 | [diff] [blame] | 19 | |
[email protected] | 493c800 | 2011-04-14 16:56:01 | [diff] [blame] | 20 | namespace base { |
[email protected] | 493c800 | 2011-04-14 16:56:01 | [diff] [blame] | 21 | |
[email protected] | b8ca91f | 2011-03-18 04:11:49 | [diff] [blame] | 22 | // This class lets you register interest in changes on a FilePath. |
[email protected] | b4e439f | 2013-04-02 22:17:49 | [diff] [blame] | 23 | // The callback will get called whenever the file or directory referenced by the |
[email protected] | b8ca91f | 2011-03-18 04:11:49 | [diff] [blame] | 24 | // FilePath is changed, including created or deleted. Due to limitations in the |
[email protected] | 7b3ee8b | 2011-04-01 18:48:19 | [diff] [blame] | 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 |
[email protected] | 20f0609 | 2012-06-04 10:25:30 | [diff] [blame] | 29 | // not detect modifications to those files. See file_path_watcher_kqueue.cc for |
[email protected] | 7b3ee8b | 2011-04-01 18:48:19 | [diff] [blame] | 30 | // details. |
fdoray | 1ce19c6 | 2016-11-14 22:37:55 | [diff] [blame] | 31 | // |
| 32 | // Must be destroyed on the sequence that invokes Watch(). |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 33 | class BASE_EXPORT FilePathWatcher { |
[email protected] | b8ca91f | 2011-03-18 04:11:49 | [diff] [blame] | 34 | public: |
[email protected] | 10e38b6 | 2012-06-04 14:21:27 | [diff] [blame] | 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. |
kylechar | b2695fc | 2019-04-24 14:51:20 | [diff] [blame] | 38 | using Callback = |
| 39 | base::RepeatingCallback<void(const FilePath& path, bool error)>; |
[email protected] | 10e38b6 | 2012-06-04 14:21:27 | [diff] [blame] | 40 | |
[email protected] | b8ca91f | 2011-03-18 04:11:49 | [diff] [blame] | 41 | // Used internally to encapsulate different members on different platforms. |
fdoray | 0ac2f0f8 | 2017-01-09 23:38:42 | [diff] [blame] | 42 | class PlatformDelegate { |
[email protected] | b8ca91f | 2011-03-18 04:11:49 | [diff] [blame] | 43 | public: |
[email protected] | 55f2c516 | 2011-03-18 04:28:24 | [diff] [blame] | 44 | PlatformDelegate(); |
fdoray | 0ac2f0f8 | 2017-01-09 23:38:42 | [diff] [blame] | 45 | virtual ~PlatformDelegate(); |
[email protected] | 55f2c516 | 2011-03-18 04:28:24 | [diff] [blame] | 46 | |
[email protected] | b8ca91f | 2011-03-18 04:11:49 | [diff] [blame] | 47 | // Start watching for the given |path| and notify |delegate| about changes. |
[email protected] | 7b3ee8b | 2011-04-01 18:48:19 | [diff] [blame] | 48 | virtual bool Watch(const FilePath& path, |
[email protected] | 66a5940 | 2012-12-05 00:36:39 | [diff] [blame] | 49 | bool recursive, |
[email protected] | 6b5d002 | 2013-01-15 00:37:47 | [diff] [blame] | 50 | const Callback& callback) WARN_UNUSED_RESULT = 0; |
[email protected] | b8ca91f | 2011-03-18 04:11:49 | [diff] [blame] | 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 | |
[email protected] | b8ca91f | 2011-03-18 04:11:49 | [diff] [blame] | 56 | protected: |
[email protected] | 29ec5c1 | 2011-11-23 20:44:00 | [diff] [blame] | 57 | friend class FilePathWatcher; |
| 58 | |
fdoray | caa1245b | 2016-12-23 18:56:44 | [diff] [blame] | 59 | scoped_refptr<SequencedTaskRunner> task_runner() const { |
skyostil | 054861d | 2015-04-30 19:06:15 | [diff] [blame] | 60 | return task_runner_; |
[email protected] | b8ca91f | 2011-03-18 04:11:49 | [diff] [blame] | 61 | } |
| 62 | |
fdoray | caa1245b | 2016-12-23 18:56:44 | [diff] [blame] | 63 | void set_task_runner(scoped_refptr<SequencedTaskRunner> runner) { |
danakj | 0c8d4aa | 2015-11-25 05:29:58 | [diff] [blame] | 64 | task_runner_ = std::move(runner); |
[email protected] | b8ca91f | 2011-03-18 04:11:49 | [diff] [blame] | 65 | } |
| 66 | |
[email protected] | d64abe1 | 2011-03-22 20:14:06 | [diff] [blame] | 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 | |
[email protected] | b8ca91f | 2011-03-18 04:11:49 | [diff] [blame] | 76 | private: |
fdoray | caa1245b | 2016-12-23 18:56:44 | [diff] [blame] | 77 | scoped_refptr<SequencedTaskRunner> task_runner_; |
[email protected] | d64abe1 | 2011-03-22 20:14:06 | [diff] [blame] | 78 | bool cancelled_; |
fdoray | 0ac2f0f8 | 2017-01-09 23:38:42 | [diff] [blame] | 79 | |
| 80 | DISALLOW_COPY_AND_ASSIGN(PlatformDelegate); |
[email protected] | b8ca91f | 2011-03-18 04:11:49 | [diff] [blame] | 81 | }; |
| 82 | |
[email protected] | 29ec5c1 | 2011-11-23 20:44:00 | [diff] [blame] | 83 | FilePathWatcher(); |
fdoray | 1ce19c6 | 2016-11-14 22:37:55 | [diff] [blame] | 84 | ~FilePathWatcher(); |
[email protected] | 29ec5c1 | 2011-11-23 20:44:00 | [diff] [blame] | 85 | |
[email protected] | 6d2d92a | 2014-06-11 07:15:24 | [diff] [blame] | 86 | // Returns true if the platform and OS version support recursive watches. |
| 87 | static bool RecursiveWatchAvailable(); |
| 88 | |
[email protected] | 10e38b6 | 2012-06-04 14:21:27 | [diff] [blame] | 89 | // Invokes |callback| whenever updates to |path| are detected. This should be |
fdoray | caa1245b | 2016-12-23 18:56:44 | [diff] [blame] | 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. |
fdoray | 52e11d48 | 2016-11-29 22:19:58 | [diff] [blame] | 93 | // |
| 94 | // On POSIX, this must be called from a thread that supports |
| 95 | // FileDescriptorWatcher. |
[email protected] | 66a5940 | 2012-12-05 00:36:39 | [diff] [blame] | 96 | // |
[email protected] | 6d2d92a | 2014-06-11 07:15:24 | [diff] [blame] | 97 | // Recursive watch is not supported on all platforms and file systems. |
[email protected] | 66a5940 | 2012-12-05 00:36:39 | [diff] [blame] | 98 | // Watch() will return false in the case of failure. |
| 99 | bool Watch(const FilePath& path, bool recursive, const Callback& callback); |
[email protected] | 10e38b6 | 2012-06-04 14:21:27 | [diff] [blame] | 100 | |
[email protected] | b8ca91f | 2011-03-18 04:11:49 | [diff] [blame] | 101 | private: |
fdoray | 0ac2f0f8 | 2017-01-09 23:38:42 | [diff] [blame] | 102 | std::unique_ptr<PlatformDelegate> impl_; |
[email protected] | b8ca91f | 2011-03-18 04:11:49 | [diff] [blame] | 103 | |
fdoray | 1ce19c6 | 2016-11-14 22:37:55 | [diff] [blame] | 104 | SequenceChecker sequence_checker_; |
| 105 | |
[email protected] | b8ca91f | 2011-03-18 04:11:49 | [diff] [blame] | 106 | DISALLOW_COPY_AND_ASSIGN(FilePathWatcher); |
| 107 | }; |
| 108 | |
[email protected] | 493c800 | 2011-04-14 16:56:01 | [diff] [blame] | 109 | } // namespace base |
| 110 | |
| 111 | #endif // BASE_FILES_FILE_PATH_WATCHER_H_ |