blob: 69a63a5e075e5a6094e024eeef53803a0749b122 [file] [log] [blame]
keybuk@google.combf4649a2012-02-15 06:29:06 +09001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
satorux@chromium.org163f1cb2011-08-18 05:58:12 +09002// 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_
satorux@chromium.org163f1cb2011-08-18 05:58:12 +09007
satorux@chromium.org163f1cb2011-08-18 05:58:12 +09008#include <dbus/dbus.h>
9
thestig@chromium.org519c9ac2011-11-15 09:29:48 +090010#include <map>
dcheng30c5a172016-04-09 07:55:04 +090011#include <memory>
thestig@chromium.org519c9ac2011-11-15 09:29:48 +090012#include <string>
13#include <utility>
14
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090015#include "base/callback.h"
16#include "base/memory/ref_counted.h"
satorux@chromium.org01fbb892011-08-20 10:07:17 +090017#include "base/synchronization/waitable_event.h"
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090018#include "base/threading/platform_thread.h"
avi@chromium.org78a7e7b2013-06-29 00:20:02 +090019#include "base/time/time.h"
tfarina@chromium.org7928ea22012-11-05 10:56:14 +090020#include "dbus/dbus_export.h"
keybuk@google.combf4649a2012-02-15 06:29:06 +090021#include "dbus/object_path.h"
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090022
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090023namespace dbus {
24
25class Bus;
26class MethodCall;
27class Response;
satorux@chromium.org7f0c4512011-08-23 16:29:21 +090028class Signal;
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090029
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.
tfarina@chromium.org7928ea22012-11-05 10:56:14 +090035class CHROME_DBUS_EXPORT ExportedObject
36 : public base::RefCountedThreadSafe<ExportedObject> {
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090037 public:
38 // Client code should use Bus::GetExportedObject() instead of this
39 // constructor.
keybuk@chromium.org9cb73f02012-03-10 10:12:52 +090040 ExportedObject(Bus* bus, const ObjectPath& object_path);
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090041
satorux@chromium.orgf7fc5652012-11-29 10:52:21 +090042 // Called to send a response from an exported method. |response| is the
43 // response message. Callers should pass NULL in the event of an error that
44 // prevents the sending of a response.
dcheng30c5a172016-04-09 07:55:04 +090045 typedef base::Callback<void(std::unique_ptr<Response> response)>
46 ResponseSender;
vlaviano@chromium.org241215d2011-11-30 13:57:42 +090047
satorux@chromium.orgf7fc5652012-11-29 10:52:21 +090048 // 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|.
53 typedef base::Callback<void (MethodCall* method_call, ResponseSender sender)>
54 MethodCallCallback;
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090055
56 // Called when method exporting is done.
satorux@chromium.orgf7fc5652012-11-29 10:52:21 +090057 // |success| indicates whether exporting was successful or not.
58 typedef base::Callback<void (const std::string& interface_name,
59 const std::string& method_name,
60 bool success)>
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090061 OnExportedCallback;
62
63 // Exports the method specified by |interface_name| and |method_name|,
64 // and blocks until exporting is done. Returns true on success.
65 //
66 // |method_call_callback| will be called in the origin thread, when the
67 // exported method is called. As it's called in the origin thread,
satorux@chromium.org7f0c4512011-08-23 16:29:21 +090068 // |method_callback| can safely reference objects in the origin thread
69 // (i.e. UI thread in most cases).
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090070 //
satorux@chromium.org49e0bc72014-01-09 13:39:17 +090071 // IMPORTANT NOTE: You should export all methods before requesting a
72 // service name by Bus::RequestOwnership/AndBlock(). If you do it in the
73 // wrong order (i.e. request a service name then export methods), there
74 // will be a short time period where your service is unable to respond to
75 // method calls because these methods aren't yet exposed. This race is a
76 // real problem as clients may start calling methods of your service as
77 // soon as you acquire a service name, by watching the name owner change.
78 //
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090079 // BLOCKING CALL.
80 virtual bool ExportMethodAndBlock(const std::string& interface_name,
81 const std::string& method_name,
82 MethodCallCallback method_call_callback);
83
84 // Requests to export the method specified by |interface_name| and
85 // |method_name|. See Also ExportMethodAndBlock().
86 //
87 // |on_exported_callback| is called when the method is exported or
88 // failed to be exported, in the origin thread.
89 //
90 // Must be called in the origin thread.
91 virtual void ExportMethod(const std::string& interface_name,
92 const std::string& method_name,
93 MethodCallCallback method_call_callback,
94 OnExportedCallback on_exported_callback);
95
satorux@chromium.org7f0c4512011-08-23 16:29:21 +090096 // Requests to send the signal from this object. The signal will be sent
chirantand796bf72015-04-10 04:36:01 +090097 // synchronously if this method is called from the message loop in the D-Bus
98 // thread and asynchronously otherwise.
satorux@chromium.org7f0c4512011-08-23 16:29:21 +090099 virtual void SendSignal(Signal* signal);
100
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900101 // Unregisters the object from the bus. The Bus object will take care of
102 // unregistering so you don't have to do this manually.
103 //
104 // BLOCKING CALL.
105 virtual void Unregister();
106
satorux@chromium.orgf77861f2011-08-25 14:18:29 +0900107 protected:
108 // This is protected, so we can define sub classes.
109 virtual ~ExportedObject();
110
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900111 private:
112 friend class base::RefCountedThreadSafe<ExportedObject>;
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900113
114 // Helper function for ExportMethod().
115 void ExportMethodInternal(const std::string& interface_name,
116 const std::string& method_name,
117 MethodCallCallback method_call_callback,
118 OnExportedCallback exported_callback);
119
120 // Called when the object is exported.
121 void OnExported(OnExportedCallback on_exported_callback,
122 const std::string& interface_name,
123 const std::string& method_name,
124 bool success);
125
satorux@chromium.org7f0c4512011-08-23 16:29:21 +0900126 // Helper function for SendSignal().
satorux@chromium.org5a92cf32011-09-07 05:53:30 +0900127 void SendSignalInternal(base::TimeTicks start_time,
satorux@chromium.org47d706b2011-10-04 22:47:21 +0900128 DBusMessage* signal_message);
satorux@chromium.org7f0c4512011-08-23 16:29:21 +0900129
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900130 // Registers this object to the bus.
131 // Returns true on success, or the object is already registered.
132 //
133 // BLOCKING CALL.
134 bool Register();
135
136 // Handles the incoming request messages and dispatches to the exported
137 // methods.
138 DBusHandlerResult HandleMessage(DBusConnection* connection,
139 DBusMessage* raw_message);
140
141 // Runs the method. Helper function for HandleMessage().
142 void RunMethod(MethodCallCallback method_call_callback,
dcheng30c5a172016-04-09 07:55:04 +0900143 std::unique_ptr<MethodCall> method_call,
satorux@chromium.orgc9ebea22011-10-08 01:26:30 +0900144 base::TimeTicks start_time);
145
vlaviano@chromium.org241215d2011-11-30 13:57:42 +0900146 // Callback invoked by service provider to send a response to a method call.
147 // Can be called immediately from a MethodCallCallback to implement a
148 // synchronous service or called later to implement an asynchronous service.
149 void SendResponse(base::TimeTicks start_time,
dcheng30c5a172016-04-09 07:55:04 +0900150 std::unique_ptr<MethodCall> method_call,
151 std::unique_ptr<Response> response);
vlaviano@chromium.org241215d2011-11-30 13:57:42 +0900152
153 // Called on completion of the method run from SendResponse().
satorux@chromium.orgc9ebea22011-10-08 01:26:30 +0900154 // Takes ownership of |method_call| and |response|.
dcheng30c5a172016-04-09 07:55:04 +0900155 void OnMethodCompleted(std::unique_ptr<MethodCall> method_call,
156 std::unique_ptr<Response> response,
satorux@chromium.orgc9ebea22011-10-08 01:26:30 +0900157 base::TimeTicks start_time);
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900158
159 // Called when the object is unregistered.
160 void OnUnregistered(DBusConnection* connection);
161
162 // Redirects the function call to HandleMessage().
163 static DBusHandlerResult HandleMessageThunk(DBusConnection* connection,
164 DBusMessage* raw_message,
165 void* user_data);
166
167 // Redirects the function call to OnUnregistered().
168 static void OnUnregisteredThunk(DBusConnection* connection,
169 void* user_data);
170
satorux@chromium.orgf06eb892011-10-13 09:45:26 +0900171 scoped_refptr<Bus> bus_;
keybuk@google.combf4649a2012-02-15 06:29:06 +0900172 ObjectPath object_path_;
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900173 bool object_is_registered_;
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900174
175 // The method table where keys are absolute method names (i.e. interface
176 // name + method name), and values are the corresponding callbacks.
177 typedef std::map<std::string, MethodCallCallback> MethodTable;
178 MethodTable method_table_;
179};
180
181} // namespace dbus
182
183#endif // DBUS_EXPORTED_OBJECT_H_