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" |
craig.schlenter@chromium.org | b2d581e | 2011-04-15 01:56:01 +0900 | [diff] [blame] | 13 | #include "base/file_path.h" |
| 14 | #include "base/memory/ref_counted.h" |
| 15 | #include "base/message_loop_proxy.h" |
| 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. |
| 20 | // The delegate will get called whenever the file or directory referenced by the |
| 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. |
jhawkins@chromium.org | fce1f41 | 2011-11-24 05:44:00 +0900 | [diff] [blame] | 36 | // TODO(jhawkins): Move this into its own file. Also fix the confusing naming |
| 37 | // wrt Delegate vs PlatformDelegate. |
craig.schlenter@chromium.org | b2d581e | 2011-04-15 01:56:01 +0900 | [diff] [blame] | 38 | class PlatformDelegate : public base::RefCountedThreadSafe<PlatformDelegate> { |
| 39 | public: |
| 40 | PlatformDelegate(); |
| 41 | |
| 42 | // Start watching for the given |path| and notify |delegate| about changes. |
| 43 | virtual bool Watch(const FilePath& path, |
kmadhusu@chromium.org | c9126e5 | 2012-12-05 09:36:39 +0900 | [diff] [blame] | 44 | bool recursive, |
darin@chromium.org | 9498d47 | 2013-01-15 09:37:47 +0900 | [diff] [blame] | 45 | const Callback& callback) WARN_UNUSED_RESULT = 0; |
craig.schlenter@chromium.org | b2d581e | 2011-04-15 01:56:01 +0900 | [diff] [blame] | 46 | |
| 47 | // Stop watching. This is called from FilePathWatcher's dtor in order to |
| 48 | // allow to shut down properly while the object is still alive. |
| 49 | // It can be called from any thread. |
| 50 | virtual void Cancel() = 0; |
| 51 | |
| 52 | protected: |
rsleevi@chromium.org | b5eb00e | 2012-04-25 09:42:51 +0900 | [diff] [blame] | 53 | friend class base::RefCountedThreadSafe<PlatformDelegate>; |
jhawkins@chromium.org | fce1f41 | 2011-11-24 05:44:00 +0900 | [diff] [blame] | 54 | friend class FilePathWatcher; |
| 55 | |
craig.schlenter@chromium.org | b2d581e | 2011-04-15 01:56:01 +0900 | [diff] [blame] | 56 | virtual ~PlatformDelegate(); |
| 57 | |
| 58 | // Stop watching. This is only called on the thread of the appropriate |
| 59 | // message loop. Since it can also be called more than once, it should |
| 60 | // check |is_cancelled()| to avoid duplicate work. |
| 61 | virtual void CancelOnMessageLoopThread() = 0; |
| 62 | |
| 63 | scoped_refptr<base::MessageLoopProxy> message_loop() const { |
| 64 | return message_loop_; |
| 65 | } |
| 66 | |
| 67 | void set_message_loop(base::MessageLoopProxy* loop) { |
| 68 | message_loop_ = loop; |
| 69 | } |
| 70 | |
| 71 | // Must be called before the PlatformDelegate is deleted. |
| 72 | void set_cancelled() { |
| 73 | cancelled_ = true; |
| 74 | } |
| 75 | |
| 76 | bool is_cancelled() const { |
| 77 | return cancelled_; |
| 78 | } |
| 79 | |
| 80 | private: |
craig.schlenter@chromium.org | b2d581e | 2011-04-15 01:56:01 +0900 | [diff] [blame] | 81 | scoped_refptr<base::MessageLoopProxy> message_loop_; |
| 82 | bool cancelled_; |
| 83 | }; |
| 84 | |
jhawkins@chromium.org | fce1f41 | 2011-11-24 05:44:00 +0900 | [diff] [blame] | 85 | FilePathWatcher(); |
szym@chromium.org | 5ef94e2 | 2012-02-25 07:18:52 +0900 | [diff] [blame] | 86 | virtual ~FilePathWatcher(); |
jhawkins@chromium.org | fce1f41 | 2011-11-24 05:44:00 +0900 | [diff] [blame] | 87 | |
| 88 | // A callback that always cleans up the PlatformDelegate, either when executed |
| 89 | // or when deleted without having been executed at all, as can happen during |
| 90 | // shutdown. |
| 91 | static void CancelWatch(const scoped_refptr<PlatformDelegate>& delegate); |
| 92 | |
joaodasilva@chromium.org | 5e57506 | 2012-06-04 23:21:27 +0900 | [diff] [blame] | 93 | // Invokes |callback| whenever updates to |path| are detected. This should be |
kmadhusu@chromium.org | c9126e5 | 2012-12-05 09:36:39 +0900 | [diff] [blame] | 94 | // called at most once, and from a MessageLoop of TYPE_IO. Set |recursive| to |
| 95 | // true, to watch |path| and its children. The callback will be invoked on |
| 96 | // the same loop. Returns true on success. |
| 97 | // |
| 98 | // NOTE: Recursive watch is not supported on all platforms and file systems. |
| 99 | // Watch() will return false in the case of failure. |
| 100 | bool Watch(const FilePath& path, bool recursive, const Callback& callback); |
joaodasilva@chromium.org | 5e57506 | 2012-06-04 23:21:27 +0900 | [diff] [blame] | 101 | |
craig.schlenter@chromium.org | b2d581e | 2011-04-15 01:56:01 +0900 | [diff] [blame] | 102 | private: |
| 103 | scoped_refptr<PlatformDelegate> impl_; |
| 104 | |
| 105 | DISALLOW_COPY_AND_ASSIGN(FilePathWatcher); |
| 106 | }; |
| 107 | |
craig.schlenter@chromium.org | b2d581e | 2011-04-15 01:56:01 +0900 | [diff] [blame] | 108 | } // namespace base |
| 109 | |
| 110 | #endif // BASE_FILES_FILE_PATH_WATCHER_H_ |