Separate Simple Backend creation from initialization.
[chromium-blink-merge.git] / chromeos / dbus / blocking_method_caller.cc
blob3c288f1449c7caa0fd44c11a8b29ed9573b31f44
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.
5 #include "chromeos/dbus/blocking_method_caller.h"
7 #include "base/bind.h"
8 #include "base/location.h"
9 #include "base/threading/thread_restrictions.h"
10 #include "dbus/bus.h"
11 #include "dbus/object_proxy.h"
13 namespace chromeos {
15 namespace {
17 // This function is a part of CallMethodAndBlock implementation.
18 void CallMethodAndBlockInternal(
19 scoped_ptr<dbus::Response>* response,
20 base::ScopedClosureRunner* signaler,
21 dbus::ObjectProxy* proxy,
22 dbus::MethodCall* method_call) {
23 *response = proxy->CallMethodAndBlock(
24 method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT);
27 } // namespace
29 BlockingMethodCaller::BlockingMethodCaller(dbus::Bus* bus,
30 dbus::ObjectProxy* proxy)
31 : bus_(bus),
32 proxy_(proxy),
33 on_blocking_method_call_(false /* manual_reset */,
34 false /* initially_signaled */) {
37 BlockingMethodCaller::~BlockingMethodCaller() {
40 scoped_ptr<dbus::Response> BlockingMethodCaller::CallMethodAndBlock(
41 dbus::MethodCall* method_call) {
42 // on_blocking_method_call_->Signal() will be called when |signaler| is
43 // destroyed.
44 base::Closure signal_task(
45 base::Bind(&base::WaitableEvent::Signal,
46 base::Unretained(&on_blocking_method_call_)));
47 base::ScopedClosureRunner* signaler =
48 new base::ScopedClosureRunner(signal_task);
50 scoped_ptr<dbus::Response> response;
51 bus_->PostTaskToDBusThread(
52 FROM_HERE,
53 base::Bind(&CallMethodAndBlockInternal,
54 &response,
55 base::Owned(signaler),
56 base::Unretained(proxy_),
57 method_call));
58 // http://crbug.com/125360
59 base::ThreadRestrictions::ScopedAllowWait allow_wait;
60 on_blocking_method_call_.Wait();
61 return response.Pass();
64 } // namespace chromeos