file_path_watcher_mac.cc 1.6 KB

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