Chromium Code Reviews| 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 22 matching lines...) Expand all Loading... | |
| 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. | 50 // It can be called from any thread. |
| 51 virtual void Cancel() = 0; | 51 virtual void Cancel() = 0; |
| 52 | 52 |
| 53 protected: | 53 protected: |
| 54 friend class base::RefCountedThreadSafe<PlatformDelegate>; | 54 friend class base::RefCountedThreadSafe<PlatformDelegate>; |
| 55 friend class FilePathWatcher; | 55 friend class FilePathWatcher; |
| 56 | 56 |
| 57 virtual ~PlatformDelegate(); | 57 virtual ~PlatformDelegate(); |
| 58 | 58 |
| 59 scoped_refptr<base::SingleThreadTaskRunner> task_runner() const { | 59 scoped_refptr<SequencedTaskRunner> task_runner() const { |
| 60 return task_runner_; | 60 return task_runner_; |
| 61 } | 61 } |
| 62 | 62 |
| 63 void set_task_runner(scoped_refptr<base::SingleThreadTaskRunner> runner) { | 63 void set_task_runner(scoped_refptr<SequencedTaskRunner> runner) { |
| 64 task_runner_ = std::move(runner); | 64 task_runner_ = std::move(runner); |
| 65 } | 65 } |
| 66 | 66 |
| 67 // Must be called before the PlatformDelegate is deleted. | 67 // Must be called before the PlatformDelegate is deleted. |
| 68 void set_cancelled() { | 68 void set_cancelled() { |
| 69 cancelled_ = true; | 69 cancelled_ = true; |
| 70 } | 70 } |
| 71 | 71 |
| 72 bool is_cancelled() const { | 72 bool is_cancelled() const { |
| 73 return cancelled_; | 73 return cancelled_; |
| 74 } | 74 } |
| 75 | 75 |
| 76 private: | 76 private: |
| 77 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 77 scoped_refptr<SequencedTaskRunner> task_runner_; |
| 78 bool cancelled_; | 78 bool cancelled_; |
| 79 }; | 79 }; |
| 80 | 80 |
| 81 FilePathWatcher(); | 81 FilePathWatcher(); |
| 82 ~FilePathWatcher(); | 82 ~FilePathWatcher(); |
| 83 | 83 |
| 84 // A callback that always cleans up the PlatformDelegate, either when executed | 84 // 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 | 85 // or when deleted without having been executed at all, as can happen during |
| 86 // shutdown. | 86 // shutdown. |
| 87 static void CancelWatch(const scoped_refptr<PlatformDelegate>& delegate); | 87 static void CancelWatch(const scoped_refptr<PlatformDelegate>& delegate); |
| 88 | 88 |
| 89 // Returns true if the platform and OS version support recursive watches. | 89 // Returns true if the platform and OS version support recursive watches. |
| 90 static bool RecursiveWatchAvailable(); | 90 static bool RecursiveWatchAvailable(); |
| 91 | 91 |
| 92 // Invokes |callback| whenever updates to |path| are detected. This should be | 92 // 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 | 93 // 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 | 94 // children. The callback will be invoked on the same thread. Returns true on |
|
gab
2016/12/23 15:35:14
Update "thread"
fdoray
2016/12/23 17:37:42
Done.
| |
| 95 // success. | 95 // success. |
| 96 // | 96 // |
| 97 // On POSIX, this must be called from a thread that supports | 97 // On POSIX, this must be called from a thread that supports |
| 98 // FileDescriptorWatcher. | 98 // FileDescriptorWatcher. |
| 99 // | 99 // |
| 100 // Recursive watch is not supported on all platforms and file systems. | 100 // Recursive watch is not supported on all platforms and file systems. |
| 101 // Watch() will return false in the case of failure. | 101 // Watch() will return false in the case of failure. |
| 102 bool Watch(const FilePath& path, bool recursive, const Callback& callback); | 102 bool Watch(const FilePath& path, bool recursive, const Callback& callback); |
| 103 | 103 |
| 104 private: | 104 private: |
| 105 scoped_refptr<PlatformDelegate> impl_; | 105 scoped_refptr<PlatformDelegate> impl_; |
| 106 | 106 |
| 107 SequenceChecker sequence_checker_; | 107 SequenceChecker sequence_checker_; |
| 108 | 108 |
| 109 DISALLOW_COPY_AND_ASSIGN(FilePathWatcher); | 109 DISALLOW_COPY_AND_ASSIGN(FilePathWatcher); |
| 110 }; | 110 }; |
| 111 | 111 |
| 112 } // namespace base | 112 } // namespace base |
| 113 | 113 |
| 114 #endif // BASE_FILES_FILE_PATH_WATCHER_H_ | 114 #endif // BASE_FILES_FILE_PATH_WATCHER_H_ |
| OLD | NEW |