blob: b2b509be8c6a89f668295610caf0e49d5b237aef [file] [log] [blame]
[email protected]20bed012012-02-10 21:45:231// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]a51076112011-08-17 20:58:122// 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_
[email protected]a51076112011-08-17 20:58:127
[email protected]320eff42011-11-15 00:29:488#include <dbus/dbus.h>
9
[email protected]b8ae0512011-08-25 05:18:2910#include <map>
dcheng2a193282016-04-08 22:55:0411#include <memory>
[email protected]0ad9ef82011-11-23 22:08:3812#include <set>
[email protected]a51076112011-08-17 20:58:1213#include <string>
[email protected]012e781dc2013-04-24 00:12:3514#include <vector>
[email protected]a51076112011-08-17 20:58:1215
16#include "base/callback.h"
avi22437c692015-12-22 18:12:4517#include "base/macros.h"
[email protected]a51076112011-08-17 20:58:1218#include "base/memory/ref_counted.h"
Gabriel Charettee926fc12019-12-16 19:00:0219#include "base/sequenced_task_runner.h"
[email protected]daf079a2013-04-17 21:42:4020#include "base/strings/string_piece.h"
[email protected]b43e5562013-06-28 15:20:0221#include "base/time/time.h"
[email protected]e3024462012-11-05 01:56:1422#include "dbus/dbus_export.h"
[email protected]216ed0b2012-02-14 21:29:0623#include "dbus/object_path.h"
[email protected]a51076112011-08-17 20:58:1224
[email protected]a51076112011-08-17 20:58:1225namespace dbus {
26
27class Bus;
[email protected]91fe7ea2012-04-20 03:18:2728class ErrorResponse;
[email protected]a51076112011-08-17 20:58:1229class MethodCall;
30class Response;
avakulenko6b571de2014-09-17 01:44:0931class ScopedDBusError;
[email protected]3beaaa4e2011-08-23 07:29:2132class Signal;
[email protected]a51076112011-08-17 20:58:1233
34// ObjectProxy is used to communicate with remote objects, mainly for
35// calling methods of these objects.
36//
37// ObjectProxy is a ref counted object, to ensure that |this| of the
avakulenko6b571de2014-09-17 01:44:0938// object is alive when callbacks referencing |this| are called; the
[email protected]3b6205c2012-03-21 23:39:1739// bus always holds at least one of those references so object proxies
40// always last as long as the bus that created them.
[email protected]e3024462012-11-05 01:56:1441class CHROME_DBUS_EXPORT ObjectProxy
42 : public base::RefCountedThreadSafe<ObjectProxy> {
[email protected]a51076112011-08-17 20:58:1243 public:
[email protected]20bed012012-02-10 21:45:2344 // Client code should use Bus::GetObjectProxy() or
45 // Bus::GetObjectProxyWithOptions() instead of this constructor.
[email protected]a51076112011-08-17 20:58:1246 ObjectProxy(Bus* bus,
47 const std::string& service_name,
[email protected]216ed0b2012-02-14 21:29:0648 const ObjectPath& object_path,
[email protected]20bed012012-02-10 21:45:2349 int options);
50
51 // Options to be OR-ed together when calling Bus::GetObjectProxyWithOptions().
52 // Set the IGNORE_SERVICE_UNKNOWN_ERRORS option to silence logging of
[email protected]57051da2014-08-08 21:57:1953 // org.freedesktop.DBus.Error.ServiceUnknown errors and
54 // org.freedesktop.DBus.Error.ObjectUnknown errors.
[email protected]20bed012012-02-10 21:45:2355 enum Options {
56 DEFAULT_OPTIONS = 0,
57 IGNORE_SERVICE_UNKNOWN_ERRORS = 1 << 0
58 };
[email protected]a51076112011-08-17 20:58:1259
60 // Special timeout constants.
61 //
62 // The constants correspond to DBUS_TIMEOUT_USE_DEFAULT and
63 // DBUS_TIMEOUT_INFINITE. Here we use literal numbers instead of these
64 // macros as these aren't defined with D-Bus earlier than 1.4.12.
65 enum {
66 TIMEOUT_USE_DEFAULT = -1,
67 TIMEOUT_INFINITE = 0x7fffffff,
68 };
69
[email protected]91fe7ea2012-04-20 03:18:2770 // Called when an error response is returned or no response is returned.
71 // Used for CallMethodWithErrorCallback().
Ryo Hashimoto2a363a82017-07-21 08:11:1472 using ErrorCallback = base::OnceCallback<void(ErrorResponse*)>;
[email protected]91fe7ea2012-04-20 03:18:2773
[email protected]a51076112011-08-17 20:58:1274 // Called when the response is returned. Used for CallMethod().
Ryo Hashimoto2a363a82017-07-21 08:11:1475 using ResponseCallback = base::OnceCallback<void(Response*)>;
[email protected]a51076112011-08-17 20:58:1276
Hidehiko Abefe6246c62017-10-19 19:54:5677 // Called when the response is returned or an error occurs. Used for
78 // CallMethodWithErrorResponse().
79 // Note that even in error case, ErrorResponse* may be nullptr.
80 // E.g. out-of-memory error is found in libdbus, or the connection of
81 // |bus_| is not yet established.
82 using ResponseOrErrorCallback =
83 base::OnceCallback<void(Response*, ErrorResponse*)>;
84
[email protected]3beaaa4e2011-08-23 07:29:2185 // Called when a signal is received. Signal* is the incoming signal.
Reilly Grantd4e66132019-11-22 17:14:5086 using SignalCallback = base::RepeatingCallback<void(Signal*)>;
[email protected]3beaaa4e2011-08-23 07:29:2187
[email protected]db817802e2013-09-27 07:12:0388 // Called when NameOwnerChanged signal is received.
Ryo Hashimoto2a363a82017-07-21 08:11:1489 using NameOwnerChangedCallback =
Reilly Grantd4e66132019-11-22 17:14:5090 base::RepeatingCallback<void(const std::string& old_owner,
91 const std::string& new_owner)>;
[email protected]db817802e2013-09-27 07:12:0392
[email protected]118090c52013-10-02 07:53:0993 // Called when the service becomes available.
Ryo Hashimoto2a363a82017-07-21 08:11:1494 using WaitForServiceToBeAvailableCallback =
Ryo Hashimoto0fc9c712017-09-12 04:53:5095 base::OnceCallback<void(bool service_is_available)>;
[email protected]118090c52013-10-02 07:53:0996
[email protected]3beaaa4e2011-08-23 07:29:2197 // Called when the object proxy is connected to the signal.
98 // Parameters:
99 // - the interface name.
100 // - the signal name.
101 // - whether it was successful or not.
Ryo Hashimoto2a363a82017-07-21 08:11:14102 using OnConnectedCallback =
Ryo Hashimoto0fc9c712017-09-12 04:53:50103 base::OnceCallback<void(const std::string&, const std::string&, bool)>;
[email protected]3beaaa4e2011-08-23 07:29:21104
[email protected]a51076112011-08-17 20:58:12105 // Calls the method of the remote object and blocks until the response
avakulenko6b571de2014-09-17 01:44:09106 // is returned. Returns NULL on error with the error details specified
107 // in the |error| object.
108 //
109 // BLOCKING CALL.
dcheng2a193282016-04-08 22:55:04110 virtual std::unique_ptr<Response> CallMethodAndBlockWithErrorDetails(
avakulenko6b571de2014-09-17 01:44:09111 MethodCall* method_call,
112 int timeout_ms,
113 ScopedDBusError* error);
114
115 // Calls the method of the remote object and blocks until the response
[email protected]06ead872011-08-24 03:32:06116 // is returned. Returns NULL on error.
[email protected]a51076112011-08-17 20:58:12117 //
118 // BLOCKING CALL.
dcheng2a193282016-04-08 22:55:04119 virtual std::unique_ptr<Response> CallMethodAndBlock(MethodCall* method_call,
120 int timeout_ms);
[email protected]a51076112011-08-17 20:58:12121
122 // Requests to call the method of the remote object.
123 //
124 // |callback| will be called in the origin thread, once the method call
125 // is complete. As it's called in the origin thread, |callback| can
126 // safely reference objects in the origin thread (i.e. UI thread in most
Peter Kasting341e1fb2018-02-24 00:03:01127 // cases).
[email protected]a51076112011-08-17 20:58:12128 //
129 // If the method call is successful, a pointer to Response object will
Ben Chan14d500372017-11-09 20:20:16130 // be passed to the callback. If unsuccessful, nullptr will be passed to
[email protected]a51076112011-08-17 20:58:12131 // the callback.
132 //
133 // Must be called in the origin thread.
134 virtual void CallMethod(MethodCall* method_call,
135 int timeout_ms,
136 ResponseCallback callback);
137
[email protected]91fe7ea2012-04-20 03:18:27138 // Requests to call the method of the remote object.
139 //
Hidehiko Abefe6246c62017-10-19 19:54:56140 // This is almost as same as CallMethod() defined above.
141 // The difference is that, the |callback| can take ErrorResponse.
142 // In case of error, ErrorResponse object is passed to the |callback|
143 // if the remote object returned an error, or nullptr if a response was not
144 // received at all (e.g., D-Bus connection is not established). In either
145 // error case, Response* should be nullptr.
146 virtual void CallMethodWithErrorResponse(MethodCall* method_call,
147 int timeout_ms,
148 ResponseOrErrorCallback callback);
149
150 // DEPRECATED. Please use CallMethodWithErrorResponse() instead.
151 // TODO(hidehiko): Remove this when migration is done.
152 // Requests to call the method of the remote object.
153 //
[email protected]91fe7ea2012-04-20 03:18:27154 // |callback| and |error_callback| will be called in the origin thread, once
155 // the method call is complete. As it's called in the origin thread,
156 // |callback| can safely reference objects in the origin thread (i.e.
Peter Kasting341e1fb2018-02-24 00:03:01157 // UI thread in most cases).
[email protected]91fe7ea2012-04-20 03:18:27158 //
hashimotofd6b5512016-09-01 05:18:38159 // If the method call is successful, |callback| will be invoked with a
160 // Response object. If unsuccessful, |error_callback| will be invoked with an
161 // ErrorResponse object (if the remote object returned an error) or nullptr
162 // (if a response was not received at all).
[email protected]91fe7ea2012-04-20 03:18:27163 //
164 // Must be called in the origin thread.
165 virtual void CallMethodWithErrorCallback(MethodCall* method_call,
166 int timeout_ms,
167 ResponseCallback callback,
168 ErrorCallback error_callback);
169
quicheb2702362015-10-23 15:06:03170 // Requests to connect to the signal from the remote object.
[email protected]3beaaa4e2011-08-23 07:29:21171 //
172 // |signal_callback| will be called in the origin thread, when the
173 // signal is received from the remote object. As it's called in the
174 // origin thread, |signal_callback| can safely reference objects in the
175 // origin thread (i.e. UI thread in most cases).
176 //
177 // |on_connected_callback| is called when the object proxy is connected
178 // to the signal, or failed to be connected, in the origin thread.
179 //
quicheb2702362015-10-23 15:06:03180 // If a SignalCallback has already been registered for the given
181 // |interface_name| and |signal_name|, |signal_callback| will be
182 // added to the list of callbacks for |interface_name| and
183 // |signal_name|.
184 //
[email protected]3beaaa4e2011-08-23 07:29:21185 // Must be called in the origin thread.
186 virtual void ConnectToSignal(const std::string& interface_name,
187 const std::string& signal_name,
188 SignalCallback signal_callback,
189 OnConnectedCallback on_connected_callback);
190
[email protected]6d36c0c2012-11-14 11:02:59191 // Sets a callback for "NameOwnerChanged" signal. The callback is called on
192 // the origin thread when D-Bus system sends "NameOwnerChanged" for the name
193 // represented by |service_name_|.
[email protected]db817802e2013-09-27 07:12:03194 virtual void SetNameOwnerChangedCallback(NameOwnerChangedCallback callback);
[email protected]6d36c0c2012-11-14 11:02:59195
derat77fb8182016-08-12 01:41:06196 // Registers |callback| to run when the service becomes available. If the
197 // service is already available, or if connecting to the name-owner-changed
198 // signal fails, |callback| will be run once asynchronously. Otherwise,
199 // |callback| will be run once in the future after the service becomes
200 // available.
[email protected]118090c52013-10-02 07:53:09201 virtual void WaitForServiceToBeAvailable(
202 WaitForServiceToBeAvailableCallback callback);
203
[email protected]3beaaa4e2011-08-23 07:29:21204 // Detaches from the remote object. The Bus object will take care of
205 // detaching so you don't have to do this manually.
206 //
207 // BLOCKING CALL.
208 virtual void Detach();
209
[email protected]ee9941382013-10-07 22:56:57210 const ObjectPath& object_path() const { return object_path_; }
211
[email protected]b8ae0512011-08-25 05:18:29212 protected:
213 // This is protected, so we can define sub classes.
214 virtual ~ObjectProxy();
215
[email protected]a51076112011-08-17 20:58:12216 private:
217 friend class base::RefCountedThreadSafe<ObjectProxy>;
[email protected]a51076112011-08-17 20:58:12218
Hidehiko Abe7f05f782017-11-21 06:01:39219 // Callback passed to CallMethod and its family should be deleted on the
220 // origin thread in any cases. This class manages the work.
221 class ReplyCallbackHolder {
222 public:
223 // Designed to be created on the origin thread.
224 // Both |origin_task_runner| and |callback| must not be null.
Gabriel Charettee926fc12019-12-16 19:00:02225 ReplyCallbackHolder(
226 scoped_refptr<base::SequencedTaskRunner> origin_task_runner,
227 ResponseOrErrorCallback callback);
[email protected]a51076112011-08-17 20:58:12228
Hidehiko Abe7f05f782017-11-21 06:01:39229 // This is movable to be bound to an OnceCallback.
230 ReplyCallbackHolder(ReplyCallbackHolder&& other);
231
232 // |callback_| needs to be destroyed on the origin thread.
233 // If this is not destroyed on non-origin thread, it PostTask()s the
234 // callback to the origin thread for destroying.
235 ~ReplyCallbackHolder();
236
237 // Returns |callback_| with releasing its ownership.
238 // This must be called on the origin thread.
239 ResponseOrErrorCallback ReleaseCallback();
240
241 private:
Gabriel Charettee926fc12019-12-16 19:00:02242 scoped_refptr<base::SequencedTaskRunner> origin_task_runner_;
Hidehiko Abe7f05f782017-11-21 06:01:39243 ResponseOrErrorCallback callback_;
244 DISALLOW_COPY_AND_ASSIGN(ReplyCallbackHolder);
[email protected]a51076112011-08-17 20:58:12245 };
246
247 // Starts the async method call. This is a helper function to implement
248 // CallMethod().
249 void StartAsyncMethodCall(int timeout_ms,
[email protected]1d887b272011-10-04 13:47:21250 DBusMessage* request_message,
Hidehiko Abe7f05f782017-11-21 06:01:39251 ReplyCallbackHolder callback_holder,
[email protected]a73389892011-09-06 20:53:30252 base::TimeTicks start_time);
[email protected]a51076112011-08-17 20:58:12253
254 // Called when the pending call is complete.
Hidehiko Abe7f05f782017-11-21 06:01:39255 void OnPendingCallIsComplete(ReplyCallbackHolder callback_holder,
256 base::TimeTicks start_time,
257 DBusPendingCall* pending_call);
[email protected]a51076112011-08-17 20:58:12258
Hidehiko Abefe6246c62017-10-19 19:54:56259 // Runs the ResponseOrErrorCallback with the given response object.
Hidehiko Abe7f05f782017-11-21 06:01:39260 void RunResponseOrErrorCallback(ReplyCallbackHolder callback_holderk,
Hidehiko Abefe6246c62017-10-19 19:54:56261 base::TimeTicks start_time,
262 Response* response,
263 ErrorResponse* error_response);
[email protected]a51076112011-08-17 20:58:12264
[email protected]118090c52013-10-02 07:53:09265 // Connects to NameOwnerChanged signal.
266 bool ConnectToNameOwnerChangedSignal();
267
Bing Xue55502d8b2019-08-06 17:12:11268 // Tries to connect to NameOwnerChanged signal, ignores any error.
269 void TryConnectToNameOwnerChangedSignal();
270
[email protected]3beaaa4e2011-08-23 07:29:21271 // Helper function for ConnectToSignal().
[email protected]92616752013-09-26 06:40:04272 bool ConnectToSignalInternal(const std::string& interface_name,
273 const std::string& signal_name,
274 SignalCallback signal_callback);
[email protected]3beaaa4e2011-08-23 07:29:21275
[email protected]118090c52013-10-02 07:53:09276 // Helper function for WaitForServiceToBeAvailable().
277 void WaitForServiceToBeAvailableInternal();
278
[email protected]3beaaa4e2011-08-23 07:29:21279 // Handles the incoming request messages and dispatches to the signal
280 // callbacks.
281 DBusHandlerResult HandleMessage(DBusConnection* connection,
282 DBusMessage* raw_message);
283
284 // Runs the method. Helper function for HandleMessage().
[email protected]a73389892011-09-06 20:53:30285 void RunMethod(base::TimeTicks start_time,
[email protected]012e781dc2013-04-24 00:12:35286 std::vector<SignalCallback> signal_callbacks,
[email protected]a73389892011-09-06 20:53:30287 Signal* signal);
[email protected]3beaaa4e2011-08-23 07:29:21288
289 // Redirects the function call to HandleMessage().
290 static DBusHandlerResult HandleMessageThunk(DBusConnection* connection,
291 DBusMessage* raw_message,
292 void* user_data);
293
[email protected]20bed012012-02-10 21:45:23294 // Helper method for logging response errors appropriately.
[email protected]6b0f97382012-05-31 06:12:11295 void LogMethodCallFailure(const base::StringPiece& interface_name,
296 const base::StringPiece& method_name,
297 const base::StringPiece& error_name,
[email protected]20bed012012-02-10 21:45:23298 const base::StringPiece& error_message) const;
299
Hidehiko Abefe6246c62017-10-19 19:54:56300 // Used as ResponseOrErrorCallback by CallMethod().
301 // Logs error message, and drops |error_response| from the arguments to pass
302 // |response_callback|.
Ryo Hashimoto2a363a82017-07-21 08:11:14303 void OnCallMethod(const std::string& interface_name,
304 const std::string& method_name,
305 ResponseCallback response_callback,
306 Response* response,
307 ErrorResponse* error_response);
[email protected]91fe7ea2012-04-20 03:18:27308
[email protected]8bbe31e2012-10-29 06:27:33309 // Adds the match rule to the bus and associate the callback with the signal.
310 bool AddMatchRuleWithCallback(const std::string& match_rule,
311 const std::string& absolute_signal_name,
312 SignalCallback signal_callback);
313
314 // Adds the match rule to the bus so that HandleMessage can see the signal.
315 bool AddMatchRuleWithoutCallback(const std::string& match_rule,
316 const std::string& absolute_signal_name);
317
318 // Calls D-Bus's GetNameOwner method synchronously to update
319 // |service_name_owner_| with the current owner of |service_name_|.
320 //
321 // BLOCKING CALL.
322 void UpdateNameOwnerAndBlock();
323
324 // Handles NameOwnerChanged signal from D-Bus's special message bus.
dcheng2a193282016-04-08 22:55:04325 DBusHandlerResult HandleNameOwnerChanged(
326 std::unique_ptr<dbus::Signal> signal);
[email protected]8bbe31e2012-10-29 06:27:33327
[email protected]db817802e2013-09-27 07:12:03328 // Runs |name_owner_changed_callback_|.
329 void RunNameOwnerChangedCallback(const std::string& old_owner,
330 const std::string& new_owner);
331
[email protected]118090c52013-10-02 07:53:09332 // Runs |wait_for_service_to_be_available_callbacks_|.
333 void RunWaitForServiceToBeAvailableCallbacks(bool service_is_available);
334
[email protected]6477a412011-10-13 00:45:26335 scoped_refptr<Bus> bus_;
[email protected]a51076112011-08-17 20:58:12336 std::string service_name_;
[email protected]216ed0b2012-02-14 21:29:06337 ObjectPath object_path_;
[email protected]a51076112011-08-17 20:58:12338
[email protected]3beaaa4e2011-08-23 07:29:21339 // The method table where keys are absolute signal names (i.e. interface
[email protected]012e781dc2013-04-24 00:12:35340 // name + signal name), and values are lists of the corresponding callbacks.
Ryo Hashimoto2a363a82017-07-21 08:11:14341 using MethodTable = std::map<std::string, std::vector<SignalCallback>>;
[email protected]3beaaa4e2011-08-23 07:29:21342 MethodTable method_table_;
343
[email protected]6d36c0c2012-11-14 11:02:59344 // The callback called when NameOwnerChanged signal is received.
[email protected]db817802e2013-09-27 07:12:03345 NameOwnerChangedCallback name_owner_changed_callback_;
[email protected]6d36c0c2012-11-14 11:02:59346
[email protected]118090c52013-10-02 07:53:09347 // Called when the service becomes available.
348 std::vector<WaitForServiceToBeAvailableCallback>
349 wait_for_service_to_be_available_callbacks_;
350
[email protected]0ad9ef82011-11-23 22:08:38351 std::set<std::string> match_rules_;
[email protected]3beaaa4e2011-08-23 07:29:21352
[email protected]20bed012012-02-10 21:45:23353 const bool ignore_service_unknown_errors_;
354
avakulenko6b571de2014-09-17 01:44:09355 // Known name owner of the well-known bus name represented by |service_name_|.
[email protected]8bbe31e2012-10-29 06:27:33356 std::string service_name_owner_;
357
hashimoto41ece522015-03-30 07:01:39358 std::set<DBusPendingCall*> pending_calls_;
359
[email protected]a51076112011-08-17 20:58:12360 DISALLOW_COPY_AND_ASSIGN(ObjectProxy);
361};
362
363} // namespace dbus
364
365#endif // DBUS_OBJECT_PROXY_H_