blob: dd2b37fc78d80a937c00afcb803cfc784970047b [file] [log] [blame]
craig.schlenter@chromium.orgb2d581e2011-04-15 01:56:01 +09001// 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
13namespace base {
14namespace files {
15
joaodasilva@chromium.org5e575062012-06-04 23:21:27 +090016namespace {
17
18// A delegate implementation for the callback interface.
19class 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.orgb2d581e2011-04-15 01:56:01 +090043FilePathWatcher::~FilePathWatcher() {
44 impl_->Cancel();
45}
46
jhawkins@chromium.orgfce1f412011-11-24 05:44:00 +090047// static
48void FilePathWatcher::CancelWatch(
49 const scoped_refptr<PlatformDelegate>& delegate) {
50 delegate->CancelOnMessageLoopThread();
51}
52
craig.schlenter@chromium.orgb2d581e2011-04-15 01:56:01 +090053bool FilePathWatcher::Watch(const FilePath& path, Delegate* delegate) {
54 DCHECK(path.IsAbsolute());
55 return impl_->Watch(path, delegate);
56}
57
58FilePathWatcher::PlatformDelegate::PlatformDelegate(): cancelled_(false) {
59}
60
61FilePathWatcher::PlatformDelegate::~PlatformDelegate() {
62 DCHECK(is_cancelled());
63}
64
joaodasilva@chromium.org5e575062012-06-04 23:21:27 +090065bool FilePathWatcher::Watch(const FilePath& path, const Callback& callback) {
66 return Watch(path, new FilePathWatcherDelegate(callback));
67}
68
craig.schlenter@chromium.orgb2d581e2011-04-15 01:56:01 +090069} // namespace files
70} // namespace base