[email protected] | 216ed0b | 2012-02-14 21:29:06 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [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 DBUS_EXPORTED_OBJECT_H_ |
| 6 | #define DBUS_EXPORTED_OBJECT_H_ |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 7 | |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 8 | #include <dbus/dbus.h> |
| 9 | |
[email protected] | 320eff4 | 2011-11-15 00:29:48 | [diff] [blame] | 10 | #include <map> |
dcheng | 2a19328 | 2016-04-08 22:55:04 | [diff] [blame] | 11 | #include <memory> |
[email protected] | 320eff4 | 2011-11-15 00:29:48 | [diff] [blame] | 12 | #include <string> |
| 13 | #include <utility> |
| 14 | |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 15 | #include "base/callback.h" |
| 16 | #include "base/memory/ref_counted.h" |
[email protected] | 12f9766 | 2011-08-20 01:07:17 | [diff] [blame] | 17 | #include "base/synchronization/waitable_event.h" |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 18 | #include "base/threading/platform_thread.h" |
[email protected] | b43e556 | 2013-06-28 15:20:02 | [diff] [blame] | 19 | #include "base/time/time.h" |
[email protected] | e302446 | 2012-11-05 01:56:14 | [diff] [blame] | 20 | #include "dbus/dbus_export.h" |
[email protected] | 216ed0b | 2012-02-14 21:29:06 | [diff] [blame] | 21 | #include "dbus/object_path.h" |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 22 | |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 23 | namespace dbus { |
| 24 | |
| 25 | class Bus; |
| 26 | class MethodCall; |
| 27 | class Response; |
[email protected] | 3beaaa4e | 2011-08-23 07:29:21 | [diff] [blame] | 28 | class Signal; |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 29 | |
| 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] | e302446 | 2012-11-05 01:56:14 | [diff] [blame] | 35 | class CHROME_DBUS_EXPORT ExportedObject |
| 36 | : public base::RefCountedThreadSafe<ExportedObject> { |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 37 | public: |
| 38 | // Client code should use Bus::GetExportedObject() instead of this |
| 39 | // constructor. |
[email protected] | 15e7b16 | 2012-03-10 01:12:52 | [diff] [blame] | 40 | ExportedObject(Bus* bus, const ObjectPath& object_path); |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 41 | |
[email protected] | 0c9a8a59 | 2012-11-29 01:52:21 | [diff] [blame] | 42 | // Called to send a response from an exported method. |response| is the |
Reilly Grant | d4e6613 | 2019-11-22 17:14:50 | [diff] [blame] | 43 | // response message. Callers should pass nullptr in the event of an error that |
[email protected] | 0c9a8a59 | 2012-11-29 01:52:21 | [diff] [blame] | 44 | // prevents the sending of a response. |
Reilly Grant | d4e6613 | 2019-11-22 17:14:50 | [diff] [blame] | 45 | using ResponseSender = |
| 46 | base::OnceCallback<void(std::unique_ptr<Response> response)>; |
[email protected] | 9aa74cc | 2011-11-30 04:57:42 | [diff] [blame] | 47 | |
[email protected] | 0c9a8a59 | 2012-11-29 01:52:21 | [diff] [blame] | 48 | // 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 Grant | d4e6613 | 2019-11-22 17:14:50 | [diff] [blame] | 53 | using MethodCallCallback = |
| 54 | base::RepeatingCallback<void(MethodCall* method_call, |
| 55 | ResponseSender sender)>; |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 56 | |
| 57 | // Called when method exporting is done. |
[email protected] | 0c9a8a59 | 2012-11-29 01:52:21 | [diff] [blame] | 58 | // |success| indicates whether exporting was successful or not. |
Reilly Grant | d4e6613 | 2019-11-22 17:14:50 | [diff] [blame] | 59 | using OnExportedCallback = |
| 60 | base::OnceCallback<void(const std::string& interface_name, |
| 61 | const std::string& method_name, |
| 62 | bool success)>; |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 63 | |
Sonny Sasaka | e463407 | 2018-12-12 20:29:33 | [diff] [blame] | 64 | // Called when method unexporting is done. |
| 65 | // |success| indicates whether unexporting was successful or not. |
Reilly Grant | d4e6613 | 2019-11-22 17:14:50 | [diff] [blame] | 66 | using OnUnexportedCallback = |
| 67 | base::OnceCallback<void(const std::string& interface_name, |
Sonny Sasaka | e463407 | 2018-12-12 20:29:33 | [diff] [blame] | 68 | const std::string& method_name, |
Reilly Grant | d4e6613 | 2019-11-22 17:14:50 | [diff] [blame] | 69 | bool success)>; |
Sonny Sasaka | e463407 | 2018-12-12 20:29:33 | [diff] [blame] | 70 | |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 71 | // 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] | 3beaaa4e | 2011-08-23 07:29:21 | [diff] [blame] | 76 | // |method_callback| can safely reference objects in the origin thread |
| 77 | // (i.e. UI thread in most cases). |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 78 | // |
[email protected] | 332577d | 2014-01-09 04:39:17 | [diff] [blame] | 79 | // 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] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 87 | // BLOCKING CALL. |
Reilly Grant | d4e6613 | 2019-11-22 17:14:50 | [diff] [blame] | 88 | virtual bool ExportMethodAndBlock( |
| 89 | const std::string& interface_name, |
| 90 | const std::string& method_name, |
| 91 | const MethodCallCallback& method_call_callback); |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 92 | |
Sonny Sasaka | e463407 | 2018-12-12 20:29:33 | [diff] [blame] | 93 | // 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] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 98 | // 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 Grant | d4e6613 | 2019-11-22 17:14:50 | [diff] [blame] | 107 | const MethodCallCallback& method_call_callback, |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 108 | OnExportedCallback on_exported_callback); |
| 109 | |
Sonny Sasaka | e463407 | 2018-12-12 20:29:33 | [diff] [blame] | 110 | // 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] | 3beaaa4e | 2011-08-23 07:29:21 | [diff] [blame] | 121 | // Requests to send the signal from this object. The signal will be sent |
chirantan | 722c977 | 2015-04-09 19:36:01 | [diff] [blame] | 122 | // synchronously if this method is called from the message loop in the D-Bus |
| 123 | // thread and asynchronously otherwise. |
[email protected] | 3beaaa4e | 2011-08-23 07:29:21 | [diff] [blame] | 124 | virtual void SendSignal(Signal* signal); |
| 125 | |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 126 | // 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] | b8ae051 | 2011-08-25 05:18:29 | [diff] [blame] | 132 | protected: |
| 133 | // This is protected, so we can define sub classes. |
| 134 | virtual ~ExportedObject(); |
| 135 | |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 136 | private: |
| 137 | friend class base::RefCountedThreadSafe<ExportedObject>; |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 138 | |
| 139 | // Helper function for ExportMethod(). |
| 140 | void ExportMethodInternal(const std::string& interface_name, |
| 141 | const std::string& method_name, |
Reilly Grant | d4e6613 | 2019-11-22 17:14:50 | [diff] [blame] | 142 | const MethodCallCallback& method_call_callback, |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 143 | OnExportedCallback exported_callback); |
| 144 | |
Sonny Sasaka | e463407 | 2018-12-12 20:29:33 | [diff] [blame] | 145 | // 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] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 150 | // 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 Sasaka | e463407 | 2018-12-12 20:29:33 | [diff] [blame] | 156 | // 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] | 3beaaa4e | 2011-08-23 07:29:21 | [diff] [blame] | 162 | // Helper function for SendSignal(). |
[email protected] | a7338989 | 2011-09-06 20:53:30 | [diff] [blame] | 163 | void SendSignalInternal(base::TimeTicks start_time, |
[email protected] | 1d887b27 | 2011-10-04 13:47:21 | [diff] [blame] | 164 | DBusMessage* signal_message); |
[email protected] | 3beaaa4e | 2011-08-23 07:29:21 | [diff] [blame] | 165 | |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 166 | // 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 Grant | d4e6613 | 2019-11-22 17:14:50 | [diff] [blame] | 178 | void RunMethod(const MethodCallCallback& method_call_callback, |
dcheng | 2a19328 | 2016-04-08 22:55:04 | [diff] [blame] | 179 | std::unique_ptr<MethodCall> method_call, |
[email protected] | e184cce | 2011-10-07 16:26:30 | [diff] [blame] | 180 | base::TimeTicks start_time); |
| 181 | |
[email protected] | 9aa74cc | 2011-11-30 04:57:42 | [diff] [blame] | 182 | // 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, |
dcheng | 2a19328 | 2016-04-08 22:55:04 | [diff] [blame] | 186 | std::unique_ptr<MethodCall> method_call, |
| 187 | std::unique_ptr<Response> response); |
[email protected] | 9aa74cc | 2011-11-30 04:57:42 | [diff] [blame] | 188 | |
| 189 | // Called on completion of the method run from SendResponse(). |
[email protected] | e184cce | 2011-10-07 16:26:30 | [diff] [blame] | 190 | // Takes ownership of |method_call| and |response|. |
dcheng | 2a19328 | 2016-04-08 22:55:04 | [diff] [blame] | 191 | void OnMethodCompleted(std::unique_ptr<MethodCall> method_call, |
| 192 | std::unique_ptr<Response> response, |
[email protected] | e184cce | 2011-10-07 16:26:30 | [diff] [blame] | 193 | base::TimeTicks start_time); |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 194 | |
| 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] | 6477a41 | 2011-10-13 00:45:26 | [diff] [blame] | 207 | scoped_refptr<Bus> bus_; |
[email protected] | 216ed0b | 2012-02-14 21:29:06 | [diff] [blame] | 208 | ObjectPath object_path_; |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 209 | bool object_is_registered_; |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 210 | |
| 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_ |