Define DevTools content API
[chromium-blink-merge.git] / base / win / object_watcher.cc
blob6b500cd1c9937ce57f38ecb2f4910b386af4511d
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/win/object_watcher.h"
7 #include "base/bind.h"
8 #include "base/logging.h"
10 namespace base {
11 namespace win {
13 //-----------------------------------------------------------------------------
15 ObjectWatcher::ObjectWatcher()
16 : weak_factory_(this),
17 object_(NULL),
18 wait_object_(NULL),
19 origin_loop_(NULL) {
22 ObjectWatcher::~ObjectWatcher() {
23 StopWatching();
26 bool ObjectWatcher::StartWatching(HANDLE object, Delegate* delegate) {
27 if (wait_object_) {
28 NOTREACHED() << "Already watching an object";
29 return false;
32 // Since our job is to just notice when an object is signaled and report the
33 // result back to this thread, we can just run on a Windows wait thread.
34 DWORD wait_flags = WT_EXECUTEINWAITTHREAD | WT_EXECUTEONLYONCE;
36 // DoneWaiting can be synchronously called from RegisterWaitForSingleObject,
37 // so set up all state now.
38 callback_ = base::Bind(&ObjectWatcher::Signal, weak_factory_.GetWeakPtr(),
39 delegate);
40 object_ = object;
41 origin_loop_ = MessageLoop::current();
43 if (!RegisterWaitForSingleObject(&wait_object_, object, DoneWaiting,
44 this, INFINITE, wait_flags)) {
45 NOTREACHED() << "RegisterWaitForSingleObject failed: " << GetLastError();
46 object_ = NULL;
47 wait_object_ = NULL;
48 return false;
51 // We need to know if the current message loop is going away so we can
52 // prevent the wait thread from trying to access a dead message loop.
53 MessageLoop::current()->AddDestructionObserver(this);
54 return true;
57 bool ObjectWatcher::StopWatching() {
58 if (!wait_object_)
59 return false;
61 // Make sure ObjectWatcher is used in a single-threaded fashion.
62 DCHECK(origin_loop_ == MessageLoop::current());
64 // Blocking call to cancel the wait. Any callbacks already in progress will
65 // finish before we return from this call.
66 if (!UnregisterWaitEx(wait_object_, INVALID_HANDLE_VALUE)) {
67 NOTREACHED() << "UnregisterWaitEx failed: " << GetLastError();
68 return false;
71 weak_factory_.InvalidateWeakPtrs();
72 object_ = NULL;
73 wait_object_ = NULL;
75 MessageLoop::current()->RemoveDestructionObserver(this);
76 return true;
79 HANDLE ObjectWatcher::GetWatchedObject() {
80 return object_;
83 // static
84 void CALLBACK ObjectWatcher::DoneWaiting(void* param, BOOLEAN timed_out) {
85 DCHECK(!timed_out);
87 // The destructor blocks on any callbacks that are in flight, so we know that
88 // that is always a pointer to a valid ObjectWater.
89 ObjectWatcher* that = static_cast<ObjectWatcher*>(param);
90 that->origin_loop_->PostTask(FROM_HERE, that->callback_);
91 that->callback_.Reset();
94 void ObjectWatcher::Signal(Delegate* delegate) {
95 // Signaling the delegate may result in our destruction or a nested call to
96 // StartWatching(). As a result, we save any state we need and clear previous
97 // watcher state before signaling the delegate.
98 HANDLE object = object_;
99 StopWatching();
100 delegate->OnObjectSignaled(object);
103 void ObjectWatcher::WillDestroyCurrentMessageLoop() {
104 // Need to shutdown the watch so that we don't try to access the MessageLoop
105 // after this point.
106 StopWatching();
109 } // namespace win
110 } // namespace base