blob: bc8db37f1b636881348be5a46a830ec314d7d833 [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());
kmadhusu@chromium.orgc9126e52012-12-05 09:36:39 +090055 return impl_->Watch(path, false, delegate);
craig.schlenter@chromium.orgb2d581e2011-04-15 01:56:01 +090056}
57
58FilePathWatcher::PlatformDelegate::PlatformDelegate(): cancelled_(false) {
59}
60
61FilePathWatcher::PlatformDelegate::~PlatformDelegate() {
62 DCHECK(is_cancelled());
63}
64
kmadhusu@chromium.orgc9126e52012-12-05 09:36:39 +090065bool 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.org5e575062012-06-04 23:21:27 +090070}
71
craig.schlenter@chromium.orgb2d581e2011-04-15 01:56:01 +090072} // namespace files
73} // namespace base