| 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 // Cross platform methods for FilePathWatcher. See the various platform | 5 // Cross platform methods for FilePathWatcher. See the various platform |
| 6 // specific implementation files, too. | 6 // specific implementation files, too. |
| 7 | 7 |
| 8 #include "base/files/file_path_watcher.h" | 8 #include "base/files/file_path_watcher.h" |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
| 12 | 12 |
| 13 namespace base { | 13 namespace base { |
| 14 namespace files { | 14 namespace files { |
| 15 | 15 |
| 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 | |
| 43 FilePathWatcher::~FilePathWatcher() { | 16 FilePathWatcher::~FilePathWatcher() { |
| 44 impl_->Cancel(); | 17 impl_->Cancel(); |
| 45 } | 18 } |
| 46 | 19 |
| 47 // static | 20 // static |
| 48 void FilePathWatcher::CancelWatch( | 21 void FilePathWatcher::CancelWatch( |
| 49 const scoped_refptr<PlatformDelegate>& delegate) { | 22 const scoped_refptr<PlatformDelegate>& delegate) { |
| 50 delegate->CancelOnMessageLoopThread(); | 23 delegate->CancelOnMessageLoopThread(); |
| 51 } | 24 } |
| 52 | 25 |
| 53 bool FilePathWatcher::Watch(const FilePath& path, Delegate* delegate) { | |
| 54 DCHECK(path.IsAbsolute()); | |
| 55 return impl_->Watch(path, false, delegate); | |
| 56 } | |
| 57 | |
| 58 FilePathWatcher::PlatformDelegate::PlatformDelegate(): cancelled_(false) { | 26 FilePathWatcher::PlatformDelegate::PlatformDelegate(): cancelled_(false) { |
| 59 } | 27 } |
| 60 | 28 |
| 61 FilePathWatcher::PlatformDelegate::~PlatformDelegate() { | 29 FilePathWatcher::PlatformDelegate::~PlatformDelegate() { |
| 62 DCHECK(is_cancelled()); | 30 DCHECK(is_cancelled()); |
| 63 } | 31 } |
| 64 | 32 |
| 65 bool FilePathWatcher::Watch(const FilePath& path, | 33 bool FilePathWatcher::Watch(const FilePath& path, |
| 66 bool recursive, | 34 bool recursive, |
| 67 const Callback& callback) { | 35 const Callback& callback) { |
| 68 DCHECK(path.IsAbsolute()); | 36 DCHECK(path.IsAbsolute()); |
| 69 return impl_->Watch(path, recursive, new FilePathWatcherDelegate(callback)); | 37 return impl_->Watch(path, recursive, callback); |
| 70 } | 38 } |
| 71 | 39 |
| 72 } // namespace files | 40 } // namespace files |
| 73 } // namespace base | 41 } // namespace base |
| OLD | NEW |