1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
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/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 "base/time.h"
28 // ExportedObject is used to export objects and methods to other D-Bus
31 // ExportedObject is a ref counted object, to ensure that |this| of the
32 // object is alive when callbacks referencing |this| are called.
33 class ExportedObject
: public base::RefCountedThreadSafe
<ExportedObject
> {
35 // Client code should use Bus::GetExportedObject() instead of this
37 ExportedObject(Bus
* bus
,
38 const std::string
& service_name
,
39 const std::string
& object_path
);
41 // Called when an exported method is called. MethodCall* is the request
43 typedef base::Callback
<Response
* (MethodCall
*)> MethodCallCallback
;
45 // Called when method exporting is done.
47 // - the interface name.
49 // - whether exporting was successful or not.
50 typedef base::Callback
<void (const std::string
&, const std::string
&, bool)>
53 // Exports the method specified by |interface_name| and |method_name|,
54 // and blocks until exporting is done. Returns true on success.
56 // |method_call_callback| will be called in the origin thread, when the
57 // exported method is called. As it's called in the origin thread,
58 // |method_callback| can safely reference objects in the origin thread
59 // (i.e. UI thread in most cases).
62 virtual bool ExportMethodAndBlock(const std::string
& interface_name
,
63 const std::string
& method_name
,
64 MethodCallCallback method_call_callback
);
66 // Requests to export the method specified by |interface_name| and
67 // |method_name|. See Also ExportMethodAndBlock().
69 // |on_exported_callback| is called when the method is exported or
70 // failed to be exported, in the origin thread.
72 // Must be called in the origin thread.
73 virtual void ExportMethod(const std::string
& interface_name
,
74 const std::string
& method_name
,
75 MethodCallCallback method_call_callback
,
76 OnExportedCallback on_exported_callback
);
78 // Requests to send the signal from this object. The signal will be sent
79 // asynchronously from the message loop in the D-Bus thread.
80 virtual void SendSignal(Signal
* signal
);
82 // Unregisters the object from the bus. The Bus object will take care of
83 // unregistering so you don't have to do this manually.
86 virtual void Unregister();
89 // This is protected, so we can define sub classes.
90 virtual ~ExportedObject();
93 friend class base::RefCountedThreadSafe
<ExportedObject
>;
95 // Helper function for ExportMethod().
96 void ExportMethodInternal(const std::string
& interface_name
,
97 const std::string
& method_name
,
98 MethodCallCallback method_call_callback
,
99 OnExportedCallback exported_callback
);
101 // Called when the object is exported.
102 void OnExported(OnExportedCallback on_exported_callback
,
103 const std::string
& interface_name
,
104 const std::string
& method_name
,
107 // Helper function for SendSignal().
108 void SendSignalInternal(base::TimeTicks start_time
,
109 DBusMessage
* signal_message
);
111 // Registers this object to the bus.
112 // Returns true on success, or the object is already registered.
117 // Handles the incoming request messages and dispatches to the exported
119 DBusHandlerResult
HandleMessage(DBusConnection
* connection
,
120 DBusMessage
* raw_message
);
122 // Runs the method. Helper function for HandleMessage().
123 void RunMethod(MethodCallCallback method_call_callback
,
124 MethodCall
* method_call
,
125 base::TimeTicks start_time
);
127 // Called on completion of the method run from RunMethod().
128 // Takes ownership of |method_call| and |response|.
129 void OnMethodCompleted(MethodCall
* method_call
,
131 base::TimeTicks start_time
);
133 // Called when the object is unregistered.
134 void OnUnregistered(DBusConnection
* connection
);
136 // Redirects the function call to HandleMessage().
137 static DBusHandlerResult
HandleMessageThunk(DBusConnection
* connection
,
138 DBusMessage
* raw_message
,
141 // Redirects the function call to OnUnregistered().
142 static void OnUnregisteredThunk(DBusConnection
* connection
,
145 scoped_refptr
<Bus
> bus_
;
146 std::string service_name_
;
147 std::string object_path_
;
148 bool object_is_registered_
;
150 // The method table where keys are absolute method names (i.e. interface
151 // name + method name), and values are the corresponding callbacks.
152 typedef std::map
<std::string
, MethodCallCallback
> MethodTable
;
153 MethodTable method_table_
;
158 #endif // DBUS_EXPORTED_OBJECT_H_