summaryrefslogtreecommitdiffstats
path: root/chrome/browser/file_path_watcher.h
diff options
context:
space:
mode:
authormnissler@chromium.org <mnissler@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-08-17 14:26:03 +0000
committermnissler@chromium.org <mnissler@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-08-17 14:26:03 +0000
commit392d74d532301d0db584a1c320d32f4899d58d8f (patch)
treea3e31864beb5433f92494887f25db45ca9518686 /chrome/browser/file_path_watcher.h
parenta66c64480aee2b73328961fb5ffdd074098a74fb (diff)
downloadchromium_src-392d74d532301d0db584a1c320d32f4899d58d8f.zip
chromium_src-392d74d532301d0db584a1c320d32f4899d58d8f.tar.gz
chromium_src-392d74d532301d0db584a1c320d32f4899d58d8f.tar.bz2
Add support for watching directories to FileWatcher.
BUG=none TEST=Unit tests in file_watcher_unittest.cc. Review URL: http://codereview.chromium.org/3149004 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@56341 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/file_path_watcher.h')
-rw-r--r--chrome/browser/file_path_watcher.h70
1 files changed, 70 insertions, 0 deletions
diff --git a/chrome/browser/file_path_watcher.h b/chrome/browser/file_path_watcher.h
new file mode 100644
index 0000000..b8c857f
--- /dev/null
+++ b/chrome/browser/file_path_watcher.h
@@ -0,0 +1,70 @@
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// This module provides a way to monitor a file or directory for changes.
+
+#ifndef CHROME_BROWSER_FILE_PATH_WATCHER_H_
+#define CHROME_BROWSER_FILE_PATH_WATCHER_H_
+#pragma once
+
+#include "base/basictypes.h"
+#include "base/file_path.h"
+#include "base/ref_counted.h"
+#include "chrome/browser/chrome_thread.h"
+
+// This class lets you register interest in changes on a FilePath.
+// The delegate will get called whenever the file or directory referenced by the
+// FilePath is changed, including created or deleted. Due to limitations in the
+// underlying OS APIs, spurious notifications might occur that don't relate to
+// an actual change to the watch target.
+class FilePathWatcher {
+ public:
+ // Declares the callback client code implements to receive notifications. Note
+ // that implementations of this interface should not keep a reference to the
+ // corresponding FileWatcher object to prevent a reference cycle.
+ class Delegate : public base::RefCountedThreadSafe<Delegate> {
+ public:
+ virtual ~Delegate() {}
+ virtual void OnFilePathChanged(const FilePath& path) = 0;
+ // Called when platform specific code detected an error. The watcher will
+ // not call OnFilePathChanged for future changes.
+ virtual void OnError() {}
+ };
+
+ FilePathWatcher();
+ ~FilePathWatcher() {
+ impl_->Cancel();
+ }
+
+ // Register interest in any changes on |path|. OnPathChanged will be called
+ // back for each change. Returns true on success.
+ bool Watch(const FilePath& path, Delegate* delegate) WARN_UNUSED_RESULT {
+ DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE));
+ DCHECK(path.IsAbsolute());
+ return impl_->Watch(path, delegate);
+ }
+
+ // Used internally to encapsulate different members on different platforms.
+ class PlatformDelegate
+ : public base::RefCountedThreadSafe<PlatformDelegate,
+ ChromeThread::DeleteOnFileThread> {
+ public:
+ virtual ~PlatformDelegate() {}
+
+ // Start watching for the given |path| and notify |delegate| about changes.
+ virtual bool Watch(const FilePath& path, Delegate* delegate)
+ WARN_UNUSED_RESULT = 0;
+
+ // Stop watching. This is called from FilePathWatcher's dtor in order to
+ // allow to shut down properly while the object is still alive.
+ virtual void Cancel() {}
+ };
+
+ private:
+ scoped_refptr<PlatformDelegate> impl_;
+
+ DISALLOW_COPY_AND_ASSIGN(FilePathWatcher);
+};
+
+#endif // CHROME_BROWSER_FILE_PATH_WATCHER_H_