blob: 94a3f9ad15f524961331f26c1ee78c1caccb47d8 [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"
craig.schlenter@chromium.orgb2d581e2011-04-15 01:56:01 +090013#include "base/file_path.h"
14#include "base/memory/ref_counted.h"
15#include "base/message_loop_proxy.h"
16
17namespace base {
18namespace files {
19
20// This class lets you register interest in changes on a FilePath.
21// The delegate will get called whenever the file or directory referenced by the
22// FilePath is changed, including created or deleted. Due to limitations in the
23// 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
joaodasilva@chromium.org685080d2012-06-04 19:25:30 +090027// not detect modifications to those files. See file_path_watcher_kqueue.cc for
craig.schlenter@chromium.orgb2d581e2011-04-15 01:56:01 +090028// details.
darin@chromium.orge585bed2011-08-06 00:34:00 +090029class BASE_EXPORT FilePathWatcher {
craig.schlenter@chromium.orgb2d581e2011-04-15 01:56:01 +090030 public:
joaodasilva@chromium.org5e575062012-06-04 23:21:27 +090031 // Callback type for Watch(). |path| points to the file that was updated,
32 // and |error| is true if the platform specific code detected an error. In
33 // that case, the callback won't be invoked again.
34 typedef base::Callback<void(const FilePath& path, bool error)> Callback;
35
craig.schlenter@chromium.orgb2d581e2011-04-15 01:56:01 +090036 // Declares the callback client code implements to receive notifications. Note
37 // that implementations of this interface should not keep a reference to the
38 // corresponding FileWatcher object to prevent a reference cycle.
joaodasilva@chromium.org5e575062012-06-04 23:21:27 +090039 //
40 // Deprecated: see comment on Watch() below.
craig.schlenter@chromium.orgb2d581e2011-04-15 01:56:01 +090041 class Delegate : public base::RefCountedThreadSafe<Delegate> {
42 public:
craig.schlenter@chromium.orgb2d581e2011-04-15 01:56:01 +090043 virtual void OnFilePathChanged(const FilePath& path) = 0;
44 // Called when platform specific code detected an error. The watcher will
45 // not call OnFilePathChanged for future changes.
46 virtual void OnFilePathError(const FilePath& path) {}
rsleevi@chromium.orgb5eb00e2012-04-25 09:42:51 +090047
48 protected:
49 friend class base::RefCountedThreadSafe<Delegate>;
50 virtual ~Delegate() {}
craig.schlenter@chromium.orgb2d581e2011-04-15 01:56:01 +090051 };
52
craig.schlenter@chromium.orgb2d581e2011-04-15 01:56:01 +090053 // Used internally to encapsulate different members on different platforms.
jhawkins@chromium.orgfce1f412011-11-24 05:44:00 +090054 // TODO(jhawkins): Move this into its own file. Also fix the confusing naming
55 // wrt Delegate vs PlatformDelegate.
craig.schlenter@chromium.orgb2d581e2011-04-15 01:56:01 +090056 class PlatformDelegate : public base::RefCountedThreadSafe<PlatformDelegate> {
57 public:
58 PlatformDelegate();
59
60 // Start watching for the given |path| and notify |delegate| about changes.
61 virtual bool Watch(const FilePath& path,
kmadhusu@chromium.orgc9126e52012-12-05 09:36:39 +090062 bool recursive,
craig.schlenter@chromium.orgb2d581e2011-04-15 01:56:01 +090063 Delegate* delegate) WARN_UNUSED_RESULT = 0;
64
65 // Stop watching. This is called from FilePathWatcher's dtor in order to
66 // allow to shut down properly while the object is still alive.
67 // It can be called from any thread.
68 virtual void Cancel() = 0;
69
70 protected:
rsleevi@chromium.orgb5eb00e2012-04-25 09:42:51 +090071 friend class base::RefCountedThreadSafe<PlatformDelegate>;
jhawkins@chromium.orgfce1f412011-11-24 05:44:00 +090072 friend class FilePathWatcher;
73
craig.schlenter@chromium.orgb2d581e2011-04-15 01:56:01 +090074 virtual ~PlatformDelegate();
75
76 // Stop watching. This is only called on the thread of the appropriate
77 // message loop. Since it can also be called more than once, it should
78 // check |is_cancelled()| to avoid duplicate work.
79 virtual void CancelOnMessageLoopThread() = 0;
80
81 scoped_refptr<base::MessageLoopProxy> message_loop() const {
82 return message_loop_;
83 }
84
85 void set_message_loop(base::MessageLoopProxy* loop) {
86 message_loop_ = loop;
87 }
88
89 // Must be called before the PlatformDelegate is deleted.
90 void set_cancelled() {
91 cancelled_ = true;
92 }
93
94 bool is_cancelled() const {
95 return cancelled_;
96 }
97
98 private:
craig.schlenter@chromium.orgb2d581e2011-04-15 01:56:01 +090099 scoped_refptr<base::MessageLoopProxy> message_loop_;
100 bool cancelled_;
101 };
102
jhawkins@chromium.orgfce1f412011-11-24 05:44:00 +0900103 FilePathWatcher();
szym@chromium.org5ef94e22012-02-25 07:18:52 +0900104 virtual ~FilePathWatcher();
jhawkins@chromium.orgfce1f412011-11-24 05:44:00 +0900105
106 // A callback that always cleans up the PlatformDelegate, either when executed
107 // or when deleted without having been executed at all, as can happen during
108 // shutdown.
109 static void CancelWatch(const scoped_refptr<PlatformDelegate>& delegate);
110
111 // Register interest in any changes on |path|. OnPathChanged will be called
112 // back for each change. Returns true on success.
113 // OnFilePathChanged() will be called on the same thread as Watch() is called,
114 // which should have a MessageLoop of TYPE_IO.
joaodasilva@chromium.org5e575062012-06-04 23:21:27 +0900115 //
116 // Deprecated: new code should use the callback interface, declared below.
117 // The FilePathWatcher::Delegate interface will be removed once all client
118 // code has been updated. http://crbug.com/130980
szym@chromium.org5ef94e22012-02-25 07:18:52 +0900119 virtual bool Watch(const FilePath& path, Delegate* delegate)
120 WARN_UNUSED_RESULT;
jhawkins@chromium.orgfce1f412011-11-24 05:44:00 +0900121
joaodasilva@chromium.org5e575062012-06-04 23:21:27 +0900122 // Invokes |callback| whenever updates to |path| are detected. This should be
kmadhusu@chromium.orgc9126e52012-12-05 09:36:39 +0900123 // called at most once, and from a MessageLoop of TYPE_IO. Set |recursive| to
124 // true, to watch |path| and its children. The callback will be invoked on
125 // the same loop. Returns true on success.
126 //
127 // NOTE: Recursive watch is not supported on all platforms and file systems.
128 // Watch() will return false in the case of failure.
129 bool Watch(const FilePath& path, bool recursive, const Callback& callback);
joaodasilva@chromium.org5e575062012-06-04 23:21:27 +0900130
craig.schlenter@chromium.orgb2d581e2011-04-15 01:56:01 +0900131 private:
132 scoped_refptr<PlatformDelegate> impl_;
133
134 DISALLOW_COPY_AND_ASSIGN(FilePathWatcher);
135};
136
137} // namespace files
138} // namespace base
139
140#endif // BASE_FILES_FILE_PATH_WATCHER_H_