file_path_watcher.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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
  3. // found in the LICENSE file.
  4. // This module provides a way to monitor a file or directory for changes.
  5. #ifndef BASE_FILES_FILE_PATH_WATCHER_H_
  6. #define BASE_FILES_FILE_PATH_WATCHER_H_
  7. #include <memory>
  8. #include "base/base_export.h"
  9. #include "base/callback.h"
  10. #include "base/files/file_path.h"
  11. #include "base/macros.h"
  12. #include "base/memory/ref_counted.h"
  13. #include "base/sequence_checker.h"
  14. #include "base/sequenced_task_runner.h"
  15. namespace base {
  16. // This class lets you register interest in changes on a FilePath.
  17. // The callback will get called whenever the file or directory referenced by the
  18. // FilePath is changed, including created or deleted. Due to limitations in the
  19. // underlying OS APIs, FilePathWatcher has slightly different semantics on OS X
  20. // than on Windows or Linux. FilePathWatcher on Linux and Windows will detect
  21. // modifications to files in a watched directory. FilePathWatcher on Mac will
  22. // detect the creation and deletion of files in a watched directory, but will
  23. // not detect modifications to those files. See file_path_watcher_kqueue.cc for
  24. // details.
  25. //
  26. // Must be destroyed on the sequence that invokes Watch().
  27. class BASE_EXPORT FilePathWatcher {
  28. public:
  29. // Callback type for Watch(). |path| points to the file that was updated,
  30. // and |error| is true if the platform specific code detected an error. In
  31. // that case, the callback won't be invoked again.
  32. using Callback =
  33. base::RepeatingCallback<void(const FilePath& path, bool error)>;
  34. // Used internally to encapsulate different members on different platforms.
  35. class PlatformDelegate {
  36. public:
  37. PlatformDelegate();
  38. virtual ~PlatformDelegate();
  39. // Start watching for the given |path| and notify |delegate| about changes.
  40. virtual bool Watch(const FilePath& path,
  41. bool recursive,
  42. const Callback& callback) WARN_UNUSED_RESULT = 0;
  43. // Stop watching. This is called from FilePathWatcher's dtor in order to
  44. // allow to shut down properly while the object is still alive.
  45. virtual void Cancel() = 0;
  46. protected:
  47. friend class FilePathWatcher;
  48. scoped_refptr<SequencedTaskRunner> task_runner() const {
  49. return task_runner_;
  50. }
  51. void set_task_runner(scoped_refptr<SequencedTaskRunner> runner) {
  52. task_runner_ = std::move(runner);
  53. }
  54. // Must be called before the PlatformDelegate is deleted.
  55. void set_cancelled() {
  56. cancelled_ = true;
  57. }
  58. bool is_cancelled() const {
  59. return cancelled_;
  60. }
  61. private:
  62. scoped_refptr<SequencedTaskRunner> task_runner_;
  63. bool cancelled_;
  64. DISALLOW_COPY_AND_ASSIGN(PlatformDelegate);
  65. };
  66. FilePathWatcher();
  67. ~FilePathWatcher();
  68. // Returns true if the platform and OS version support recursive watches.
  69. static bool RecursiveWatchAvailable();
  70. // Invokes |callback| whenever updates to |path| are detected. This should be
  71. // called at most once. Set |recursive| to true to watch |path| and its
  72. // children. The callback will be invoked on the same sequence. Returns true
  73. // on success.
  74. //
  75. // On POSIX, this must be called from a thread that supports
  76. // FileDescriptorWatcher.
  77. //
  78. // Recursive watch is not supported on all platforms and file systems.
  79. // Watch() will return false in the case of failure.
  80. bool Watch(const FilePath& path, bool recursive, const Callback& callback);
  81. private:
  82. std::unique_ptr<PlatformDelegate> impl_;
  83. SequenceChecker sequence_checker_;
  84. DISALLOW_COPY_AND_ASSIGN(FilePathWatcher);
  85. };
  86. } // namespace base
  87. #endif // BASE_FILES_FILE_PATH_WATCHER_H_