Darin Petkov | 63138a9 | 2012-02-06 14:09:15 +0100 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Chris Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 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_ADAPTOR_H_ |
| 6 | #define SHILL_DBUS_ADAPTOR_H_ |
| 7 | |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 8 | #include <map> |
Chris Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 9 | #include <string> |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 10 | #include <vector> |
Chris Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 11 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 12 | #include <base/callback.h> |
Ben Chan | cc67c52 | 2014-09-03 07:19:18 -0700 | [diff] [blame] | 13 | #include <base/macros.h> |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 14 | #include <base/memory/weak_ptr.h> |
Chris Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 15 | #include <dbus-c++/dbus.h> |
Paul Stewart | f3b3840 | 2015-01-16 12:59:03 -0800 | [diff] [blame] | 16 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
Chris Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 17 | |
Chris Masone | 889666b | 2011-07-03 12:58:50 -0700 | [diff] [blame] | 18 | #include "shill/accessor_interface.h" |
Darin Petkov | e5bc2cb | 2011-12-07 14:47:32 +0100 | [diff] [blame] | 19 | #include "shill/adaptor_interfaces.h" |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 20 | #include "shill/callbacks.h" |
Darin Petkov | e5bc2cb | 2011-12-07 14:47:32 +0100 | [diff] [blame] | 21 | #include "shill/error.h" |
Chris Masone | 889666b | 2011-07-03 12:58:50 -0700 | [diff] [blame] | 22 | |
Chris Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 23 | namespace shill { |
| 24 | |
| 25 | #define SHILL_INTERFACE "org.chromium.flimflam" |
| 26 | #define SHILL_PATH "/org/chromium/flimflam" |
| 27 | |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 28 | class KeyValueStore; |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 29 | class PropertyStore; |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 30 | |
Chris Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 31 | // Superclass for all DBus-backed Adaptor objects |
mukesh agrawal | f1f490c | 2011-09-06 15:20:22 -0700 | [diff] [blame] | 32 | class DBusAdaptor : public DBus::ObjectAdaptor, |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 33 | public DBus::IntrospectableAdaptor, |
| 34 | public base::SupportsWeakPtr<DBusAdaptor> { |
Chris Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 35 | public: |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 36 | static const char kNullPath[]; |
| 37 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 38 | DBusAdaptor(DBus::Connection* conn, const std::string& object_path); |
Ben Chan | 5ea763b | 2014-08-13 11:07:54 -0700 | [diff] [blame] | 39 | ~DBusAdaptor() override; |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 40 | |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 41 | // Set the property with |name| through |store|. Returns true if and |
| 42 | // only if the property was changed. Updates |error| if a) an error |
| 43 | // was encountered, and b) |error| is non-NULL. Otherwise, |error| is |
| 44 | // unchanged. |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 45 | static bool SetProperty(PropertyStore* store, |
| 46 | const std::string& name, |
| 47 | const ::DBus::Variant& value, |
| 48 | ::DBus::Error* error); |
| 49 | static bool GetProperties(const PropertyStore& store, |
| 50 | std::map<std::string, ::DBus::Variant>* out, |
| 51 | ::DBus::Error* error); |
mukesh agrawal | 8abd2f6 | 2012-01-30 14:56:14 -0800 | [diff] [blame] | 52 | // Look for a property with |name| in |store|. If found, reset the |
| 53 | // property to its "factory" value. If the property can not be |
| 54 | // found, or if it can not be cleared (e.g., because it is |
| 55 | // read-only), set |error| accordingly. |
| 56 | // |
| 57 | // Returns true if the property was found and cleared; returns false |
| 58 | // otherwise. |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 59 | static bool ClearProperty(PropertyStore* store, |
| 60 | const std::string& name, |
| 61 | ::DBus::Error* error); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 62 | static void ArgsToKeyValueStore( |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 63 | const std::map<std::string, ::DBus::Variant>& args, |
| 64 | KeyValueStore* out, |
| 65 | Error* error); |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 66 | |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 67 | static ::DBus::Variant BoolToVariant(bool value); |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 68 | static ::DBus::Variant ByteArraysToVariant(const ByteArrays& value); |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 69 | static ::DBus::Variant ByteToVariant(uint8_t value); |
| 70 | static ::DBus::Variant Int16ToVariant(int16_t value); |
| 71 | static ::DBus::Variant Int32ToVariant(int32_t value); |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 72 | static ::DBus::Variant KeyValueStoreToVariant(const KeyValueStore& value); |
| 73 | static ::DBus::Variant PathToVariant(const ::DBus::Path& value); |
| 74 | static ::DBus::Variant PathsToVariant(const std::vector<::DBus::Path>& value); |
| 75 | static ::DBus::Variant StringToVariant(const std::string& value); |
| 76 | static ::DBus::Variant StringmapToVariant(const Stringmap& value); |
| 77 | static ::DBus::Variant StringmapsToVariant(const Stringmaps& value); |
| 78 | static ::DBus::Variant StringsToVariant(const Strings& value); |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 79 | static ::DBus::Variant Uint16ToVariant(uint16_t value); |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 80 | static ::DBus::Variant Uint16sToVariant(const Uint16s& value); |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 81 | static ::DBus::Variant Uint32ToVariant(uint32_t value); |
| 82 | static ::DBus::Variant Uint64ToVariant(uint64_t value); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 83 | |
| 84 | static bool IsBool(::DBus::Signature signature); |
| 85 | static bool IsByte(::DBus::Signature signature); |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 86 | static bool IsByteArrays(::DBus::Signature signature); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 87 | static bool IsInt16(::DBus::Signature signature); |
| 88 | static bool IsInt32(::DBus::Signature signature); |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 89 | static bool IsPath(::DBus::Signature signature); |
mukesh agrawal | 2366eed | 2012-03-20 18:21:50 -0700 | [diff] [blame] | 90 | static bool IsPaths(::DBus::Signature signature); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 91 | static bool IsString(::DBus::Signature signature); |
| 92 | static bool IsStringmap(::DBus::Signature signature); |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 93 | static bool IsStringmaps(::DBus::Signature signature); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 94 | static bool IsStrings(::DBus::Signature signature); |
| 95 | static bool IsUint16(::DBus::Signature signature); |
mukesh agrawal | e7c7e65 | 2013-06-18 17:19:39 -0700 | [diff] [blame] | 96 | static bool IsUint16s(::DBus::Signature signature); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 97 | static bool IsUint32(::DBus::Signature signature); |
Paul Stewart | e18c33b | 2012-07-10 20:48:44 -0700 | [diff] [blame] | 98 | static bool IsUint64(::DBus::Signature signature); |
Eric Shienbrood | b23d4b9 | 2012-02-16 12:32:42 -0500 | [diff] [blame] | 99 | static bool IsKeyValueStore(::DBus::Signature signature); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 100 | |
Darin Petkov | e5bc2cb | 2011-12-07 14:47:32 +0100 | [diff] [blame] | 101 | protected: |
Paul Stewart | f3b3840 | 2015-01-16 12:59:03 -0800 | [diff] [blame] | 102 | FRIEND_TEST(DBusAdaptorTest, SanitizePathElement); |
| 103 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 104 | ResultCallback GetMethodReplyCallback(const DBus::Tag* tag); |
Christopher Wiley | 0d9cf0c | 2013-02-19 19:24:57 -0800 | [diff] [blame] | 105 | // It would be nice if these two methods could be templated. Unfortunately, |
| 106 | // attempts to do so will trigger some fairly esoteric warnings from the |
| 107 | // base library. |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 108 | ResultStringCallback GetStringMethodReplyCallback(const DBus::Tag* tag); |
| 109 | ResultBoolCallback GetBoolMethodReplyCallback(const DBus::Tag* tag); |
Christopher Wiley | 0d9cf0c | 2013-02-19 19:24:57 -0800 | [diff] [blame] | 110 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 111 | // Adaptors call this method just before returning. If |error| |
| 112 | // indicates that the operation has completed, with no asynchronously |
| 113 | // delivered result expected, then a DBus method reply is immediately |
| 114 | // sent to the client that initiated the method invocation. Otherwise, |
| 115 | // the operation is ongoing, and the result will be sent to the client |
| 116 | // when the operation completes at some later time. |
| 117 | // |
| 118 | // Adaptors should always construct an Error initialized to the value |
| 119 | // Error::kOperationInitiated. A pointer to this Error is passed down |
| 120 | // through the call stack. Any layer that determines that the operation |
| 121 | // has completed, either because of a failure that prevents carrying it |
| 122 | // out, or because it was possible to complete it without sending a request |
| 123 | // to an external server, should call error.Reset() to indicate success, |
| 124 | // or to some error type to reflect the kind of failure that occurred. |
| 125 | // Otherwise, they should leave the Error alone. |
| 126 | // |
| 127 | // The general structure of an adaptor method is |
| 128 | // |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 129 | // void XXXXDBusAdaptor::SomeMethod(<args...>, DBus::Error& error) { |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 130 | // Error e(Error::kOperationInitiated); |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 131 | // DBus::Tag* tag = new DBus::Tag(); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 132 | // xxxx_->SomeMethod(<args...>, &e, GetMethodReplyCallback(tag)); |
| 133 | // ReturnResultOrDefer(tag, e, &error); |
| 134 | // } |
| 135 | // |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 136 | void ReturnResultOrDefer(const DBus::Tag* tag, |
| 137 | const Error& error, |
| 138 | DBus::Error* dberror); |
Darin Petkov | e5bc2cb | 2011-12-07 14:47:32 +0100 | [diff] [blame] | 139 | |
Paul Stewart | f3b3840 | 2015-01-16 12:59:03 -0800 | [diff] [blame] | 140 | // Returns an object path fragment that conforms to D-Bus specifications. |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 141 | static std::string SanitizePathElement(const std::string& object_path); |
Paul Stewart | f3b3840 | 2015-01-16 12:59:03 -0800 | [diff] [blame] | 142 | |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 143 | private: |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 144 | void MethodReplyCallback(const DBus::Tag* tag, const Error& error); |
| 145 | void StringMethodReplyCallback(const DBus::Tag* tag, const Error& error, |
| 146 | const std::string& returned); |
| 147 | void BoolMethodReplyCallback(const DBus::Tag* tag, const Error& error, |
Christopher Wiley | 0d9cf0c | 2013-02-19 19:24:57 -0800 | [diff] [blame] | 148 | bool returned); |
| 149 | template<typename T> |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 150 | void TypedMethodReplyCallback(const DBus::Tag* tag, const Error& error, |
| 151 | const T& returned); |
| 152 | void DeferReply(const DBus::Tag* tag); |
| 153 | void ReplyNow(const DBus::Tag* tag); |
Christopher Wiley | 0d9cf0c | 2013-02-19 19:24:57 -0800 | [diff] [blame] | 154 | template <typename T> |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 155 | void TypedReplyNow(const DBus::Tag* tag, const T& value); |
| 156 | void ReplyNowWithError(const DBus::Tag* tag, const DBus::Error& error); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 157 | |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 158 | DISALLOW_COPY_AND_ASSIGN(DBusAdaptor); |
Chris Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 159 | }; |
| 160 | |
| 161 | } // namespace shill |
Ben Chan | c45688b | 2014-07-02 23:50:45 -0700 | [diff] [blame] | 162 | |
Chris Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 163 | #endif // SHILL_DBUS_ADAPTOR_H_ |