blob: b9cd78aa3718dbf4951df8227df7aad54465ed9d [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
[email protected]0bea7252011-08-05 15:34:0010#include "base/base_export.h"
[email protected]10e38b62012-06-04 14:21:2711#include "base/callback.h"
[email protected]57999812013-02-24 05:40:5212#include "base/files/file_path.h"
avi543540e2015-12-24 05:15:3213#include "base/macros.h"
[email protected]3b63f8f42011-03-28 01:54:1514#include "base/memory/ref_counted.h"
fdoray1ce19c62016-11-14 22:37:5515#include "base/sequence_checker.h"
skyostil054861d2015-04-30 19:06:1516#include "base/single_thread_task_runner.h"
[email protected]b8ca91f2011-03-18 04:11:4917
[email protected]493c8002011-04-14 16:56:0118namespace base {
[email protected]493c8002011-04-14 16:56:0119
[email protected]b8ca91f2011-03-18 04:11:4920// This class lets you register interest in changes on a FilePath.
[email protected]b4e439f2013-04-02 22:17:4921// The callback will get called whenever the file or directory referenced by the
[email protected]b8ca91f2011-03-18 04:11:4922// FilePath is changed, including created or deleted. Due to limitations in the
[email protected]7b3ee8b2011-04-01 18:48:1923// underlying OS APIs, FilePathWatcher has slightly different semantics on OS X
24// than on Windows or Linux. FilePathWatcher on Linux and Windows will detect
25// modifications to files in a watched directory. FilePathWatcher on Mac will
26// detect the creation and deletion of files in a watched directory, but will
[email protected]20f06092012-06-04 10:25:3027// not detect modifications to those files. See file_path_watcher_kqueue.cc for
[email protected]7b3ee8b2011-04-01 18:48:1928// details.
fdoray1ce19c62016-11-14 22:37:5529//
30// Must be destroyed on the sequence that invokes Watch().
[email protected]0bea7252011-08-05 15:34:0031class BASE_EXPORT FilePathWatcher {
[email protected]b8ca91f2011-03-18 04:11:4932 public:
[email protected]10e38b62012-06-04 14:21:2733 // Callback type for Watch(). |path| points to the file that was updated,
34 // and |error| is true if the platform specific code detected an error. In
35 // that case, the callback won't be invoked again.
36 typedef base::Callback<void(const FilePath& path, bool error)> Callback;
37
[email protected]b8ca91f2011-03-18 04:11:4938 // Used internally to encapsulate different members on different platforms.
[email protected]d64abe12011-03-22 20:14:0639 class PlatformDelegate : public base::RefCountedThreadSafe<PlatformDelegate> {
[email protected]b8ca91f2011-03-18 04:11:4940 public:
[email protected]55f2c5162011-03-18 04:28:2441 PlatformDelegate();
42
[email protected]b8ca91f2011-03-18 04:11:4943 // Start watching for the given |path| and notify |delegate| about changes.
[email protected]7b3ee8b2011-04-01 18:48:1944 virtual bool Watch(const FilePath& path,
[email protected]66a59402012-12-05 00:36:3945 bool recursive,
[email protected]6b5d0022013-01-15 00:37:4746 const Callback& callback) WARN_UNUSED_RESULT = 0;
[email protected]b8ca91f2011-03-18 04:11:4947
48 // Stop watching. This is called from FilePathWatcher's dtor in order to
49 // allow to shut down properly while the object is still alive.
[email protected]d64abe12011-03-22 20:14:0650 // It can be called from any thread.
[email protected]b8ca91f2011-03-18 04:11:4951 virtual void Cancel() = 0;
52
[email protected]b8ca91f2011-03-18 04:11:4953 protected:
[email protected]a9aaa9d12012-04-25 00:42:5154 friend class base::RefCountedThreadSafe<PlatformDelegate>;
[email protected]29ec5c12011-11-23 20:44:0055 friend class FilePathWatcher;
56
[email protected]b8ca91f2011-03-18 04:11:4957 virtual ~PlatformDelegate();
58
skyostil054861d2015-04-30 19:06:1559 scoped_refptr<base::SingleThreadTaskRunner> task_runner() const {
60 return task_runner_;
[email protected]b8ca91f2011-03-18 04:11:4961 }
62
danakj0c8d4aa2015-11-25 05:29:5863 void set_task_runner(scoped_refptr<base::SingleThreadTaskRunner> runner) {
64 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:
skyostil054861d2015-04-30 19:06:1577 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
[email protected]d64abe12011-03-22 20:14:0678 bool cancelled_;
[email protected]b8ca91f2011-03-18 04:11:4979 };
80
[email protected]29ec5c12011-11-23 20:44:0081 FilePathWatcher();
fdoray1ce19c62016-11-14 22:37:5582 ~FilePathWatcher();
[email protected]29ec5c12011-11-23 20:44:0083
84 // A callback that always cleans up the PlatformDelegate, either when executed
85 // or when deleted without having been executed at all, as can happen during
86 // shutdown.
87 static void CancelWatch(const scoped_refptr<PlatformDelegate>& delegate);
88
[email protected]6d2d92a2014-06-11 07:15:2489 // Returns true if the platform and OS version support recursive watches.
90 static bool RecursiveWatchAvailable();
91
[email protected]10e38b62012-06-04 14:21:2792 // Invokes |callback| whenever updates to |path| are detected. This should be
fdoray52e11d482016-11-29 22:19:5893 // called at most once. Set |recursive| to true, to watch |path| and its
94 // children. The callback will be invoked on the same thread. Returns true on
95 // success.
96 //
97 // On POSIX, this must be called from a thread that supports
98 // FileDescriptorWatcher.
[email protected]66a59402012-12-05 00:36:3999 //
[email protected]6d2d92a2014-06-11 07:15:24100 // Recursive watch is not supported on all platforms and file systems.
[email protected]66a59402012-12-05 00:36:39101 // Watch() will return false in the case of failure.
102 bool Watch(const FilePath& path, bool recursive, const Callback& callback);
[email protected]10e38b62012-06-04 14:21:27103
[email protected]b8ca91f2011-03-18 04:11:49104 private:
105 scoped_refptr<PlatformDelegate> impl_;
106
fdoray1ce19c62016-11-14 22:37:55107 SequenceChecker sequence_checker_;
108
[email protected]b8ca91f2011-03-18 04:11:49109 DISALLOW_COPY_AND_ASSIGN(FilePathWatcher);
110};
111
[email protected]493c8002011-04-14 16:56:01112} // namespace base
113
114#endif // BASE_FILES_FILE_PATH_WATCHER_H_