[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 | |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 10 | #include "base/base_export.h" |
[email protected] | 10e38b6 | 2012-06-04 14:21:27 | [diff] [blame] | 11 | #include "base/callback.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 12 | #include "base/files/file_path.h" |
avi | 543540e | 2015-12-24 05:15:32 | [diff] [blame] | 13 | #include "base/macros.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 14 | #include "base/memory/ref_counted.h" |
fdoray | 1ce19c6 | 2016-11-14 22:37:55 | [diff] [blame] | 15 | #include "base/sequence_checker.h" |
skyostil | 054861d | 2015-04-30 19:06:15 | [diff] [blame] | 16 | #include "base/single_thread_task_runner.h" |
[email protected] | b8ca91f | 2011-03-18 04:11:49 | [diff] [blame] | 17 | |
[email protected] | 493c800 | 2011-04-14 16:56:01 | [diff] [blame] | 18 | namespace base { |
[email protected] | 493c800 | 2011-04-14 16:56:01 | [diff] [blame] | 19 | |
[email protected] | b8ca91f | 2011-03-18 04:11:49 | [diff] [blame] | 20 | // This class lets you register interest in changes on a FilePath. |
[email protected] | b4e439f | 2013-04-02 22:17:49 | [diff] [blame] | 21 | // The callback will get called whenever the file or directory referenced by the |
[email protected] | b8ca91f | 2011-03-18 04:11:49 | [diff] [blame] | 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 |
[email protected] | 20f0609 | 2012-06-04 10:25:30 | [diff] [blame] | 27 | // not detect modifications to those files. See file_path_watcher_kqueue.cc for |
[email protected] | 7b3ee8b | 2011-04-01 18:48:19 | [diff] [blame] | 28 | // details. |
fdoray | 1ce19c6 | 2016-11-14 22:37:55 | [diff] [blame] | 29 | // |
| 30 | // Must be destroyed on the sequence that invokes Watch(). |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 31 | class BASE_EXPORT FilePathWatcher { |
[email protected] | b8ca91f | 2011-03-18 04:11:49 | [diff] [blame] | 32 | public: |
[email protected] | 10e38b6 | 2012-06-04 14:21:27 | [diff] [blame] | 33 | // Callback type for Watch(). |path| points to the file that was updated, |
| 34 | // and |error| is true if the platform specific code detected an error. In |
| 35 | // that case, the callback won't be invoked again. |
| 36 | typedef base::Callback<void(const FilePath& path, bool error)> Callback; |
| 37 | |
[email protected] | b8ca91f | 2011-03-18 04:11:49 | [diff] [blame] | 38 | // Used internally to encapsulate different members on different platforms. |
[email protected] | d64abe1 | 2011-03-22 20:14:06 | [diff] [blame] | 39 | class PlatformDelegate : public base::RefCountedThreadSafe<PlatformDelegate> { |
[email protected] | b8ca91f | 2011-03-18 04:11:49 | [diff] [blame] | 40 | public: |
[email protected] | 55f2c516 | 2011-03-18 04:28:24 | [diff] [blame] | 41 | PlatformDelegate(); |
| 42 | |
[email protected] | b8ca91f | 2011-03-18 04:11:49 | [diff] [blame] | 43 | // Start watching for the given |path| and notify |delegate| about changes. |
[email protected] | 7b3ee8b | 2011-04-01 18:48:19 | [diff] [blame] | 44 | virtual bool Watch(const FilePath& path, |
[email protected] | 66a5940 | 2012-12-05 00:36:39 | [diff] [blame] | 45 | bool recursive, |
[email protected] | 6b5d002 | 2013-01-15 00:37:47 | [diff] [blame] | 46 | const Callback& callback) WARN_UNUSED_RESULT = 0; |
[email protected] | b8ca91f | 2011-03-18 04:11:49 | [diff] [blame] | 47 | |
| 48 | // Stop watching. This is called from FilePathWatcher's dtor in order to |
| 49 | // allow to shut down properly while the object is still alive. |
[email protected] | d64abe1 | 2011-03-22 20:14:06 | [diff] [blame] | 50 | // It can be called from any thread. |
[email protected] | b8ca91f | 2011-03-18 04:11:49 | [diff] [blame] | 51 | virtual void Cancel() = 0; |
| 52 | |
[email protected] | b8ca91f | 2011-03-18 04:11:49 | [diff] [blame] | 53 | protected: |
[email protected] | a9aaa9d1 | 2012-04-25 00:42:51 | [diff] [blame] | 54 | friend class base::RefCountedThreadSafe<PlatformDelegate>; |
[email protected] | 29ec5c1 | 2011-11-23 20:44:00 | [diff] [blame] | 55 | friend class FilePathWatcher; |
| 56 | |
[email protected] | b8ca91f | 2011-03-18 04:11:49 | [diff] [blame] | 57 | virtual ~PlatformDelegate(); |
| 58 | |
skyostil | 054861d | 2015-04-30 19:06:15 | [diff] [blame] | 59 | scoped_refptr<base::SingleThreadTaskRunner> task_runner() const { |
| 60 | return task_runner_; |
[email protected] | b8ca91f | 2011-03-18 04:11:49 | [diff] [blame] | 61 | } |
| 62 | |
danakj | 0c8d4aa | 2015-11-25 05:29:58 | [diff] [blame] | 63 | void set_task_runner(scoped_refptr<base::SingleThreadTaskRunner> runner) { |
| 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: |
skyostil | 054861d | 2015-04-30 19:06:15 | [diff] [blame] | 77 | scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
[email protected] | d64abe1 | 2011-03-22 20:14:06 | [diff] [blame] | 78 | bool cancelled_; |
[email protected] | b8ca91f | 2011-03-18 04:11:49 | [diff] [blame] | 79 | }; |
| 80 | |
[email protected] | 29ec5c1 | 2011-11-23 20:44:00 | [diff] [blame] | 81 | FilePathWatcher(); |
fdoray | 1ce19c6 | 2016-11-14 22:37:55 | [diff] [blame] | 82 | ~FilePathWatcher(); |
[email protected] | 29ec5c1 | 2011-11-23 20:44:00 | [diff] [blame] | 83 | |
| 84 | // A callback that always cleans up the PlatformDelegate, either when executed |
| 85 | // or when deleted without having been executed at all, as can happen during |
| 86 | // shutdown. |
| 87 | static void CancelWatch(const scoped_refptr<PlatformDelegate>& delegate); |
| 88 | |
[email protected] | 6d2d92a | 2014-06-11 07:15:24 | [diff] [blame] | 89 | // Returns true if the platform and OS version support recursive watches. |
| 90 | static bool RecursiveWatchAvailable(); |
| 91 | |
[email protected] | 10e38b6 | 2012-06-04 14:21:27 | [diff] [blame] | 92 | // Invokes |callback| whenever updates to |path| are detected. This should be |
fdoray | 52e11d48 | 2016-11-29 22:19:58 | [diff] [blame^] | 93 | // called at most once. Set |recursive| to true, to watch |path| and its |
| 94 | // children. The callback will be invoked on the same thread. Returns true on |
| 95 | // success. |
| 96 | // |
| 97 | // On POSIX, this must be called from a thread that supports |
| 98 | // FileDescriptorWatcher. |
[email protected] | 66a5940 | 2012-12-05 00:36:39 | [diff] [blame] | 99 | // |
[email protected] | 6d2d92a | 2014-06-11 07:15:24 | [diff] [blame] | 100 | // Recursive watch is not supported on all platforms and file systems. |
[email protected] | 66a5940 | 2012-12-05 00:36:39 | [diff] [blame] | 101 | // Watch() will return false in the case of failure. |
| 102 | bool Watch(const FilePath& path, bool recursive, const Callback& callback); |
[email protected] | 10e38b6 | 2012-06-04 14:21:27 | [diff] [blame] | 103 | |
[email protected] | b8ca91f | 2011-03-18 04:11:49 | [diff] [blame] | 104 | private: |
| 105 | scoped_refptr<PlatformDelegate> impl_; |
| 106 | |
fdoray | 1ce19c6 | 2016-11-14 22:37:55 | [diff] [blame] | 107 | SequenceChecker sequence_checker_; |
| 108 | |
[email protected] | b8ca91f | 2011-03-18 04:11:49 | [diff] [blame] | 109 | DISALLOW_COPY_AND_ASSIGN(FilePathWatcher); |
| 110 | }; |
| 111 | |
[email protected] | 493c800 | 2011-04-14 16:56:01 | [diff] [blame] | 112 | } // namespace base |
| 113 | |
| 114 | #endif // BASE_FILES_FILE_PATH_WATCHER_H_ |