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 for changes. | 5 // This module provides a way to monitor a file for changes. |
6 | 6 |
7 #ifndef BASE_FILE_WATCHER_H_ | 7 #ifndef CHROME_BROWSER_FILE_WATCHER_H_ |
8 #define BASE_FILE_WATCHER_H_ | 8 #define CHROME_BROWSER_FILE_WATCHER_H_ |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "base/ref_counted.h" | 11 #include "base/ref_counted.h" |
| 12 #include "chrome/browser/chrome_thread.h" |
12 | 13 |
13 class FilePath; | 14 class FilePath; |
14 class MessageLoop; | |
15 | |
16 // This class lets you register interest in changes on a file. The delegate | 15 // This class lets you register interest in changes on a file. The delegate |
17 // will get called whenever the file is changed, including created or deleted. | 16 // will get called whenever the file is changed, including created or deleted. |
18 // WARNING: To be able to get create/delete notifications and to work cross | 17 // WARNING: To be able to get create/delete notifications and to work cross |
19 // platform, we actually listen for changes to the directory containing | 18 // platform, we actually listen for changes to the directory containing |
20 // the file. | 19 // the file. |
21 // WARNING: On OSX and Windows, the OS API doesn't tell us which file in the | 20 // WARNING: On OSX and Windows, the OS API doesn't tell us which file in the |
22 // directory changed. We work around this by watching the file time, but this | 21 // directory changed. We work around this by watching the file time, but this |
23 // can result in some extra notifications if we get other notifications within | 22 // can result in some extra notifications if we get other notifications within |
24 // 2s of the file having changed. | 23 // 2s of the file having changed. |
25 class FileWatcher { | 24 class FileWatcher { |
26 public: | 25 public: |
27 class Delegate { | 26 class Delegate { |
28 public: | 27 public: |
29 virtual ~Delegate() {} | 28 virtual ~Delegate() {} |
30 virtual void OnFileChanged(const FilePath& path) = 0; | 29 virtual void OnFileChanged(const FilePath& path) = 0; |
31 }; | 30 }; |
32 | 31 |
33 FileWatcher(); | 32 FileWatcher(); |
34 ~FileWatcher() {} | 33 ~FileWatcher() {} |
35 | 34 |
36 // Register interest in any changes on the file |path|. | 35 // Register interest in any changes on the file |path|. |
37 // OnFileChanged will be called back for each change to the file. | 36 // OnFileChanged will be called back for each change to the file. Any |
38 // Any background operations will be ran on |backend_loop|, or inside Watch | 37 // background operations will be ran on |backend_thread_id|. Note: The |
39 // if |backend_loop| is NULL. Note: The directory containing |path| must | 38 // directory containing |path| must exist before you try to watch the file. |
40 // exist before you try to watch the file. | 39 // Returns false if the watch can't be added. |
41 // Returns false on error. | 40 bool Watch(const FilePath& path, Delegate* delegate) { |
42 bool Watch(const FilePath& path, Delegate* delegate, | 41 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE)); |
43 MessageLoop* backend_loop) { | 42 return impl_->Watch(path, delegate); |
44 return impl_->Watch(path, delegate, backend_loop); | |
45 } | 43 } |
46 | 44 |
47 // Used internally to encapsulate different members on different platforms. | 45 // Used internally to encapsulate different members on different platforms. |
48 class PlatformDelegate : public base::RefCounted<PlatformDelegate> { | 46 class PlatformDelegate |
| 47 : public base::RefCountedThreadSafe<PlatformDelegate, |
| 48 ChromeThread::DeleteOnFileThread> { |
49 public: | 49 public: |
50 virtual bool Watch(const FilePath& path, Delegate* delegate, | 50 virtual ~PlatformDelegate() {} |
51 MessageLoop* backend_loop) = 0; | |
52 | 51 |
53 protected: | 52 virtual bool Watch(const FilePath& path, Delegate* delegate) = 0; |
54 friend class base::RefCounted<PlatformDelegate>; | |
55 | |
56 virtual ~PlatformDelegate() {} | |
57 }; | 53 }; |
58 | 54 |
59 private: | 55 private: |
60 scoped_refptr<PlatformDelegate> impl_; | 56 scoped_refptr<PlatformDelegate> impl_; |
61 | 57 |
62 DISALLOW_COPY_AND_ASSIGN(FileWatcher); | 58 DISALLOW_COPY_AND_ASSIGN(FileWatcher); |
63 }; | 59 }; |
64 | 60 |
65 #endif // BASE_FILE_WATCHER_H_ | 61 #endif // CHROME_BROWSER_FILE_WATCHER_H_ |
OLD | NEW |