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 #pragma once | 9 #pragma once |
10 | 10 |
11 #include "base/base_export.h" | 11 #include "base/base_export.h" |
12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
13 #include "base/callback.h" | |
13 #include "base/file_path.h" | 14 #include "base/file_path.h" |
14 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
15 #include "base/message_loop_proxy.h" | 16 #include "base/message_loop_proxy.h" |
16 | 17 |
17 namespace base { | 18 namespace base { |
18 namespace files { | 19 namespace files { |
19 | 20 |
20 // This class lets you register interest in changes on a FilePath. | 21 // This class lets you register interest in changes on a FilePath. |
21 // The delegate will get called whenever the file or directory referenced by the | 22 // The delegate will get called whenever the file or directory referenced by the |
22 // FilePath is changed, including created or deleted. Due to limitations in the | 23 // FilePath is changed, including created or deleted. Due to limitations in the |
23 // underlying OS APIs, FilePathWatcher has slightly different semantics on OS X | 24 // underlying OS APIs, FilePathWatcher has slightly different semantics on OS X |
24 // than on Windows or Linux. FilePathWatcher on Linux and Windows will detect | 25 // than on Windows or Linux. FilePathWatcher on Linux and Windows will detect |
25 // modifications to files in a watched directory. FilePathWatcher on Mac will | 26 // 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 | 27 // detect the creation and deletion of files in a watched directory, but will |
27 // not detect modifications to those files. See file_path_watcher_mac.cc for | 28 // not detect modifications to those files. See file_path_watcher_mac.cc for |
28 // details. | 29 // details. |
29 class BASE_EXPORT FilePathWatcher { | 30 class BASE_EXPORT FilePathWatcher { |
30 public: | 31 public: |
32 // Callback type for Watch(). |path| points to the file that was updated, | |
33 // and |error| is true if the platform specific code detected an error. In | |
34 // that case, the callback won't be invoked again. | |
35 typedef base::Callback<void(const FilePath& path, bool error)> Callback; | |
36 | |
31 // Declares the callback client code implements to receive notifications. Note | 37 // Declares the callback client code implements to receive notifications. Note |
32 // that implementations of this interface should not keep a reference to the | 38 // that implementations of this interface should not keep a reference to the |
33 // corresponding FileWatcher object to prevent a reference cycle. | 39 // corresponding FileWatcher object to prevent a reference cycle. |
40 // | |
41 // Deprecated: see comment on Watch() below. | |
34 class Delegate : public base::RefCountedThreadSafe<Delegate> { | 42 class Delegate : public base::RefCountedThreadSafe<Delegate> { |
35 public: | 43 public: |
36 virtual void OnFilePathChanged(const FilePath& path) = 0; | 44 virtual void OnFilePathChanged(const FilePath& path) = 0; |
37 // Called when platform specific code detected an error. The watcher will | 45 // Called when platform specific code detected an error. The watcher will |
38 // not call OnFilePathChanged for future changes. | 46 // not call OnFilePathChanged for future changes. |
39 virtual void OnFilePathError(const FilePath& path) {} | 47 virtual void OnFilePathError(const FilePath& path) {} |
40 | 48 |
41 protected: | 49 protected: |
42 friend class base::RefCountedThreadSafe<Delegate>; | 50 friend class base::RefCountedThreadSafe<Delegate>; |
43 virtual ~Delegate() {} | 51 virtual ~Delegate() {} |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
97 | 105 |
98 // A callback that always cleans up the PlatformDelegate, either when executed | 106 // A callback that always cleans up the PlatformDelegate, either when executed |
99 // or when deleted without having been executed at all, as can happen during | 107 // or when deleted without having been executed at all, as can happen during |
100 // shutdown. | 108 // shutdown. |
101 static void CancelWatch(const scoped_refptr<PlatformDelegate>& delegate); | 109 static void CancelWatch(const scoped_refptr<PlatformDelegate>& delegate); |
102 | 110 |
103 // Register interest in any changes on |path|. OnPathChanged will be called | 111 // Register interest in any changes on |path|. OnPathChanged will be called |
104 // back for each change. Returns true on success. | 112 // back for each change. Returns true on success. |
105 // OnFilePathChanged() will be called on the same thread as Watch() is called, | 113 // OnFilePathChanged() will be called on the same thread as Watch() is called, |
106 // which should have a MessageLoop of TYPE_IO. | 114 // which should have a MessageLoop of TYPE_IO. |
115 // | |
116 // Deprecated: new code should use the callback interface, declared below. | |
117 // The FilePathWatcher::Delegate interface will be removed once all client | |
118 // code has been updated. http://crbug.com/130980 | |
107 virtual bool Watch(const FilePath& path, Delegate* delegate) | 119 virtual bool Watch(const FilePath& path, Delegate* delegate) |
108 WARN_UNUSED_RESULT; | 120 WARN_UNUSED_RESULT; |
109 | 121 |
122 // Invokes |callback| whenever updates to |path| are detected. This should be | |
123 // called at most once, and from a MessageLoop of TYPE_IO. The callback will | |
Mattias Nissler (ping if slow)
2012/06/04 12:26:39
I'm wondering whether it'd be better if we just al
Joao da Silva
2012/06/04 12:45:42
The Watch() implementations do IO operations, so t
| |
124 // be invoked on the same loop. Returns true on success. | |
125 bool Watch(const FilePath& path, const Callback& callback); | |
126 | |
110 private: | 127 private: |
111 scoped_refptr<PlatformDelegate> impl_; | 128 scoped_refptr<PlatformDelegate> impl_; |
112 | 129 |
113 DISALLOW_COPY_AND_ASSIGN(FilePathWatcher); | 130 DISALLOW_COPY_AND_ASSIGN(FilePathWatcher); |
114 }; | 131 }; |
115 | 132 |
116 } // namespace files | 133 } // namespace files |
117 } // namespace base | 134 } // namespace base |
118 | 135 |
119 #endif // BASE_FILES_FILE_PATH_WATCHER_H_ | 136 #endif // BASE_FILES_FILE_PATH_WATCHER_H_ |
OLD | NEW |