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 | // Cross platform methods for FilePathWatcher. See the various platform |
| 6 | // specific implementation files, too. |
| 7 | |
| 8 | #include "base/files/file_path_watcher.h" |
| 9 | |
| 10 | #include "base/logging.h" |
| 11 | #include "base/message_loop.h" |
| 12 | |
| 13 | namespace base { |
| 14 | namespace files { |
| 15 | |
joaodasilva@chromium.org | 5e57506 | 2012-06-04 23:21:27 +0900 | [diff] [blame] | 16 | namespace { |
| 17 | |
| 18 | // A delegate implementation for the callback interface. |
| 19 | class FilePathWatcherDelegate : public base::files::FilePathWatcher::Delegate { |
| 20 | public: |
| 21 | explicit FilePathWatcherDelegate(const FilePathWatcher::Callback& callback) |
| 22 | : callback_(callback) {} |
| 23 | |
| 24 | // FilePathWatcher::Delegate implementation. |
| 25 | virtual void OnFilePathChanged(const FilePath& path) OVERRIDE { |
| 26 | callback_.Run(path, false); |
| 27 | } |
| 28 | |
| 29 | virtual void OnFilePathError(const FilePath& path) OVERRIDE { |
| 30 | callback_.Run(path, true); |
| 31 | } |
| 32 | |
| 33 | private: |
| 34 | virtual ~FilePathWatcherDelegate() {} |
| 35 | |
| 36 | FilePathWatcher::Callback callback_; |
| 37 | |
| 38 | DISALLOW_COPY_AND_ASSIGN(FilePathWatcherDelegate); |
| 39 | }; |
| 40 | |
| 41 | } // namespace |
| 42 | |
craig.schlenter@chromium.org | b2d581e | 2011-04-15 01:56:01 +0900 | [diff] [blame] | 43 | FilePathWatcher::~FilePathWatcher() { |
| 44 | impl_->Cancel(); |
| 45 | } |
| 46 | |
jhawkins@chromium.org | fce1f41 | 2011-11-24 05:44:00 +0900 | [diff] [blame] | 47 | // static |
| 48 | void FilePathWatcher::CancelWatch( |
| 49 | const scoped_refptr<PlatformDelegate>& delegate) { |
| 50 | delegate->CancelOnMessageLoopThread(); |
| 51 | } |
| 52 | |
craig.schlenter@chromium.org | b2d581e | 2011-04-15 01:56:01 +0900 | [diff] [blame] | 53 | bool FilePathWatcher::Watch(const FilePath& path, Delegate* delegate) { |
| 54 | DCHECK(path.IsAbsolute()); |
kmadhusu@chromium.org | c9126e5 | 2012-12-05 09:36:39 +0900 | [diff] [blame] | 55 | return impl_->Watch(path, false, delegate); |
craig.schlenter@chromium.org | b2d581e | 2011-04-15 01:56:01 +0900 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | FilePathWatcher::PlatformDelegate::PlatformDelegate(): cancelled_(false) { |
| 59 | } |
| 60 | |
| 61 | FilePathWatcher::PlatformDelegate::~PlatformDelegate() { |
| 62 | DCHECK(is_cancelled()); |
| 63 | } |
| 64 | |
kmadhusu@chromium.org | c9126e5 | 2012-12-05 09:36:39 +0900 | [diff] [blame] | 65 | bool FilePathWatcher::Watch(const FilePath& path, |
| 66 | bool recursive, |
| 67 | const Callback& callback) { |
| 68 | DCHECK(path.IsAbsolute()); |
| 69 | return impl_->Watch(path, recursive, new FilePathWatcherDelegate(callback)); |
joaodasilva@chromium.org | 5e57506 | 2012-06-04 23:21:27 +0900 | [diff] [blame] | 70 | } |
| 71 | |
craig.schlenter@chromium.org | b2d581e | 2011-04-15 01:56:01 +0900 | [diff] [blame] | 72 | } // namespace files |
| 73 | } // namespace base |