blob: 0289334ba6806d2e8a99fcab94c86a6810097e5d [file] [log] [blame]
[email protected]85109f5df2012-02-24 22:18:521// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]b8ca91f2011-03-18 04:11:492// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// This module provides a way to monitor a file or directory for changes.
6
[email protected]493c8002011-04-14 16:56:017#ifndef BASE_FILES_FILE_PATH_WATCHER_H_
8#define BASE_FILES_FILE_PATH_WATCHER_H_
[email protected]b8ca91f2011-03-18 04:11:499
fdoray0ac2f0f82017-01-09 23:38:4210#include <memory>
11
[email protected]0bea7252011-08-05 15:34:0012#include "base/base_export.h"
[email protected]10e38b62012-06-04 14:21:2713#include "base/callback.h"
[email protected]57999812013-02-24 05:40:5214#include "base/files/file_path.h"
avi543540e2015-12-24 05:15:3215#include "base/macros.h"
[email protected]3b63f8f42011-03-28 01:54:1516#include "base/memory/ref_counted.h"
fdoray1ce19c62016-11-14 22:37:5517#include "base/sequence_checker.h"
fdoraycaa1245b2016-12-23 18:56:4418#include "base/sequenced_task_runner.h"
[email protected]b8ca91f2011-03-18 04:11:4919
[email protected]493c8002011-04-14 16:56:0120namespace base {
[email protected]493c8002011-04-14 16:56:0121
[email protected]b8ca91f2011-03-18 04:11:4922// This class lets you register interest in changes on a FilePath.
[email protected]b4e439f2013-04-02 22:17:4923// The callback will get called whenever the file or directory referenced by the
[email protected]b8ca91f2011-03-18 04:11:4924// FilePath is changed, including created or deleted. Due to limitations in the
[email protected]7b3ee8b2011-04-01 18:48:1925// underlying OS APIs, FilePathWatcher has slightly different semantics on OS X
26// than on Windows or Linux. FilePathWatcher on Linux and Windows will detect
27// modifications to files in a watched directory. FilePathWatcher on Mac will
28// detect the creation and deletion of files in a watched directory, but will
[email protected]20f06092012-06-04 10:25:3029// not detect modifications to those files. See file_path_watcher_kqueue.cc for
[email protected]7b3ee8b2011-04-01 18:48:1930// details.
fdoray1ce19c62016-11-14 22:37:5531//
32// Must be destroyed on the sequence that invokes Watch().
[email protected]0bea7252011-08-05 15:34:0033class BASE_EXPORT FilePathWatcher {
[email protected]b8ca91f2011-03-18 04:11:4934 public:
[email protected]10e38b62012-06-04 14:21:2735 // Callback type for Watch(). |path| points to the file that was updated,
36 // and |error| is true if the platform specific code detected an error. In
37 // that case, the callback won't be invoked again.
kylecharb2695fc2019-04-24 14:51:2038 using Callback =
39 base::RepeatingCallback<void(const FilePath& path, bool error)>;
[email protected]10e38b62012-06-04 14:21:2740
[email protected]b8ca91f2011-03-18 04:11:4941 // Used internally to encapsulate different members on different platforms.
fdoray0ac2f0f82017-01-09 23:38:4242 class PlatformDelegate {
[email protected]b8ca91f2011-03-18 04:11:4943 public:
[email protected]55f2c5162011-03-18 04:28:2444 PlatformDelegate();
fdoray0ac2f0f82017-01-09 23:38:4245 virtual ~PlatformDelegate();
[email protected]55f2c5162011-03-18 04:28:2446
[email protected]b8ca91f2011-03-18 04:11:4947 // Start watching for the given |path| and notify |delegate| about changes.
[email protected]7b3ee8b2011-04-01 18:48:1948 virtual bool Watch(const FilePath& path,
[email protected]66a59402012-12-05 00:36:3949 bool recursive,
[email protected]6b5d0022013-01-15 00:37:4750 const Callback& callback) WARN_UNUSED_RESULT = 0;
[email protected]b8ca91f2011-03-18 04:11:4951
52 // Stop watching. This is called from FilePathWatcher's dtor in order to
53 // allow to shut down properly while the object is still alive.
54 virtual void Cancel() = 0;
55
[email protected]b8ca91f2011-03-18 04:11:4956 protected:
[email protected]29ec5c12011-11-23 20:44:0057 friend class FilePathWatcher;
58
fdoraycaa1245b2016-12-23 18:56:4459 scoped_refptr<SequencedTaskRunner> task_runner() const {
skyostil054861d2015-04-30 19:06:1560 return task_runner_;
[email protected]b8ca91f2011-03-18 04:11:4961 }
62
fdoraycaa1245b2016-12-23 18:56:4463 void set_task_runner(scoped_refptr<SequencedTaskRunner> runner) {
danakj0c8d4aa2015-11-25 05:29:5864 task_runner_ = std::move(runner);
[email protected]b8ca91f2011-03-18 04:11:4965 }
66
[email protected]d64abe12011-03-22 20:14:0667 // Must be called before the PlatformDelegate is deleted.
68 void set_cancelled() {
69 cancelled_ = true;
70 }
71
72 bool is_cancelled() const {
73 return cancelled_;
74 }
75
[email protected]b8ca91f2011-03-18 04:11:4976 private:
fdoraycaa1245b2016-12-23 18:56:4477 scoped_refptr<SequencedTaskRunner> task_runner_;
[email protected]d64abe12011-03-22 20:14:0678 bool cancelled_;
fdoray0ac2f0f82017-01-09 23:38:4279
80 DISALLOW_COPY_AND_ASSIGN(PlatformDelegate);
[email protected]b8ca91f2011-03-18 04:11:4981 };
82
[email protected]29ec5c12011-11-23 20:44:0083 FilePathWatcher();
fdoray1ce19c62016-11-14 22:37:5584 ~FilePathWatcher();
[email protected]29ec5c12011-11-23 20:44:0085
[email protected]6d2d92a2014-06-11 07:15:2486 // Returns true if the platform and OS version support recursive watches.
87 static bool RecursiveWatchAvailable();
88
[email protected]10e38b62012-06-04 14:21:2789 // Invokes |callback| whenever updates to |path| are detected. This should be
fdoraycaa1245b2016-12-23 18:56:4490 // called at most once. Set |recursive| to true to watch |path| and its
91 // children. The callback will be invoked on the same sequence. Returns true
92 // on success.
fdoray52e11d482016-11-29 22:19:5893 //
94 // On POSIX, this must be called from a thread that supports
95 // FileDescriptorWatcher.
[email protected]66a59402012-12-05 00:36:3996 //
[email protected]6d2d92a2014-06-11 07:15:2497 // Recursive watch is not supported on all platforms and file systems.
[email protected]66a59402012-12-05 00:36:3998 // Watch() will return false in the case of failure.
99 bool Watch(const FilePath& path, bool recursive, const Callback& callback);
[email protected]10e38b62012-06-04 14:21:27100
[email protected]b8ca91f2011-03-18 04:11:49101 private:
fdoray0ac2f0f82017-01-09 23:38:42102 std::unique_ptr<PlatformDelegate> impl_;
[email protected]b8ca91f2011-03-18 04:11:49103
fdoray1ce19c62016-11-14 22:37:55104 SequenceChecker sequence_checker_;
105
[email protected]b8ca91f2011-03-18 04:11:49106 DISALLOW_COPY_AND_ASSIGN(FilePathWatcher);
107};
108
[email protected]493c8002011-04-14 16:56:01109} // namespace base
110
111#endif // BASE_FILES_FILE_PATH_WATCHER_H_