blob: 7477cbec5eb5fc8fae1a9ecb1865e2c8e4dc58ee [file] [log] [blame]
[email protected]216ed0b2012-02-14 21:29:061// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]a51076112011-08-17 20:58:122// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef DBUS_EXPORTED_OBJECT_H_
6#define DBUS_EXPORTED_OBJECT_H_
[email protected]a51076112011-08-17 20:58:127
[email protected]a51076112011-08-17 20:58:128#include <dbus/dbus.h>
9
[email protected]320eff42011-11-15 00:29:4810#include <map>
dcheng2a193282016-04-08 22:55:0411#include <memory>
[email protected]320eff42011-11-15 00:29:4812#include <string>
13#include <utility>
14
[email protected]a51076112011-08-17 20:58:1215#include "base/callback.h"
16#include "base/memory/ref_counted.h"
[email protected]12f97662011-08-20 01:07:1717#include "base/synchronization/waitable_event.h"
[email protected]a51076112011-08-17 20:58:1218#include "base/threading/platform_thread.h"
[email protected]b43e5562013-06-28 15:20:0219#include "base/time/time.h"
[email protected]e3024462012-11-05 01:56:1420#include "dbus/dbus_export.h"
[email protected]216ed0b2012-02-14 21:29:0621#include "dbus/object_path.h"
[email protected]a51076112011-08-17 20:58:1222
[email protected]a51076112011-08-17 20:58:1223namespace dbus {
24
25class Bus;
26class MethodCall;
27class Response;
[email protected]3beaaa4e2011-08-23 07:29:2128class Signal;
[email protected]a51076112011-08-17 20:58:1229
30// ExportedObject is used to export objects and methods to other D-Bus
31// clients.
32//
33// ExportedObject is a ref counted object, to ensure that |this| of the
34// object is alive when callbacks referencing |this| are called.
[email protected]e3024462012-11-05 01:56:1435class CHROME_DBUS_EXPORT ExportedObject
36 : public base::RefCountedThreadSafe<ExportedObject> {
[email protected]a51076112011-08-17 20:58:1237 public:
38 // Client code should use Bus::GetExportedObject() instead of this
39 // constructor.
[email protected]15e7b162012-03-10 01:12:5240 ExportedObject(Bus* bus, const ObjectPath& object_path);
[email protected]a51076112011-08-17 20:58:1241
[email protected]0c9a8a592012-11-29 01:52:2142 // Called to send a response from an exported method. |response| is the
Reilly Grantd4e66132019-11-22 17:14:5043 // response message. Callers should pass nullptr in the event of an error that
[email protected]0c9a8a592012-11-29 01:52:2144 // prevents the sending of a response.
Reilly Grantd4e66132019-11-22 17:14:5045 using ResponseSender =
46 base::OnceCallback<void(std::unique_ptr<Response> response)>;
[email protected]9aa74cc2011-11-30 04:57:4247
[email protected]0c9a8a592012-11-29 01:52:2148 // Called when an exported method is called. |method_call| is the request
49 // message. |sender| is the callback that's used to send a response.
50 //
51 // |method_call| is owned by ExportedObject, hence client code should not
52 // delete |method_call|.
Reilly Grantd4e66132019-11-22 17:14:5053 using MethodCallCallback =
54 base::RepeatingCallback<void(MethodCall* method_call,
55 ResponseSender sender)>;
[email protected]a51076112011-08-17 20:58:1256
57 // Called when method exporting is done.
[email protected]0c9a8a592012-11-29 01:52:2158 // |success| indicates whether exporting was successful or not.
Reilly Grantd4e66132019-11-22 17:14:5059 using OnExportedCallback =
60 base::OnceCallback<void(const std::string& interface_name,
61 const std::string& method_name,
62 bool success)>;
[email protected]a51076112011-08-17 20:58:1263
Sonny Sasakae4634072018-12-12 20:29:3364 // Called when method unexporting is done.
65 // |success| indicates whether unexporting was successful or not.
Reilly Grantd4e66132019-11-22 17:14:5066 using OnUnexportedCallback =
67 base::OnceCallback<void(const std::string& interface_name,
Sonny Sasakae4634072018-12-12 20:29:3368 const std::string& method_name,
Reilly Grantd4e66132019-11-22 17:14:5069 bool success)>;
Sonny Sasakae4634072018-12-12 20:29:3370
[email protected]a51076112011-08-17 20:58:1271 // Exports the method specified by |interface_name| and |method_name|,
72 // and blocks until exporting is done. Returns true on success.
73 //
74 // |method_call_callback| will be called in the origin thread, when the
75 // exported method is called. As it's called in the origin thread,
[email protected]3beaaa4e2011-08-23 07:29:2176 // |method_callback| can safely reference objects in the origin thread
77 // (i.e. UI thread in most cases).
[email protected]a51076112011-08-17 20:58:1278 //
[email protected]332577d2014-01-09 04:39:1779 // IMPORTANT NOTE: You should export all methods before requesting a
80 // service name by Bus::RequestOwnership/AndBlock(). If you do it in the
81 // wrong order (i.e. request a service name then export methods), there
82 // will be a short time period where your service is unable to respond to
83 // method calls because these methods aren't yet exposed. This race is a
84 // real problem as clients may start calling methods of your service as
85 // soon as you acquire a service name, by watching the name owner change.
86 //
[email protected]a51076112011-08-17 20:58:1287 // BLOCKING CALL.
Reilly Grantd4e66132019-11-22 17:14:5088 virtual bool ExportMethodAndBlock(
89 const std::string& interface_name,
90 const std::string& method_name,
91 const MethodCallCallback& method_call_callback);
[email protected]a51076112011-08-17 20:58:1292
Sonny Sasakae4634072018-12-12 20:29:3393 // Unexports the method specified by |interface_name| and |method_name|,
94 // and blocks until unexporting is done. Returns true on success.
95 virtual bool UnexportMethodAndBlock(const std::string& interface_name,
96 const std::string& method_name);
97
[email protected]a51076112011-08-17 20:58:1298 // Requests to export the method specified by |interface_name| and
99 // |method_name|. See Also ExportMethodAndBlock().
100 //
101 // |on_exported_callback| is called when the method is exported or
102 // failed to be exported, in the origin thread.
103 //
104 // Must be called in the origin thread.
105 virtual void ExportMethod(const std::string& interface_name,
106 const std::string& method_name,
Reilly Grantd4e66132019-11-22 17:14:50107 const MethodCallCallback& method_call_callback,
[email protected]a51076112011-08-17 20:58:12108 OnExportedCallback on_exported_callback);
109
Sonny Sasakae4634072018-12-12 20:29:33110 // Requests to unexport the method specified by |interface_name| and
111 // |method_name|. See also UnexportMethodAndBlock().
112 //
113 // |on_unexported_callback| is called when the method is unexported or
114 // failed to be unexported, in the origin thread.
115 //
116 // Must be called in the origin thread.
117 virtual void UnexportMethod(const std::string& interface_name,
118 const std::string& method_name,
119 OnUnexportedCallback on_unexported_callback);
120
[email protected]3beaaa4e2011-08-23 07:29:21121 // Requests to send the signal from this object. The signal will be sent
chirantan722c9772015-04-09 19:36:01122 // synchronously if this method is called from the message loop in the D-Bus
123 // thread and asynchronously otherwise.
[email protected]3beaaa4e2011-08-23 07:29:21124 virtual void SendSignal(Signal* signal);
125
[email protected]a51076112011-08-17 20:58:12126 // Unregisters the object from the bus. The Bus object will take care of
127 // unregistering so you don't have to do this manually.
128 //
129 // BLOCKING CALL.
130 virtual void Unregister();
131
[email protected]b8ae0512011-08-25 05:18:29132 protected:
133 // This is protected, so we can define sub classes.
134 virtual ~ExportedObject();
135
[email protected]a51076112011-08-17 20:58:12136 private:
137 friend class base::RefCountedThreadSafe<ExportedObject>;
[email protected]a51076112011-08-17 20:58:12138
139 // Helper function for ExportMethod().
140 void ExportMethodInternal(const std::string& interface_name,
141 const std::string& method_name,
Reilly Grantd4e66132019-11-22 17:14:50142 const MethodCallCallback& method_call_callback,
[email protected]a51076112011-08-17 20:58:12143 OnExportedCallback exported_callback);
144
Sonny Sasakae4634072018-12-12 20:29:33145 // Helper function for UnexportMethod().
146 void UnexportMethodInternal(const std::string& interface_name,
147 const std::string& method_name,
148 OnUnexportedCallback unexported_callback);
149
[email protected]a51076112011-08-17 20:58:12150 // Called when the object is exported.
151 void OnExported(OnExportedCallback on_exported_callback,
152 const std::string& interface_name,
153 const std::string& method_name,
154 bool success);
155
Sonny Sasakae4634072018-12-12 20:29:33156 // Called when a method is unexported.
157 void OnUnexported(OnExportedCallback on_unexported_callback,
158 const std::string& interface_name,
159 const std::string& method_name,
160 bool success);
161
[email protected]3beaaa4e2011-08-23 07:29:21162 // Helper function for SendSignal().
[email protected]a73389892011-09-06 20:53:30163 void SendSignalInternal(base::TimeTicks start_time,
[email protected]1d887b272011-10-04 13:47:21164 DBusMessage* signal_message);
[email protected]3beaaa4e2011-08-23 07:29:21165
[email protected]a51076112011-08-17 20:58:12166 // Registers this object to the bus.
167 // Returns true on success, or the object is already registered.
168 //
169 // BLOCKING CALL.
170 bool Register();
171
172 // Handles the incoming request messages and dispatches to the exported
173 // methods.
174 DBusHandlerResult HandleMessage(DBusConnection* connection,
175 DBusMessage* raw_message);
176
177 // Runs the method. Helper function for HandleMessage().
Reilly Grantd4e66132019-11-22 17:14:50178 void RunMethod(const MethodCallCallback& method_call_callback,
dcheng2a193282016-04-08 22:55:04179 std::unique_ptr<MethodCall> method_call,
[email protected]e184cce2011-10-07 16:26:30180 base::TimeTicks start_time);
181
[email protected]9aa74cc2011-11-30 04:57:42182 // Callback invoked by service provider to send a response to a method call.
183 // Can be called immediately from a MethodCallCallback to implement a
184 // synchronous service or called later to implement an asynchronous service.
185 void SendResponse(base::TimeTicks start_time,
dcheng2a193282016-04-08 22:55:04186 std::unique_ptr<MethodCall> method_call,
187 std::unique_ptr<Response> response);
[email protected]9aa74cc2011-11-30 04:57:42188
189 // Called on completion of the method run from SendResponse().
[email protected]e184cce2011-10-07 16:26:30190 // Takes ownership of |method_call| and |response|.
dcheng2a193282016-04-08 22:55:04191 void OnMethodCompleted(std::unique_ptr<MethodCall> method_call,
192 std::unique_ptr<Response> response,
[email protected]e184cce2011-10-07 16:26:30193 base::TimeTicks start_time);
[email protected]a51076112011-08-17 20:58:12194
195 // Called when the object is unregistered.
196 void OnUnregistered(DBusConnection* connection);
197
198 // Redirects the function call to HandleMessage().
199 static DBusHandlerResult HandleMessageThunk(DBusConnection* connection,
200 DBusMessage* raw_message,
201 void* user_data);
202
203 // Redirects the function call to OnUnregistered().
204 static void OnUnregisteredThunk(DBusConnection* connection,
205 void* user_data);
206
[email protected]6477a412011-10-13 00:45:26207 scoped_refptr<Bus> bus_;
[email protected]216ed0b2012-02-14 21:29:06208 ObjectPath object_path_;
[email protected]a51076112011-08-17 20:58:12209 bool object_is_registered_;
[email protected]a51076112011-08-17 20:58:12210
211 // The method table where keys are absolute method names (i.e. interface
212 // name + method name), and values are the corresponding callbacks.
213 typedef std::map<std::string, MethodCallCallback> MethodTable;
214 MethodTable method_table_;
215};
216
217} // namespace dbus
218
219#endif // DBUS_EXPORTED_OBJECT_H_