This source file includes following definitions.
- origin_loop_
- StartWatching
- StopWatching
- GetWatchedObject
- DoneWaiting
- Signal
- WillDestroyCurrentMessageLoop
#include "base/win/object_watcher.h"
#include "base/bind.h"
#include "base/logging.h"
namespace base {
namespace win {
ObjectWatcher::ObjectWatcher()
: weak_factory_(this),
object_(NULL),
wait_object_(NULL),
origin_loop_(NULL) {
}
ObjectWatcher::~ObjectWatcher() {
StopWatching();
}
bool ObjectWatcher::StartWatching(HANDLE object, Delegate* delegate) {
CHECK(delegate);
if (wait_object_) {
NOTREACHED() << "Already watching an object";
return false;
}
DWORD wait_flags = WT_EXECUTEINWAITTHREAD | WT_EXECUTEONLYONCE;
callback_ = base::Bind(&ObjectWatcher::Signal, weak_factory_.GetWeakPtr(),
delegate);
object_ = object;
origin_loop_ = MessageLoop::current();
if (!RegisterWaitForSingleObject(&wait_object_, object, DoneWaiting,
this, INFINITE, wait_flags)) {
DLOG_GETLASTERROR(FATAL) << "RegisterWaitForSingleObject failed";
object_ = NULL;
wait_object_ = NULL;
return false;
}
MessageLoop::current()->AddDestructionObserver(this);
return true;
}
bool ObjectWatcher::StopWatching() {
if (!wait_object_)
return false;
DCHECK_EQ(origin_loop_, MessageLoop::current());
if (!UnregisterWaitEx(wait_object_, INVALID_HANDLE_VALUE)) {
DLOG_GETLASTERROR(FATAL) << "UnregisterWaitEx failed";
return false;
}
weak_factory_.InvalidateWeakPtrs();
object_ = NULL;
wait_object_ = NULL;
MessageLoop::current()->RemoveDestructionObserver(this);
return true;
}
HANDLE ObjectWatcher::GetWatchedObject() {
return object_;
}
void CALLBACK ObjectWatcher::DoneWaiting(void* param, BOOLEAN timed_out) {
DCHECK(!timed_out);
ObjectWatcher* that = static_cast<ObjectWatcher*>(param);
that->origin_loop_->PostTask(FROM_HERE, that->callback_);
that->callback_.Reset();
}
void ObjectWatcher::Signal(Delegate* delegate) {
HANDLE object = object_;
StopWatching();
delegate->OnObjectSignaled(object);
}
void ObjectWatcher::WillDestroyCurrentMessageLoop() {
StopWatching();
}
}
}