OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 CHROME_BROWSER_FILE_PATH_WATCHER_FILE_PATH_WATCHER_H_ | 7 #ifndef CHROME_COMMON_FILE_PATH_WATCHER_FILE_PATH_WATCHER_H_ |
8 #define CHROME_BROWSER_FILE_PATH_WATCHER_FILE_PATH_WATCHER_H_ | 8 #define CHROME_COMMON_FILE_PATH_WATCHER_FILE_PATH_WATCHER_H_ |
9 #pragma once | 9 #pragma once |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/file_path.h" | 12 #include "base/file_path.h" |
| 13 #include "base/message_loop_proxy.h" |
13 #include "base/ref_counted.h" | 14 #include "base/ref_counted.h" |
14 #include "content/browser/browser_thread.h" | |
15 | 15 |
16 // This class lets you register interest in changes on a FilePath. | 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 | 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 | 18 // FilePath is changed, including created or deleted. Due to limitations in the |
19 // underlying OS APIs, spurious notifications might occur that don't relate to | 19 // underlying OS APIs, spurious notifications might occur that don't relate to |
20 // an actual change to the watch target. | 20 // an actual change to the watch target. |
21 class FilePathWatcher { | 21 class FilePathWatcher { |
22 public: | 22 public: |
23 // Declares the callback client code implements to receive notifications. Note | 23 // Declares the callback client code implements to receive notifications. Note |
24 // that implementations of this interface should not keep a reference to the | 24 // that implementations of this interface should not keep a reference to the |
25 // corresponding FileWatcher object to prevent a reference cycle. | 25 // corresponding FileWatcher object to prevent a reference cycle. |
26 class Delegate : public base::RefCountedThreadSafe<Delegate> { | 26 class Delegate : public base::RefCountedThreadSafe<Delegate> { |
27 public: | 27 public: |
28 virtual ~Delegate() {} | 28 virtual ~Delegate() {} |
29 virtual void OnFilePathChanged(const FilePath& path) = 0; | 29 virtual void OnFilePathChanged(const FilePath& path) = 0; |
30 // Called when platform specific code detected an error. The watcher will | 30 // Called when platform specific code detected an error. The watcher will |
31 // not call OnFilePathChanged for future changes. | 31 // not call OnFilePathChanged for future changes. |
32 virtual void OnError() {} | 32 virtual void OnError() {} |
33 }; | 33 }; |
34 | 34 |
35 FilePathWatcher(); | 35 FilePathWatcher(); |
36 ~FilePathWatcher(); | 36 ~FilePathWatcher(); |
37 | 37 |
38 // Register interest in any changes on |path|. OnPathChanged will be called | 38 // Register interest in any changes on |path|. OnPathChanged will be called |
39 // back for each change. Returns true on success. | 39 // back for each change. Returns true on success. |loop| is only used |
40 bool Watch(const FilePath& path, Delegate* delegate) WARN_UNUSED_RESULT; | 40 // by the Mac implementation right now, and must be backed by a CFRunLoop |
| 41 // based MessagePump. This is usually going to be a MessageLoop of type |
| 42 // TYPE_UI. |
| 43 bool Watch(const FilePath& path, |
| 44 Delegate* delegate, |
| 45 scoped_refptr<base::MessageLoopProxy> loop) WARN_UNUSED_RESULT; |
| 46 |
| 47 class PlatformDelegate; |
| 48 |
| 49 // Traits for PlatformDelegate, which must delete itself on the IO message |
| 50 // loop that Watch was called from. |
| 51 struct DeletePlatformDelegate { |
| 52 static void Destruct(const PlatformDelegate* delegate); |
| 53 }; |
41 | 54 |
42 // Used internally to encapsulate different members on different platforms. | 55 // Used internally to encapsulate different members on different platforms. |
43 class PlatformDelegate | 56 class PlatformDelegate |
44 : public base::RefCountedThreadSafe<PlatformDelegate, | 57 : public base::RefCountedThreadSafe<PlatformDelegate, |
45 BrowserThread::DeleteOnFileThread> { | 58 DeletePlatformDelegate> { |
46 public: | 59 public: |
47 // Start watching for the given |path| and notify |delegate| about changes. | 60 // Start watching for the given |path| and notify |delegate| about changes. |
48 virtual bool Watch(const FilePath& path, Delegate* delegate) | 61 // |loop| is only used by the Mac implementation right now, and must be |
49 WARN_UNUSED_RESULT = 0; | 62 // backed by a CFRunLoop based MessagePump. This is usually going to be a |
| 63 // MessageLoop of type TYPE_UI. |
| 64 virtual bool Watch( |
| 65 const FilePath& path, |
| 66 Delegate* delegate, |
| 67 scoped_refptr<base::MessageLoopProxy> loop) WARN_UNUSED_RESULT = 0; |
50 | 68 |
51 // Stop watching. This is called from FilePathWatcher's dtor in order to | 69 // Stop watching. This is called from FilePathWatcher's dtor in order to |
52 // allow to shut down properly while the object is still alive. | 70 // allow to shut down properly while the object is still alive. |
53 virtual void Cancel() {} | 71 virtual void Cancel() = 0; |
| 72 |
| 73 scoped_refptr<base::MessageLoopProxy> message_loop() const { |
| 74 return message_loop_; |
| 75 } |
| 76 void set_message_loop(scoped_refptr<base::MessageLoopProxy> loop); |
54 | 77 |
55 protected: | 78 protected: |
56 friend struct BrowserThread::DeleteOnThread<BrowserThread::FILE>; | |
57 friend class DeleteTask<PlatformDelegate>; | 79 friend class DeleteTask<PlatformDelegate>; |
| 80 friend struct DeletePlatformDelegate; |
| 81 virtual ~PlatformDelegate(); |
58 | 82 |
59 virtual ~PlatformDelegate() {} | 83 private: |
| 84 // IO Message Loop. |
| 85 scoped_refptr<base::MessageLoopProxy> message_loop_; |
60 }; | 86 }; |
61 | 87 |
62 private: | 88 private: |
63 scoped_refptr<PlatformDelegate> impl_; | 89 scoped_refptr<PlatformDelegate> impl_; |
64 | 90 |
65 DISALLOW_COPY_AND_ASSIGN(FilePathWatcher); | 91 DISALLOW_COPY_AND_ASSIGN(FilePathWatcher); |
66 }; | 92 }; |
67 | 93 |
68 #endif // CHROME_BROWSER_FILE_PATH_WATCHER_FILE_PATH_WATCHER_H_ | 94 #endif // CHROME_COMMON_FILE_PATH_WATCHER_FILE_PATH_WATCHER_H_ |
OLD | NEW |