| Index: net/base/file_path_watcher_callback.cc
|
| diff --git a/net/base/file_path_watcher_callback.cc b/net/base/file_path_watcher_callback.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..afa24c30ab6fd3cf1e3d284369aa586e01f4298d
|
| --- /dev/null
|
| +++ b/net/base/file_path_watcher_callback.cc
|
| @@ -0,0 +1,87 @@
|
| +// Copyright (c) 2012 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.
|
| +
|
| +#include "net/base/file_path_watcher_callback.h"
|
| +
|
| +#include "base/callback.h"
|
| +#include "base/files/file_path_watcher.h"
|
| +#include "base/logging.h"
|
| +
|
| +namespace net {
|
| +
|
| +// A FilePathWatcher::Delegate that forwards calls to FilePathWatcherCallback.
|
| +class FilePathWatcherCallback::WatcherDelegate
|
| + : public base::files::FilePathWatcher::Delegate {
|
| + public:
|
| + explicit WatcherDelegate(FilePathWatcherCallback* watcher)
|
| + : watcher_(watcher) {}
|
| +
|
| + void Cancel() {
|
| + watcher_ = NULL;
|
| + }
|
| +
|
| + void OnFilePathChanged(const FilePath& path) OVERRIDE {
|
| + if (watcher_)
|
| + watcher_->OnFilePathChanged();
|
| + }
|
| +
|
| + void OnFilePathError(const FilePath& path) OVERRIDE {
|
| + if (watcher_)
|
| + watcher_->OnFilePathError();
|
| + }
|
| +
|
| + private:
|
| + virtual ~WatcherDelegate() {}
|
| + FilePathWatcherCallback* watcher_;
|
| +};
|
| +
|
| +FilePathWatcherCallback::FilePathWatcherCallback() {
|
| + // No need to restrict the thread until the state is created in Watch.
|
| + DetachFromThread();
|
| +}
|
| +
|
| +FilePathWatcherCallback::~FilePathWatcherCallback() {
|
| + Cancel();
|
| +}
|
| +
|
| +void FilePathWatcherCallback::Cancel() {
|
| + DCHECK(CalledOnValidThread());
|
| + if (delegate_) {
|
| + delegate_->Cancel();
|
| + delegate_.release();
|
| + }
|
| + watcher_.reset(NULL);
|
| + // All state is gone so it's okay to detach from thread.
|
| + DetachFromThread();
|
| +}
|
| +
|
| +bool FilePathWatcherCallback::Watch(
|
| + const FilePath& path,
|
| + const base::Callback<void(bool succeeded)>& callback) {
|
| + DCHECK(CalledOnValidThread());
|
| + if (delegate_)
|
| + delegate_->Cancel();
|
| + watcher_.reset(new base::files::FilePathWatcher());
|
| + delegate_ = new WatcherDelegate(this);
|
| + if (watcher_->Watch(path, delegate_.get()))
|
| + return true;
|
| +
|
| + Cancel();
|
| + return false;
|
| +}
|
| +
|
| +void FilePathWatcherCallback::OnFilePathChanged() {
|
| + DCHECK(CalledOnValidThread());
|
| + callback_.Run(true);
|
| +}
|
| +
|
| +void FilePathWatcherCallback::OnFilePathError() {
|
| + DCHECK(CalledOnValidThread());
|
| + base::Callback<void(bool succeeded)> callback = callback_;
|
| + Cancel();
|
| + callback.Run(false);
|
| +}
|
| +
|
| +} // namespace net
|
| +
|
|
|