Index: base/win/object_watcher.cc |
diff --git a/base/win/object_watcher.cc b/base/win/object_watcher.cc |
index 4df54a45d6bf90b5286da96e93dcf674653c2d68..89eebe033c0af642d5757c26b126809cf526feeb 100644 |
--- a/base/win/object_watcher.cc |
+++ b/base/win/object_watcher.cc |
@@ -6,19 +6,14 @@ |
#include "base/bind.h" |
#include "base/logging.h" |
+#include "base/threading/thread_task_runner_handle.h" |
namespace base { |
namespace win { |
//----------------------------------------------------------------------------- |
-ObjectWatcher::ObjectWatcher() |
- : object_(NULL), |
- wait_object_(NULL), |
- origin_loop_(NULL), |
- run_once_(true), |
- weak_factory_(this) { |
-} |
+ObjectWatcher::ObjectWatcher() : weak_factory_(this) {} |
ObjectWatcher::~ObjectWatcher() { |
StopWatching(); |
@@ -38,7 +33,7 @@ bool ObjectWatcher::StopWatching() { |
return false; |
// Make sure ObjectWatcher is used in a single-threaded fashion. |
- DCHECK_EQ(origin_loop_, MessageLoop::current()); |
+ DCHECK(task_runner_->BelongsToCurrentThread()); |
// Blocking call to cancel the wait. Any callbacks already in progress will |
// finish before we return from this call. |
@@ -47,16 +42,12 @@ bool ObjectWatcher::StopWatching() { |
return false; |
} |
- weak_factory_.InvalidateWeakPtrs(); |
- object_ = NULL; |
- wait_object_ = NULL; |
- |
- origin_loop_->RemoveDestructionObserver(this); |
+ Reset(); |
return true; |
} |
bool ObjectWatcher::IsWatching() const { |
- return object_ != NULL; |
+ return object_ != nullptr; |
} |
HANDLE ObjectWatcher::GetWatchedObject() const { |
@@ -70,22 +61,18 @@ void CALLBACK ObjectWatcher::DoneWaiting(void* param, BOOLEAN timed_out) { |
// The destructor blocks on any callbacks that are in flight, so we know that |
// that is always a pointer to a valid ObjectWater. |
ObjectWatcher* that = static_cast<ObjectWatcher*>(param); |
- that->origin_loop_->task_runner()->PostTask(FROM_HERE, that->callback_); |
+ that->task_runner_->PostTask(FROM_HERE, that->callback_); |
if (that->run_once_) |
that->callback_.Reset(); |
} |
bool ObjectWatcher::StartWatchingInternal(HANDLE object, Delegate* delegate, |
bool execute_only_once) { |
- CHECK(delegate); |
- if (wait_object_) { |
- NOTREACHED() << "Already watching an object"; |
- return false; |
- } |
+ DCHECK(delegate); |
+ DCHECK(!wait_object_) << "Already watching an object"; |
+ DCHECK(ThreadTaskRunnerHandle::IsSet()); |
- origin_loop_ = MessageLoop::current(); |
- if (!origin_loop_) |
- return false; |
+ task_runner_ = ThreadTaskRunnerHandle::Get(); |
run_once_ = execute_only_once; |
@@ -104,14 +91,10 @@ bool ObjectWatcher::StartWatchingInternal(HANDLE object, Delegate* delegate, |
if (!RegisterWaitForSingleObject(&wait_object_, object, DoneWaiting, |
this, INFINITE, wait_flags)) { |
DPLOG(FATAL) << "RegisterWaitForSingleObject failed"; |
- object_ = NULL; |
- wait_object_ = NULL; |
+ Reset(); |
return false; |
} |
- // We need to know if the current message loop is going away so we can |
- // prevent the wait thread from trying to access a dead message loop. |
- origin_loop_->AddDestructionObserver(this); |
return true; |
} |
@@ -125,10 +108,13 @@ void ObjectWatcher::Signal(Delegate* delegate) { |
delegate->OnObjectSignaled(object); |
} |
-void ObjectWatcher::WillDestroyCurrentMessageLoop() { |
- // Need to shutdown the watch so that we don't try to access the MessageLoop |
- // after this point. |
- StopWatching(); |
+void ObjectWatcher::Reset() { |
+ callback_.Reset(); |
+ object_ = nullptr; |
+ wait_object_ = nullptr; |
+ task_runner_ = nullptr; |
+ run_once_ = true; |
+ weak_factory_.InvalidateWeakPtrs(); |
} |
} // namespace win |