blob: 31406289fe376d2e4ae9a87dfa2ff20247c02f5d [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_
9#pragma once
10
darin@chromium.orge585bed2011-08-06 00:34:00 +090011#include "base/base_export.h"
craig.schlenter@chromium.orgb2d581e2011-04-15 01:56:01 +090012#include "base/basictypes.h"
13#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
27// not detect modifications to those files. See file_path_watcher_mac.cc for
28// details.
darin@chromium.orge585bed2011-08-06 00:34:00 +090029class BASE_EXPORT FilePathWatcher {
craig.schlenter@chromium.orgb2d581e2011-04-15 01:56:01 +090030 public:
31 // Declares the callback client code implements to receive notifications. Note
32 // that implementations of this interface should not keep a reference to the
33 // corresponding FileWatcher object to prevent a reference cycle.
34 class Delegate : public base::RefCountedThreadSafe<Delegate> {
35 public:
craig.schlenter@chromium.orgb2d581e2011-04-15 01:56:01 +090036 virtual void OnFilePathChanged(const FilePath& path) = 0;
37 // Called when platform specific code detected an error. The watcher will
38 // not call OnFilePathChanged for future changes.
39 virtual void OnFilePathError(const FilePath& path) {}
rsleevi@chromium.orgb5eb00e2012-04-25 09:42:51 +090040
41 protected:
42 friend class base::RefCountedThreadSafe<Delegate>;
43 virtual ~Delegate() {}
craig.schlenter@chromium.orgb2d581e2011-04-15 01:56:01 +090044 };
45
craig.schlenter@chromium.orgb2d581e2011-04-15 01:56:01 +090046 // Used internally to encapsulate different members on different platforms.
jhawkins@chromium.orgfce1f412011-11-24 05:44:00 +090047 // TODO(jhawkins): Move this into its own file. Also fix the confusing naming
48 // wrt Delegate vs PlatformDelegate.
craig.schlenter@chromium.orgb2d581e2011-04-15 01:56:01 +090049 class PlatformDelegate : public base::RefCountedThreadSafe<PlatformDelegate> {
50 public:
51 PlatformDelegate();
52
53 // Start watching for the given |path| and notify |delegate| about changes.
54 virtual bool Watch(const FilePath& path,
55 Delegate* delegate) WARN_UNUSED_RESULT = 0;
56
57 // Stop watching. This is called from FilePathWatcher's dtor in order to
58 // allow to shut down properly while the object is still alive.
59 // It can be called from any thread.
60 virtual void Cancel() = 0;
61
62 protected:
rsleevi@chromium.orgb5eb00e2012-04-25 09:42:51 +090063 friend class base::RefCountedThreadSafe<PlatformDelegate>;
jhawkins@chromium.orgfce1f412011-11-24 05:44:00 +090064 friend class FilePathWatcher;
65
craig.schlenter@chromium.orgb2d581e2011-04-15 01:56:01 +090066 virtual ~PlatformDelegate();
67
68 // Stop watching. This is only called on the thread of the appropriate
69 // message loop. Since it can also be called more than once, it should
70 // check |is_cancelled()| to avoid duplicate work.
71 virtual void CancelOnMessageLoopThread() = 0;
72
73 scoped_refptr<base::MessageLoopProxy> message_loop() const {
74 return message_loop_;
75 }
76
77 void set_message_loop(base::MessageLoopProxy* loop) {
78 message_loop_ = loop;
79 }
80
81 // Must be called before the PlatformDelegate is deleted.
82 void set_cancelled() {
83 cancelled_ = true;
84 }
85
86 bool is_cancelled() const {
87 return cancelled_;
88 }
89
90 private:
craig.schlenter@chromium.orgb2d581e2011-04-15 01:56:01 +090091 scoped_refptr<base::MessageLoopProxy> message_loop_;
92 bool cancelled_;
93 };
94
jhawkins@chromium.orgfce1f412011-11-24 05:44:00 +090095 FilePathWatcher();
szym@chromium.org5ef94e22012-02-25 07:18:52 +090096 virtual ~FilePathWatcher();
jhawkins@chromium.orgfce1f412011-11-24 05:44:00 +090097
98 // A callback that always cleans up the PlatformDelegate, either when executed
99 // or when deleted without having been executed at all, as can happen during
100 // shutdown.
101 static void CancelWatch(const scoped_refptr<PlatformDelegate>& delegate);
102
103 // Register interest in any changes on |path|. OnPathChanged will be called
104 // back for each change. Returns true on success.
105 // OnFilePathChanged() will be called on the same thread as Watch() is called,
106 // which should have a MessageLoop of TYPE_IO.
szym@chromium.org5ef94e22012-02-25 07:18:52 +0900107 virtual bool Watch(const FilePath& path, Delegate* delegate)
108 WARN_UNUSED_RESULT;
jhawkins@chromium.orgfce1f412011-11-24 05:44:00 +0900109
craig.schlenter@chromium.orgb2d581e2011-04-15 01:56:01 +0900110 private:
111 scoped_refptr<PlatformDelegate> impl_;
112
113 DISALLOW_COPY_AND_ASSIGN(FilePathWatcher);
114};
115
116} // namespace files
117} // namespace base
118
119#endif // BASE_FILES_FILE_PATH_WATCHER_H_