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