blob: 59199944e16a5c1060bc0eb0f14443bb7cb25722 [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#pragma once
10
[email protected]0bea7252011-08-05 15:34:0011#include "base/base_export.h"
[email protected]b8ca91f2011-03-18 04:11:4912#include "base/basictypes.h"
[email protected]10e38b62012-06-04 14:21:2713#include "base/callback.h"
[email protected]b8ca91f2011-03-18 04:11:4914#include "base/file_path.h"
[email protected]3b63f8f42011-03-28 01:54:1515#include "base/memory/ref_counted.h"
[email protected]b8ca91f2011-03-18 04:11:4916#include "base/message_loop_proxy.h"
[email protected]b8ca91f2011-03-18 04:11:4917
[email protected]493c8002011-04-14 16:56:0118namespace base {
19namespace files {
20
[email protected]b8ca91f2011-03-18 04:11:4921// This class lets you register interest in changes on a FilePath.
22// The delegate will get called whenever the file or directory referenced by the
23// FilePath is changed, including created or deleted. Due to limitations in the
[email protected]7b3ee8b2011-04-01 18:48:1924// underlying OS APIs, FilePathWatcher has slightly different semantics on OS X
25// than on Windows or Linux. FilePathWatcher on Linux and Windows will detect
26// modifications to files in a watched directory. FilePathWatcher on Mac will
27// detect the creation and deletion of files in a watched directory, but will
[email protected]20f06092012-06-04 10:25:3028// not detect modifications to those files. See file_path_watcher_kqueue.cc for
[email protected]7b3ee8b2011-04-01 18:48:1929// details.
[email protected]0bea7252011-08-05 15:34:0030class BASE_EXPORT FilePathWatcher {
[email protected]b8ca91f2011-03-18 04:11:4931 public:
[email protected]10e38b62012-06-04 14:21:2732 // Callback type for Watch(). |path| points to the file that was updated,
33 // and |error| is true if the platform specific code detected an error. In
34 // that case, the callback won't be invoked again.
35 typedef base::Callback<void(const FilePath& path, bool error)> Callback;
36
[email protected]b8ca91f2011-03-18 04:11:4937 // Declares the callback client code implements to receive notifications. Note
38 // that implementations of this interface should not keep a reference to the
39 // corresponding FileWatcher object to prevent a reference cycle.
[email protected]10e38b62012-06-04 14:21:2740 //
41 // Deprecated: see comment on Watch() below.
[email protected]b8ca91f2011-03-18 04:11:4942 class Delegate : public base::RefCountedThreadSafe<Delegate> {
43 public:
[email protected]b8ca91f2011-03-18 04:11:4944 virtual void OnFilePathChanged(const FilePath& path) = 0;
45 // Called when platform specific code detected an error. The watcher will
46 // not call OnFilePathChanged for future changes.
[email protected]7b3ee8b2011-04-01 18:48:1947 virtual void OnFilePathError(const FilePath& path) {}
[email protected]a9aaa9d12012-04-25 00:42:5148
49 protected:
50 friend class base::RefCountedThreadSafe<Delegate>;
51 virtual ~Delegate() {}
[email protected]b8ca91f2011-03-18 04:11:4952 };
53
[email protected]b8ca91f2011-03-18 04:11:4954 // Used internally to encapsulate different members on different platforms.
[email protected]29ec5c12011-11-23 20:44:0055 // TODO(jhawkins): Move this into its own file. Also fix the confusing naming
56 // wrt Delegate vs PlatformDelegate.
[email protected]d64abe12011-03-22 20:14:0657 class PlatformDelegate : public base::RefCountedThreadSafe<PlatformDelegate> {
[email protected]b8ca91f2011-03-18 04:11:4958 public:
[email protected]55f2c5162011-03-18 04:28:2459 PlatformDelegate();
60
[email protected]b8ca91f2011-03-18 04:11:4961 // Start watching for the given |path| and notify |delegate| about changes.
[email protected]7b3ee8b2011-04-01 18:48:1962 virtual bool Watch(const FilePath& path,
63 Delegate* delegate) WARN_UNUSED_RESULT = 0;
[email protected]b8ca91f2011-03-18 04:11:4964
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.
[email protected]d64abe12011-03-22 20:14:0667 // It can be called from any thread.
[email protected]b8ca91f2011-03-18 04:11:4968 virtual void Cancel() = 0;
69
[email protected]b8ca91f2011-03-18 04:11:4970 protected:
[email protected]a9aaa9d12012-04-25 00:42:5171 friend class base::RefCountedThreadSafe<PlatformDelegate>;
[email protected]29ec5c12011-11-23 20:44:0072 friend class FilePathWatcher;
73
[email protected]b8ca91f2011-03-18 04:11:4974 virtual ~PlatformDelegate();
75
[email protected]d64abe12011-03-22 20:14:0676 // 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
[email protected]b8ca91f2011-03-18 04:11:4981 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
[email protected]d64abe12011-03-22 20:14:0689 // 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
[email protected]b8ca91f2011-03-18 04:11:4998 private:
[email protected]b8ca91f2011-03-18 04:11:4999 scoped_refptr<base::MessageLoopProxy> message_loop_;
[email protected]d64abe12011-03-22 20:14:06100 bool cancelled_;
[email protected]b8ca91f2011-03-18 04:11:49101 };
102
[email protected]29ec5c12011-11-23 20:44:00103 FilePathWatcher();
[email protected]85109f5df2012-02-24 22:18:52104 virtual ~FilePathWatcher();
[email protected]29ec5c12011-11-23 20:44:00105
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.
[email protected]10e38b62012-06-04 14:21:27115 //
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
[email protected]85109f5df2012-02-24 22:18:52119 virtual bool Watch(const FilePath& path, Delegate* delegate)
120 WARN_UNUSED_RESULT;
[email protected]29ec5c12011-11-23 20:44:00121
[email protected]10e38b62012-06-04 14:21:27122 // Invokes |callback| whenever updates to |path| are detected. This should be
123 // called at most once, and from a MessageLoop of TYPE_IO. The callback will
124 // be invoked on the same loop. Returns true on success.
125 bool Watch(const FilePath& path, const Callback& callback);
126
[email protected]b8ca91f2011-03-18 04:11:49127 private:
128 scoped_refptr<PlatformDelegate> impl_;
129
130 DISALLOW_COPY_AND_ASSIGN(FilePathWatcher);
131};
132
[email protected]493c8002011-04-14 16:56:01133} // namespace files
134} // namespace base
135
136#endif // BASE_FILES_FILE_PATH_WATCHER_H_