Eliminate FilePathWatcher::Delegate.

BUG=130980

Review URL: https://codereview.chromium.org/11876025

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@176768 0039d316-1c4b-4281-b951-d872f2087c98


CrOS-Libchrome-Original-Commit: 6b5d002b9b35024c284717dbee2bb2f1eb816902
diff --git a/base/files/file_path_watcher.cc b/base/files/file_path_watcher.cc
index bc8db37..3802ee3 100644
--- a/base/files/file_path_watcher.cc
+++ b/base/files/file_path_watcher.cc
@@ -13,33 +13,6 @@
 namespace base {
 namespace files {
 
-namespace {
-
-// A delegate implementation for the callback interface.
-class FilePathWatcherDelegate : public base::files::FilePathWatcher::Delegate {
- public:
-  explicit FilePathWatcherDelegate(const FilePathWatcher::Callback& callback)
-      : callback_(callback) {}
-
-  // FilePathWatcher::Delegate implementation.
-  virtual void OnFilePathChanged(const FilePath& path) OVERRIDE {
-    callback_.Run(path, false);
-  }
-
-  virtual void OnFilePathError(const FilePath& path) OVERRIDE {
-    callback_.Run(path, true);
-  }
-
- private:
-  virtual ~FilePathWatcherDelegate() {}
-
-  FilePathWatcher::Callback callback_;
-
-  DISALLOW_COPY_AND_ASSIGN(FilePathWatcherDelegate);
-};
-
-}  // namespace
-
 FilePathWatcher::~FilePathWatcher() {
   impl_->Cancel();
 }
@@ -50,11 +23,6 @@
   delegate->CancelOnMessageLoopThread();
 }
 
-bool FilePathWatcher::Watch(const FilePath& path, Delegate* delegate) {
-  DCHECK(path.IsAbsolute());
-  return impl_->Watch(path, false, delegate);
-}
-
 FilePathWatcher::PlatformDelegate::PlatformDelegate(): cancelled_(false) {
 }
 
@@ -66,7 +34,7 @@
                             bool recursive,
                             const Callback& callback) {
   DCHECK(path.IsAbsolute());
-  return impl_->Watch(path, recursive, new FilePathWatcherDelegate(callback));
+  return impl_->Watch(path, recursive, callback);
 }
 
 }  // namespace files
diff --git a/base/files/file_path_watcher.h b/base/files/file_path_watcher.h
index 94a3f9a..57b875a 100644
--- a/base/files/file_path_watcher.h
+++ b/base/files/file_path_watcher.h
@@ -33,23 +33,6 @@
   // that case, the callback won't be invoked again.
   typedef base::Callback<void(const FilePath& path, bool error)> Callback;
 
-  // 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.
-  //
-  // Deprecated: see comment on Watch() below.
-  class Delegate : public base::RefCountedThreadSafe<Delegate> {
-   public:
-    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 OnFilePathError(const FilePath& path) {}
-
-   protected:
-    friend class base::RefCountedThreadSafe<Delegate>;
-    virtual ~Delegate() {}
-  };
-
   // 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.
@@ -60,7 +43,7 @@
     // Start watching for the given |path| and notify |delegate| about changes.
     virtual bool Watch(const FilePath& path,
                        bool recursive,
-                       Delegate* delegate) WARN_UNUSED_RESULT = 0;
+                       const Callback& callback) 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.
@@ -108,17 +91,6 @@
   // 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.
-  //
-  // Deprecated: new code should use the callback interface, declared below.
-  // The FilePathWatcher::Delegate interface will be removed once all client
-  // code has been updated. http://crbug.com/130980
-  virtual bool Watch(const FilePath& path, Delegate* delegate)
-      WARN_UNUSED_RESULT;
-
   // Invokes |callback| whenever updates to |path| are detected. This should be
   // called at most once, and from a MessageLoop of TYPE_IO. Set |recursive| to
   // true, to watch |path| and its children. The callback will be invoked on
diff --git a/base/files/file_path_watcher_kqueue.cc b/base/files/file_path_watcher_kqueue.cc
index aebe15a..72c41f1 100644
--- a/base/files/file_path_watcher_kqueue.cc
+++ b/base/files/file_path_watcher_kqueue.cc
@@ -66,7 +66,7 @@
   // FilePathWatcher::PlatformDelegate overrides.
   virtual bool Watch(const FilePath& path,
                      bool recursive,
-                     FilePathWatcher::Delegate* delegate) OVERRIDE;
+                     const FilePathWatcher::Callback& callback) OVERRIDE;
   virtual void Cancel() OVERRIDE;
 
  protected:
@@ -141,7 +141,7 @@
   EventVector events_;
   scoped_refptr<base::MessageLoopProxy> io_message_loop_;
   MessageLoopForIO::FileDescriptorWatcher kqueue_watcher_;
-  scoped_refptr<FilePathWatcher::Delegate> delegate_;
+  FilePathWatcher::Callback callback_;
   FilePath target_;
   int kqueue_;
 
@@ -364,7 +364,7 @@
   // Error values are stored within updates, so check to make sure that no
   // errors occurred.
   if (!AreKeventValuesValid(&updates[0], count)) {
-    delegate_->OnFilePathError(target_);
+    callback_.Run(target_, true /* error */);
     Cancel();
     return;
   }
@@ -411,13 +411,13 @@
 
   if (update_watches) {
     if (!UpdateWatches(&send_notification)) {
-      delegate_->OnFilePathError(target_);
+      callback_.Run(target_, true /* error */);
       Cancel();
     }
   }
 
   if (send_notification) {
-    delegate_->OnFilePathChanged(target_);
+    callback_.Run(target_, false);
   }
 }
 
@@ -431,10 +431,10 @@
 
 bool FilePathWatcherImpl::Watch(const FilePath& path,
                                 bool recursive,
-                                FilePathWatcher::Delegate* delegate) {
+                                const FilePathWatcher::Callback& callback) {
   DCHECK(MessageLoopForIO::current());
   DCHECK(target_.value().empty());  // Can only watch one path.
-  DCHECK(delegate);
+  DCHECK(!callback.is_null());
   DCHECK_EQ(kqueue_, -1);
 
   if (recursive) {
@@ -443,7 +443,7 @@
     return false;
   }
 
-  delegate_ = delegate;
+  callback_ = callback;
   target_ = path;
 
   MessageLoop::current()->AddDestructionObserver(this);
@@ -499,7 +499,7 @@
     events_.clear();
     io_message_loop_ = NULL;
     MessageLoop::current()->RemoveDestructionObserver(this);
-    delegate_ = NULL;
+    callback_.Reset();
   }
 }
 
diff --git a/base/files/file_path_watcher_linux.cc b/base/files/file_path_watcher_linux.cc
index 9e55022..690ac6d 100644
--- a/base/files/file_path_watcher_linux.cc
+++ b/base/files/file_path_watcher_linux.cc
@@ -101,7 +101,7 @@
   // Returns true if watch for |path| has been added successfully.
   virtual bool Watch(const FilePath& path,
                      bool recursive,
-                     FilePathWatcher::Delegate* delegate) OVERRIDE;
+                     const FilePathWatcher::Callback& callback) OVERRIDE;
 
   // Cancel the watch. This unregisters the instance with InotifyReader.
   virtual void Cancel() OVERRIDE;
@@ -137,8 +137,8 @@
   // that exists. Updates |watched_path_|. Returns true on success.
   bool UpdateWatches() WARN_UNUSED_RESULT;
 
-  // Delegate to notify upon changes.
-  scoped_refptr<FilePathWatcher::Delegate> delegate_;
+  // Callback to notify upon changes.
+  FilePathWatcher::Callback callback_;
 
   // The file or directory we're supposed to watch.
   FilePath target_;
@@ -295,8 +295,7 @@
   }
 }
 
-FilePathWatcherImpl::FilePathWatcherImpl()
-    : delegate_(NULL) {
+FilePathWatcherImpl::FilePathWatcherImpl() {
 }
 
 void FilePathWatcherImpl::OnFilePathChanged(InotifyReader::Watch fired_watch,
@@ -339,7 +338,7 @@
       // IN_ISDIR set in the event masks. As a result we may sometimes
       // call UpdateWatches() unnecessarily.
       if (change_on_target_path && !UpdateWatches()) {
-        delegate_->OnFilePathError(target_);
+        callback_.Run(target_, true /* error */);
         return;
       }
 
@@ -354,7 +353,7 @@
       if (target_changed ||
           (change_on_target_path && !created) ||
           (change_on_target_path && file_util::PathExists(target_))) {
-        delegate_->OnFilePathChanged(target_);
+        callback_.Run(target_, false);
         return;
       }
     }
@@ -363,7 +362,7 @@
 
 bool FilePathWatcherImpl::Watch(const FilePath& path,
                                 bool recursive,
-                                FilePathWatcher::Delegate* delegate) {
+                                const FilePathWatcher::Callback& callback) {
   DCHECK(target_.empty());
   DCHECK(MessageLoopForIO::current());
   if (recursive) {
@@ -373,7 +372,7 @@
   }
 
   set_message_loop(base::MessageLoopProxy::current());
-  delegate_ = delegate;
+  callback_ = callback;
   target_ = path;
   MessageLoop::current()->AddDestructionObserver(this);
 
@@ -390,7 +389,7 @@
 }
 
 void FilePathWatcherImpl::Cancel() {
-  if (!delegate_) {
+  if (callback_.is_null()) {
     // Watch was never called, or the |message_loop_| thread is already gone.
     set_cancelled();
     return;
@@ -410,9 +409,9 @@
   if (!is_cancelled())
     set_cancelled();
 
-  if (delegate_) {
+  if (!callback_.is_null()) {
     MessageLoop::current()->RemoveDestructionObserver(this);
-    delegate_ = NULL;
+    callback_.Reset();
   }
 
   for (WatchVector::iterator watch_entry(watches_.begin());
diff --git a/base/files/file_path_watcher_stub.cc b/base/files/file_path_watcher_stub.cc
index 0c25d7f..fc3001f 100644
--- a/base/files/file_path_watcher_stub.cc
+++ b/base/files/file_path_watcher_stub.cc
@@ -16,7 +16,7 @@
  public:
   virtual bool Watch(const FilePath& path,
                      bool recursive,
-                     FilePathWatcher::Delegate* delegate) OVERRIDE {
+                     const FilePathWatcher::Callback& callback) OVERRIDE {
     return false;
   }