[email protected] | b4481b22 | 2012-03-16 17:13:11 | [diff] [blame^] | 1 | // Copyright (c) 2012 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 | #ifndef NET_FILE_PATH_WATCHER_CALLBACK_H_ |
| 6 | #define NET_FILE_PATH_WATCHER_WRAPPER_H_ |
| 7 | #pragma once |
| 8 | |
| 9 | #include <string> |
| 10 | |
| 11 | #include "base/basictypes.h" |
| 12 | #include "base/callback.h" |
| 13 | #include "base/file_path.h" |
| 14 | #include "base/memory/ref_counted.h" |
| 15 | #include "base/memory/scoped_ptr.h" |
| 16 | #include "base/threading/non_thread_safe.h" |
| 17 | #include "net/base/net_export.h" |
| 18 | |
| 19 | namespace base { |
| 20 | namespace files { |
| 21 | class FilePathWatcher; |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | namespace net { |
| 26 | |
| 27 | // Convenience wrapper which holds a FilePathWatcher and a |
| 28 | // FilePathWatcher::Delegate which forwards its calls to a Callback. |
| 29 | class NET_EXPORT FilePathWatcherWrapper |
| 30 | : NON_EXPORTED_BASE(public base::NonThreadSafe) { |
| 31 | public: |
| 32 | FilePathWatcherWrapper(); |
| 33 | // When deleted, automatically cancels the FilePathWatcher. |
| 34 | virtual ~FilePathWatcherWrapper(); |
| 35 | |
| 36 | // Starts watching the file at |path|. Returns true if Watch succeeds. |
| 37 | // If so, the delegate will call callback.Run(true) from OnFilePathChanged and |
| 38 | // callback.Run(false) from OnFilePathError. After failure the watch is |
| 39 | // cancelled and will have to be restarted. If called again, the previous |
| 40 | // watch is cancelled. |
| 41 | bool Watch(const FilePath& path, |
| 42 | const base::Callback<void(bool succeeded)>& callback); |
| 43 | |
| 44 | bool IsWatching() const; |
| 45 | |
| 46 | // Cancels the current watch. |
| 47 | void Cancel(); |
| 48 | |
| 49 | protected: |
| 50 | // Creates a FilePathWatcher. Override to provide a different implementation. |
| 51 | virtual scoped_ptr<base::files::FilePathWatcher> CreateFilePathWatcher(); |
| 52 | |
| 53 | private: |
| 54 | class WatcherDelegate; |
| 55 | |
| 56 | void OnFilePathChanged(); |
| 57 | void OnFilePathError(); |
| 58 | |
| 59 | base::Callback<void(bool succeeded)> callback_; |
| 60 | scoped_ptr<base::files::FilePathWatcher> watcher_; |
| 61 | scoped_refptr<WatcherDelegate> delegate_; |
| 62 | DISALLOW_COPY_AND_ASSIGN(FilePathWatcherWrapper); |
| 63 | }; |
| 64 | |
| 65 | } // namespace net |
| 66 | |
| 67 | #endif // NET_FILE_PATH_WATCHER_CALLBACK_H_ |
| 68 | |