1 // Copyright 2014 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 #include "base/files/file_path_watcher.h"
6 #include "base/files/file_path_watcher_kqueue.h"
7 #include "build/build_config.h"
8 
9 #if !defined(OS_IOS)
10 #include "base/files/file_path_watcher_fsevents.h"
11 #endif
12 
13 namespace base {
14 
15 namespace {
16 
17 class FilePathWatcherImpl : public FilePathWatcher::PlatformDelegate {
18  public:
Watch(const FilePath & path,bool recursive,const FilePathWatcher::Callback & callback)19   bool Watch(const FilePath& path,
20              bool recursive,
21              const FilePathWatcher::Callback& callback) override {
22     // Use kqueue for non-recursive watches and FSEvents for recursive ones.
23     DCHECK(!impl_.get());
24     if (recursive) {
25       if (!FilePathWatcher::RecursiveWatchAvailable())
26         return false;
27 #if !defined(OS_IOS)
28       impl_ = new FilePathWatcherFSEvents();
29 #endif  // OS_IOS
30     } else {
31       impl_ = new FilePathWatcherKQueue();
32     }
33     DCHECK(impl_.get());
34     return impl_->Watch(path, recursive, callback);
35   }
36 
Cancel()37   void Cancel() override {
38     if (impl_.get())
39       impl_->Cancel();
40     set_cancelled();
41   }
42 
CancelOnMessageLoopThread()43   void CancelOnMessageLoopThread() override {
44     if (impl_.get())
45       impl_->Cancel();
46     set_cancelled();
47   }
48 
49  protected:
~FilePathWatcherImpl()50   ~FilePathWatcherImpl() override {}
51 
52   scoped_refptr<PlatformDelegate> impl_;
53 };
54 
55 }  // namespace
56 
FilePathWatcher()57 FilePathWatcher::FilePathWatcher() {
58   impl_ = new FilePathWatcherImpl();
59 }
60 
61 }  // namespace base
62