summaryrefslogtreecommitdiffstats
path: root/base/files
diff options
context:
space:
mode:
authorjhawkins@chromium.org <jhawkins@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-11-23 20:44:00 +0000
committerjhawkins@chromium.org <jhawkins@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-11-23 20:44:00 +0000
commit29ec5c1efd883fcb60d4dd4f4b8f00151c634e9c (patch)
tree08a180ed93fc077d53126ca01e845dcdb18f2a2e /base/files
parentc2b1b307c22a71c62f17bf486ce26f0baf0281e2 (diff)
downloadchromium_src-29ec5c1efd883fcb60d4dd4f4b8f00151c634e9c.zip
chromium_src-29ec5c1efd883fcb60d4dd4f4b8f00151c634e9c.tar.gz
chromium_src-29ec5c1efd883fcb60d4dd4f4b8f00151c634e9c.tar.bz2
base::Bind: Convert FilePathWatcher.
BUG=none TEST=none R=willchan@chromium.org Review URL: http://codereview.chromium.org/8677015 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@111405 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/files')
-rw-r--r--base/files/file_path_watcher.cc6
-rw-r--r--base/files/file_path_watcher.h49
-rw-r--r--base/files/file_path_watcher_browsertest.cc52
-rw-r--r--base/files/file_path_watcher_linux.cc3
-rw-r--r--base/files/file_path_watcher_win.cc4
5 files changed, 44 insertions, 70 deletions
diff --git a/base/files/file_path_watcher.cc b/base/files/file_path_watcher.cc
index 7ce64ac..086da1e 100644
--- a/base/files/file_path_watcher.cc
+++ b/base/files/file_path_watcher.cc
@@ -17,6 +17,12 @@ FilePathWatcher::~FilePathWatcher() {
impl_->Cancel();
}
+// static
+void FilePathWatcher::CancelWatch(
+ const scoped_refptr<PlatformDelegate>& delegate) {
+ delegate->CancelOnMessageLoopThread();
+}
+
bool FilePathWatcher::Watch(const FilePath& path, Delegate* delegate) {
DCHECK(path.IsAbsolute());
return impl_->Watch(path, delegate);
diff --git a/base/files/file_path_watcher.h b/base/files/file_path_watcher.h
index ed17464..9c27ce4 100644
--- a/base/files/file_path_watcher.h
+++ b/base/files/file_path_watcher.h
@@ -40,37 +40,9 @@ class BASE_EXPORT FilePathWatcher {
virtual void OnFilePathError(const FilePath& path) {}
};
- FilePathWatcher();
- ~FilePathWatcher();
-
- // Register interest in any changes on |path|. OnPathChanged will be called
- // back for each change. Returns true on success.
- // OnFilePathChanged() will be called on the same thread as Watch() is called,
- // which should have a MessageLoop of TYPE_IO.
- bool Watch(const FilePath& path, Delegate* delegate) WARN_UNUSED_RESULT;
-
- class PlatformDelegate;
-
- // A custom Task that always cleans up the PlatformDelegate, either when
- // executed or when deleted without having been executed at all, as can
- // happen during shutdown.
- class CancelTask : public Task {
- public:
- CancelTask(PlatformDelegate* delegate): delegate_(delegate) {}
- virtual ~CancelTask() {
- delegate_->CancelOnMessageLoopThread();
- }
-
- virtual void Run() OVERRIDE {
- delegate_->CancelOnMessageLoopThread();
- }
- private:
- scoped_refptr<PlatformDelegate> delegate_;
-
- DISALLOW_COPY_AND_ASSIGN(CancelTask);
- };
-
// Used internally to encapsulate different members on different platforms.
+ // TODO(jhawkins): Move this into its own file. Also fix the confusing naming
+ // wrt Delegate vs PlatformDelegate.
class PlatformDelegate : public base::RefCountedThreadSafe<PlatformDelegate> {
public:
PlatformDelegate();
@@ -85,6 +57,8 @@ class BASE_EXPORT FilePathWatcher {
virtual void Cancel() = 0;
protected:
+ friend class FilePathWatcher;
+
virtual ~PlatformDelegate();
// Stop watching. This is only called on the thread of the appropriate
@@ -111,12 +85,25 @@ class BASE_EXPORT FilePathWatcher {
private:
friend class base::RefCountedThreadSafe<PlatformDelegate>;
- friend class CancelTask;
scoped_refptr<base::MessageLoopProxy> message_loop_;
bool cancelled_;
};
+ FilePathWatcher();
+ ~FilePathWatcher();
+
+ // A callback that always cleans up the PlatformDelegate, either when executed
+ // or when deleted without having been executed at all, as can happen during
+ // shutdown.
+ static void CancelWatch(const scoped_refptr<PlatformDelegate>& delegate);
+
+ // Register interest in any changes on |path|. OnPathChanged will be called
+ // back for each change. Returns true on success.
+ // OnFilePathChanged() will be called on the same thread as Watch() is called,
+ // which should have a MessageLoop of TYPE_IO.
+ bool Watch(const FilePath& path, Delegate* delegate) WARN_UNUSED_RESULT;
+
private:
scoped_refptr<PlatformDelegate> impl_;
diff --git a/base/files/file_path_watcher_browsertest.cc b/base/files/file_path_watcher_browsertest.cc
index f48a3b4..f0f789a 100644
--- a/base/files/file_path_watcher_browsertest.cc
+++ b/base/files/file_path_watcher_browsertest.cc
@@ -72,7 +72,7 @@ class NotificationCollector
// Check whether all delegates have been signaled.
if (signaled_ == delegates_)
- loop_->PostTask(FROM_HERE, new MessageLoop::QuitTask());
+ loop_->PostTask(FROM_HERE, MessageLoop::QuitClosure());
}
// Set of registered delegates.
@@ -113,34 +113,14 @@ class TestDelegate : public FilePathWatcher::Delegate {
DISALLOW_COPY_AND_ASSIGN(TestDelegate);
};
-// A helper class for setting up watches on the file thread.
-class SetupWatchTask : public Task {
- public:
- SetupWatchTask(const FilePath& target,
- FilePathWatcher* watcher,
- FilePathWatcher::Delegate* delegate,
- bool* result,
- base::WaitableEvent* completion)
- : target_(target),
- watcher_(watcher),
- delegate_(delegate),
- result_(result),
- completion_(completion) {}
-
- void Run() {
- *result_ = watcher_->Watch(target_, delegate_);
- completion_->Signal();
- }
-
- private:
- const FilePath target_;
- FilePathWatcher* watcher_;
- FilePathWatcher::Delegate* delegate_;
- bool* result_;
- base::WaitableEvent* completion_;
-
- DISALLOW_COPY_AND_ASSIGN(SetupWatchTask);
-};
+void SetupWatchCallback(const FilePath& target,
+ FilePathWatcher* watcher,
+ FilePathWatcher::Delegate* delegate,
+ bool* result,
+ base::WaitableEvent* completion) {
+ *result = watcher->Watch(target, delegate);
+ completion->Signal();
+}
class FilePathWatcherTest : public testing::Test {
public:
@@ -182,12 +162,10 @@ class FilePathWatcherTest : public testing::Test {
FilePathWatcher::Delegate* delegate) WARN_UNUSED_RESULT {
base::WaitableEvent completion(false, false);
bool result;
- file_thread_.message_loop_proxy()->PostTask(FROM_HERE,
- new SetupWatchTask(target,
- watcher,
- delegate,
- &result,
- &completion));
+ file_thread_.message_loop_proxy()->PostTask(
+ FROM_HERE,
+ base::Bind(SetupWatchCallback, target, watcher,
+ make_scoped_refptr(delegate), &result, &completion));
completion.Wait();
return result;
}
@@ -266,7 +244,7 @@ class Deleter : public FilePathWatcher::Delegate {
virtual void OnFilePathChanged(const FilePath& path) {
watcher_.reset();
- loop_->PostTask(FROM_HERE, new MessageLoop::QuitTask());
+ loop_->PostTask(FROM_HERE, MessageLoop::QuitClosure());
}
scoped_ptr<FilePathWatcher> watcher_;
@@ -778,7 +756,7 @@ TEST_F(FilePathWatcherTest, DirAttributesChanged) {
// to access the file.
ASSERT_TRUE(ChangeFilePermissions(test_dir1, Read, false));
loop_.PostDelayedTask(FROM_HERE,
- new MessageLoop::QuitTask,
+ MessageLoop::QuitClosure(),
TestTimeouts::tiny_timeout_ms());
ASSERT_FALSE(WaitForEvents());
ASSERT_TRUE(ChangeFilePermissions(test_dir1, Read, true));
diff --git a/base/files/file_path_watcher_linux.cc b/base/files/file_path_watcher_linux.cc
index c9be4ba..86c963c 100644
--- a/base/files/file_path_watcher_linux.cc
+++ b/base/files/file_path_watcher_linux.cc
@@ -412,7 +412,8 @@ void FilePathWatcherImpl::Cancel() {
// Switch to the message_loop_ if necessary so we can access |watches_|.
if (!message_loop()->BelongsToCurrentThread()) {
message_loop()->PostTask(FROM_HERE,
- new FilePathWatcher::CancelTask(this));
+ base::Bind(&FilePathWatcher::CancelWatch,
+ make_scoped_refptr(this)));
} else {
CancelOnMessageLoopThread();
}
diff --git a/base/files/file_path_watcher_win.cc b/base/files/file_path_watcher_win.cc
index 84a5398..37b1e7b 100644
--- a/base/files/file_path_watcher_win.cc
+++ b/base/files/file_path_watcher_win.cc
@@ -4,6 +4,7 @@
#include "base/files/file_path_watcher.h"
+#include "base/bind.h"
#include "base/file_path.h"
#include "base/file_util.h"
#include "base/logging.h"
@@ -104,7 +105,8 @@ void FilePathWatcherImpl::Cancel() {
// Switch to the file thread if necessary so we can stop |watcher_|.
if (!message_loop()->BelongsToCurrentThread()) {
message_loop()->PostTask(FROM_HERE,
- new FilePathWatcher::CancelTask(this));
+ base::Bind(&FilePathWatcher::CancelWatch,
+ make_scoped_refptr(this)));
} else {
CancelOnMessageLoopThread();
}