blob: 7dd8ca577510038ef4ffbcc70b4c8f9fd14fc1a1 [file] [log] [blame]
satorux@chromium.org163f1cb2011-08-18 05:58:12 +09001// 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.
4
5#ifndef DBUS_EXPORTED_OBJECT_H_
6#define DBUS_EXPORTED_OBJECT_H_
7#pragma once
8
9#include <string>
10#include <map>
11#include <utility>
12
13#include <dbus/dbus.h>
14
15#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"
satorux@chromium.org5a92cf32011-09-07 05:53:30 +090019#include "base/time.h"
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090020
21class MessageLoop;
22
23namespace 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.
35class ExportedObject : public base::RefCountedThreadSafe<ExportedObject> {
36 public:
37 // Client code should use Bus::GetExportedObject() instead of this
38 // constructor.
39 ExportedObject(Bus* bus,
40 const std::string& service_name,
41 const std::string& object_path);
42
43 // Called when an exported method is called. MethodCall* is the request
44 // message.
45 typedef base::Callback<Response* (MethodCall*)> MethodCallCallback;
46
47 // Called when method exporting is done.
48 // Parameters:
49 // - the interface name.
50 // - the method name.
51 // - whether exporting was successful or not.
52 typedef base::Callback<void (const std::string&, const std::string&, bool)>
53 OnExportedCallback;
54
55 // Exports the method specified by |interface_name| and |method_name|,
56 // and blocks until exporting is done. Returns true on success.
57 //
58 // |method_call_callback| will be called in the origin thread, when the
59 // exported method is called. As it's called in the origin thread,
satorux@chromium.org7f0c4512011-08-23 16:29:21 +090060 // |method_callback| can safely reference objects in the origin thread
61 // (i.e. UI thread in most cases).
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090062 //
63 // BLOCKING CALL.
64 virtual bool ExportMethodAndBlock(const std::string& interface_name,
65 const std::string& method_name,
66 MethodCallCallback method_call_callback);
67
68 // Requests to export the method specified by |interface_name| and
69 // |method_name|. See Also ExportMethodAndBlock().
70 //
71 // |on_exported_callback| is called when the method is exported or
72 // failed to be exported, in the origin thread.
73 //
74 // Must be called in the origin thread.
75 virtual void ExportMethod(const std::string& interface_name,
76 const std::string& method_name,
77 MethodCallCallback method_call_callback,
78 OnExportedCallback on_exported_callback);
79
satorux@chromium.org7f0c4512011-08-23 16:29:21 +090080 // Requests to send the signal from this object. The signal will be sent
81 // asynchronously from the message loop in the D-Bus thread.
82 virtual void SendSignal(Signal* signal);
83
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090084 // Unregisters the object from the bus. The Bus object will take care of
85 // unregistering so you don't have to do this manually.
86 //
87 // BLOCKING CALL.
88 virtual void Unregister();
89
satorux@chromium.orgf77861f2011-08-25 14:18:29 +090090 protected:
91 // This is protected, so we can define sub classes.
92 virtual ~ExportedObject();
93
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090094 private:
95 friend class base::RefCountedThreadSafe<ExportedObject>;
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090096
97 // Helper function for ExportMethod().
98 void ExportMethodInternal(const std::string& interface_name,
99 const std::string& method_name,
100 MethodCallCallback method_call_callback,
101 OnExportedCallback exported_callback);
102
103 // Called when the object is exported.
104 void OnExported(OnExportedCallback on_exported_callback,
105 const std::string& interface_name,
106 const std::string& method_name,
107 bool success);
108
satorux@chromium.org7f0c4512011-08-23 16:29:21 +0900109 // Helper function for SendSignal().
satorux@chromium.org5a92cf32011-09-07 05:53:30 +0900110 void SendSignalInternal(base::TimeTicks start_time,
satorux@chromium.org47d706b2011-10-04 22:47:21 +0900111 DBusMessage* signal_message);
satorux@chromium.org7f0c4512011-08-23 16:29:21 +0900112
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900113 // Registers this object to the bus.
114 // Returns true on success, or the object is already registered.
115 //
116 // BLOCKING CALL.
117 bool Register();
118
119 // Handles the incoming request messages and dispatches to the exported
120 // methods.
121 DBusHandlerResult HandleMessage(DBusConnection* connection,
122 DBusMessage* raw_message);
123
124 // Runs the method. Helper function for HandleMessage().
125 void RunMethod(MethodCallCallback method_call_callback,
satorux@chromium.orgc9ebea22011-10-08 01:26:30 +0900126 MethodCall* method_call,
127 base::TimeTicks start_time);
128
129 // Called on completion of the method run from RunMethod().
130 // Takes ownership of |method_call| and |response|.
131 void OnMethodCompleted(MethodCall* method_call,
132 Response* response,
133 base::TimeTicks start_time);
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900134
135 // Called when the object is unregistered.
136 void OnUnregistered(DBusConnection* connection);
137
138 // Redirects the function call to HandleMessage().
139 static DBusHandlerResult HandleMessageThunk(DBusConnection* connection,
140 DBusMessage* raw_message,
141 void* user_data);
142
143 // Redirects the function call to OnUnregistered().
144 static void OnUnregisteredThunk(DBusConnection* connection,
145 void* user_data);
146
satorux@chromium.orgf06eb892011-10-13 09:45:26 +0900147 scoped_refptr<Bus> bus_;
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900148 std::string service_name_;
149 std::string object_path_;
150 bool object_is_registered_;
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900151
152 // The method table where keys are absolute method names (i.e. interface
153 // name + method name), and values are the corresponding callbacks.
154 typedef std::map<std::string, MethodCallCallback> MethodTable;
155 MethodTable method_table_;
156};
157
158} // namespace dbus
159
160#endif // DBUS_EXPORTED_OBJECT_H_