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 filepath for changes in the path. |
| 6 |
| 7 #ifndef BASE_FILE_PATH_COMPONENT_WATCHER_H_ |
| 8 #define BASE_FILE_PATH_COMPONENT_WATCHER_H_ |
| 9 #pragma once |
| 10 |
| 11 #include "base/basictypes.h" |
| 12 #include "base/file_path.h" |
| 13 #include "base/ref_counted.h" |
| 14 |
| 15 namespace base { |
| 16 // This class lets you register interest in changes on a FilePath. |
| 17 // The delegate will get called whenever any components of a FilePath are |
| 18 // changed (moved, deleted, attributes changed). This is different from |
| 19 // FilePathWatcher, in that FilePathWatcher watches a single file, and it's |
| 20 // parent directory for changes, whereas this watches each component in a file's |
| 21 // path for changes. |
| 22 // A watcher will only register a single notification. |
| 23 class FilePathComponentWatcher { |
| 24 public: |
| 25 // Declares the callback client code implements to receive notifications. Note |
| 26 // that implementations of this interface should not keep a reference to the |
| 27 // corresponding FileWatcher object to prevent a reference cycle. |
| 28 class Delegate : public base::RefCountedThreadSafe<Delegate> { |
| 29 public: |
| 30 virtual ~Delegate() {} |
| 31 virtual void OnFilePathComponentsChanged(const FilePath& old_path, |
| 32 const FilePath& new_path) = 0; |
| 33 // Called when platform specific code detected an error. The watcher will |
| 34 // not call OnFilePathChanged for future changes. |
| 35 virtual void OnError() {} |
| 36 }; |
| 37 |
| 38 FilePathComponentWatcher(); |
| 39 ~FilePathComponentWatcher(); |
| 40 |
| 41 // Register interest in any changes on |path|. OnPathChanged will be called |
| 42 // back for each change. Returns true on success. |
| 43 bool Watch(const FilePath& path, Delegate* delegate) WARN_UNUSED_RESULT; |
| 44 |
| 45 // Used internally to encapsulate different members on different platforms. |
| 46 class PlatformDelegate |
| 47 : public base::RefCountedThreadSafe<PlatformDelegate> { |
| 48 public: |
| 49 virtual ~PlatformDelegate() {} |
| 50 |
| 51 // Start watching for the given |path| and notify |delegate| about changes. |
| 52 virtual bool Watch(const FilePath& path, Delegate* delegate) |
| 53 WARN_UNUSED_RESULT = 0; |
| 54 |
| 55 // Stop watching. This is called from FilePathComponentWatcher's dtor |
| 56 // in order to allow it to shut down properly while the object is still |
| 57 // alive. |
| 58 virtual void Cancel() {} |
| 59 }; |
| 60 |
| 61 private: |
| 62 scoped_refptr<PlatformDelegate> impl_; |
| 63 |
| 64 DISALLOW_COPY_AND_ASSIGN(FilePathComponentWatcher); |
| 65 }; |
| 66 |
| 67 } // namespace |
| 68 |
| 69 #endif // BASE_FILE_PATH_COMPONENT_WATCHER_H_ |
OLD | NEW |