OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 // This module provides a way to monitor a file or directory for changes. | |
6 | |
7 #ifndef CONTENT_COMMON_FILE_PATH_WATCHER_FILE_PATH_WATCHER_H_ | |
8 #define CONTENT_COMMON_FILE_PATH_WATCHER_FILE_PATH_WATCHER_H_ | |
9 #pragma once | |
10 | |
11 #include "base/basictypes.h" | |
12 #include "base/file_path.h" | |
13 #include "base/memory/ref_counted.h" | |
14 #include "base/message_loop_proxy.h" | |
15 | |
16 // This class lets you register interest in changes on a FilePath. | |
17 // The delegate will get called whenever the file or directory referenced by the | |
18 // FilePath is changed, including created or deleted. Due to limitations in the | |
19 // underlying OS APIs, FilePathWatcher has slightly different semantics on OS X | |
20 // than on Windows or Linux. FilePathWatcher on Linux and Windows will detect | |
21 // modifications to files in a watched directory. FilePathWatcher on Mac will | |
22 // detect the creation and deletion of files in a watched directory, but will | |
23 // not detect modifications to those files. See file_path_watcher_mac.cc for | |
24 // details. | |
25 class FilePathWatcher { | |
26 public: | |
27 // Declares the callback client code implements to receive notifications. Note | |
28 // that implementations of this interface should not keep a reference to the | |
29 // corresponding FileWatcher object to prevent a reference cycle. | |
30 class Delegate : public base::RefCountedThreadSafe<Delegate> { | |
31 public: | |
32 virtual ~Delegate() {} | |
33 virtual void OnFilePathChanged(const FilePath& path) = 0; | |
34 // Called when platform specific code detected an error. The watcher will | |
35 // not call OnFilePathChanged for future changes. | |
36 virtual void OnFilePathError(const FilePath& path) {} | |
37 }; | |
38 | |
39 FilePathWatcher(); | |
40 ~FilePathWatcher(); | |
41 | |
42 // Register interest in any changes on |path|. OnPathChanged will be called | |
43 // back for each change. Returns true on success. | |
44 // OnFilePathChanged() will be called on the same thread as Watch() is called, | |
45 // which should have a MessageLoop of TYPE_IO. | |
46 bool Watch(const FilePath& path, Delegate* delegate) WARN_UNUSED_RESULT; | |
47 | |
48 class PlatformDelegate; | |
49 | |
50 // A custom Task that always cleans up the PlatformDelegate, either when | |
51 // executed or when deleted without having been executed at all, as can | |
52 // happen during shutdown. | |
53 class CancelTask : public Task { | |
54 public: | |
55 CancelTask(PlatformDelegate* delegate): delegate_(delegate) {} | |
56 virtual ~CancelTask() { | |
57 delegate_->CancelOnMessageLoopThread(); | |
58 } | |
59 | |
60 virtual void Run() { | |
61 delegate_->CancelOnMessageLoopThread(); | |
62 } | |
63 private: | |
64 scoped_refptr<PlatformDelegate> delegate_; | |
65 | |
66 DISALLOW_COPY_AND_ASSIGN(CancelTask); | |
67 }; | |
68 | |
69 // Used internally to encapsulate different members on different platforms. | |
70 class PlatformDelegate : public base::RefCountedThreadSafe<PlatformDelegate> { | |
71 public: | |
72 PlatformDelegate(); | |
73 | |
74 // Start watching for the given |path| and notify |delegate| about changes. | |
75 virtual bool Watch(const FilePath& path, | |
76 Delegate* delegate) WARN_UNUSED_RESULT = 0; | |
77 | |
78 // Stop watching. This is called from FilePathWatcher's dtor in order to | |
79 // allow to shut down properly while the object is still alive. | |
80 // It can be called from any thread. | |
81 virtual void Cancel() = 0; | |
82 | |
83 protected: | |
84 virtual ~PlatformDelegate(); | |
85 | |
86 // Stop watching. This is only called on the thread of the appropriate | |
87 // message loop. Since it can also be called more than once, it should | |
88 // check |is_cancelled()| to avoid duplicate work. | |
89 virtual void CancelOnMessageLoopThread() = 0; | |
90 | |
91 scoped_refptr<base::MessageLoopProxy> message_loop() const { | |
92 return message_loop_; | |
93 } | |
94 | |
95 void set_message_loop(base::MessageLoopProxy* loop) { | |
96 message_loop_ = loop; | |
97 } | |
98 | |
99 // Must be called before the PlatformDelegate is deleted. | |
100 void set_cancelled() { | |
101 cancelled_ = true; | |
102 } | |
103 | |
104 bool is_cancelled() const { | |
105 return cancelled_; | |
106 } | |
107 | |
108 private: | |
109 friend class base::RefCountedThreadSafe<PlatformDelegate>; | |
110 friend class CancelTask; | |
111 | |
112 scoped_refptr<base::MessageLoopProxy> message_loop_; | |
113 bool cancelled_; | |
114 }; | |
115 | |
116 private: | |
117 scoped_refptr<PlatformDelegate> impl_; | |
118 | |
119 DISALLOW_COPY_AND_ASSIGN(FilePathWatcher); | |
120 }; | |
121 | |
122 #endif // CONTENT_COMMON_FILE_PATH_WATCHER_FILE_PATH_WATCHER_H_ | |
OLD | NEW |