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