| 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 "chrome/browser/chromeos/dbus/blocking_method_caller.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "dbus/bus.h" | |
| 9 #include "dbus/object_proxy.h" | |
| 10 | |
| 11 namespace chromeos { | |
| 12 | |
| 13 namespace { | |
| 14 | |
| 15 // A utility class to ensure the WaitableEvent is signaled. | |
| 16 class WaitableEventSignaler { | |
| 17 public: | |
| 18 explicit WaitableEventSignaler(base::WaitableEvent* event) : event_(event) {} | |
| 19 | |
| 20 ~WaitableEventSignaler() { | |
| 21 event_->Signal(); | |
| 22 } | |
| 23 | |
| 24 private: | |
| 25 base::WaitableEvent* event_; | |
| 26 }; | |
| 27 | |
| 28 // This function is a part of CallMethodAndBlock implementation. | |
| 29 void CallMethodAndBlockInternal( | |
| 30 dbus::Response** response, | |
| 31 WaitableEventSignaler* signaler, | |
| 32 dbus::ObjectProxy* proxy, | |
| 33 dbus::MethodCall* method_call) { | |
| 34 *response = proxy->CallMethodAndBlock( | |
| 35 method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT); | |
| 36 } | |
| 37 | |
| 38 } // namespace | |
| 39 | |
| 40 BlockingMethodCaller::BlockingMethodCaller(dbus::Bus* bus, | |
| 41 dbus::ObjectProxy* proxy) | |
| 42 : bus_(bus), | |
| 43 proxy_(proxy), | |
| 44 on_blocking_method_call_(false /* manual_reset */, | |
| 45 false /* initially_signaled */) { | |
| 46 } | |
| 47 | |
| 48 BlockingMethodCaller::~BlockingMethodCaller() { | |
| 49 } | |
| 50 | |
| 51 dbus::Response* BlockingMethodCaller::CallMethodAndBlock( | |
| 52 dbus::MethodCall* method_call) { | |
| 53 WaitableEventSignaler* signaler = | |
| 54 new WaitableEventSignaler(&on_blocking_method_call_); | |
| 55 dbus::Response* response = NULL; | |
| 56 bus_->PostTaskToDBusThread( | |
| 57 FROM_HERE, | |
| 58 base::Bind(&CallMethodAndBlockInternal, | |
| 59 &response, | |
| 60 base::Owned(signaler), | |
| 61 base::Unretained(proxy_), | |
| 62 method_call)); | |
| 63 on_blocking_method_call_.Wait(); | |
| 64 return response; | |
| 65 } | |
| 66 | |
| 67 } // namespace chromeos | |
| OLD | NEW |