blob: 1ad84a43c0360b649ebc869a9fa07453aab00a13 [file] [log] [blame]
Darin Petkov63138a92012-02-06 14:09:15 +01001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Chris Masoned7732e42011-05-20 11:08:56 -07002// 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 Masone8fe2c7e2011-06-09 15:51:19 -07008#include <map>
Chris Masoned7732e42011-05-20 11:08:56 -07009#include <string>
Chris Masone8fe2c7e2011-06-09 15:51:19 -070010#include <vector>
Chris Masoned7732e42011-05-20 11:08:56 -070011
Eric Shienbrood9a245532012-03-07 14:20:39 -050012#include <base/callback.h>
Ben Chancc67c522014-09-03 07:19:18 -070013#include <base/macros.h>
Eric Shienbrood9a245532012-03-07 14:20:39 -050014#include <base/memory/weak_ptr.h>
Chris Masoned7732e42011-05-20 11:08:56 -070015#include <dbus-c++/dbus.h>
Paul Stewartf3b38402015-01-16 12:59:03 -080016#include <gtest/gtest_prod.h> // for FRIEND_TEST
Chris Masoned7732e42011-05-20 11:08:56 -070017
Chris Masone889666b2011-07-03 12:58:50 -070018#include "shill/accessor_interface.h"
Darin Petkove5bc2cb2011-12-07 14:47:32 +010019#include "shill/adaptor_interfaces.h"
Eric Shienbrood9a245532012-03-07 14:20:39 -050020#include "shill/callbacks.h"
Darin Petkove5bc2cb2011-12-07 14:47:32 +010021#include "shill/error.h"
Chris Masone889666b2011-07-03 12:58:50 -070022
Chris Masoned7732e42011-05-20 11:08:56 -070023namespace shill {
24
25#define SHILL_INTERFACE "org.chromium.flimflam"
26#define SHILL_PATH "/org/chromium/flimflam"
27
mukesh agrawal7a4e4002011-09-06 11:26:05 -070028class KeyValueStore;
Chris Masoneb925cc82011-06-22 15:39:57 -070029class PropertyStore;
Chris Masone8fe2c7e2011-06-09 15:51:19 -070030
Chris Masoned7732e42011-05-20 11:08:56 -070031// Superclass for all DBus-backed Adaptor objects
mukesh agrawalf1f490c2011-09-06 15:20:22 -070032class DBusAdaptor : public DBus::ObjectAdaptor,
Eric Shienbrood9a245532012-03-07 14:20:39 -050033 public DBus::IntrospectableAdaptor,
34 public base::SupportsWeakPtr<DBusAdaptor> {
Chris Masoned7732e42011-05-20 11:08:56 -070035 public:
mukesh agrawalcbfb34e2013-04-17 19:33:25 -070036 static const char kNullPath[];
37
Paul Stewarta794cd62015-06-16 13:13:10 -070038 DBusAdaptor(DBus::Connection* conn, const std::string& object_path);
Ben Chan5ea763b2014-08-13 11:07:54 -070039 ~DBusAdaptor() override;
Chris Masoned0ceb8c2011-06-02 10:05:39 -070040
mukesh agrawalbebf1b82013-04-23 15:06:33 -070041 // 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 Stewarta794cd62015-06-16 13:13:10 -070045 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 agrawal8abd2f62012-01-30 14:56:14 -080052 // 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 Stewarta794cd62015-06-16 13:13:10 -070059 static bool ClearProperty(PropertyStore* store,
60 const std::string& name,
61 ::DBus::Error* error);
mukesh agrawal7a4e4002011-09-06 11:26:05 -070062 static void ArgsToKeyValueStore(
Paul Stewarta794cd62015-06-16 13:13:10 -070063 const std::map<std::string, ::DBus::Variant>& args,
64 KeyValueStore* out,
65 Error* error);
Chris Masoned0ceb8c2011-06-02 10:05:39 -070066
Chris Masone8fe2c7e2011-06-09 15:51:19 -070067 static ::DBus::Variant BoolToVariant(bool value);
Paul Stewarta794cd62015-06-16 13:13:10 -070068 static ::DBus::Variant ByteArraysToVariant(const ByteArrays& value);
Ben Chan7fab8972014-08-10 17:14:46 -070069 static ::DBus::Variant ByteToVariant(uint8_t value);
70 static ::DBus::Variant Int16ToVariant(int16_t value);
71 static ::DBus::Variant Int32ToVariant(int32_t value);
Paul Stewarta794cd62015-06-16 13:13:10 -070072 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 Chan7fab8972014-08-10 17:14:46 -070079 static ::DBus::Variant Uint16ToVariant(uint16_t value);
Paul Stewarta794cd62015-06-16 13:13:10 -070080 static ::DBus::Variant Uint16sToVariant(const Uint16s& value);
Ben Chan7fab8972014-08-10 17:14:46 -070081 static ::DBus::Variant Uint32ToVariant(uint32_t value);
82 static ::DBus::Variant Uint64ToVariant(uint64_t value);
Chris Masone8fe2c7e2011-06-09 15:51:19 -070083
84 static bool IsBool(::DBus::Signature signature);
85 static bool IsByte(::DBus::Signature signature);
Paul Stewartced6a0b2011-11-08 15:32:04 -080086 static bool IsByteArrays(::DBus::Signature signature);
Chris Masone8fe2c7e2011-06-09 15:51:19 -070087 static bool IsInt16(::DBus::Signature signature);
88 static bool IsInt32(::DBus::Signature signature);
Chris Masone3bd3c8c2011-06-13 08:20:26 -070089 static bool IsPath(::DBus::Signature signature);
mukesh agrawal2366eed2012-03-20 18:21:50 -070090 static bool IsPaths(::DBus::Signature signature);
Chris Masone8fe2c7e2011-06-09 15:51:19 -070091 static bool IsString(::DBus::Signature signature);
92 static bool IsStringmap(::DBus::Signature signature);
Chris Masone27c4aa52011-07-02 13:10:14 -070093 static bool IsStringmaps(::DBus::Signature signature);
Chris Masone8fe2c7e2011-06-09 15:51:19 -070094 static bool IsStrings(::DBus::Signature signature);
95 static bool IsUint16(::DBus::Signature signature);
mukesh agrawale7c7e652013-06-18 17:19:39 -070096 static bool IsUint16s(::DBus::Signature signature);
Chris Masone8fe2c7e2011-06-09 15:51:19 -070097 static bool IsUint32(::DBus::Signature signature);
Paul Stewarte18c33b2012-07-10 20:48:44 -070098 static bool IsUint64(::DBus::Signature signature);
Eric Shienbroodb23d4b92012-02-16 12:32:42 -050099 static bool IsKeyValueStore(::DBus::Signature signature);
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700100
Darin Petkove5bc2cb2011-12-07 14:47:32 +0100101 protected:
Paul Stewartf3b38402015-01-16 12:59:03 -0800102 FRIEND_TEST(DBusAdaptorTest, SanitizePathElement);
103
Paul Stewarta794cd62015-06-16 13:13:10 -0700104 ResultCallback GetMethodReplyCallback(const DBus::Tag* tag);
Christopher Wiley0d9cf0c2013-02-19 19:24:57 -0800105 // 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 Stewarta794cd62015-06-16 13:13:10 -0700108 ResultStringCallback GetStringMethodReplyCallback(const DBus::Tag* tag);
109 ResultBoolCallback GetBoolMethodReplyCallback(const DBus::Tag* tag);
Christopher Wiley0d9cf0c2013-02-19 19:24:57 -0800110
Eric Shienbrood9a245532012-03-07 14:20:39 -0500111 // 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 Stewarta794cd62015-06-16 13:13:10 -0700129 // void XXXXDBusAdaptor::SomeMethod(<args...>, DBus::Error& error) {
Eric Shienbrood9a245532012-03-07 14:20:39 -0500130 // Error e(Error::kOperationInitiated);
Paul Stewarta794cd62015-06-16 13:13:10 -0700131 // DBus::Tag* tag = new DBus::Tag();
Eric Shienbrood9a245532012-03-07 14:20:39 -0500132 // xxxx_->SomeMethod(<args...>, &e, GetMethodReplyCallback(tag));
133 // ReturnResultOrDefer(tag, e, &error);
134 // }
135 //
Paul Stewarta794cd62015-06-16 13:13:10 -0700136 void ReturnResultOrDefer(const DBus::Tag* tag,
137 const Error& error,
138 DBus::Error* dberror);
Darin Petkove5bc2cb2011-12-07 14:47:32 +0100139
Paul Stewartf3b38402015-01-16 12:59:03 -0800140 // Returns an object path fragment that conforms to D-Bus specifications.
Paul Stewarta794cd62015-06-16 13:13:10 -0700141 static std::string SanitizePathElement(const std::string& object_path);
Paul Stewartf3b38402015-01-16 12:59:03 -0800142
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700143 private:
Paul Stewarta794cd62015-06-16 13:13:10 -0700144 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 Wiley0d9cf0c2013-02-19 19:24:57 -0800148 bool returned);
149 template<typename T>
Paul Stewarta794cd62015-06-16 13:13:10 -0700150 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 Wiley0d9cf0c2013-02-19 19:24:57 -0800154 template <typename T>
Paul Stewarta794cd62015-06-16 13:13:10 -0700155 void TypedReplyNow(const DBus::Tag* tag, const T& value);
156 void ReplyNowWithError(const DBus::Tag* tag, const DBus::Error& error);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500157
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700158 DISALLOW_COPY_AND_ASSIGN(DBusAdaptor);
Chris Masoned7732e42011-05-20 11:08:56 -0700159};
160
161} // namespace shill
Ben Chanc45688b2014-07-02 23:50:45 -0700162
Chris Masoned7732e42011-05-20 11:08:56 -0700163#endif // SHILL_DBUS_ADAPTOR_H_