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 | |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 12 | #include <base/basictypes.h> |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 13 | #include <base/callback.h> |
| 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> |
| 16 | |
Chris Masone | 889666b | 2011-07-03 12:58:50 -0700 | [diff] [blame] | 17 | #include "shill/accessor_interface.h" |
Darin Petkov | e5bc2cb | 2011-12-07 14:47:32 +0100 | [diff] [blame] | 18 | #include "shill/adaptor_interfaces.h" |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 19 | #include "shill/callbacks.h" |
Darin Petkov | e5bc2cb | 2011-12-07 14:47:32 +0100 | [diff] [blame] | 20 | #include "shill/error.h" |
Chris Masone | 889666b | 2011-07-03 12:58:50 -0700 | [diff] [blame] | 21 | |
Chris Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 22 | namespace shill { |
| 23 | |
| 24 | #define SHILL_INTERFACE "org.chromium.flimflam" |
| 25 | #define SHILL_PATH "/org/chromium/flimflam" |
| 26 | |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 27 | class KeyValueStore; |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 28 | class PropertyStore; |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 29 | |
Chris Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 30 | // Superclass for all DBus-backed Adaptor objects |
mukesh agrawal | f1f490c | 2011-09-06 15:20:22 -0700 | [diff] [blame] | 31 | class DBusAdaptor : public DBus::ObjectAdaptor, |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 32 | public DBus::IntrospectableAdaptor, |
| 33 | public base::SupportsWeakPtr<DBusAdaptor> { |
Chris Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 34 | public: |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 35 | DBusAdaptor(DBus::Connection* conn, const std::string &object_path); |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 36 | virtual ~DBusAdaptor(); |
| 37 | |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 38 | static bool SetProperty(PropertyStore *store, |
| 39 | const std::string &name, |
| 40 | const ::DBus::Variant &value, |
| 41 | ::DBus::Error *error); |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 42 | static bool GetProperties(const PropertyStore &store, |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 43 | std::map<std::string, ::DBus::Variant> *out, |
| 44 | ::DBus::Error *error); |
mukesh agrawal | 8abd2f6 | 2012-01-30 14:56:14 -0800 | [diff] [blame] | 45 | // Look for a property with |name| in |store|. If found, reset the |
| 46 | // property to its "factory" value. If the property can not be |
| 47 | // found, or if it can not be cleared (e.g., because it is |
| 48 | // read-only), set |error| accordingly. |
| 49 | // |
| 50 | // Returns true if the property was found and cleared; returns false |
| 51 | // otherwise. |
| 52 | static bool ClearProperty(PropertyStore *store, |
| 53 | const std::string &name, |
| 54 | ::DBus::Error *error); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 55 | static void ArgsToKeyValueStore( |
| 56 | const std::map<std::string, ::DBus::Variant> &args, |
| 57 | KeyValueStore *out, |
| 58 | Error *error); |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 59 | |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 60 | static ::DBus::Variant BoolToVariant(bool value); |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 61 | static ::DBus::Variant ByteArraysToVariant(const ByteArrays &value); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 62 | static ::DBus::Variant ByteToVariant(uint8 value); |
| 63 | static ::DBus::Variant Int16ToVariant(int16 value); |
| 64 | static ::DBus::Variant Int32ToVariant(int32 value); |
Darin Petkov | 63138a9 | 2012-02-06 14:09:15 +0100 | [diff] [blame] | 65 | static ::DBus::Variant KeyValueStoreToVariant(const KeyValueStore &value); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 66 | static ::DBus::Variant PathToVariant(const ::DBus::Path &value); |
mukesh agrawal | 2366eed | 2012-03-20 18:21:50 -0700 | [diff] [blame] | 67 | static ::DBus::Variant PathsToVariant( |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 68 | const std::vector< ::DBus::Path> &value); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 69 | static ::DBus::Variant StringToVariant(const std::string &value); |
Chris Masone | 889666b | 2011-07-03 12:58:50 -0700 | [diff] [blame] | 70 | static ::DBus::Variant StringmapToVariant(const Stringmap &value); |
| 71 | static ::DBus::Variant StringmapsToVariant(const Stringmaps &value); |
| 72 | static ::DBus::Variant StringsToVariant(const Strings &value); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 73 | static ::DBus::Variant Uint16ToVariant(uint16 value); |
| 74 | static ::DBus::Variant Uint32ToVariant(uint32 value); |
Paul Stewart | e18c33b | 2012-07-10 20:48:44 -0700 | [diff] [blame] | 75 | static ::DBus::Variant Uint64ToVariant(uint64 value); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 76 | |
| 77 | static bool IsBool(::DBus::Signature signature); |
| 78 | static bool IsByte(::DBus::Signature signature); |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 79 | static bool IsByteArrays(::DBus::Signature signature); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 80 | static bool IsInt16(::DBus::Signature signature); |
| 81 | static bool IsInt32(::DBus::Signature signature); |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 82 | static bool IsPath(::DBus::Signature signature); |
mukesh agrawal | 2366eed | 2012-03-20 18:21:50 -0700 | [diff] [blame] | 83 | static bool IsPaths(::DBus::Signature signature); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 84 | static bool IsString(::DBus::Signature signature); |
| 85 | static bool IsStringmap(::DBus::Signature signature); |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 86 | static bool IsStringmaps(::DBus::Signature signature); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 87 | static bool IsStrings(::DBus::Signature signature); |
| 88 | static bool IsUint16(::DBus::Signature signature); |
| 89 | static bool IsUint32(::DBus::Signature signature); |
Paul Stewart | e18c33b | 2012-07-10 20:48:44 -0700 | [diff] [blame] | 90 | static bool IsUint64(::DBus::Signature signature); |
Eric Shienbrood | b23d4b9 | 2012-02-16 12:32:42 -0500 | [diff] [blame] | 91 | static bool IsKeyValueStore(::DBus::Signature signature); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 92 | |
Darin Petkov | e5bc2cb | 2011-12-07 14:47:32 +0100 | [diff] [blame] | 93 | protected: |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 94 | ResultCallback GetMethodReplyCallback(const DBus::Tag *tag); |
| 95 | // Adaptors call this method just before returning. If |error| |
| 96 | // indicates that the operation has completed, with no asynchronously |
| 97 | // delivered result expected, then a DBus method reply is immediately |
| 98 | // sent to the client that initiated the method invocation. Otherwise, |
| 99 | // the operation is ongoing, and the result will be sent to the client |
| 100 | // when the operation completes at some later time. |
| 101 | // |
| 102 | // Adaptors should always construct an Error initialized to the value |
| 103 | // Error::kOperationInitiated. A pointer to this Error is passed down |
| 104 | // through the call stack. Any layer that determines that the operation |
| 105 | // has completed, either because of a failure that prevents carrying it |
| 106 | // out, or because it was possible to complete it without sending a request |
| 107 | // to an external server, should call error.Reset() to indicate success, |
| 108 | // or to some error type to reflect the kind of failure that occurred. |
| 109 | // Otherwise, they should leave the Error alone. |
| 110 | // |
| 111 | // The general structure of an adaptor method is |
| 112 | // |
| 113 | // void XXXXDBusAdaptor::SomeMethod(<args...>, DBus::Error &error) { |
| 114 | // Error e(Error::kOperationInitiated); |
| 115 | // DBus::Tag *tag = new DBus::Tag(); |
| 116 | // xxxx_->SomeMethod(<args...>, &e, GetMethodReplyCallback(tag)); |
| 117 | // ReturnResultOrDefer(tag, e, &error); |
| 118 | // } |
| 119 | // |
| 120 | void ReturnResultOrDefer(const DBus::Tag *tag, |
| 121 | const Error &error, |
| 122 | DBus::Error *dberror); |
Darin Petkov | e5bc2cb | 2011-12-07 14:47:32 +0100 | [diff] [blame] | 123 | |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 124 | private: |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 125 | static const char kByteArraysSig[]; |
mukesh agrawal | 2366eed | 2012-03-20 18:21:50 -0700 | [diff] [blame] | 126 | static const char kPathsSig[]; |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 127 | static const char kStringmapSig[]; |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 128 | static const char kStringmapsSig[]; |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 129 | static const char kStringsSig[]; |
Darin Petkov | e5bc2cb | 2011-12-07 14:47:32 +0100 | [diff] [blame] | 130 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 131 | void MethodReplyCallback(const DBus::Tag *tag, const Error &error); |
| 132 | void DeferReply(const DBus::Tag *tag); |
| 133 | void ReplyNow(const DBus::Tag *tag); |
| 134 | void ReplyNowWithError(const DBus::Tag *tag, const DBus::Error &error); |
| 135 | |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 136 | DISALLOW_COPY_AND_ASSIGN(DBusAdaptor); |
Chris Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 137 | }; |
| 138 | |
| 139 | } // namespace shill |
| 140 | #endif // SHILL_DBUS_ADAPTOR_H_ |