adamk@chromium.org | 35c0eef | 2012-02-11 06:45:23 +0900 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [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_OBJECT_PROXY_H_ |
| 6 | #define DBUS_OBJECT_PROXY_H_ |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 7 | |
thestig@chromium.org | 519c9ac | 2011-11-15 09:29:48 +0900 | [diff] [blame] | 8 | #include <dbus/dbus.h> |
| 9 | |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 10 | #include <map> |
dcheng | 30c5a17 | 2016-04-09 07:55:04 +0900 | [diff] [blame] | 11 | #include <memory> |
satorux@chromium.org | 5b3e496 | 2011-11-24 07:08:38 +0900 | [diff] [blame] | 12 | #include <set> |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 13 | #include <string> |
keybuk@chromium.org | 2594a71 | 2013-04-24 09:12:35 +0900 | [diff] [blame] | 14 | #include <vector> |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 15 | |
| 16 | #include "base/callback.h" |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 17 | #include "base/macros.h" |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 18 | #include "base/memory/ref_counted.h" |
tfarina@chromium.org | 0eed10b | 2013-04-18 06:42:40 +0900 | [diff] [blame] | 19 | #include "base/strings/string_piece.h" |
avi@chromium.org | 78a7e7b | 2013-06-29 00:20:02 +0900 | [diff] [blame] | 20 | #include "base/time/time.h" |
tfarina@chromium.org | 7928ea2 | 2012-11-05 10:56:14 +0900 | [diff] [blame] | 21 | #include "dbus/dbus_export.h" |
keybuk@google.com | bf4649a | 2012-02-15 06:29:06 +0900 | [diff] [blame] | 22 | #include "dbus/object_path.h" |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 23 | |
Hidehiko Abe | fb4bdf4 | 2017-11-21 15:01:39 +0900 | [diff] [blame] | 24 | namespace base { |
| 25 | class TaskRunner; |
| 26 | } // namespace base |
| 27 | |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 28 | namespace dbus { |
| 29 | |
| 30 | class Bus; |
hashimoto@chromium.org | 0d2477e | 2012-04-20 12:18:27 +0900 | [diff] [blame] | 31 | class ErrorResponse; |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 32 | class MethodCall; |
| 33 | class Response; |
avakulenko | 1d8962b | 2014-09-17 10:44:09 +0900 | [diff] [blame] | 34 | class ScopedDBusError; |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 35 | class Signal; |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 36 | |
| 37 | // ObjectProxy is used to communicate with remote objects, mainly for |
| 38 | // calling methods of these objects. |
| 39 | // |
| 40 | // ObjectProxy is a ref counted object, to ensure that |this| of the |
avakulenko | 1d8962b | 2014-09-17 10:44:09 +0900 | [diff] [blame] | 41 | // object is alive when callbacks referencing |this| are called; the |
keybuk@chromium.org | de7b4a9 | 2012-03-22 08:39:17 +0900 | [diff] [blame] | 42 | // bus always holds at least one of those references so object proxies |
| 43 | // always last as long as the bus that created them. |
tfarina@chromium.org | 7928ea2 | 2012-11-05 10:56:14 +0900 | [diff] [blame] | 44 | class CHROME_DBUS_EXPORT ObjectProxy |
| 45 | : public base::RefCountedThreadSafe<ObjectProxy> { |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 46 | public: |
adamk@chromium.org | 35c0eef | 2012-02-11 06:45:23 +0900 | [diff] [blame] | 47 | // Client code should use Bus::GetObjectProxy() or |
| 48 | // Bus::GetObjectProxyWithOptions() instead of this constructor. |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 49 | ObjectProxy(Bus* bus, |
| 50 | const std::string& service_name, |
keybuk@google.com | bf4649a | 2012-02-15 06:29:06 +0900 | [diff] [blame] | 51 | const ObjectPath& object_path, |
adamk@chromium.org | 35c0eef | 2012-02-11 06:45:23 +0900 | [diff] [blame] | 52 | int options); |
| 53 | |
| 54 | // Options to be OR-ed together when calling Bus::GetObjectProxyWithOptions(). |
| 55 | // Set the IGNORE_SERVICE_UNKNOWN_ERRORS option to silence logging of |
stevenjb@chromium.org | c415c48 | 2014-08-09 06:57:19 +0900 | [diff] [blame] | 56 | // org.freedesktop.DBus.Error.ServiceUnknown errors and |
| 57 | // org.freedesktop.DBus.Error.ObjectUnknown errors. |
adamk@chromium.org | 35c0eef | 2012-02-11 06:45:23 +0900 | [diff] [blame] | 58 | enum Options { |
| 59 | DEFAULT_OPTIONS = 0, |
| 60 | IGNORE_SERVICE_UNKNOWN_ERRORS = 1 << 0 |
| 61 | }; |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 62 | |
| 63 | // Special timeout constants. |
| 64 | // |
| 65 | // The constants correspond to DBUS_TIMEOUT_USE_DEFAULT and |
| 66 | // DBUS_TIMEOUT_INFINITE. Here we use literal numbers instead of these |
| 67 | // macros as these aren't defined with D-Bus earlier than 1.4.12. |
| 68 | enum { |
| 69 | TIMEOUT_USE_DEFAULT = -1, |
| 70 | TIMEOUT_INFINITE = 0x7fffffff, |
| 71 | }; |
| 72 | |
hashimoto@chromium.org | 0d2477e | 2012-04-20 12:18:27 +0900 | [diff] [blame] | 73 | // Called when an error response is returned or no response is returned. |
| 74 | // Used for CallMethodWithErrorCallback(). |
Ryo Hashimoto | 0102c16 | 2017-07-21 17:11:14 +0900 | [diff] [blame] | 75 | using ErrorCallback = base::OnceCallback<void(ErrorResponse*)>; |
hashimoto@chromium.org | 0d2477e | 2012-04-20 12:18:27 +0900 | [diff] [blame] | 76 | |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 77 | // Called when the response is returned. Used for CallMethod(). |
Ryo Hashimoto | 0102c16 | 2017-07-21 17:11:14 +0900 | [diff] [blame] | 78 | using ResponseCallback = base::OnceCallback<void(Response*)>; |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 79 | |
Hidehiko Abe | fb4fdb4 | 2017-10-20 04:54:56 +0900 | [diff] [blame] | 80 | // Called when the response is returned or an error occurs. Used for |
| 81 | // CallMethodWithErrorResponse(). |
| 82 | // Note that even in error case, ErrorResponse* may be nullptr. |
| 83 | // E.g. out-of-memory error is found in libdbus, or the connection of |
| 84 | // |bus_| is not yet established. |
| 85 | using ResponseOrErrorCallback = |
| 86 | base::OnceCallback<void(Response*, ErrorResponse*)>; |
| 87 | |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 88 | // Called when a signal is received. Signal* is the incoming signal. |
Ryo Hashimoto | 0102c16 | 2017-07-21 17:11:14 +0900 | [diff] [blame] | 89 | using SignalCallback = base::Callback<void(Signal*)>; |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 90 | |
hashimoto@chromium.org | 4f3851c | 2013-09-27 16:12:03 +0900 | [diff] [blame] | 91 | // Called when NameOwnerChanged signal is received. |
Ryo Hashimoto | 0102c16 | 2017-07-21 17:11:14 +0900 | [diff] [blame] | 92 | using NameOwnerChangedCallback = |
| 93 | base::Callback<void(const std::string& old_owner, |
| 94 | const std::string& new_owner)>; |
hashimoto@chromium.org | 4f3851c | 2013-09-27 16:12:03 +0900 | [diff] [blame] | 95 | |
hashimoto@chromium.org | ed26809 | 2013-10-02 16:53:09 +0900 | [diff] [blame] | 96 | // Called when the service becomes available. |
Ryo Hashimoto | 0102c16 | 2017-07-21 17:11:14 +0900 | [diff] [blame] | 97 | using WaitForServiceToBeAvailableCallback = |
Ryo Hashimoto | baabdc7 | 2017-09-12 13:53:50 +0900 | [diff] [blame] | 98 | base::OnceCallback<void(bool service_is_available)>; |
hashimoto@chromium.org | ed26809 | 2013-10-02 16:53:09 +0900 | [diff] [blame] | 99 | |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 100 | // Called when the object proxy is connected to the signal. |
| 101 | // Parameters: |
| 102 | // - the interface name. |
| 103 | // - the signal name. |
| 104 | // - whether it was successful or not. |
Ryo Hashimoto | 0102c16 | 2017-07-21 17:11:14 +0900 | [diff] [blame] | 105 | using OnConnectedCallback = |
Ryo Hashimoto | baabdc7 | 2017-09-12 13:53:50 +0900 | [diff] [blame] | 106 | base::OnceCallback<void(const std::string&, const std::string&, bool)>; |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 107 | |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 108 | // Calls the method of the remote object and blocks until the response |
avakulenko | 1d8962b | 2014-09-17 10:44:09 +0900 | [diff] [blame] | 109 | // is returned. Returns NULL on error with the error details specified |
| 110 | // in the |error| object. |
| 111 | // |
| 112 | // BLOCKING CALL. |
dcheng | 30c5a17 | 2016-04-09 07:55:04 +0900 | [diff] [blame] | 113 | virtual std::unique_ptr<Response> CallMethodAndBlockWithErrorDetails( |
avakulenko | 1d8962b | 2014-09-17 10:44:09 +0900 | [diff] [blame] | 114 | MethodCall* method_call, |
| 115 | int timeout_ms, |
| 116 | ScopedDBusError* error); |
| 117 | |
| 118 | // Calls the method of the remote object and blocks until the response |
satorux@chromium.org | ffa83a9 | 2011-08-24 12:32:06 +0900 | [diff] [blame] | 119 | // is returned. Returns NULL on error. |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 120 | // |
| 121 | // BLOCKING CALL. |
dcheng | 30c5a17 | 2016-04-09 07:55:04 +0900 | [diff] [blame] | 122 | virtual std::unique_ptr<Response> CallMethodAndBlock(MethodCall* method_call, |
| 123 | int timeout_ms); |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 124 | |
| 125 | // Requests to call the method of the remote object. |
| 126 | // |
| 127 | // |callback| will be called in the origin thread, once the method call |
| 128 | // is complete. As it's called in the origin thread, |callback| can |
| 129 | // safely reference objects in the origin thread (i.e. UI thread in most |
Peter Kasting | 24efe5e | 2018-02-24 09:03:01 +0900 | [diff] [blame] | 130 | // cases). |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 131 | // |
| 132 | // If the method call is successful, a pointer to Response object will |
Ben Chan | 333256c | 2017-11-10 05:20:16 +0900 | [diff] [blame] | 133 | // be passed to the callback. If unsuccessful, nullptr will be passed to |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 134 | // the callback. |
| 135 | // |
| 136 | // Must be called in the origin thread. |
| 137 | virtual void CallMethod(MethodCall* method_call, |
| 138 | int timeout_ms, |
| 139 | ResponseCallback callback); |
| 140 | |
hashimoto@chromium.org | 0d2477e | 2012-04-20 12:18:27 +0900 | [diff] [blame] | 141 | // Requests to call the method of the remote object. |
| 142 | // |
Hidehiko Abe | fb4fdb4 | 2017-10-20 04:54:56 +0900 | [diff] [blame] | 143 | // This is almost as same as CallMethod() defined above. |
| 144 | // The difference is that, the |callback| can take ErrorResponse. |
| 145 | // In case of error, ErrorResponse object is passed to the |callback| |
| 146 | // if the remote object returned an error, or nullptr if a response was not |
| 147 | // received at all (e.g., D-Bus connection is not established). In either |
| 148 | // error case, Response* should be nullptr. |
| 149 | virtual void CallMethodWithErrorResponse(MethodCall* method_call, |
| 150 | int timeout_ms, |
| 151 | ResponseOrErrorCallback callback); |
| 152 | |
| 153 | // DEPRECATED. Please use CallMethodWithErrorResponse() instead. |
| 154 | // TODO(hidehiko): Remove this when migration is done. |
| 155 | // Requests to call the method of the remote object. |
| 156 | // |
hashimoto@chromium.org | 0d2477e | 2012-04-20 12:18:27 +0900 | [diff] [blame] | 157 | // |callback| and |error_callback| will be called in the origin thread, once |
| 158 | // the method call is complete. As it's called in the origin thread, |
| 159 | // |callback| can safely reference objects in the origin thread (i.e. |
Peter Kasting | 24efe5e | 2018-02-24 09:03:01 +0900 | [diff] [blame] | 160 | // UI thread in most cases). |
hashimoto@chromium.org | 0d2477e | 2012-04-20 12:18:27 +0900 | [diff] [blame] | 161 | // |
hashimoto | 1a4a2e5 | 2016-09-01 14:18:38 +0900 | [diff] [blame] | 162 | // If the method call is successful, |callback| will be invoked with a |
| 163 | // Response object. If unsuccessful, |error_callback| will be invoked with an |
| 164 | // ErrorResponse object (if the remote object returned an error) or nullptr |
| 165 | // (if a response was not received at all). |
hashimoto@chromium.org | 0d2477e | 2012-04-20 12:18:27 +0900 | [diff] [blame] | 166 | // |
| 167 | // Must be called in the origin thread. |
| 168 | virtual void CallMethodWithErrorCallback(MethodCall* method_call, |
| 169 | int timeout_ms, |
| 170 | ResponseCallback callback, |
| 171 | ErrorCallback error_callback); |
| 172 | |
quiche | a91b3d3 | 2015-10-24 00:06:03 +0900 | [diff] [blame] | 173 | // Requests to connect to the signal from the remote object. |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 174 | // |
| 175 | // |signal_callback| will be called in the origin thread, when the |
| 176 | // signal is received from the remote object. As it's called in the |
| 177 | // origin thread, |signal_callback| can safely reference objects in the |
| 178 | // origin thread (i.e. UI thread in most cases). |
| 179 | // |
| 180 | // |on_connected_callback| is called when the object proxy is connected |
| 181 | // to the signal, or failed to be connected, in the origin thread. |
| 182 | // |
quiche | a91b3d3 | 2015-10-24 00:06:03 +0900 | [diff] [blame] | 183 | // If a SignalCallback has already been registered for the given |
| 184 | // |interface_name| and |signal_name|, |signal_callback| will be |
| 185 | // added to the list of callbacks for |interface_name| and |
| 186 | // |signal_name|. |
| 187 | // |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 188 | // Must be called in the origin thread. |
| 189 | virtual void ConnectToSignal(const std::string& interface_name, |
| 190 | const std::string& signal_name, |
| 191 | SignalCallback signal_callback, |
| 192 | OnConnectedCallback on_connected_callback); |
| 193 | |
haruki@chromium.org | c8d231a | 2012-11-14 20:02:59 +0900 | [diff] [blame] | 194 | // Sets a callback for "NameOwnerChanged" signal. The callback is called on |
| 195 | // the origin thread when D-Bus system sends "NameOwnerChanged" for the name |
| 196 | // represented by |service_name_|. |
hashimoto@chromium.org | 4f3851c | 2013-09-27 16:12:03 +0900 | [diff] [blame] | 197 | virtual void SetNameOwnerChangedCallback(NameOwnerChangedCallback callback); |
haruki@chromium.org | c8d231a | 2012-11-14 20:02:59 +0900 | [diff] [blame] | 198 | |
derat | 05e85b3 | 2016-08-12 10:41:06 +0900 | [diff] [blame] | 199 | // Registers |callback| to run when the service becomes available. If the |
| 200 | // service is already available, or if connecting to the name-owner-changed |
| 201 | // signal fails, |callback| will be run once asynchronously. Otherwise, |
| 202 | // |callback| will be run once in the future after the service becomes |
| 203 | // available. |
hashimoto@chromium.org | ed26809 | 2013-10-02 16:53:09 +0900 | [diff] [blame] | 204 | virtual void WaitForServiceToBeAvailable( |
| 205 | WaitForServiceToBeAvailableCallback callback); |
| 206 | |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 207 | // Detaches from the remote object. The Bus object will take care of |
| 208 | // detaching so you don't have to do this manually. |
| 209 | // |
| 210 | // BLOCKING CALL. |
| 211 | virtual void Detach(); |
| 212 | |
stevenjb@chromium.org | b53cfb3 | 2013-10-08 07:56:57 +0900 | [diff] [blame] | 213 | const ObjectPath& object_path() const { return object_path_; } |
| 214 | |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 215 | protected: |
| 216 | // This is protected, so we can define sub classes. |
| 217 | virtual ~ObjectProxy(); |
| 218 | |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 219 | private: |
| 220 | friend class base::RefCountedThreadSafe<ObjectProxy>; |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 221 | |
Hidehiko Abe | fb4bdf4 | 2017-11-21 15:01:39 +0900 | [diff] [blame] | 222 | // Callback passed to CallMethod and its family should be deleted on the |
| 223 | // origin thread in any cases. This class manages the work. |
| 224 | class ReplyCallbackHolder { |
| 225 | public: |
| 226 | // Designed to be created on the origin thread. |
| 227 | // Both |origin_task_runner| and |callback| must not be null. |
| 228 | ReplyCallbackHolder(scoped_refptr<base::TaskRunner> origin_task_runner, |
| 229 | ResponseOrErrorCallback callback); |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 230 | |
Hidehiko Abe | fb4bdf4 | 2017-11-21 15:01:39 +0900 | [diff] [blame] | 231 | // This is movable to be bound to an OnceCallback. |
| 232 | ReplyCallbackHolder(ReplyCallbackHolder&& other); |
| 233 | |
| 234 | // |callback_| needs to be destroyed on the origin thread. |
| 235 | // If this is not destroyed on non-origin thread, it PostTask()s the |
| 236 | // callback to the origin thread for destroying. |
| 237 | ~ReplyCallbackHolder(); |
| 238 | |
| 239 | // Returns |callback_| with releasing its ownership. |
| 240 | // This must be called on the origin thread. |
| 241 | ResponseOrErrorCallback ReleaseCallback(); |
| 242 | |
| 243 | private: |
| 244 | scoped_refptr<base::TaskRunner> origin_task_runner_; |
| 245 | ResponseOrErrorCallback callback_; |
| 246 | DISALLOW_COPY_AND_ASSIGN(ReplyCallbackHolder); |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 247 | }; |
| 248 | |
| 249 | // Starts the async method call. This is a helper function to implement |
| 250 | // CallMethod(). |
| 251 | void StartAsyncMethodCall(int timeout_ms, |
satorux@chromium.org | 47d706b | 2011-10-04 22:47:21 +0900 | [diff] [blame] | 252 | DBusMessage* request_message, |
Hidehiko Abe | fb4bdf4 | 2017-11-21 15:01:39 +0900 | [diff] [blame] | 253 | ReplyCallbackHolder callback_holder, |
satorux@chromium.org | 5a92cf3 | 2011-09-07 05:53:30 +0900 | [diff] [blame] | 254 | base::TimeTicks start_time); |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 255 | |
| 256 | // Called when the pending call is complete. |
Hidehiko Abe | fb4bdf4 | 2017-11-21 15:01:39 +0900 | [diff] [blame] | 257 | void OnPendingCallIsComplete(ReplyCallbackHolder callback_holder, |
| 258 | base::TimeTicks start_time, |
| 259 | DBusPendingCall* pending_call); |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 260 | |
Hidehiko Abe | fb4fdb4 | 2017-10-20 04:54:56 +0900 | [diff] [blame] | 261 | // Runs the ResponseOrErrorCallback with the given response object. |
Hidehiko Abe | fb4bdf4 | 2017-11-21 15:01:39 +0900 | [diff] [blame] | 262 | void RunResponseOrErrorCallback(ReplyCallbackHolder callback_holderk, |
Hidehiko Abe | fb4fdb4 | 2017-10-20 04:54:56 +0900 | [diff] [blame] | 263 | base::TimeTicks start_time, |
| 264 | Response* response, |
| 265 | ErrorResponse* error_response); |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 266 | |
hashimoto@chromium.org | ed26809 | 2013-10-02 16:53:09 +0900 | [diff] [blame] | 267 | // Connects to NameOwnerChanged signal. |
| 268 | bool ConnectToNameOwnerChangedSignal(); |
| 269 | |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 270 | // Helper function for ConnectToSignal(). |
hashimoto@chromium.org | ce5c615 | 2013-09-26 15:40:04 +0900 | [diff] [blame] | 271 | bool ConnectToSignalInternal(const std::string& interface_name, |
| 272 | const std::string& signal_name, |
| 273 | SignalCallback signal_callback); |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 274 | |
hashimoto@chromium.org | ed26809 | 2013-10-02 16:53:09 +0900 | [diff] [blame] | 275 | // Helper function for WaitForServiceToBeAvailable(). |
| 276 | void WaitForServiceToBeAvailableInternal(); |
| 277 | |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 278 | // Handles the incoming request messages and dispatches to the signal |
| 279 | // callbacks. |
| 280 | DBusHandlerResult HandleMessage(DBusConnection* connection, |
| 281 | DBusMessage* raw_message); |
| 282 | |
| 283 | // Runs the method. Helper function for HandleMessage(). |
satorux@chromium.org | 5a92cf3 | 2011-09-07 05:53:30 +0900 | [diff] [blame] | 284 | void RunMethod(base::TimeTicks start_time, |
keybuk@chromium.org | 2594a71 | 2013-04-24 09:12:35 +0900 | [diff] [blame] | 285 | std::vector<SignalCallback> signal_callbacks, |
satorux@chromium.org | 5a92cf3 | 2011-09-07 05:53:30 +0900 | [diff] [blame] | 286 | Signal* signal); |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 287 | |
| 288 | // Redirects the function call to HandleMessage(). |
| 289 | static DBusHandlerResult HandleMessageThunk(DBusConnection* connection, |
| 290 | DBusMessage* raw_message, |
| 291 | void* user_data); |
| 292 | |
adamk@chromium.org | 35c0eef | 2012-02-11 06:45:23 +0900 | [diff] [blame] | 293 | // Helper method for logging response errors appropriately. |
satorux@chromium.org | 31bb21e | 2012-05-31 15:12:11 +0900 | [diff] [blame] | 294 | void LogMethodCallFailure(const base::StringPiece& interface_name, |
| 295 | const base::StringPiece& method_name, |
| 296 | const base::StringPiece& error_name, |
adamk@chromium.org | 35c0eef | 2012-02-11 06:45:23 +0900 | [diff] [blame] | 297 | const base::StringPiece& error_message) const; |
| 298 | |
Hidehiko Abe | fb4fdb4 | 2017-10-20 04:54:56 +0900 | [diff] [blame] | 299 | // Used as ResponseOrErrorCallback by CallMethod(). |
| 300 | // Logs error message, and drops |error_response| from the arguments to pass |
| 301 | // |response_callback|. |
Ryo Hashimoto | 0102c16 | 2017-07-21 17:11:14 +0900 | [diff] [blame] | 302 | void OnCallMethod(const std::string& interface_name, |
| 303 | const std::string& method_name, |
| 304 | ResponseCallback response_callback, |
| 305 | Response* response, |
| 306 | ErrorResponse* error_response); |
hashimoto@chromium.org | 0d2477e | 2012-04-20 12:18:27 +0900 | [diff] [blame] | 307 | |
haruki@chromium.org | c5623ec | 2012-10-29 15:27:33 +0900 | [diff] [blame] | 308 | // Adds the match rule to the bus and associate the callback with the signal. |
| 309 | bool AddMatchRuleWithCallback(const std::string& match_rule, |
| 310 | const std::string& absolute_signal_name, |
| 311 | SignalCallback signal_callback); |
| 312 | |
| 313 | // Adds the match rule to the bus so that HandleMessage can see the signal. |
| 314 | bool AddMatchRuleWithoutCallback(const std::string& match_rule, |
| 315 | const std::string& absolute_signal_name); |
| 316 | |
| 317 | // Calls D-Bus's GetNameOwner method synchronously to update |
| 318 | // |service_name_owner_| with the current owner of |service_name_|. |
| 319 | // |
| 320 | // BLOCKING CALL. |
| 321 | void UpdateNameOwnerAndBlock(); |
| 322 | |
| 323 | // Handles NameOwnerChanged signal from D-Bus's special message bus. |
dcheng | 30c5a17 | 2016-04-09 07:55:04 +0900 | [diff] [blame] | 324 | DBusHandlerResult HandleNameOwnerChanged( |
| 325 | std::unique_ptr<dbus::Signal> signal); |
haruki@chromium.org | c5623ec | 2012-10-29 15:27:33 +0900 | [diff] [blame] | 326 | |
hashimoto@chromium.org | 4f3851c | 2013-09-27 16:12:03 +0900 | [diff] [blame] | 327 | // Runs |name_owner_changed_callback_|. |
| 328 | void RunNameOwnerChangedCallback(const std::string& old_owner, |
| 329 | const std::string& new_owner); |
| 330 | |
hashimoto@chromium.org | ed26809 | 2013-10-02 16:53:09 +0900 | [diff] [blame] | 331 | // Runs |wait_for_service_to_be_available_callbacks_|. |
| 332 | void RunWaitForServiceToBeAvailableCallbacks(bool service_is_available); |
| 333 | |
satorux@chromium.org | f06eb89 | 2011-10-13 09:45:26 +0900 | [diff] [blame] | 334 | scoped_refptr<Bus> bus_; |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 335 | std::string service_name_; |
keybuk@google.com | bf4649a | 2012-02-15 06:29:06 +0900 | [diff] [blame] | 336 | ObjectPath object_path_; |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 337 | |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 338 | // The method table where keys are absolute signal names (i.e. interface |
keybuk@chromium.org | 2594a71 | 2013-04-24 09:12:35 +0900 | [diff] [blame] | 339 | // name + signal name), and values are lists of the corresponding callbacks. |
Ryo Hashimoto | 0102c16 | 2017-07-21 17:11:14 +0900 | [diff] [blame] | 340 | using MethodTable = std::map<std::string, std::vector<SignalCallback>>; |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 341 | MethodTable method_table_; |
| 342 | |
haruki@chromium.org | c8d231a | 2012-11-14 20:02:59 +0900 | [diff] [blame] | 343 | // The callback called when NameOwnerChanged signal is received. |
hashimoto@chromium.org | 4f3851c | 2013-09-27 16:12:03 +0900 | [diff] [blame] | 344 | NameOwnerChangedCallback name_owner_changed_callback_; |
haruki@chromium.org | c8d231a | 2012-11-14 20:02:59 +0900 | [diff] [blame] | 345 | |
hashimoto@chromium.org | ed26809 | 2013-10-02 16:53:09 +0900 | [diff] [blame] | 346 | // Called when the service becomes available. |
| 347 | std::vector<WaitForServiceToBeAvailableCallback> |
| 348 | wait_for_service_to_be_available_callbacks_; |
| 349 | |
satorux@chromium.org | 5b3e496 | 2011-11-24 07:08:38 +0900 | [diff] [blame] | 350 | std::set<std::string> match_rules_; |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 351 | |
adamk@chromium.org | 35c0eef | 2012-02-11 06:45:23 +0900 | [diff] [blame] | 352 | const bool ignore_service_unknown_errors_; |
| 353 | |
avakulenko | 1d8962b | 2014-09-17 10:44:09 +0900 | [diff] [blame] | 354 | // Known name owner of the well-known bus name represented by |service_name_|. |
haruki@chromium.org | c5623ec | 2012-10-29 15:27:33 +0900 | [diff] [blame] | 355 | std::string service_name_owner_; |
| 356 | |
hashimoto | 14eb150 | 2015-03-30 16:01:39 +0900 | [diff] [blame] | 357 | std::set<DBusPendingCall*> pending_calls_; |
| 358 | |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 359 | DISALLOW_COPY_AND_ASSIGN(ObjectProxy); |
| 360 | }; |
| 361 | |
| 362 | } // namespace dbus |
| 363 | |
| 364 | #endif // DBUS_OBJECT_PROXY_H_ |