Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 CONTENT_COMMON_FILE_PATH_WATCHER_FILE_PATH_WATCHER_H_ | 7 #ifndef CONTENT_COMMON_FILE_PATH_WATCHER_FILE_PATH_WATCHER_H_ |
| 8 #define CONTENT_COMMON_FILE_PATH_WATCHER_FILE_PATH_WATCHER_H_ | 8 #define CONTENT_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/message_loop_proxy.h" |
| 14 #include "base/ref_counted.h" | 14 #include "base/ref_counted.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, FilePathWatcher has slightly different semantics on OS X |
| 20 // an actual change to the watch target. | 20 // than on Windows or Linux. FilePathWatcher on Linux and Windows will detect |
| 21 // modifications to files in a watched directory. FilePathWatcher on Mac will | |
| 22 // detect the creation and deletion of files in a watched directory, but will | |
| 23 // not detect modifications to those files. See file_path_watcher_mac.cc for | |
| 24 // details. | |
|
Mark Mentovai
2011/03/29 21:27:25
Thanks for revising this comment to match the new
| |
| 21 class FilePathWatcher { | 25 class FilePathWatcher { |
| 22 public: | 26 public: |
| 23 // Declares the callback client code implements to receive notifications. Note | 27 // Declares the callback client code implements to receive notifications. Note |
| 24 // that implementations of this interface should not keep a reference to the | 28 // that implementations of this interface should not keep a reference to the |
| 25 // corresponding FileWatcher object to prevent a reference cycle. | 29 // corresponding FileWatcher object to prevent a reference cycle. |
| 26 class Delegate : public base::RefCountedThreadSafe<Delegate> { | 30 class Delegate : public base::RefCountedThreadSafe<Delegate> { |
| 27 public: | 31 public: |
| 28 virtual ~Delegate() {} | 32 virtual ~Delegate() {} |
| 29 virtual void OnFilePathChanged(const FilePath& path) = 0; | 33 virtual void OnFilePathChanged(const FilePath& path) = 0; |
| 30 // Called when platform specific code detected an error. The watcher will | 34 // Called when platform specific code detected an error. The watcher will |
| 31 // not call OnFilePathChanged for future changes. | 35 // not call OnFilePathChanged for future changes. |
| 32 virtual void OnError() {} | 36 virtual void OnError() {} |
| 33 }; | 37 }; |
| 34 | 38 |
| 35 FilePathWatcher(); | 39 FilePathWatcher(); |
| 36 ~FilePathWatcher(); | 40 ~FilePathWatcher(); |
| 37 | 41 |
| 38 // Register interest in any changes on |path|. OnPathChanged will be called | 42 // Register interest in any changes on |path|. OnPathChanged will be called |
| 39 // back for each change. Returns true on success. |loop| is only used | 43 // back for each change. Returns true on success. |
| 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 // OnFilePathChanged() will be called on the same thread as Watch() is called, | 44 // OnFilePathChanged() will be called on the same thread as Watch() is called, |
| 44 // which should have a MessageLoop of TYPE_IO. | 45 // which should have a MessageLoop of TYPE_IO. |
| 45 bool Watch(const FilePath& path, | 46 bool Watch(const FilePath& path, Delegate* delegate) WARN_UNUSED_RESULT; |
| 46 Delegate* delegate, | |
| 47 base::MessageLoopProxy* loop) WARN_UNUSED_RESULT; | |
| 48 | 47 |
| 49 class PlatformDelegate; | 48 class PlatformDelegate; |
| 50 | 49 |
| 51 // A custom Task that always cleans up the PlatformDelegate, either when | 50 // A custom Task that always cleans up the PlatformDelegate, either when |
| 52 // executed or when deleted without having been executed at all, as can | 51 // executed or when deleted without having been executed at all, as can |
| 53 // happen during shutdown. | 52 // happen during shutdown. |
| 54 class CancelTask : public Task { | 53 class CancelTask : public Task { |
| 55 public: | 54 public: |
| 56 CancelTask(PlatformDelegate* delegate): delegate_(delegate) {} | 55 CancelTask(PlatformDelegate* delegate): delegate_(delegate) {} |
| 57 virtual ~CancelTask() { | 56 virtual ~CancelTask() { |
| 58 delegate_->CancelOnMessageLoopThread(); | 57 delegate_->CancelOnMessageLoopThread(); |
| 59 } | 58 } |
| 60 | 59 |
| 61 virtual void Run() { | 60 virtual void Run() { |
| 62 delegate_->CancelOnMessageLoopThread(); | 61 delegate_->CancelOnMessageLoopThread(); |
| 63 } | 62 } |
| 64 private: | 63 private: |
| 65 scoped_refptr<PlatformDelegate> delegate_; | 64 scoped_refptr<PlatformDelegate> delegate_; |
| 66 | 65 |
| 67 DISALLOW_COPY_AND_ASSIGN(CancelTask); | 66 DISALLOW_COPY_AND_ASSIGN(CancelTask); |
| 68 }; | 67 }; |
| 69 | 68 |
| 70 // Used internally to encapsulate different members on different platforms. | 69 // Used internally to encapsulate different members on different platforms. |
| 71 class PlatformDelegate : public base::RefCountedThreadSafe<PlatformDelegate> { | 70 class PlatformDelegate : public base::RefCountedThreadSafe<PlatformDelegate> { |
| 72 public: | 71 public: |
| 73 PlatformDelegate(); | 72 PlatformDelegate(); |
| 74 | 73 |
| 75 // Start watching for the given |path| and notify |delegate| about changes. | 74 // Start watching for the given |path| and notify |delegate| about changes. |
| 76 // |loop| is only used by the Mac implementation right now, and must be | 75 virtual bool Watch(const FilePath& path, |
| 77 // backed by a CFRunLoop based MessagePump. This is usually going to be a | 76 Delegate* delegate) WARN_UNUSED_RESULT = 0; |
| 78 // MessageLoop of type TYPE_UI. | |
| 79 virtual bool Watch( | |
| 80 const FilePath& path, | |
| 81 Delegate* delegate, | |
| 82 base::MessageLoopProxy* loop) WARN_UNUSED_RESULT = 0; | |
| 83 | 77 |
| 84 // Stop watching. This is called from FilePathWatcher's dtor in order to | 78 // Stop watching. This is called from FilePathWatcher's dtor in order to |
| 85 // allow to shut down properly while the object is still alive. | 79 // allow to shut down properly while the object is still alive. |
| 86 // It can be called from any thread. | 80 // It can be called from any thread. |
| 87 virtual void Cancel() = 0; | 81 virtual void Cancel() = 0; |
| 88 | 82 |
| 89 protected: | 83 protected: |
| 90 virtual ~PlatformDelegate(); | 84 virtual ~PlatformDelegate(); |
| 91 | 85 |
| 92 // Stop watching. This is only called on the thread of the appropriate | 86 // Stop watching. This is only called on the thread of the appropriate |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 119 bool cancelled_; | 113 bool cancelled_; |
| 120 }; | 114 }; |
| 121 | 115 |
| 122 private: | 116 private: |
| 123 scoped_refptr<PlatformDelegate> impl_; | 117 scoped_refptr<PlatformDelegate> impl_; |
| 124 | 118 |
| 125 DISALLOW_COPY_AND_ASSIGN(FilePathWatcher); | 119 DISALLOW_COPY_AND_ASSIGN(FilePathWatcher); |
| 126 }; | 120 }; |
| 127 | 121 |
| 128 #endif // CONTENT_COMMON_FILE_PATH_WATCHER_FILE_PATH_WATCHER_H_ | 122 #endif // CONTENT_COMMON_FILE_PATH_WATCHER_FILE_PATH_WATCHER_H_ |
| OLD | NEW |