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