blob: 9e29d0a9d5375ccd7f09fd939867e67b58ac78bb [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.
38 typedef base::Callback<void(const FilePath& path, bool error)> Callback;
39
[email protected]b8ca91f2011-03-18 04:11:4940 // Used internally to encapsulate different members on different platforms.
fdoray0ac2f0f82017-01-09 23:38:4241 class PlatformDelegate {
[email protected]b8ca91f2011-03-18 04:11:4942 public:
[email protected]55f2c5162011-03-18 04:28:2443 PlatformDelegate();
fdoray0ac2f0f82017-01-09 23:38:4244 virtual ~PlatformDelegate();
[email protected]55f2c5162011-03-18 04:28:2445
[email protected]b8ca91f2011-03-18 04:11:4946 // Start watching for the given |path| and notify |delegate| about changes.
[email protected]7b3ee8b2011-04-01 18:48:1947 virtual bool Watch(const FilePath& path,
[email protected]66a59402012-12-05 00:36:3948 bool recursive,
[email protected]6b5d0022013-01-15 00:37:4749 const Callback& callback) WARN_UNUSED_RESULT = 0;
[email protected]b8ca91f2011-03-18 04:11:4950
51 // Stop watching. This is called from FilePathWatcher's dtor in order to
52 // allow to shut down properly while the object is still alive.
53 virtual void Cancel() = 0;
54
[email protected]b8ca91f2011-03-18 04:11:4955 protected:
[email protected]29ec5c12011-11-23 20:44:0056 friend class FilePathWatcher;
57
fdoraycaa1245b2016-12-23 18:56:4458 scoped_refptr<SequencedTaskRunner> task_runner() const {
skyostil054861d2015-04-30 19:06:1559 return task_runner_;
[email protected]b8ca91f2011-03-18 04:11:4960 }
61
fdoraycaa1245b2016-12-23 18:56:4462 void set_task_runner(scoped_refptr<SequencedTaskRunner> runner) {
danakj0c8d4aa2015-11-25 05:29:5863 task_runner_ = std::move(runner);
[email protected]b8ca91f2011-03-18 04:11:4964 }
65
[email protected]d64abe12011-03-22 20:14:0666 // Must be called before the PlatformDelegate is deleted.
67 void set_cancelled() {
68 cancelled_ = true;
69 }
70
71 bool is_cancelled() const {
72 return cancelled_;
73 }
74
[email protected]b8ca91f2011-03-18 04:11:4975 private:
fdoraycaa1245b2016-12-23 18:56:4476 scoped_refptr<SequencedTaskRunner> task_runner_;
[email protected]d64abe12011-03-22 20:14:0677 bool cancelled_;
fdoray0ac2f0f82017-01-09 23:38:4278
79 DISALLOW_COPY_AND_ASSIGN(PlatformDelegate);
[email protected]b8ca91f2011-03-18 04:11:4980 };
81
[email protected]29ec5c12011-11-23 20:44:0082 FilePathWatcher();
fdoray1ce19c62016-11-14 22:37:5583 ~FilePathWatcher();
[email protected]29ec5c12011-11-23 20:44:0084
[email protected]6d2d92a2014-06-11 07:15:2485 // Returns true if the platform and OS version support recursive watches.
86 static bool RecursiveWatchAvailable();
87
[email protected]10e38b62012-06-04 14:21:2788 // Invokes |callback| whenever updates to |path| are detected. This should be
fdoraycaa1245b2016-12-23 18:56:4489 // called at most once. Set |recursive| to true to watch |path| and its
90 // children. The callback will be invoked on the same sequence. Returns true
91 // on success.
fdoray52e11d482016-11-29 22:19:5892 //
93 // On POSIX, this must be called from a thread that supports
94 // FileDescriptorWatcher.
[email protected]66a59402012-12-05 00:36:3995 //
[email protected]6d2d92a2014-06-11 07:15:2496 // Recursive watch is not supported on all platforms and file systems.
[email protected]66a59402012-12-05 00:36:3997 // Watch() will return false in the case of failure.
98 bool Watch(const FilePath& path, bool recursive, const Callback& callback);
[email protected]10e38b62012-06-04 14:21:2799
[email protected]b8ca91f2011-03-18 04:11:49100 private:
fdoray0ac2f0f82017-01-09 23:38:42101 std::unique_ptr<PlatformDelegate> impl_;
[email protected]b8ca91f2011-03-18 04:11:49102
fdoray1ce19c62016-11-14 22:37:55103 SequenceChecker sequence_checker_;
104
[email protected]b8ca91f2011-03-18 04:11:49105 DISALLOW_COPY_AND_ASSIGN(FilePathWatcher);
106};
107
[email protected]493c8002011-04-14 16:56:01108} // namespace base
109
110#endif // BASE_FILES_FILE_PATH_WATCHER_H_