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/single_thread_task_runner.h" | 16 #include "base/single_thread_task_runner.h" |
16 | 17 |
17 namespace base { | 18 namespace base { |
18 | 19 |
19 // This class lets you register interest in changes on a FilePath. | 20 // This class lets you register interest in changes on a FilePath. |
20 // 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 |
21 // 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 |
22 // underlying OS APIs, FilePathWatcher has slightly different semantics on OS X | 23 // underlying OS APIs, FilePathWatcher has slightly different semantics on OS X |
23 // 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 |
24 // modifications to files in a watched directory. FilePathWatcher on Mac will | 25 // modifications to files in a watched directory. FilePathWatcher on Mac will |
25 // 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 |
26 // 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 |
27 // details. | 28 // details. |
| 29 // |
| 30 // Must be destroyed on the sequence that invokes Watch(). |
28 class BASE_EXPORT FilePathWatcher { | 31 class BASE_EXPORT FilePathWatcher { |
29 public: | 32 public: |
30 // Callback type for Watch(). |path| points to the file that was updated, | 33 // Callback type for Watch(). |path| points to the file that was updated, |
31 // and |error| is true if the platform specific code detected an error. In | 34 // and |error| is true if the platform specific code detected an error. In |
32 // that case, the callback won't be invoked again. | 35 // that case, the callback won't be invoked again. |
33 typedef base::Callback<void(const FilePath& path, bool error)> Callback; | 36 typedef base::Callback<void(const FilePath& path, bool error)> Callback; |
34 | 37 |
35 // Used internally to encapsulate different members on different platforms. | 38 // Used internally to encapsulate different members on different platforms. |
36 class PlatformDelegate : public base::RefCountedThreadSafe<PlatformDelegate> { | 39 class PlatformDelegate : public base::RefCountedThreadSafe<PlatformDelegate> { |
37 public: | 40 public: |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
69 bool is_cancelled() const { | 72 bool is_cancelled() const { |
70 return cancelled_; | 73 return cancelled_; |
71 } | 74 } |
72 | 75 |
73 private: | 76 private: |
74 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 77 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
75 bool cancelled_; | 78 bool cancelled_; |
76 }; | 79 }; |
77 | 80 |
78 FilePathWatcher(); | 81 FilePathWatcher(); |
79 virtual ~FilePathWatcher(); | 82 ~FilePathWatcher(); |
80 | 83 |
81 // A callback that always cleans up the PlatformDelegate, either when executed | 84 // A callback that always cleans up the PlatformDelegate, either when executed |
82 // 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 |
83 // shutdown. | 86 // shutdown. |
84 static void CancelWatch(const scoped_refptr<PlatformDelegate>& delegate); | 87 static void CancelWatch(const scoped_refptr<PlatformDelegate>& delegate); |
85 | 88 |
86 // Returns true if the platform and OS version support recursive watches. | 89 // Returns true if the platform and OS version support recursive watches. |
87 static bool RecursiveWatchAvailable(); | 90 static bool RecursiveWatchAvailable(); |
88 | 91 |
89 // Invokes |callback| whenever updates to |path| are detected. This should be | 92 // Invokes |callback| whenever updates to |path| are detected. This should be |
90 // called at most once, and from a MessageLoop of TYPE_IO. Set |recursive| to | 93 // called at most once, and from a MessageLoop of TYPE_IO. Set |recursive| to |
91 // true, to watch |path| and its children. The callback will be invoked on | 94 // true, to watch |path| and its children. The callback will be invoked on |
92 // the same loop. Returns true on success. | 95 // the same loop. Returns true on success. |
93 // | 96 // |
94 // Recursive watch is not supported on all platforms and file systems. | 97 // Recursive watch is not supported on all platforms and file systems. |
95 // Watch() will return false in the case of failure. | 98 // Watch() will return false in the case of failure. |
96 bool Watch(const FilePath& path, bool recursive, const Callback& callback); | 99 bool Watch(const FilePath& path, bool recursive, const Callback& callback); |
97 | 100 |
98 private: | 101 private: |
99 scoped_refptr<PlatformDelegate> impl_; | 102 scoped_refptr<PlatformDelegate> impl_; |
100 | 103 |
| 104 SequenceChecker sequence_checker_; |
| 105 |
101 DISALLOW_COPY_AND_ASSIGN(FilePathWatcher); | 106 DISALLOW_COPY_AND_ASSIGN(FilePathWatcher); |
102 }; | 107 }; |
103 | 108 |
104 } // namespace base | 109 } // namespace base |
105 | 110 |
106 #endif // BASE_FILES_FILE_PATH_WATCHER_H_ | 111 #endif // BASE_FILES_FILE_PATH_WATCHER_H_ |
OLD | NEW |