| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // This module provides a way to monitor a file or directory for changes. | 5 // This module provides a way to monitor a file or directory for changes. |
| 6 | 6 |
| 7 #ifndef BASE_FILES_FILE_PATH_WATCHER_H_ | 7 #ifndef BASE_FILES_FILE_PATH_WATCHER_H_ |
| 8 #define BASE_FILES_FILE_PATH_WATCHER_H_ | 8 #define BASE_FILES_FILE_PATH_WATCHER_H_ |
| 9 | 9 |
| 10 #include "base/base_export.h" | 10 #include "base/base_export.h" |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 // detect the creation and deletion of files in a watched directory, but will | 26 // detect the creation and deletion of files in a watched directory, but will |
| 27 // not detect modifications to those files. See file_path_watcher_kqueue.cc for | 27 // not detect modifications to those files. See file_path_watcher_kqueue.cc for |
| 28 // details. | 28 // details. |
| 29 class BASE_EXPORT FilePathWatcher { | 29 class BASE_EXPORT FilePathWatcher { |
| 30 public: | 30 public: |
| 31 // Callback type for Watch(). |path| points to the file that was updated, | 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 | 32 // and |error| is true if the platform specific code detected an error. In |
| 33 // that case, the callback won't be invoked again. | 33 // that case, the callback won't be invoked again. |
| 34 typedef base::Callback<void(const FilePath& path, bool error)> Callback; | 34 typedef base::Callback<void(const FilePath& path, bool error)> Callback; |
| 35 | 35 |
| 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. | |
| 39 // | |
| 40 // Deprecated: see comment on Watch() below. | |
| 41 class Delegate : public base::RefCountedThreadSafe<Delegate> { | |
| 42 public: | |
| 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. | |
| 46 virtual void OnFilePathError(const FilePath& path) {} | |
| 47 | |
| 48 protected: | |
| 49 friend class base::RefCountedThreadSafe<Delegate>; | |
| 50 virtual ~Delegate() {} | |
| 51 }; | |
| 52 | |
| 53 // Used internally to encapsulate different members on different platforms. | 36 // Used internally to encapsulate different members on different platforms. |
| 54 // TODO(jhawkins): Move this into its own file. Also fix the confusing naming | 37 // TODO(jhawkins): Move this into its own file. Also fix the confusing naming |
| 55 // wrt Delegate vs PlatformDelegate. | 38 // wrt Delegate vs PlatformDelegate. |
| 56 class PlatformDelegate : public base::RefCountedThreadSafe<PlatformDelegate> { | 39 class PlatformDelegate : public base::RefCountedThreadSafe<PlatformDelegate> { |
| 57 public: | 40 public: |
| 58 PlatformDelegate(); | 41 PlatformDelegate(); |
| 59 | 42 |
| 60 // Start watching for the given |path| and notify |delegate| about changes. | 43 // Start watching for the given |path| and notify |delegate| about changes. |
| 61 virtual bool Watch(const FilePath& path, | 44 virtual bool Watch(const FilePath& path, |
| 62 bool recursive, | 45 bool recursive, |
| 63 Delegate* delegate) WARN_UNUSED_RESULT = 0; | 46 const Callback& callback) WARN_UNUSED_RESULT = 0; |
| 64 | 47 |
| 65 // Stop watching. This is called from FilePathWatcher's dtor in order to | 48 // Stop watching. This is called from FilePathWatcher's dtor in order to |
| 66 // allow to shut down properly while the object is still alive. | 49 // allow to shut down properly while the object is still alive. |
| 67 // It can be called from any thread. | 50 // It can be called from any thread. |
| 68 virtual void Cancel() = 0; | 51 virtual void Cancel() = 0; |
| 69 | 52 |
| 70 protected: | 53 protected: |
| 71 friend class base::RefCountedThreadSafe<PlatformDelegate>; | 54 friend class base::RefCountedThreadSafe<PlatformDelegate>; |
| 72 friend class FilePathWatcher; | 55 friend class FilePathWatcher; |
| 73 | 56 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 101 }; | 84 }; |
| 102 | 85 |
| 103 FilePathWatcher(); | 86 FilePathWatcher(); |
| 104 virtual ~FilePathWatcher(); | 87 virtual ~FilePathWatcher(); |
| 105 | 88 |
| 106 // A callback that always cleans up the PlatformDelegate, either when executed | 89 // A callback that always cleans up the PlatformDelegate, either when executed |
| 107 // or when deleted without having been executed at all, as can happen during | 90 // or when deleted without having been executed at all, as can happen during |
| 108 // shutdown. | 91 // shutdown. |
| 109 static void CancelWatch(const scoped_refptr<PlatformDelegate>& delegate); | 92 static void CancelWatch(const scoped_refptr<PlatformDelegate>& delegate); |
| 110 | 93 |
| 111 // Register interest in any changes on |path|. OnPathChanged will be called | |
| 112 // back for each change. Returns true on success. | |
| 113 // OnFilePathChanged() will be called on the same thread as Watch() is called, | |
| 114 // which should have a MessageLoop of TYPE_IO. | |
| 115 // | |
| 116 // Deprecated: new code should use the callback interface, declared below. | |
| 117 // The FilePathWatcher::Delegate interface will be removed once all client | |
| 118 // code has been updated. http://crbug.com/130980 | |
| 119 virtual bool Watch(const FilePath& path, Delegate* delegate) | |
| 120 WARN_UNUSED_RESULT; | |
| 121 | |
| 122 // Invokes |callback| whenever updates to |path| are detected. This should be | 94 // Invokes |callback| whenever updates to |path| are detected. This should be |
| 123 // called at most once, and from a MessageLoop of TYPE_IO. Set |recursive| to | 95 // called at most once, and from a MessageLoop of TYPE_IO. Set |recursive| to |
| 124 // true, to watch |path| and its children. The callback will be invoked on | 96 // true, to watch |path| and its children. The callback will be invoked on |
| 125 // the same loop. Returns true on success. | 97 // the same loop. Returns true on success. |
| 126 // | 98 // |
| 127 // NOTE: Recursive watch is not supported on all platforms and file systems. | 99 // NOTE: Recursive watch is not supported on all platforms and file systems. |
| 128 // Watch() will return false in the case of failure. | 100 // Watch() will return false in the case of failure. |
| 129 bool Watch(const FilePath& path, bool recursive, const Callback& callback); | 101 bool Watch(const FilePath& path, bool recursive, const Callback& callback); |
| 130 | 102 |
| 131 private: | 103 private: |
| 132 scoped_refptr<PlatformDelegate> impl_; | 104 scoped_refptr<PlatformDelegate> impl_; |
| 133 | 105 |
| 134 DISALLOW_COPY_AND_ASSIGN(FilePathWatcher); | 106 DISALLOW_COPY_AND_ASSIGN(FilePathWatcher); |
| 135 }; | 107 }; |
| 136 | 108 |
| 137 } // namespace files | 109 } // namespace files |
| 138 } // namespace base | 110 } // namespace base |
| 139 | 111 |
| 140 #endif // BASE_FILES_FILE_PATH_WATCHER_H_ | 112 #endif // BASE_FILES_FILE_PATH_WATCHER_H_ |
| OLD | NEW |