| 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" |
| 11 #include "base/callback.h" | 11 #include "base/callback.h" |
| 12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/sequence_checker.h" | 15 #include "base/sequence_checker.h" |
| 16 #include "base/single_thread_task_runner.h" | 16 #include "base/sequenced_task_runner.h" |
| 17 | 17 |
| 18 namespace base { | 18 namespace base { |
| 19 | 19 |
| 20 // This class lets you register interest in changes on a FilePath. | 20 // This class lets you register interest in changes on a FilePath. |
| 21 // The callback will get called whenever the file or directory referenced by the | 21 // The callback will get called whenever the file or directory referenced by the |
| 22 // FilePath is changed, including created or deleted. Due to limitations in the | 22 // FilePath is changed, including created or deleted. Due to limitations in the |
| 23 // underlying OS APIs, FilePathWatcher has slightly different semantics on OS X | 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 | 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 | 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 | 26 // detect the creation and deletion of files in a watched directory, but will |
| (...skipping 13 matching lines...) Expand all Loading... |
| 40 public: | 40 public: |
| 41 PlatformDelegate(); | 41 PlatformDelegate(); |
| 42 | 42 |
| 43 // Start watching for the given |path| and notify |delegate| about changes. | 43 // Start watching for the given |path| and notify |delegate| about changes. |
| 44 virtual bool Watch(const FilePath& path, | 44 virtual bool Watch(const FilePath& path, |
| 45 bool recursive, | 45 bool recursive, |
| 46 const Callback& callback) WARN_UNUSED_RESULT = 0; | 46 const Callback& callback) WARN_UNUSED_RESULT = 0; |
| 47 | 47 |
| 48 // 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 |
| 49 // allow to shut down properly while the object is still alive. | 49 // allow to shut down properly while the object is still alive. |
| 50 // It can be called from any thread. | |
| 51 virtual void Cancel() = 0; | 50 virtual void Cancel() = 0; |
| 52 | 51 |
| 53 protected: | 52 protected: |
| 54 friend class base::RefCountedThreadSafe<PlatformDelegate>; | 53 friend class base::RefCountedThreadSafe<PlatformDelegate>; |
| 55 friend class FilePathWatcher; | 54 friend class FilePathWatcher; |
| 56 | 55 |
| 57 virtual ~PlatformDelegate(); | 56 virtual ~PlatformDelegate(); |
| 58 | 57 |
| 59 scoped_refptr<base::SingleThreadTaskRunner> task_runner() const { | 58 scoped_refptr<SequencedTaskRunner> task_runner() const { |
| 60 return task_runner_; | 59 return task_runner_; |
| 61 } | 60 } |
| 62 | 61 |
| 63 void set_task_runner(scoped_refptr<base::SingleThreadTaskRunner> runner) { | 62 void set_task_runner(scoped_refptr<SequencedTaskRunner> runner) { |
| 64 task_runner_ = std::move(runner); | 63 task_runner_ = std::move(runner); |
| 65 } | 64 } |
| 66 | 65 |
| 67 // Must be called before the PlatformDelegate is deleted. | 66 // Must be called before the PlatformDelegate is deleted. |
| 68 void set_cancelled() { | 67 void set_cancelled() { |
| 69 cancelled_ = true; | 68 cancelled_ = true; |
| 70 } | 69 } |
| 71 | 70 |
| 72 bool is_cancelled() const { | 71 bool is_cancelled() const { |
| 73 return cancelled_; | 72 return cancelled_; |
| 74 } | 73 } |
| 75 | 74 |
| 76 private: | 75 private: |
| 77 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 76 scoped_refptr<SequencedTaskRunner> task_runner_; |
| 78 bool cancelled_; | 77 bool cancelled_; |
| 79 }; | 78 }; |
| 80 | 79 |
| 81 FilePathWatcher(); | 80 FilePathWatcher(); |
| 82 ~FilePathWatcher(); | 81 ~FilePathWatcher(); |
| 83 | 82 |
| 84 // A callback that always cleans up the PlatformDelegate, either when executed | 83 // A callback that always cleans up the PlatformDelegate, either when executed |
| 85 // or when deleted without having been executed at all, as can happen during | 84 // or when deleted without having been executed at all, as can happen during |
| 86 // shutdown. | 85 // shutdown. |
| 87 static void CancelWatch(const scoped_refptr<PlatformDelegate>& delegate); | 86 static void CancelWatch(const scoped_refptr<PlatformDelegate>& delegate); |
| 88 | 87 |
| 89 // Returns true if the platform and OS version support recursive watches. | 88 // Returns true if the platform and OS version support recursive watches. |
| 90 static bool RecursiveWatchAvailable(); | 89 static bool RecursiveWatchAvailable(); |
| 91 | 90 |
| 92 // Invokes |callback| whenever updates to |path| are detected. This should be | 91 // Invokes |callback| whenever updates to |path| are detected. This should be |
| 93 // called at most once. Set |recursive| to true, to watch |path| and its | 92 // called at most once. Set |recursive| to true to watch |path| and its |
| 94 // children. The callback will be invoked on the same thread. Returns true on | 93 // children. The callback will be invoked on the same sequence. Returns true |
| 95 // success. | 94 // on success. |
| 96 // | 95 // |
| 97 // On POSIX, this must be called from a thread that supports | 96 // On POSIX, this must be called from a thread that supports |
| 98 // FileDescriptorWatcher. | 97 // FileDescriptorWatcher. |
| 99 // | 98 // |
| 100 // Recursive watch is not supported on all platforms and file systems. | 99 // Recursive watch is not supported on all platforms and file systems. |
| 101 // Watch() will return false in the case of failure. | 100 // Watch() will return false in the case of failure. |
| 102 bool Watch(const FilePath& path, bool recursive, const Callback& callback); | 101 bool Watch(const FilePath& path, bool recursive, const Callback& callback); |
| 103 | 102 |
| 104 private: | 103 private: |
| 105 scoped_refptr<PlatformDelegate> impl_; | 104 scoped_refptr<PlatformDelegate> impl_; |
| 106 | 105 |
| 107 SequenceChecker sequence_checker_; | 106 SequenceChecker sequence_checker_; |
| 108 | 107 |
| 109 DISALLOW_COPY_AND_ASSIGN(FilePathWatcher); | 108 DISALLOW_COPY_AND_ASSIGN(FilePathWatcher); |
| 110 }; | 109 }; |
| 111 | 110 |
| 112 } // namespace base | 111 } // namespace base |
| 113 | 112 |
| 114 #endif // BASE_FILES_FILE_PATH_WATCHER_H_ | 113 #endif // BASE_FILES_FILE_PATH_WATCHER_H_ |
| OLD | NEW |