mukesh agrawal | 0e9e9d1 | 2014-04-18 16:09:58 -0700 | [diff] [blame] | 1 | // Copyright (c) 2014 The Chromium OS 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 | #ifndef SHILL_DBUS_ASYNC_CALL_HELPER_H_ |
| 6 | #define SHILL_DBUS_ASYNC_CALL_HELPER_H_ |
| 7 | |
| 8 | #include "shill/logging.h" |
| 9 | |
Ben Chan | d1fce55 | 2014-10-17 01:12:52 -0700 | [diff] [blame] | 10 | #include <memory> |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 11 | #include <string> |
Ben Chan | d1fce55 | 2014-10-17 01:12:52 -0700 | [diff] [blame] | 12 | |
Ben Chan | cc67c52 | 2014-09-03 07:19:18 -0700 | [diff] [blame] | 13 | #include <base/macros.h> // for ignore_result |
mukesh agrawal | 0e9e9d1 | 2014-04-18 16:09:58 -0700 | [diff] [blame] | 14 | #include <dbus-c++/error.h> |
| 15 | |
| 16 | #include "shill/error.h" |
| 17 | |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 18 | using std::string; |
| 19 | |
mukesh agrawal | 0e9e9d1 | 2014-04-18 16:09:58 -0700 | [diff] [blame] | 20 | namespace shill { |
| 21 | |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 22 | namespace Logging { |
| 23 | static auto kModuleLogScope = ScopeLogger::kDBus; |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 24 | static string ObjectID(const DBus::Path* p) { return *p;} |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 25 | } |
| 26 | |
mukesh agrawal | 0e9e9d1 | 2014-04-18 16:09:58 -0700 | [diff] [blame] | 27 | // The dbus-c++ async call mechanism has a funny way of handling the |
| 28 | // callback parameter. In particular, the caller is responsible for |
| 29 | // cleaning up the callback. (This is unlike the low-level D-Bus |
| 30 | // library, which accepts a function pointer for the cleanup |
| 31 | // function.) |
| 32 | // |
| 33 | // In cases where the call completes asynchronously, we delete the |
| 34 | // callback in the return-handling code. However, if the call |
| 35 | // generates a synchronous error, we need to delete the callback |
| 36 | // immediately. |
| 37 | // |
| 38 | // This template simply factors out that pattern, so that we don't |
| 39 | // need to repeat the code in every async stub. |
| 40 | |
| 41 | template< |
| 42 | typename TraceMsgT, typename ProxyT, typename CallT, |
| 43 | typename CallbackT, typename... ArgTypes> |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 44 | void BeginAsyncDBusCall(const TraceMsgT& trace_msg, |
| 45 | ProxyT& proxy, // NOLINT(runtime/references) |
| 46 | const CallT& call, const CallbackT& callback, |
| 47 | Error* error, |
| 48 | void(*error_converter)(const DBus::Error&, Error*), |
mukesh agrawal | 0e9e9d1 | 2014-04-18 16:09:58 -0700 | [diff] [blame] | 49 | int timeout, ArgTypes... call_args) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 50 | SLOG(&proxy.path(), 2) << trace_msg << " [timeout=" << timeout << "]"; |
Ben Chan | d1fce55 | 2014-10-17 01:12:52 -0700 | [diff] [blame] | 51 | std::unique_ptr<CallbackT> cb(new CallbackT(callback)); |
mukesh agrawal | 0e9e9d1 | 2014-04-18 16:09:58 -0700 | [diff] [blame] | 52 | try { |
| 53 | (proxy.*call)(call_args..., cb.get(), timeout); |
mukesh agrawal | 62ba51c | 2014-04-18 16:09:58 -0700 | [diff] [blame] | 54 | // We've successfully passed ownership of |cb| to |proxy|, so we |
| 55 | // must release() ownership. (Otherwise, |cb| will be deleted on |
| 56 | // return from this function.) |
| 57 | // |
| 58 | // Since we have no further need to reference the CallbackT in |
| 59 | // this scope, we ignore the return value of release(). Ignoring |
| 60 | // the return value is normally an error, because that means |
| 61 | // you're leaking the object. However, it's fine here, because |
| 62 | // |proxy| now owns |cb|. |
| 63 | ignore_result(cb.release()); |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 64 | } catch (const DBus::Error& e) { |
mukesh agrawal | 0e9e9d1 | 2014-04-18 16:09:58 -0700 | [diff] [blame] | 65 | if (error) |
| 66 | error_converter(e, error); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | } // namespace shill |
| 71 | |
| 72 | #endif // SHILL_DBUS_ASYNC_CALL_HELPER_H_ |