| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 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. | |
| 4 | |
| 5 #include "base/win/object_watcher.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/logging.h" | |
| 9 | |
| 10 namespace base { | |
| 11 namespace win { | |
| 12 | |
| 13 //----------------------------------------------------------------------------- | |
| 14 | |
| 15 ObjectWatcher::ObjectWatcher() | |
| 16 : object_(NULL), | |
| 17 wait_object_(NULL), | |
| 18 origin_loop_(NULL), | |
| 19 weak_factory_(this) { | |
| 20 } | |
| 21 | |
| 22 ObjectWatcher::~ObjectWatcher() { | |
| 23 StopWatching(); | |
| 24 } | |
| 25 | |
| 26 bool ObjectWatcher::StartWatching(HANDLE object, Delegate* delegate) { | |
| 27 CHECK(delegate); | |
| 28 if (wait_object_) { | |
| 29 NOTREACHED() << "Already watching an object"; | |
| 30 return false; | |
| 31 } | |
| 32 | |
| 33 // Since our job is to just notice when an object is signaled and report the | |
| 34 // result back to this thread, we can just run on a Windows wait thread. | |
| 35 DWORD wait_flags = WT_EXECUTEINWAITTHREAD | WT_EXECUTEONLYONCE; | |
| 36 | |
| 37 // DoneWaiting can be synchronously called from RegisterWaitForSingleObject, | |
| 38 // so set up all state now. | |
| 39 callback_ = base::Bind(&ObjectWatcher::Signal, weak_factory_.GetWeakPtr(), | |
| 40 delegate); | |
| 41 object_ = object; | |
| 42 origin_loop_ = MessageLoop::current(); | |
| 43 | |
| 44 if (!RegisterWaitForSingleObject(&wait_object_, object, DoneWaiting, | |
| 45 this, INFINITE, wait_flags)) { | |
| 46 DPLOG(FATAL) << "RegisterWaitForSingleObject failed"; | |
| 47 object_ = NULL; | |
| 48 wait_object_ = NULL; | |
| 49 return false; | |
| 50 } | |
| 51 | |
| 52 // We need to know if the current message loop is going away so we can | |
| 53 // prevent the wait thread from trying to access a dead message loop. | |
| 54 MessageLoop::current()->AddDestructionObserver(this); | |
| 55 return true; | |
| 56 } | |
| 57 | |
| 58 bool ObjectWatcher::StopWatching() { | |
| 59 if (!wait_object_) | |
| 60 return false; | |
| 61 | |
| 62 // Make sure ObjectWatcher is used in a single-threaded fashion. | |
| 63 DCHECK_EQ(origin_loop_, MessageLoop::current()); | |
| 64 | |
| 65 // Blocking call to cancel the wait. Any callbacks already in progress will | |
| 66 // finish before we return from this call. | |
| 67 if (!UnregisterWaitEx(wait_object_, INVALID_HANDLE_VALUE)) { | |
| 68 DPLOG(FATAL) << "UnregisterWaitEx failed"; | |
| 69 return false; | |
| 70 } | |
| 71 | |
| 72 weak_factory_.InvalidateWeakPtrs(); | |
| 73 object_ = NULL; | |
| 74 wait_object_ = NULL; | |
| 75 | |
| 76 MessageLoop::current()->RemoveDestructionObserver(this); | |
| 77 return true; | |
| 78 } | |
| 79 | |
| 80 bool ObjectWatcher::IsWatching() const { | |
| 81 return object_ != NULL; | |
| 82 } | |
| 83 | |
| 84 HANDLE ObjectWatcher::GetWatchedObject() const { | |
| 85 return object_; | |
| 86 } | |
| 87 | |
| 88 // static | |
| 89 void CALLBACK ObjectWatcher::DoneWaiting(void* param, BOOLEAN timed_out) { | |
| 90 DCHECK(!timed_out); | |
| 91 | |
| 92 // The destructor blocks on any callbacks that are in flight, so we know that | |
| 93 // that is always a pointer to a valid ObjectWater. | |
| 94 ObjectWatcher* that = static_cast<ObjectWatcher*>(param); | |
| 95 that->origin_loop_->task_runner()->PostTask(FROM_HERE, that->callback_); | |
| 96 that->callback_.Reset(); | |
| 97 } | |
| 98 | |
| 99 void ObjectWatcher::Signal(Delegate* delegate) { | |
| 100 // Signaling the delegate may result in our destruction or a nested call to | |
| 101 // StartWatching(). As a result, we save any state we need and clear previous | |
| 102 // watcher state before signaling the delegate. | |
| 103 HANDLE object = object_; | |
| 104 StopWatching(); | |
| 105 delegate->OnObjectSignaled(object); | |
| 106 } | |
| 107 | |
| 108 void ObjectWatcher::WillDestroyCurrentMessageLoop() { | |
| 109 // Need to shutdown the watch so that we don't try to access the MessageLoop | |
| 110 // after this point. | |
| 111 StopWatching(); | |
| 112 } | |
| 113 | |
| 114 } // namespace win | |
| 115 } // namespace base | |
| OLD | NEW |