OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chromeos/dbus/blocking_method_caller.h" | 5 #include "chromeos/dbus/blocking_method_caller.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/callback_helpers.h" | 8 #include "base/callback_helpers.h" |
9 #include "base/location.h" | 9 #include "base/location.h" |
10 #include "base/task_runner.h" | 10 #include "base/task_runner.h" |
11 #include "base/threading/thread_restrictions.h" | 11 #include "base/threading/thread_restrictions.h" |
12 #include "dbus/bus.h" | 12 #include "dbus/bus.h" |
13 #include "dbus/object_proxy.h" | 13 #include "dbus/object_proxy.h" |
14 #include "dbus/scoped_dbus_error.h" | |
15 | 14 |
16 namespace chromeos { | 15 namespace chromeos { |
17 | 16 |
18 namespace { | 17 namespace { |
19 | 18 |
20 // This function is a part of CallMethodAndBlock implementation. | 19 // This function is a part of CallMethodAndBlock implementation. |
21 void CallMethodAndBlockInternal(std::unique_ptr<dbus::Response>* response, | 20 void CallMethodAndBlockInternal(std::unique_ptr<dbus::Response>* response, |
22 base::ScopedClosureRunner* signaler, | 21 base::ScopedClosureRunner* signaler, |
23 dbus::ObjectProxy* proxy, | 22 dbus::ObjectProxy* proxy, |
24 dbus::MethodCall* method_call, | 23 dbus::MethodCall* method_call) { |
25 dbus::ScopedDBusError* error_out) { | 24 *response = proxy->CallMethodAndBlock( |
26 *response = proxy->CallMethodAndBlockWithErrorDetails( | 25 method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT); |
27 method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, error_out); | |
28 } | 26 } |
29 | 27 |
30 } // namespace | 28 } // namespace |
31 | 29 |
32 BlockingMethodCaller::BlockingMethodCaller(dbus::Bus* bus, | 30 BlockingMethodCaller::BlockingMethodCaller(dbus::Bus* bus, |
33 dbus::ObjectProxy* proxy) | 31 dbus::ObjectProxy* proxy) |
34 : bus_(bus), | 32 : bus_(bus), |
35 proxy_(proxy), | 33 proxy_(proxy), |
36 on_blocking_method_call_( | 34 on_blocking_method_call_( |
37 base::WaitableEvent::ResetPolicy::AUTOMATIC, | 35 base::WaitableEvent::ResetPolicy::AUTOMATIC, |
38 base::WaitableEvent::InitialState::NOT_SIGNALED) {} | 36 base::WaitableEvent::InitialState::NOT_SIGNALED) {} |
39 | 37 |
40 BlockingMethodCaller::~BlockingMethodCaller() { | 38 BlockingMethodCaller::~BlockingMethodCaller() { |
41 } | 39 } |
42 | 40 |
43 std::unique_ptr<dbus::Response> BlockingMethodCaller::CallMethodAndBlock( | 41 std::unique_ptr<dbus::Response> BlockingMethodCaller::CallMethodAndBlock( |
44 dbus::MethodCall* method_call) { | 42 dbus::MethodCall* method_call) { |
45 dbus::ScopedDBusError error; | |
46 return CallMethodAndBlockWithError(method_call, &error); | |
47 } | |
48 | |
49 std::unique_ptr<dbus::Response> | |
50 BlockingMethodCaller::CallMethodAndBlockWithError( | |
51 dbus::MethodCall* method_call, | |
52 dbus::ScopedDBusError* error_out) { | |
53 // on_blocking_method_call_->Signal() will be called when |signaler| is | 43 // on_blocking_method_call_->Signal() will be called when |signaler| is |
54 // destroyed. | 44 // destroyed. |
55 base::Closure signal_task( | 45 base::Closure signal_task( |
56 base::Bind(&base::WaitableEvent::Signal, | 46 base::Bind(&base::WaitableEvent::Signal, |
57 base::Unretained(&on_blocking_method_call_))); | 47 base::Unretained(&on_blocking_method_call_))); |
58 base::ScopedClosureRunner* signaler = | 48 base::ScopedClosureRunner* signaler = |
59 new base::ScopedClosureRunner(signal_task); | 49 new base::ScopedClosureRunner(signal_task); |
60 | 50 |
61 std::unique_ptr<dbus::Response> response; | 51 std::unique_ptr<dbus::Response> response; |
62 bus_->GetDBusTaskRunner()->PostTask( | 52 bus_->GetDBusTaskRunner()->PostTask( |
63 FROM_HERE, | 53 FROM_HERE, |
64 base::Bind(&CallMethodAndBlockInternal, &response, base::Owned(signaler), | 54 base::Bind(&CallMethodAndBlockInternal, |
65 base::Unretained(proxy_), method_call, error_out)); | 55 &response, |
| 56 base::Owned(signaler), |
| 57 base::Unretained(proxy_), |
| 58 method_call)); |
66 // http://crbug.com/125360 | 59 // http://crbug.com/125360 |
67 base::ThreadRestrictions::ScopedAllowWait allow_wait; | 60 base::ThreadRestrictions::ScopedAllowWait allow_wait; |
68 on_blocking_method_call_.Wait(); | 61 on_blocking_method_call_.Wait(); |
69 return response; | 62 return response; |
70 } | 63 } |
71 | 64 |
72 } // namespace chromeos | 65 } // namespace chromeos |
OLD | NEW |