blob: 1400328fda068f7ca81a4d2ec8df56199fcba93c [file] [log] [blame]
adamk@chromium.org35c0eef2012-02-11 06:45:23 +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_OBJECT_PROXY_H_
6#define DBUS_OBJECT_PROXY_H_
satorux@chromium.org163f1cb2011-08-18 05:58:12 +09007
thestig@chromium.org519c9ac2011-11-15 09:29:48 +09008#include <dbus/dbus.h>
9
satorux@chromium.orgf77861f2011-08-25 14:18:29 +090010#include <map>
satorux@chromium.org5b3e4962011-11-24 07:08:38 +090011#include <set>
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090012#include <string>
keybuk@chromium.org2594a712013-04-24 09:12:35 +090013#include <vector>
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090014
15#include "base/callback.h"
16#include "base/memory/ref_counted.h"
tfarina@chromium.org0eed10b2013-04-18 06:42:40 +090017#include "base/strings/string_piece.h"
avi@chromium.org78a7e7b2013-06-29 00:20:02 +090018#include "base/time/time.h"
tfarina@chromium.org7928ea22012-11-05 10:56:14 +090019#include "dbus/dbus_export.h"
keybuk@google.combf4649a2012-02-15 06:29:06 +090020#include "dbus/object_path.h"
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090021
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090022namespace dbus {
23
24class Bus;
hashimoto@chromium.org0d2477e2012-04-20 12:18:27 +090025class ErrorResponse;
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090026class MethodCall;
27class Response;
avakulenko1d8962b2014-09-17 10:44:09 +090028class ScopedDBusError;
satorux@chromium.org7f0c4512011-08-23 16:29:21 +090029class Signal;
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090030
31// ObjectProxy is used to communicate with remote objects, mainly for
32// calling methods of these objects.
33//
34// ObjectProxy is a ref counted object, to ensure that |this| of the
avakulenko1d8962b2014-09-17 10:44:09 +090035// object is alive when callbacks referencing |this| are called; the
keybuk@chromium.orgde7b4a92012-03-22 08:39:17 +090036// bus always holds at least one of those references so object proxies
37// always last as long as the bus that created them.
tfarina@chromium.org7928ea22012-11-05 10:56:14 +090038class CHROME_DBUS_EXPORT ObjectProxy
39 : public base::RefCountedThreadSafe<ObjectProxy> {
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090040 public:
adamk@chromium.org35c0eef2012-02-11 06:45:23 +090041 // Client code should use Bus::GetObjectProxy() or
42 // Bus::GetObjectProxyWithOptions() instead of this constructor.
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090043 ObjectProxy(Bus* bus,
44 const std::string& service_name,
keybuk@google.combf4649a2012-02-15 06:29:06 +090045 const ObjectPath& object_path,
adamk@chromium.org35c0eef2012-02-11 06:45:23 +090046 int options);
47
48 // Options to be OR-ed together when calling Bus::GetObjectProxyWithOptions().
49 // Set the IGNORE_SERVICE_UNKNOWN_ERRORS option to silence logging of
stevenjb@chromium.orgc415c482014-08-09 06:57:19 +090050 // org.freedesktop.DBus.Error.ServiceUnknown errors and
51 // org.freedesktop.DBus.Error.ObjectUnknown errors.
adamk@chromium.org35c0eef2012-02-11 06:45:23 +090052 enum Options {
53 DEFAULT_OPTIONS = 0,
54 IGNORE_SERVICE_UNKNOWN_ERRORS = 1 << 0
55 };
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090056
57 // Special timeout constants.
58 //
59 // The constants correspond to DBUS_TIMEOUT_USE_DEFAULT and
60 // DBUS_TIMEOUT_INFINITE. Here we use literal numbers instead of these
61 // macros as these aren't defined with D-Bus earlier than 1.4.12.
62 enum {
63 TIMEOUT_USE_DEFAULT = -1,
64 TIMEOUT_INFINITE = 0x7fffffff,
65 };
66
hashimoto@chromium.org0d2477e2012-04-20 12:18:27 +090067 // Called when an error response is returned or no response is returned.
68 // Used for CallMethodWithErrorCallback().
69 typedef base::Callback<void(ErrorResponse*)> ErrorCallback;
70
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090071 // Called when the response is returned. Used for CallMethod().
72 typedef base::Callback<void(Response*)> ResponseCallback;
73
satorux@chromium.org7f0c4512011-08-23 16:29:21 +090074 // Called when a signal is received. Signal* is the incoming signal.
75 typedef base::Callback<void (Signal*)> SignalCallback;
76
hashimoto@chromium.org4f3851c2013-09-27 16:12:03 +090077 // Called when NameOwnerChanged signal is received.
78 typedef base::Callback<void(
79 const std::string& old_owner,
80 const std::string& new_owner)> NameOwnerChangedCallback;
81
hashimoto@chromium.orged268092013-10-02 16:53:09 +090082 // Called when the service becomes available.
83 typedef base::Callback<void(
84 bool service_is_available)> WaitForServiceToBeAvailableCallback;
85
satorux@chromium.org7f0c4512011-08-23 16:29:21 +090086 // Called when the object proxy is connected to the signal.
87 // Parameters:
88 // - the interface name.
89 // - the signal name.
90 // - whether it was successful or not.
91 typedef base::Callback<void (const std::string&, const std::string&, bool)>
92 OnConnectedCallback;
93
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090094 // Calls the method of the remote object and blocks until the response
avakulenko1d8962b2014-09-17 10:44:09 +090095 // is returned. Returns NULL on error with the error details specified
96 // in the |error| object.
97 //
98 // BLOCKING CALL.
99 virtual scoped_ptr<Response> CallMethodAndBlockWithErrorDetails(
100 MethodCall* method_call,
101 int timeout_ms,
102 ScopedDBusError* error);
103
104 // Calls the method of the remote object and blocks until the response
satorux@chromium.orgffa83a92011-08-24 12:32:06 +0900105 // is returned. Returns NULL on error.
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900106 //
107 // BLOCKING CALL.
yuki@chromium.orgd4eedf82013-02-07 18:46:24 +0900108 virtual scoped_ptr<Response> CallMethodAndBlock(MethodCall* method_call,
109 int timeout_ms);
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900110
111 // Requests to call the method of the remote object.
112 //
113 // |callback| will be called in the origin thread, once the method call
114 // is complete. As it's called in the origin thread, |callback| can
115 // safely reference objects in the origin thread (i.e. UI thread in most
satorux@chromium.orgb1753152011-11-11 17:36:22 +0900116 // cases). If the caller is not interested in the response from the
117 // method (i.e. calling a method that does not return a value),
118 // EmptyResponseCallback() can be passed to the |callback| parameter.
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900119 //
120 // If the method call is successful, a pointer to Response object will
121 // be passed to the callback. If unsuccessful, NULL will be passed to
122 // the callback.
123 //
124 // Must be called in the origin thread.
125 virtual void CallMethod(MethodCall* method_call,
126 int timeout_ms,
127 ResponseCallback callback);
128
hashimoto@chromium.org0d2477e2012-04-20 12:18:27 +0900129 // Requests to call the method of the remote object.
130 //
131 // |callback| and |error_callback| will be called in the origin thread, once
132 // the method call is complete. As it's called in the origin thread,
133 // |callback| can safely reference objects in the origin thread (i.e.
134 // UI thread in most cases). If the caller is not interested in the response
135 // from the method (i.e. calling a method that does not return a value),
136 // EmptyResponseCallback() can be passed to the |callback| parameter.
137 //
138 // If the method call is successful, a pointer to Response object will
139 // be passed to the callback. If unsuccessful, the error callback will be
140 // called and a pointer to ErrorResponse object will be passed to the error
141 // callback if available, otherwise NULL will be passed.
142 //
143 // Must be called in the origin thread.
144 virtual void CallMethodWithErrorCallback(MethodCall* method_call,
145 int timeout_ms,
146 ResponseCallback callback,
147 ErrorCallback error_callback);
148
keybuk@chromium.orgde7b4a92012-03-22 08:39:17 +0900149 // Requests to connect to the signal from the remote object, replacing
150 // any previous |signal_callback| connected to that signal.
satorux@chromium.org7f0c4512011-08-23 16:29:21 +0900151 //
152 // |signal_callback| will be called in the origin thread, when the
153 // signal is received from the remote object. As it's called in the
154 // origin thread, |signal_callback| can safely reference objects in the
155 // origin thread (i.e. UI thread in most cases).
156 //
157 // |on_connected_callback| is called when the object proxy is connected
158 // to the signal, or failed to be connected, in the origin thread.
159 //
160 // Must be called in the origin thread.
161 virtual void ConnectToSignal(const std::string& interface_name,
162 const std::string& signal_name,
163 SignalCallback signal_callback,
164 OnConnectedCallback on_connected_callback);
165
haruki@chromium.orgc8d231a2012-11-14 20:02:59 +0900166 // Sets a callback for "NameOwnerChanged" signal. The callback is called on
167 // the origin thread when D-Bus system sends "NameOwnerChanged" for the name
168 // represented by |service_name_|.
hashimoto@chromium.org4f3851c2013-09-27 16:12:03 +0900169 virtual void SetNameOwnerChangedCallback(NameOwnerChangedCallback callback);
haruki@chromium.orgc8d231a2012-11-14 20:02:59 +0900170
hashimoto@chromium.orged268092013-10-02 16:53:09 +0900171 // Runs the callback as soon as the service becomes available.
172 virtual void WaitForServiceToBeAvailable(
173 WaitForServiceToBeAvailableCallback callback);
174
satorux@chromium.org7f0c4512011-08-23 16:29:21 +0900175 // Detaches from the remote object. The Bus object will take care of
176 // detaching so you don't have to do this manually.
177 //
178 // BLOCKING CALL.
179 virtual void Detach();
180
stevenjb@chromium.orgb53cfb32013-10-08 07:56:57 +0900181 const ObjectPath& object_path() const { return object_path_; }
182
satorux@chromium.orgb1753152011-11-11 17:36:22 +0900183 // Returns an empty callback that does nothing. Can be used for
184 // CallMethod().
185 static ResponseCallback EmptyResponseCallback();
186
satorux@chromium.orgf77861f2011-08-25 14:18:29 +0900187 protected:
188 // This is protected, so we can define sub classes.
189 virtual ~ObjectProxy();
190
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900191 private:
192 friend class base::RefCountedThreadSafe<ObjectProxy>;
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900193
194 // Struct of data we'll be passing from StartAsyncMethodCall() to
195 // OnPendingCallIsCompleteThunk().
196 struct OnPendingCallIsCompleteData {
197 OnPendingCallIsCompleteData(ObjectProxy* in_object_proxy,
satorux@chromium.org5a92cf32011-09-07 05:53:30 +0900198 ResponseCallback in_response_callback,
hashimoto@chromium.org0d2477e2012-04-20 12:18:27 +0900199 ErrorCallback error_callback,
satorux@chromium.org5a92cf32011-09-07 05:53:30 +0900200 base::TimeTicks start_time);
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900201 ~OnPendingCallIsCompleteData();
202
203 ObjectProxy* object_proxy;
204 ResponseCallback response_callback;
hashimoto@chromium.org0d2477e2012-04-20 12:18:27 +0900205 ErrorCallback error_callback;
satorux@chromium.org5a92cf32011-09-07 05:53:30 +0900206 base::TimeTicks start_time;
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900207 };
208
209 // Starts the async method call. This is a helper function to implement
210 // CallMethod().
211 void StartAsyncMethodCall(int timeout_ms,
satorux@chromium.org47d706b2011-10-04 22:47:21 +0900212 DBusMessage* request_message,
satorux@chromium.org5a92cf32011-09-07 05:53:30 +0900213 ResponseCallback response_callback,
hashimoto@chromium.org0d2477e2012-04-20 12:18:27 +0900214 ErrorCallback error_callback,
satorux@chromium.org5a92cf32011-09-07 05:53:30 +0900215 base::TimeTicks start_time);
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900216
217 // Called when the pending call is complete.
218 void OnPendingCallIsComplete(DBusPendingCall* pending_call,
satorux@chromium.org5a92cf32011-09-07 05:53:30 +0900219 ResponseCallback response_callback,
hashimoto@chromium.org0d2477e2012-04-20 12:18:27 +0900220 ErrorCallback error_callback,
satorux@chromium.org5a92cf32011-09-07 05:53:30 +0900221 base::TimeTicks start_time);
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900222
223 // Runs the response callback with the given response object.
224 void RunResponseCallback(ResponseCallback response_callback,
hashimoto@chromium.org0d2477e2012-04-20 12:18:27 +0900225 ErrorCallback error_callback,
satorux@chromium.org5a92cf32011-09-07 05:53:30 +0900226 base::TimeTicks start_time,
satorux@chromium.org47d706b2011-10-04 22:47:21 +0900227 DBusMessage* response_message);
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900228
229 // Redirects the function call to OnPendingCallIsComplete().
230 static void OnPendingCallIsCompleteThunk(DBusPendingCall* pending_call,
231 void* user_data);
232
hashimoto@chromium.orged268092013-10-02 16:53:09 +0900233 // Connects to NameOwnerChanged signal.
234 bool ConnectToNameOwnerChangedSignal();
235
satorux@chromium.org7f0c4512011-08-23 16:29:21 +0900236 // Helper function for ConnectToSignal().
hashimoto@chromium.orgce5c6152013-09-26 15:40:04 +0900237 bool ConnectToSignalInternal(const std::string& interface_name,
238 const std::string& signal_name,
239 SignalCallback signal_callback);
satorux@chromium.org7f0c4512011-08-23 16:29:21 +0900240
hashimoto@chromium.orged268092013-10-02 16:53:09 +0900241 // Helper function for WaitForServiceToBeAvailable().
242 void WaitForServiceToBeAvailableInternal();
243
satorux@chromium.org7f0c4512011-08-23 16:29:21 +0900244 // Handles the incoming request messages and dispatches to the signal
245 // callbacks.
246 DBusHandlerResult HandleMessage(DBusConnection* connection,
247 DBusMessage* raw_message);
248
249 // Runs the method. Helper function for HandleMessage().
satorux@chromium.org5a92cf32011-09-07 05:53:30 +0900250 void RunMethod(base::TimeTicks start_time,
keybuk@chromium.org2594a712013-04-24 09:12:35 +0900251 std::vector<SignalCallback> signal_callbacks,
satorux@chromium.org5a92cf32011-09-07 05:53:30 +0900252 Signal* signal);
satorux@chromium.org7f0c4512011-08-23 16:29:21 +0900253
254 // Redirects the function call to HandleMessage().
255 static DBusHandlerResult HandleMessageThunk(DBusConnection* connection,
256 DBusMessage* raw_message,
257 void* user_data);
258
adamk@chromium.org35c0eef2012-02-11 06:45:23 +0900259 // Helper method for logging response errors appropriately.
satorux@chromium.org31bb21e2012-05-31 15:12:11 +0900260 void LogMethodCallFailure(const base::StringPiece& interface_name,
261 const base::StringPiece& method_name,
262 const base::StringPiece& error_name,
adamk@chromium.org35c0eef2012-02-11 06:45:23 +0900263 const base::StringPiece& error_message) const;
264
hashimoto@chromium.org0d2477e2012-04-20 12:18:27 +0900265 // Used as ErrorCallback by CallMethod().
satorux@chromium.org31bb21e2012-05-31 15:12:11 +0900266 void OnCallMethodError(const std::string& interface_name,
267 const std::string& method_name,
268 ResponseCallback response_callback,
hashimoto@chromium.org0d2477e2012-04-20 12:18:27 +0900269 ErrorResponse* error_response);
270
haruki@chromium.orgc5623ec2012-10-29 15:27:33 +0900271 // Adds the match rule to the bus and associate the callback with the signal.
272 bool AddMatchRuleWithCallback(const std::string& match_rule,
273 const std::string& absolute_signal_name,
274 SignalCallback signal_callback);
275
276 // Adds the match rule to the bus so that HandleMessage can see the signal.
277 bool AddMatchRuleWithoutCallback(const std::string& match_rule,
278 const std::string& absolute_signal_name);
279
280 // Calls D-Bus's GetNameOwner method synchronously to update
281 // |service_name_owner_| with the current owner of |service_name_|.
282 //
283 // BLOCKING CALL.
284 void UpdateNameOwnerAndBlock();
285
286 // Handles NameOwnerChanged signal from D-Bus's special message bus.
haruki@chromium.orgc8d231a2012-11-14 20:02:59 +0900287 DBusHandlerResult HandleNameOwnerChanged(scoped_ptr<dbus::Signal> signal);
haruki@chromium.orgc5623ec2012-10-29 15:27:33 +0900288
hashimoto@chromium.org4f3851c2013-09-27 16:12:03 +0900289 // Runs |name_owner_changed_callback_|.
290 void RunNameOwnerChangedCallback(const std::string& old_owner,
291 const std::string& new_owner);
292
hashimoto@chromium.orged268092013-10-02 16:53:09 +0900293 // Runs |wait_for_service_to_be_available_callbacks_|.
294 void RunWaitForServiceToBeAvailableCallbacks(bool service_is_available);
295
satorux@chromium.orgf06eb892011-10-13 09:45:26 +0900296 scoped_refptr<Bus> bus_;
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900297 std::string service_name_;
keybuk@google.combf4649a2012-02-15 06:29:06 +0900298 ObjectPath object_path_;
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900299
satorux@chromium.org7f0c4512011-08-23 16:29:21 +0900300 // True if the message filter was added.
301 bool filter_added_;
302
303 // The method table where keys are absolute signal names (i.e. interface
keybuk@chromium.org2594a712013-04-24 09:12:35 +0900304 // name + signal name), and values are lists of the corresponding callbacks.
305 typedef std::map<std::string, std::vector<SignalCallback> > MethodTable;
satorux@chromium.org7f0c4512011-08-23 16:29:21 +0900306 MethodTable method_table_;
307
haruki@chromium.orgc8d231a2012-11-14 20:02:59 +0900308 // The callback called when NameOwnerChanged signal is received.
hashimoto@chromium.org4f3851c2013-09-27 16:12:03 +0900309 NameOwnerChangedCallback name_owner_changed_callback_;
haruki@chromium.orgc8d231a2012-11-14 20:02:59 +0900310
hashimoto@chromium.orged268092013-10-02 16:53:09 +0900311 // Called when the service becomes available.
312 std::vector<WaitForServiceToBeAvailableCallback>
313 wait_for_service_to_be_available_callbacks_;
314
satorux@chromium.org5b3e4962011-11-24 07:08:38 +0900315 std::set<std::string> match_rules_;
satorux@chromium.org7f0c4512011-08-23 16:29:21 +0900316
adamk@chromium.org35c0eef2012-02-11 06:45:23 +0900317 const bool ignore_service_unknown_errors_;
318
avakulenko1d8962b2014-09-17 10:44:09 +0900319 // Known name owner of the well-known bus name represented by |service_name_|.
haruki@chromium.orgc5623ec2012-10-29 15:27:33 +0900320 std::string service_name_owner_;
321
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900322 DISALLOW_COPY_AND_ASSIGN(ObjectProxy);
323};
324
325} // namespace dbus
326
327#endif // DBUS_OBJECT_PROXY_H_