[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] | b8ca91f | 2011-03-18 04:11:49 | [diff] [blame] | 11 | #include "base/basictypes.h" |
[email protected] | 10e38b6 | 2012-06-04 14:21:27 | [diff] [blame] | 12 | #include "base/callback.h" |
[email protected] | b8ca91f | 2011-03-18 04:11:49 | [diff] [blame] | 13 | #include "base/file_path.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 14 | #include "base/memory/ref_counted.h" |
[email protected] | b8ca91f | 2011-03-18 04:11:49 | [diff] [blame] | 15 | #include "base/message_loop_proxy.h" |
[email protected] | b8ca91f | 2011-03-18 04:11:49 | [diff] [blame] | 16 | |
[email protected] | 493c800 | 2011-04-14 16:56:01 | [diff] [blame] | 17 | namespace base { |
| 18 | namespace files { |
| 19 | |
[email protected] | b8ca91f | 2011-03-18 04:11:49 | [diff] [blame] | 20 | // This class lets you register interest in changes on a FilePath. |
| 21 | // The delegate will get called whenever the file or directory referenced by the |
| 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. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 29 | class BASE_EXPORT FilePathWatcher { |
[email protected] | b8ca91f | 2011-03-18 04:11:49 | [diff] [blame] | 30 | public: |
[email protected] | 10e38b6 | 2012-06-04 14:21:27 | [diff] [blame] | 31 | // Callback type for Watch(). |path| points to the file that was updated, |
| 32 | // and |error| is true if the platform specific code detected an error. In |
| 33 | // that case, the callback won't be invoked again. |
| 34 | typedef base::Callback<void(const FilePath& path, bool error)> Callback; |
| 35 | |
[email protected] | b8ca91f | 2011-03-18 04:11:49 | [diff] [blame] | 36 | // Declares the callback client code implements to receive notifications. Note |
| 37 | // that implementations of this interface should not keep a reference to the |
| 38 | // corresponding FileWatcher object to prevent a reference cycle. |
[email protected] | 10e38b6 | 2012-06-04 14:21:27 | [diff] [blame] | 39 | // |
| 40 | // Deprecated: see comment on Watch() below. |
[email protected] | b8ca91f | 2011-03-18 04:11:49 | [diff] [blame] | 41 | class Delegate : public base::RefCountedThreadSafe<Delegate> { |
| 42 | public: |
[email protected] | b8ca91f | 2011-03-18 04:11:49 | [diff] [blame] | 43 | virtual void OnFilePathChanged(const FilePath& path) = 0; |
| 44 | // Called when platform specific code detected an error. The watcher will |
| 45 | // not call OnFilePathChanged for future changes. |
[email protected] | 7b3ee8b | 2011-04-01 18:48:19 | [diff] [blame] | 46 | virtual void OnFilePathError(const FilePath& path) {} |
[email protected] | a9aaa9d1 | 2012-04-25 00:42:51 | [diff] [blame] | 47 | |
| 48 | protected: |
| 49 | friend class base::RefCountedThreadSafe<Delegate>; |
| 50 | virtual ~Delegate() {} |
[email protected] | b8ca91f | 2011-03-18 04:11:49 | [diff] [blame] | 51 | }; |
| 52 | |
[email protected] | b8ca91f | 2011-03-18 04:11:49 | [diff] [blame] | 53 | // Used internally to encapsulate different members on different platforms. |
[email protected] | 29ec5c1 | 2011-11-23 20:44:00 | [diff] [blame] | 54 | // TODO(jhawkins): Move this into its own file. Also fix the confusing naming |
| 55 | // wrt Delegate vs PlatformDelegate. |
[email protected] | d64abe1 | 2011-03-22 20:14:06 | [diff] [blame] | 56 | class PlatformDelegate : public base::RefCountedThreadSafe<PlatformDelegate> { |
[email protected] | b8ca91f | 2011-03-18 04:11:49 | [diff] [blame] | 57 | public: |
[email protected] | 55f2c516 | 2011-03-18 04:28:24 | [diff] [blame] | 58 | PlatformDelegate(); |
| 59 | |
[email protected] | b8ca91f | 2011-03-18 04:11:49 | [diff] [blame] | 60 | // Start watching for the given |path| and notify |delegate| about changes. |
[email protected] | 7b3ee8b | 2011-04-01 18:48:19 | [diff] [blame] | 61 | virtual bool Watch(const FilePath& path, |
| 62 | Delegate* delegate) WARN_UNUSED_RESULT = 0; |
[email protected] | b8ca91f | 2011-03-18 04:11:49 | [diff] [blame] | 63 | |
| 64 | // Stop watching. This is called from FilePathWatcher's dtor in order to |
| 65 | // allow to shut down properly while the object is still alive. |
[email protected] | d64abe1 | 2011-03-22 20:14:06 | [diff] [blame] | 66 | // It can be called from any thread. |
[email protected] | b8ca91f | 2011-03-18 04:11:49 | [diff] [blame] | 67 | virtual void Cancel() = 0; |
| 68 | |
[email protected] | b8ca91f | 2011-03-18 04:11:49 | [diff] [blame] | 69 | protected: |
[email protected] | a9aaa9d1 | 2012-04-25 00:42:51 | [diff] [blame] | 70 | friend class base::RefCountedThreadSafe<PlatformDelegate>; |
[email protected] | 29ec5c1 | 2011-11-23 20:44:00 | [diff] [blame] | 71 | friend class FilePathWatcher; |
| 72 | |
[email protected] | b8ca91f | 2011-03-18 04:11:49 | [diff] [blame] | 73 | virtual ~PlatformDelegate(); |
| 74 | |
[email protected] | d64abe1 | 2011-03-22 20:14:06 | [diff] [blame] | 75 | // Stop watching. This is only called on the thread of the appropriate |
| 76 | // message loop. Since it can also be called more than once, it should |
| 77 | // check |is_cancelled()| to avoid duplicate work. |
| 78 | virtual void CancelOnMessageLoopThread() = 0; |
| 79 | |
[email protected] | b8ca91f | 2011-03-18 04:11:49 | [diff] [blame] | 80 | scoped_refptr<base::MessageLoopProxy> message_loop() const { |
| 81 | return message_loop_; |
| 82 | } |
| 83 | |
| 84 | void set_message_loop(base::MessageLoopProxy* loop) { |
| 85 | message_loop_ = loop; |
| 86 | } |
| 87 | |
[email protected] | d64abe1 | 2011-03-22 20:14:06 | [diff] [blame] | 88 | // Must be called before the PlatformDelegate is deleted. |
| 89 | void set_cancelled() { |
| 90 | cancelled_ = true; |
| 91 | } |
| 92 | |
| 93 | bool is_cancelled() const { |
| 94 | return cancelled_; |
| 95 | } |
| 96 | |
[email protected] | b8ca91f | 2011-03-18 04:11:49 | [diff] [blame] | 97 | private: |
[email protected] | b8ca91f | 2011-03-18 04:11:49 | [diff] [blame] | 98 | scoped_refptr<base::MessageLoopProxy> message_loop_; |
[email protected] | d64abe1 | 2011-03-22 20:14:06 | [diff] [blame] | 99 | bool cancelled_; |
[email protected] | b8ca91f | 2011-03-18 04:11:49 | [diff] [blame] | 100 | }; |
| 101 | |
[email protected] | 29ec5c1 | 2011-11-23 20:44:00 | [diff] [blame] | 102 | FilePathWatcher(); |
[email protected] | 85109f5df | 2012-02-24 22:18:52 | [diff] [blame] | 103 | virtual ~FilePathWatcher(); |
[email protected] | 29ec5c1 | 2011-11-23 20:44:00 | [diff] [blame] | 104 | |
| 105 | // A callback that always cleans up the PlatformDelegate, either when executed |
| 106 | // or when deleted without having been executed at all, as can happen during |
| 107 | // shutdown. |
| 108 | static void CancelWatch(const scoped_refptr<PlatformDelegate>& delegate); |
| 109 | |
| 110 | // Register interest in any changes on |path|. OnPathChanged will be called |
| 111 | // back for each change. Returns true on success. |
| 112 | // OnFilePathChanged() will be called on the same thread as Watch() is called, |
| 113 | // which should have a MessageLoop of TYPE_IO. |
[email protected] | 10e38b6 | 2012-06-04 14:21:27 | [diff] [blame] | 114 | // |
| 115 | // Deprecated: new code should use the callback interface, declared below. |
| 116 | // The FilePathWatcher::Delegate interface will be removed once all client |
| 117 | // code has been updated. http://crbug.com/130980 |
[email protected] | 85109f5df | 2012-02-24 22:18:52 | [diff] [blame] | 118 | virtual bool Watch(const FilePath& path, Delegate* delegate) |
| 119 | WARN_UNUSED_RESULT; |
[email protected] | 29ec5c1 | 2011-11-23 20:44:00 | [diff] [blame] | 120 | |
[email protected] | 10e38b6 | 2012-06-04 14:21:27 | [diff] [blame] | 121 | // Invokes |callback| whenever updates to |path| are detected. This should be |
| 122 | // called at most once, and from a MessageLoop of TYPE_IO. The callback will |
| 123 | // be invoked on the same loop. Returns true on success. |
| 124 | bool Watch(const FilePath& path, const Callback& callback); |
| 125 | |
[email protected] | b8ca91f | 2011-03-18 04:11:49 | [diff] [blame] | 126 | private: |
| 127 | scoped_refptr<PlatformDelegate> impl_; |
| 128 | |
| 129 | DISALLOW_COPY_AND_ASSIGN(FilePathWatcher); |
| 130 | }; |
| 131 | |
[email protected] | 493c800 | 2011-04-14 16:56:01 | [diff] [blame] | 132 | } // namespace files |
| 133 | } // namespace base |
| 134 | |
| 135 | #endif // BASE_FILES_FILE_PATH_WATCHER_H_ |