1 // Copyright 2011 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 // 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 <memory>
11 #include <utility>
12
13 #include "base/check.h"
14 #include "base/files/file_path.h"
15 #include "build/build_config.h"
16
17 namespace base {
18
~FilePathWatcher()19 FilePathWatcher::~FilePathWatcher() {
20 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
21 impl_->Cancel();
22 }
23
24 // static
RecursiveWatchAvailable()25 bool FilePathWatcher::RecursiveWatchAvailable() {
26 #if BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN) || BUILDFLAG(IS_LINUX) || \
27 BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_AIX) || \
28 BUILDFLAG(IS_FUCHSIA)
29 return true;
30 #else
31 // FSEvents isn't available on iOS.
32 return false;
33 #endif
34 }
35
36 FilePathWatcher::PlatformDelegate::PlatformDelegate() = default;
37
~PlatformDelegate()38 FilePathWatcher::PlatformDelegate::~PlatformDelegate() {
39 DCHECK(is_cancelled());
40 }
41
Watch(const FilePath & path,Type type,const Callback & callback)42 bool FilePathWatcher::Watch(const FilePath& path,
43 Type type,
44 const Callback& callback) {
45 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
46 DCHECK(path.IsAbsolute());
47 return impl_->Watch(path, type, callback);
48 }
49
WatchWithOptions(const FilePath & path,const WatchOptions & options,const Callback & callback)50 bool FilePathWatcher::WatchWithOptions(const FilePath& path,
51 const WatchOptions& options,
52 const Callback& callback) {
53 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
54 DCHECK(path.IsAbsolute());
55 return impl_->WatchWithOptions(path, options, callback);
56 }
57
WatchWithChangeInfo(const FilePath & path,const WatchOptions & options,const CallbackWithChangeInfo & callback)58 bool FilePathWatcher::WatchWithChangeInfo(
59 const FilePath& path,
60 const WatchOptions& options,
61 const CallbackWithChangeInfo& callback) {
62 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
63 DCHECK(path.IsAbsolute());
64 return impl_->WatchWithChangeInfo(path, options, callback);
65 }
66
WatchWithOptions(const FilePath & path,const WatchOptions & options,const Callback & callback)67 bool FilePathWatcher::PlatformDelegate::WatchWithOptions(
68 const FilePath& path,
69 const WatchOptions& options,
70 const Callback& callback) {
71 return Watch(path, options.type, callback);
72 }
73
WatchWithChangeInfo(const FilePath & path,const WatchOptions & options,const CallbackWithChangeInfo & callback)74 bool FilePathWatcher::PlatformDelegate::WatchWithChangeInfo(
75 const FilePath& path,
76 const WatchOptions& options,
77 const CallbackWithChangeInfo& callback) {
78 return Watch(path, options.type, base::BindRepeating(callback, ChangeInfo()));
79 }
80
FilePathWatcher(std::unique_ptr<PlatformDelegate> delegate)81 FilePathWatcher::FilePathWatcher(std::unique_ptr<PlatformDelegate> delegate) {
82 DETACH_FROM_SEQUENCE(sequence_checker_);
83 impl_ = std::move(delegate);
84 }
85
86 } // namespace base
87