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