blob: 834acbcde211523d1a3db6e590b64b5bc571ceac [file] [log] [blame]
szym@chromium.org5ef94e22012-02-25 07:18:52 +09001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
craig.schlenter@chromium.orgb2d581e2011-04-15 01:56:01 +09002// 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
7#ifndef BASE_FILES_FILE_PATH_WATCHER_H_
8#define BASE_FILES_FILE_PATH_WATCHER_H_
craig.schlenter@chromium.orgb2d581e2011-04-15 01:56:01 +09009
darin@chromium.orge585bed2011-08-06 00:34:00 +090010#include "base/base_export.h"
craig.schlenter@chromium.orgb2d581e2011-04-15 01:56:01 +090011#include "base/basictypes.h"
joaodasilva@chromium.org5e575062012-06-04 23:21:27 +090012#include "base/callback.h"
brettw@chromium.org59eef1f2013-02-24 14:40:52 +090013#include "base/files/file_path.h"
craig.schlenter@chromium.orgb2d581e2011-04-15 01:56:01 +090014#include "base/memory/ref_counted.h"
skyostil97aefe12015-05-01 04:06:15 +090015#include "base/single_thread_task_runner.h"
craig.schlenter@chromium.orgb2d581e2011-04-15 01:56:01 +090016
17namespace base {
craig.schlenter@chromium.orgb2d581e2011-04-15 01:56:01 +090018
19// This class lets you register interest in changes on a FilePath.
avi@chromium.org1bd484c2013-04-03 07:17:49 +090020// The callback will get called whenever the file or directory referenced by the
craig.schlenter@chromium.orgb2d581e2011-04-15 01:56:01 +090021// FilePath is changed, including created or deleted. Due to limitations in the
22// underlying OS APIs, FilePathWatcher has slightly different semantics on OS X
23// than on Windows or Linux. FilePathWatcher on Linux and Windows will detect
24// modifications to files in a watched directory. FilePathWatcher on Mac will
25// detect the creation and deletion of files in a watched directory, but will
joaodasilva@chromium.org685080d2012-06-04 19:25:30 +090026// not detect modifications to those files. See file_path_watcher_kqueue.cc for
craig.schlenter@chromium.orgb2d581e2011-04-15 01:56:01 +090027// details.
darin@chromium.orge585bed2011-08-06 00:34:00 +090028class BASE_EXPORT FilePathWatcher {
craig.schlenter@chromium.orgb2d581e2011-04-15 01:56:01 +090029 public:
joaodasilva@chromium.org5e575062012-06-04 23:21:27 +090030 // Callback type for Watch(). |path| points to the file that was updated,
31 // and |error| is true if the platform specific code detected an error. In
32 // that case, the callback won't be invoked again.
33 typedef base::Callback<void(const FilePath& path, bool error)> Callback;
34
craig.schlenter@chromium.orgb2d581e2011-04-15 01:56:01 +090035 // Used internally to encapsulate different members on different platforms.
36 class PlatformDelegate : public base::RefCountedThreadSafe<PlatformDelegate> {
37 public:
38 PlatformDelegate();
39
40 // Start watching for the given |path| and notify |delegate| about changes.
41 virtual bool Watch(const FilePath& path,
kmadhusu@chromium.orgc9126e52012-12-05 09:36:39 +090042 bool recursive,
darin@chromium.org9498d472013-01-15 09:37:47 +090043 const Callback& callback) WARN_UNUSED_RESULT = 0;
craig.schlenter@chromium.orgb2d581e2011-04-15 01:56:01 +090044
45 // Stop watching. This is called from FilePathWatcher's dtor in order to
46 // allow to shut down properly while the object is still alive.
47 // It can be called from any thread.
48 virtual void Cancel() = 0;
49
50 protected:
rsleevi@chromium.orgb5eb00e2012-04-25 09:42:51 +090051 friend class base::RefCountedThreadSafe<PlatformDelegate>;
jhawkins@chromium.orgfce1f412011-11-24 05:44:00 +090052 friend class FilePathWatcher;
53
craig.schlenter@chromium.orgb2d581e2011-04-15 01:56:01 +090054 virtual ~PlatformDelegate();
55
56 // Stop watching. This is only called on the thread of the appropriate
57 // message loop. Since it can also be called more than once, it should
58 // check |is_cancelled()| to avoid duplicate work.
59 virtual void CancelOnMessageLoopThread() = 0;
60
skyostil97aefe12015-05-01 04:06:15 +090061 scoped_refptr<base::SingleThreadTaskRunner> task_runner() const {
62 return task_runner_;
craig.schlenter@chromium.orgb2d581e2011-04-15 01:56:01 +090063 }
64
danakj800d2ea2015-11-25 14:29:58 +090065 void set_task_runner(scoped_refptr<base::SingleThreadTaskRunner> runner) {
66 task_runner_ = std::move(runner);
craig.schlenter@chromium.orgb2d581e2011-04-15 01:56:01 +090067 }
68
69 // Must be called before the PlatformDelegate is deleted.
70 void set_cancelled() {
71 cancelled_ = true;
72 }
73
74 bool is_cancelled() const {
75 return cancelled_;
76 }
77
78 private:
skyostil97aefe12015-05-01 04:06:15 +090079 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
craig.schlenter@chromium.orgb2d581e2011-04-15 01:56:01 +090080 bool cancelled_;
81 };
82
jhawkins@chromium.orgfce1f412011-11-24 05:44:00 +090083 FilePathWatcher();
szym@chromium.org5ef94e22012-02-25 07:18:52 +090084 virtual ~FilePathWatcher();
jhawkins@chromium.orgfce1f412011-11-24 05:44:00 +090085
86 // A callback that always cleans up the PlatformDelegate, either when executed
87 // or when deleted without having been executed at all, as can happen during
88 // shutdown.
89 static void CancelWatch(const scoped_refptr<PlatformDelegate>& delegate);
90
vandebo@chromium.orgc185d782014-06-11 16:15:24 +090091 // Returns true if the platform and OS version support recursive watches.
92 static bool RecursiveWatchAvailable();
93
joaodasilva@chromium.org5e575062012-06-04 23:21:27 +090094 // Invokes |callback| whenever updates to |path| are detected. This should be
kmadhusu@chromium.orgc9126e52012-12-05 09:36:39 +090095 // called at most once, and from a MessageLoop of TYPE_IO. Set |recursive| to
96 // true, to watch |path| and its children. The callback will be invoked on
97 // the same loop. Returns true on success.
98 //
vandebo@chromium.orgc185d782014-06-11 16:15:24 +090099 // Recursive watch is not supported on all platforms and file systems.
kmadhusu@chromium.orgc9126e52012-12-05 09:36:39 +0900100 // Watch() will return false in the case of failure.
101 bool Watch(const FilePath& path, bool recursive, const Callback& callback);
joaodasilva@chromium.org5e575062012-06-04 23:21:27 +0900102
craig.schlenter@chromium.orgb2d581e2011-04-15 01:56:01 +0900103 private:
104 scoped_refptr<PlatformDelegate> impl_;
105
106 DISALLOW_COPY_AND_ASSIGN(FilePathWatcher);
107};
108
craig.schlenter@chromium.orgb2d581e2011-04-15 01:56:01 +0900109} // namespace base
110
111#endif // BASE_FILES_FILE_PATH_WATCHER_H_