szym@chromium.org | 5ef94e2 | 2012-02-25 07:18:52 +0900 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
craig.schlenter@chromium.org | b2d581e | 2011-04-15 01:56:01 +0900 | [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 | |
| 7 | #ifndef BASE_FILES_FILE_PATH_WATCHER_H_ |
| 8 | #define BASE_FILES_FILE_PATH_WATCHER_H_ |
craig.schlenter@chromium.org | b2d581e | 2011-04-15 01:56:01 +0900 | [diff] [blame] | 9 | |
darin@chromium.org | e585bed | 2011-08-06 00:34:00 +0900 | [diff] [blame] | 10 | #include "base/base_export.h" |
craig.schlenter@chromium.org | b2d581e | 2011-04-15 01:56:01 +0900 | [diff] [blame] | 11 | #include "base/basictypes.h" |
joaodasilva@chromium.org | 5e57506 | 2012-06-04 23:21:27 +0900 | [diff] [blame] | 12 | #include "base/callback.h" |
brettw@chromium.org | 59eef1f | 2013-02-24 14:40:52 +0900 | [diff] [blame] | 13 | #include "base/files/file_path.h" |
craig.schlenter@chromium.org | b2d581e | 2011-04-15 01:56:01 +0900 | [diff] [blame] | 14 | #include "base/memory/ref_counted.h" |
skyostil | 97aefe1 | 2015-05-01 04:06:15 +0900 | [diff] [blame] | 15 | #include "base/single_thread_task_runner.h" |
craig.schlenter@chromium.org | b2d581e | 2011-04-15 01:56:01 +0900 | [diff] [blame] | 16 | |
| 17 | namespace base { |
craig.schlenter@chromium.org | b2d581e | 2011-04-15 01:56:01 +0900 | [diff] [blame] | 18 | |
| 19 | // This class lets you register interest in changes on a FilePath. |
avi@chromium.org | 1bd484c | 2013-04-03 07:17:49 +0900 | [diff] [blame] | 20 | // The callback will get called whenever the file or directory referenced by the |
craig.schlenter@chromium.org | b2d581e | 2011-04-15 01:56:01 +0900 | [diff] [blame] | 21 | // FilePath is changed, including created or deleted. Due to limitations in the |
| 22 | // underlying OS APIs, FilePathWatcher has slightly different semantics on OS X |
| 23 | // than on Windows or Linux. FilePathWatcher on Linux and Windows will detect |
| 24 | // modifications to files in a watched directory. FilePathWatcher on Mac will |
| 25 | // detect the creation and deletion of files in a watched directory, but will |
joaodasilva@chromium.org | 685080d | 2012-06-04 19:25:30 +0900 | [diff] [blame] | 26 | // not detect modifications to those files. See file_path_watcher_kqueue.cc for |
craig.schlenter@chromium.org | b2d581e | 2011-04-15 01:56:01 +0900 | [diff] [blame] | 27 | // details. |
darin@chromium.org | e585bed | 2011-08-06 00:34:00 +0900 | [diff] [blame] | 28 | class BASE_EXPORT FilePathWatcher { |
craig.schlenter@chromium.org | b2d581e | 2011-04-15 01:56:01 +0900 | [diff] [blame] | 29 | public: |
joaodasilva@chromium.org | 5e57506 | 2012-06-04 23:21:27 +0900 | [diff] [blame] | 30 | // Callback type for Watch(). |path| points to the file that was updated, |
| 31 | // and |error| is true if the platform specific code detected an error. In |
| 32 | // that case, the callback won't be invoked again. |
| 33 | typedef base::Callback<void(const FilePath& path, bool error)> Callback; |
| 34 | |
craig.schlenter@chromium.org | b2d581e | 2011-04-15 01:56:01 +0900 | [diff] [blame] | 35 | // Used internally to encapsulate different members on different platforms. |
| 36 | class PlatformDelegate : public base::RefCountedThreadSafe<PlatformDelegate> { |
| 37 | public: |
| 38 | PlatformDelegate(); |
| 39 | |
| 40 | // Start watching for the given |path| and notify |delegate| about changes. |
| 41 | virtual bool Watch(const FilePath& path, |
kmadhusu@chromium.org | c9126e5 | 2012-12-05 09:36:39 +0900 | [diff] [blame] | 42 | bool recursive, |
darin@chromium.org | 9498d47 | 2013-01-15 09:37:47 +0900 | [diff] [blame] | 43 | const Callback& callback) WARN_UNUSED_RESULT = 0; |
craig.schlenter@chromium.org | b2d581e | 2011-04-15 01:56:01 +0900 | [diff] [blame] | 44 | |
| 45 | // Stop watching. This is called from FilePathWatcher's dtor in order to |
| 46 | // allow to shut down properly while the object is still alive. |
| 47 | // It can be called from any thread. |
| 48 | virtual void Cancel() = 0; |
| 49 | |
| 50 | protected: |
rsleevi@chromium.org | b5eb00e | 2012-04-25 09:42:51 +0900 | [diff] [blame] | 51 | friend class base::RefCountedThreadSafe<PlatformDelegate>; |
jhawkins@chromium.org | fce1f41 | 2011-11-24 05:44:00 +0900 | [diff] [blame] | 52 | friend class FilePathWatcher; |
| 53 | |
craig.schlenter@chromium.org | b2d581e | 2011-04-15 01:56:01 +0900 | [diff] [blame] | 54 | virtual ~PlatformDelegate(); |
| 55 | |
| 56 | // Stop watching. This is only called on the thread of the appropriate |
| 57 | // message loop. Since it can also be called more than once, it should |
| 58 | // check |is_cancelled()| to avoid duplicate work. |
| 59 | virtual void CancelOnMessageLoopThread() = 0; |
| 60 | |
skyostil | 97aefe1 | 2015-05-01 04:06:15 +0900 | [diff] [blame] | 61 | scoped_refptr<base::SingleThreadTaskRunner> task_runner() const { |
| 62 | return task_runner_; |
craig.schlenter@chromium.org | b2d581e | 2011-04-15 01:56:01 +0900 | [diff] [blame] | 63 | } |
| 64 | |
danakj | 800d2ea | 2015-11-25 14:29:58 +0900 | [diff] [blame] | 65 | void set_task_runner(scoped_refptr<base::SingleThreadTaskRunner> runner) { |
| 66 | task_runner_ = std::move(runner); |
craig.schlenter@chromium.org | b2d581e | 2011-04-15 01:56:01 +0900 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | // Must be called before the PlatformDelegate is deleted. |
| 70 | void set_cancelled() { |
| 71 | cancelled_ = true; |
| 72 | } |
| 73 | |
| 74 | bool is_cancelled() const { |
| 75 | return cancelled_; |
| 76 | } |
| 77 | |
| 78 | private: |
skyostil | 97aefe1 | 2015-05-01 04:06:15 +0900 | [diff] [blame] | 79 | scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
craig.schlenter@chromium.org | b2d581e | 2011-04-15 01:56:01 +0900 | [diff] [blame] | 80 | bool cancelled_; |
| 81 | }; |
| 82 | |
jhawkins@chromium.org | fce1f41 | 2011-11-24 05:44:00 +0900 | [diff] [blame] | 83 | FilePathWatcher(); |
szym@chromium.org | 5ef94e2 | 2012-02-25 07:18:52 +0900 | [diff] [blame] | 84 | virtual ~FilePathWatcher(); |
jhawkins@chromium.org | fce1f41 | 2011-11-24 05:44:00 +0900 | [diff] [blame] | 85 | |
| 86 | // A callback that always cleans up the PlatformDelegate, either when executed |
| 87 | // or when deleted without having been executed at all, as can happen during |
| 88 | // shutdown. |
| 89 | static void CancelWatch(const scoped_refptr<PlatformDelegate>& delegate); |
| 90 | |
vandebo@chromium.org | c185d78 | 2014-06-11 16:15:24 +0900 | [diff] [blame] | 91 | // Returns true if the platform and OS version support recursive watches. |
| 92 | static bool RecursiveWatchAvailable(); |
| 93 | |
joaodasilva@chromium.org | 5e57506 | 2012-06-04 23:21:27 +0900 | [diff] [blame] | 94 | // Invokes |callback| whenever updates to |path| are detected. This should be |
kmadhusu@chromium.org | c9126e5 | 2012-12-05 09:36:39 +0900 | [diff] [blame] | 95 | // called at most once, and from a MessageLoop of TYPE_IO. Set |recursive| to |
| 96 | // true, to watch |path| and its children. The callback will be invoked on |
| 97 | // the same loop. Returns true on success. |
| 98 | // |
vandebo@chromium.org | c185d78 | 2014-06-11 16:15:24 +0900 | [diff] [blame] | 99 | // Recursive watch is not supported on all platforms and file systems. |
kmadhusu@chromium.org | c9126e5 | 2012-12-05 09:36:39 +0900 | [diff] [blame] | 100 | // Watch() will return false in the case of failure. |
| 101 | bool Watch(const FilePath& path, bool recursive, const Callback& callback); |
joaodasilva@chromium.org | 5e57506 | 2012-06-04 23:21:27 +0900 | [diff] [blame] | 102 | |
craig.schlenter@chromium.org | b2d581e | 2011-04-15 01:56:01 +0900 | [diff] [blame] | 103 | private: |
| 104 | scoped_refptr<PlatformDelegate> impl_; |
| 105 | |
| 106 | DISALLOW_COPY_AND_ASSIGN(FilePathWatcher); |
| 107 | }; |
| 108 | |
craig.schlenter@chromium.org | b2d581e | 2011-04-15 01:56:01 +0900 | [diff] [blame] | 109 | } // namespace base |
| 110 | |
| 111 | #endif // BASE_FILES_FILE_PATH_WATCHER_H_ |