1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef DBUS_EXPORTED_OBJECT_H_
6 #define DBUS_EXPORTED_OBJECT_H_
15 #include "base/functional/callback.h"
16 #include "base/memory/ref_counted.h"
17 #include "base/synchronization/waitable_event.h"
18 #include "base/threading/platform_thread.h"
19 #include "dbus/dbus_export.h"
20 #include "dbus/object_path.h"
29 // ExportedObject is used to export objects and methods to other D-Bus
32 // ExportedObject is a ref counted object, to ensure that |this| of the
33 // object is alive when callbacks referencing |this| are called.
34 class CHROME_DBUS_EXPORT ExportedObject
35 : public base::RefCountedThreadSafe<ExportedObject> {
37 // Client code should use Bus::GetExportedObject() instead of this
39 ExportedObject(Bus* bus, const ObjectPath& object_path);
41 // Called to send a response from an exported method. |response| is the
42 // response message. Callers should pass nullptr in the event of an error that
43 // prevents the sending of a response.
44 using ResponseSender =
45 base::OnceCallback<void(std::unique_ptr<Response> response)>;
47 // Called when an exported method is called. |method_call| is the request
48 // message. |sender| is the callback that's used to send a response.
50 // |method_call| is owned by ExportedObject, hence client code should not
51 // delete |method_call|.
52 using MethodCallCallback =
53 base::RepeatingCallback<void(MethodCall* method_call,
54 ResponseSender sender)>;
56 // Called when method exporting is done.
57 // |success| indicates whether exporting was successful or not.
58 using OnExportedCallback =
59 base::OnceCallback<void(const std::string& interface_name,
60 const std::string& method_name,
63 // Called when method unexporting is done.
64 // |success| indicates whether unexporting was successful or not.
65 using OnUnexportedCallback =
66 base::OnceCallback<void(const std::string& interface_name,
67 const std::string& method_name,
70 // Exports the method specified by |interface_name| and |method_name|,
71 // and blocks until exporting is done. Returns true on success.
73 // |method_call_callback| will be called in the origin thread, when the
74 // exported method is called. As it's called in the origin thread,
75 // |method_callback| can safely reference objects in the origin thread
76 // (i.e. UI thread in most cases).
78 // IMPORTANT NOTE: You should export all methods before requesting a
79 // service name by Bus::RequestOwnership/AndBlock(). If you do it in the
80 // wrong order (i.e. request a service name then export methods), there
81 // will be a short time period where your service is unable to respond to
82 // method calls because these methods aren't yet exposed. This race is a
83 // real problem as clients may start calling methods of your service as
84 // soon as you acquire a service name, by watching the name owner change.
87 virtual bool ExportMethodAndBlock(
88 const std::string& interface_name,
89 const std::string& method_name,
90 const MethodCallCallback& method_call_callback);
92 // Unexports the method specified by |interface_name| and |method_name|,
93 // and blocks until unexporting is done. Returns true on success.
94 virtual bool UnexportMethodAndBlock(const std::string& interface_name,
95 const std::string& method_name);
97 // Requests to export the method specified by |interface_name| and
98 // |method_name|. See Also ExportMethodAndBlock().
100 // |on_exported_callback| is called when the method is exported or
101 // failed to be exported, in the origin thread.
103 // Must be called in the origin thread.
104 virtual void ExportMethod(const std::string& interface_name,
105 const std::string& method_name,
106 const MethodCallCallback& method_call_callback,
107 OnExportedCallback on_exported_callback);
109 // Requests to unexport the method specified by |interface_name| and
110 // |method_name|. See also UnexportMethodAndBlock().
112 // |on_unexported_callback| is called when the method is unexported or
113 // failed to be unexported, in the origin thread.
115 // Must be called in the origin thread.
116 virtual void UnexportMethod(const std::string& interface_name,
117 const std::string& method_name,
118 OnUnexportedCallback on_unexported_callback);
120 // Requests to send the signal from this object. The signal will be sent
121 // synchronously if this method is called from the message loop in the D-Bus
122 // thread and asynchronously otherwise.
123 virtual void SendSignal(Signal* signal);
125 // Unregisters the object from the bus. The Bus object will take care of
126 // unregistering so you don't have to do this manually.
129 virtual void Unregister();
132 // This is protected, so we can define sub classes.
133 virtual ~ExportedObject();
136 friend class base::RefCountedThreadSafe<ExportedObject>;
138 // Helper function for ExportMethod().
139 void ExportMethodInternal(const std::string& interface_name,
140 const std::string& method_name,
141 const MethodCallCallback& method_call_callback,
142 OnExportedCallback exported_callback);
144 // Helper function for UnexportMethod().
145 void UnexportMethodInternal(const std::string& interface_name,
146 const std::string& method_name,
147 OnUnexportedCallback unexported_callback);
149 // Called when the object is exported.
150 void OnExported(OnExportedCallback on_exported_callback,
151 const std::string& interface_name,
152 const std::string& method_name,
155 // Called when a method is unexported.
156 void OnUnexported(OnExportedCallback on_unexported_callback,
157 const std::string& interface_name,
158 const std::string& method_name,
161 // Helper function for SendSignal().
162 void SendSignalInternal(DBusMessage* signal_message);
164 // Registers this object to the bus.
165 // Returns true on success, or the object is already registered.
170 // Handles the incoming request messages and dispatches to the exported
172 DBusHandlerResult HandleMessage(DBusConnection* connection,
173 DBusMessage* raw_message);
175 // Runs the method. Helper function for HandleMessage().
176 void RunMethod(const MethodCallCallback& method_call_callback,
177 std::unique_ptr<MethodCall> method_call);
179 // Callback invoked by service provider to send a response to a method call.
180 // Can be called immediately from a MethodCallCallback to implement a
181 // synchronous service or called later to implement an asynchronous service.
182 void SendResponse(std::unique_ptr<MethodCall> method_call,
183 std::unique_ptr<Response> response);
185 // Called on completion of the method run from SendResponse().
186 // Takes ownership of |method_call| and |response|.
187 void OnMethodCompleted(std::unique_ptr<MethodCall> method_call,
188 std::unique_ptr<Response> response);
190 // Called when the object is unregistered.
191 void OnUnregistered(DBusConnection* connection);
193 // Redirects the function call to HandleMessage().
194 static DBusHandlerResult HandleMessageThunk(DBusConnection* connection,
195 DBusMessage* raw_message,
198 // Redirects the function call to OnUnregistered().
199 static void OnUnregisteredThunk(DBusConnection* connection,
202 scoped_refptr<Bus> bus_;
203 ObjectPath object_path_;
204 bool object_is_registered_;
206 // The method table where keys are absolute method names (i.e. interface
207 // name + method name), and values are the corresponding callbacks.
208 typedef std::map<std::string, MethodCallCallback> MethodTable;
209 MethodTable method_table_;
214 #endif // DBUS_EXPORTED_OBJECT_H_