Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(298)

Unified Diff: net/base/file_path_watcher_callback.cc

Issue 9540011: [net] Add DNS-related signals and NetLog to NetworkChangeNotifier. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Moved DNS watch to a separate IO thread on mac. Created 8 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « net/base/file_path_watcher_callback.h ('k') | net/base/network_change_notifier.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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
+
« no previous file with comments | « net/base/file_path_watcher_callback.h ('k') | net/base/network_change_notifier.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698