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