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_BROWSER_FILE_PATH_WATCHER_FILE_PATH_WATCHER_H_ |
8 #define CHROME_BROWSER_FILE_PATH_WATCHER_FILE_PATH_WATCHER_H_ | 8 #define CHROME_BROWSER_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/ref_counted.h" | 13 #include "base/ref_counted.h" |
14 #include "content/browser/browser_thread.h" | |
15 | 14 |
16 // This class lets you register interest in changes on a FilePath. | 15 // 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 | 16 // 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 | 17 // 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 | 18 // underlying OS APIs, spurious notifications might occur that don't relate to |
20 // an actual change to the watch target. | 19 // an actual change to the watch target. |
21 class FilePathWatcher { | 20 class FilePathWatcher { |
22 public: | 21 public: |
23 // Declares the callback client code implements to receive notifications. Note | 22 // Declares the callback client code implements to receive notifications. Note |
24 // that implementations of this interface should not keep a reference to the | 23 // that implementations of this interface should not keep a reference to the |
25 // corresponding FileWatcher object to prevent a reference cycle. | 24 // corresponding FileWatcher object to prevent a reference cycle. |
26 class Delegate : public base::RefCountedThreadSafe<Delegate> { | 25 class Delegate : public base::RefCountedThreadSafe<Delegate> { |
27 public: | 26 public: |
28 virtual ~Delegate() {} | 27 virtual ~Delegate() {} |
29 virtual void OnFilePathChanged(const FilePath& path) = 0; | 28 virtual void OnFilePathChanged(const FilePath& path) = 0; |
30 // Called when platform specific code detected an error. The watcher will | 29 // Called when platform specific code detected an error. The watcher will |
31 // not call OnFilePathChanged for future changes. | 30 // not call OnFilePathChanged for future changes. |
32 virtual void OnError() {} | 31 virtual void OnError() {} |
33 }; | 32 }; |
34 | 33 |
35 FilePathWatcher(); | 34 FilePathWatcher(); |
36 ~FilePathWatcher(); | 35 ~FilePathWatcher(); |
37 | 36 |
38 // Register interest in any changes on |path|. OnPathChanged will be called | 37 // Register interest in any changes on |path|. OnPathChanged will be called |
39 // back for each change. Returns true on success. | 38 // back for each change. Returns true on success. |
Bernhard Bauer
2011/03/15 22:00:07
While you're at it, could you add a comment statin
Joao da Silva
2011/03/18 16:09:17
Done.
| |
40 bool Watch(const FilePath& path, Delegate* delegate) WARN_UNUSED_RESULT; | 39 bool Watch(const FilePath& path, Delegate* delegate) WARN_UNUSED_RESULT; |
41 | 40 |
42 // Used internally to encapsulate different members on different platforms. | 41 // Used internally to encapsulate different members on different platforms. |
43 class PlatformDelegate | 42 class PlatformDelegate: public base::RefCountedThreadSafe<PlatformDelegate> { |
44 : public base::RefCountedThreadSafe<PlatformDelegate, | |
45 BrowserThread::DeleteOnFileThread> { | |
46 public: | 43 public: |
47 // Start watching for the given |path| and notify |delegate| about changes. | 44 // Start watching for the given |path| and notify |delegate| about changes. |
48 virtual bool Watch(const FilePath& path, Delegate* delegate) | 45 virtual bool Watch(const FilePath& path, Delegate* delegate) |
49 WARN_UNUSED_RESULT = 0; | 46 WARN_UNUSED_RESULT = 0; |
50 | 47 |
51 // Stop watching. This is called from FilePathWatcher's dtor in order to | 48 // Stop watching. This is called from FilePathWatcher's dtor in order to |
52 // allow to shut down properly while the object is still alive. | 49 // allow to shut down properly while the object is still alive. |
53 virtual void Cancel() {} | 50 virtual void Cancel() {} |
54 | 51 |
55 protected: | 52 protected: |
56 friend struct BrowserThread::DeleteOnThread<BrowserThread::FILE>; | 53 friend struct base::RefCountedThreadSafe<PlatformDelegate>; |
57 friend class DeleteTask<PlatformDelegate>; | |
58 | 54 |
59 virtual ~PlatformDelegate() {} | 55 virtual ~PlatformDelegate() {} |
60 }; | 56 }; |
61 | 57 |
62 private: | 58 private: |
63 scoped_refptr<PlatformDelegate> impl_; | 59 scoped_refptr<PlatformDelegate> impl_; |
64 | 60 |
65 DISALLOW_COPY_AND_ASSIGN(FilePathWatcher); | 61 DISALLOW_COPY_AND_ASSIGN(FilePathWatcher); |
66 }; | 62 }; |
67 | 63 |
68 #endif // CHROME_BROWSER_FILE_PATH_WATCHER_FILE_PATH_WATCHER_H_ | 64 #endif // CHROME_BROWSER_FILE_PATH_WATCHER_FILE_PATH_WATCHER_H_ |
OLD | NEW |