| 1 |
= cockpit.js: DBus Client |
| 2 |
|
| 3 |
Cockpit allows access to DBus services via this API. |
| 4 |
|
| 5 |
[[cockpit-dbus-types]] |
| 6 |
== DBus Types |
| 7 |
|
| 8 |
DBus values are represented as javascript values and objects as follows: |
| 9 |
|
| 10 |
`+BYTE 'y'+`:: |
| 11 |
Javascript number. |
| 12 |
`+BOOLEAN 'b'+`:: |
| 13 |
Javascript boolean. |
| 14 |
`+INT16 'n'+`:: |
| 15 |
Javascript number. |
| 16 |
`+UINT16 'q'+`:: |
| 17 |
Javascript number. |
| 18 |
`+INT32 'i'+`:: |
| 19 |
Javascript number. |
| 20 |
`+UINT32 'u'+`:: |
| 21 |
Javascript number. |
| 22 |
`+INT64 'x'+`:: |
| 23 |
Javascript number. |
| 24 |
`+UINT64 't'+`:: |
| 25 |
Javascript number. |
| 26 |
`+DOUBLE 'd'+`:: |
| 27 |
Javascript number. |
| 28 |
`+STRING 's'+`:: |
| 29 |
Javascript string. |
| 30 |
`+OBJECT_PATH 'o'+`:: |
| 31 |
Javascript string. |
| 32 |
`+SIGNATURE 'g'+`:: |
| 33 |
Javascript string. |
| 34 |
`+ARRAY of BYTE 'ay'+`:: |
| 35 |
A string containing base64 encoded data. |
| 36 |
`+ARRAY of DICT_ENTRY with STRING keys 'a{s?}'+`:: |
| 37 |
A javascript plain object with the keys as property names. |
| 38 |
`+ARRAY of DICT_ENTRY with other keys 'a{??}'+`:: |
| 39 |
A javascript plain object each key JSON encoded into a string property |
| 40 |
name. |
| 41 |
`+ARRAY of other+`:: |
| 42 |
A javascript array. |
| 43 |
`+VARIANT+`:: |
| 44 |
A javascript plain object with the `+"t"+` property set to a DBus type |
| 45 |
string, and the `+"v"+` property set to a value. |
| 46 |
`+HANDLE 'h'+`:: |
| 47 |
A javascript object that describes a cockpit channel which represents |
| 48 |
the passed file descriptor. The `+payload+` is always set to |
| 49 |
`+stream+`. Pass it to |
| 50 |
xref:cockpit-channels.adoc#cockpit-channels-channel[cockpit.channel()] to create the |
| 51 |
channel and start reading or writing on it. Handles can only be |
| 52 |
received, not sent from within cockpit. |
| 53 |
|
| 54 |
[[cockpit-dbus-dbus]] |
| 55 |
== cockpit.dbus() |
| 56 |
|
| 57 |
.... |
| 58 |
client = cockpit.dbus(name, [options]) |
| 59 |
.... |
| 60 |
|
| 61 |
Create a DBus client for the given bus `+name+` (eg: service name). Use |
| 62 |
the following functions to make DBus method calls, watch for events, |
| 63 |
etc. The optional `+options+` argument is a javascript plain object, and |
| 64 |
may include: |
| 65 |
|
| 66 |
`+"bus"+`:: |
| 67 |
The DBus bus to connect to. Specifying `+"session"+` will connect to |
| 68 |
the DBus user session bus, `+"user"+` will connect to the user bus (on |
| 69 |
some systems this is identical to the session bus), `+"system"+` will |
| 70 |
connect to the DBus system bus, and `+"none"+` to the non-standard bus |
| 71 |
specified with the `+address+` option. This defaults to "system" if |
| 72 |
not present. |
| 73 |
`+"address"+`:: |
| 74 |
The bus address to connect to in case `+bus+` is `+"none"+`. |
| 75 |
`+"superuser"+`:: |
| 76 |
Set to `+"require"+` to talk to this service as root. The DBus service |
| 77 |
will see the DBus method calls and accesses as coming from root, |
| 78 |
rather than the logged in user. This is useful for talking to services |
| 79 |
that do not correctly use |
| 80 |
https://www.freedesktop.org/software/polkit[polkit] to authorize |
| 81 |
administrative users. If the currently logged in user is not permitted |
| 82 |
to become root (eg: via `+pkexec+`) then the `+client+` will |
| 83 |
immediately be link:#cockpit-dbus-onclose[closed] with a |
| 84 |
`+"access-denied"+` problem code. |
| 85 |
+ |
| 86 |
Set to `+"try"+` to try to talk as root, but if that fails, fall back |
| 87 |
to unprivileged. |
| 88 |
`+"track"+`:: |
| 89 |
It is valid for a DBus service to exit, and be restarted in such a way |
| 90 |
that clients continue to talk to it across the restart. Some services |
| 91 |
are not written with this in mind. If the `+"track"+` option is set to |
| 92 |
`+true+` then the channel will close when the service exits and/or |
| 93 |
disconnects from the DBus bus. |
| 94 |
|
| 95 |
If the `+name+` argument is null, and no options other than `+"bus"+` |
| 96 |
are specified, then a shared DBus `+client+` is created. When using such |
| 97 |
a client with a DBus bus, a `+"name"+` option must be specified on |
| 98 |
various other methods in order to specify which client to talk to. |
| 99 |
|
| 100 |
[[cockpit-dbus-wait]] |
| 101 |
== client.wait() |
| 102 |
|
| 103 |
.... |
| 104 |
promise = client.wait([callback]) |
| 105 |
.... |
| 106 |
|
| 107 |
Returns a `+promise+` that is ready when the client is ready, or fails |
| 108 |
if the client closes. If a `+callback+` is specified, it is attached to |
| 109 |
the promise. |
| 110 |
|
| 111 |
[[cockpit-dbus-close]] |
| 112 |
== client.close() |
| 113 |
|
| 114 |
.... |
| 115 |
client.close([problem]) |
| 116 |
.... |
| 117 |
|
| 118 |
Close the DBus client. If `+problem+` is specified it should be a |
| 119 |
xref:cockpit-error.adoc#cockpit-problems[problem code] string. |
| 120 |
|
| 121 |
[[cockpit-dbus-onclose]] |
| 122 |
== client.onclose |
| 123 |
|
| 124 |
.... |
| 125 |
client.addEventListener("close", options => { ... }) |
| 126 |
.... |
| 127 |
|
| 128 |
An event triggered when the DBus client closes. This can happen either |
| 129 |
because link:#cockpit-dbus-close[client.close()] function was called, or |
| 130 |
the DBus service went away, or some other problem or disconnection. |
| 131 |
|
| 132 |
The `+options+` will contain various close information, including a |
| 133 |
`+"problem"+` field which will be set if the channel was closed because |
| 134 |
of a problem. |
| 135 |
|
| 136 |
[[cockpit-dbus-onowned]] |
| 137 |
== client.onowner |
| 138 |
|
| 139 |
.... |
| 140 |
client.addEventListener("owner", (event, owner) => { ... }) |
| 141 |
.... |
| 142 |
|
| 143 |
An event triggered when the owner of the DBus name changes. The owner |
| 144 |
value will be the id of the name owner on the bus or null if the name is |
| 145 |
unowned. The absence of an owner should not be treated as a |
| 146 |
disconnection. However this makes it possible to take some action based |
| 147 |
on the actual status of the service, for example disconnecting a pending |
| 148 |
signal handler. |
| 149 |
|
| 150 |
[[cockpit-dbus-options]] |
| 151 |
== client.options |
| 152 |
|
| 153 |
Set to the options used when creating the client. Will not change for |
| 154 |
the life of the client. |
| 155 |
|
| 156 |
[[cockpit-dbus-unique-name]] |
| 157 |
== client.unique_name |
| 158 |
|
| 159 |
The unique DBus name of the client. Initially null, and becomes valid |
| 160 |
once the the client is ready. |
| 161 |
|
| 162 |
[[cockpit-dbus-proxy]] |
| 163 |
== client.proxy() |
| 164 |
|
| 165 |
.... |
| 166 |
proxy = client.proxy([interface, path], [options]) |
| 167 |
.... |
| 168 |
|
| 169 |
Create proxy javascript object for a DBus `+interface+`. At the |
| 170 |
specified DBus object `+path+`. The proxy will have properties, methods |
| 171 |
and signals from to the DBus interface, and allows for natural |
| 172 |
interaction. If no `+interface+` is specified then the DBus bus name of |
| 173 |
the client is used. If no `+path+` is specified, then the DBus name of |
| 174 |
the client is converted to a path. |
| 175 |
|
| 176 |
If creating lots of proxies for a given `+interface+` it is more |
| 177 |
efficient to use the link:#cockpit-dbus-proxies[`+client.proxies()+`] |
| 178 |
function. |
| 179 |
|
| 180 |
The proxy is loaded when the |
| 181 |
link:#cockpit-dbus-proxy-valid[`+proxy.valid+`] field is `+true+`, and |
| 182 |
it is set to `+false+` if the underlying `+interface+` and/or `+path+` |
| 183 |
don't or no longer exist, or the `+client+` has closed. You can wait for |
| 184 |
proxy to become valid by passing a callback to its |
| 185 |
link:#cockpit-dbus-proxy-wait[`+proxy.wait()+`] function. The |
| 186 |
link:#cockpit-dbus-proxy-onchanged[`+proxy.onchanged+`] event will also |
| 187 |
fire when the proxy becomes valid or invalid. DBus properties and |
| 188 |
methods on the proxy are not defined until the proxy becomes valid. |
| 189 |
|
| 190 |
.... |
| 191 |
value = proxy.Prop1 |
| 192 |
proxy.WritableProp = value |
| 193 |
.... |
| 194 |
|
| 195 |
All DBus properties on the `+interface+` that start with an upper case |
| 196 |
letter (as is convention) will be automatically defined on this proxy, |
| 197 |
and will update their values as the DBus property values change. In |
| 198 |
addition the link:#cockpit-dbus-proxy-onchanged[`+proxy.onchanged+`] |
| 199 |
event will fire every time the properties change. |
| 200 |
|
| 201 |
If you assign a value to a writable property on the proxy, the proxy |
| 202 |
will try to set that property on the DBus `+interface+` at `+path+`. The |
| 203 |
actual proxy property value will not update until the DBus service has |
| 204 |
notified the proxy of the change. If setting a property fails a warning |
| 205 |
will be logged. In order to have more reliable setting of properties, or |
| 206 |
track when they have been set, or if setting fails, use the |
| 207 |
link:#cockpit-dbus-call[`+client.call()+`] directly. It should be noted |
| 208 |
that DBus service implementations may also be inconsistent in their |
| 209 |
behavior when setting a property fails. |
| 210 |
|
| 211 |
You can access the raw property data using the |
| 212 |
link:#cockpit-dbus-proxy-data[`+proxy.data+`] field, including data for |
| 213 |
properties that do not start with an upper case letter. |
| 214 |
|
| 215 |
.... |
| 216 |
proxy.Method(arg1, arg2) |
| 217 |
.then((retval1, retval2) => { |
| 218 |
... |
| 219 |
}) |
| 220 |
.catch(ex => { |
| 221 |
... |
| 222 |
}); |
| 223 |
.... |
| 224 |
|
| 225 |
All DBus methods on the `+interface+` that start with an upper case |
| 226 |
letter (as is convention) will be automatically defined on this proxy. |
| 227 |
These methods are called with arguments as normal javascript arguments. |
| 228 |
A |
| 229 |
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise[Promise] |
| 230 |
that will complete successfully when the method returns, or fail if an |
| 231 |
error occurs. The return values from the DBus method will be passed to |
| 232 |
the `+then+` handler function directly. |
| 233 |
|
| 234 |
Methods that do not start with an upper case letter can be invoked by |
| 235 |
using the usual link:#cockpit-dbus-proxy-call[`+proxy.call()+`] |
| 236 |
directly. |
| 237 |
|
| 238 |
.... |
| 239 |
proxy.addEventListener("signal", (event, arg1, arg2) => { |
| 240 |
... |
| 241 |
}); |
| 242 |
.... |
| 243 |
|
| 244 |
All DBus signals on the `+interface+` that start with an upper case |
| 245 |
letter (as is convention) will be automatically emit events on this |
| 246 |
proxy. These events will contain the signal arguments after the standard |
| 247 |
`+event+` argument. |
| 248 |
|
| 249 |
Signals that do not start with an upper case letter can be subscribed to |
| 250 |
by using link:#cockpit-dbus-proxy-signal[`+proxy.onsignal+`] directly. |
| 251 |
|
| 252 |
Usually a proxy asks the `+client+` to watch and notify it of changes to |
| 253 |
the relevant object or path. You can pass an `+options+` argument with |
| 254 |
the `+watch+` field set to `+false+` to prevent this. |
| 255 |
|
| 256 |
[[cockpit-dbus-proxy-client]] |
| 257 |
== proxy.client |
| 258 |
|
| 259 |
Set to the DBus client of the proxy. Will not change for the life of the |
| 260 |
proxy. |
| 261 |
|
| 262 |
[[cockpit-dbus-proxy-path]] |
| 263 |
== proxy.path |
| 264 |
|
| 265 |
Set to the DBus object path of the proxy. Will not change for the life |
| 266 |
of the proxy. |
| 267 |
|
| 268 |
[[cockpit-dbus-proxy-iface]] |
| 269 |
== proxy.iface |
| 270 |
|
| 271 |
Set to the DBus interface name of the proxy. Will not change for the |
| 272 |
life of the proxy. |
| 273 |
|
| 274 |
[[cockpit-dbus-proxy-valid]] |
| 275 |
== proxy.valid |
| 276 |
|
| 277 |
Set to `+true+` when the proxy's DBus interface is present at its DBus |
| 278 |
path, and all information for the proxy has loaded. Is set to `+false+` |
| 279 |
while loading, and after the proxy no longer refers a DBus interface and |
| 280 |
path. Also set to `+false+` if the `+client+` closes. |
| 281 |
|
| 282 |
Use the by link:#cockpit-dbus-proxy-wait[`+proxy.wait()+`] function to |
| 283 |
wait for a proxy to load. The |
| 284 |
link:#cockpit-dbus-proxy-onchanged[`+proxy.onchanged+`] event will also |
| 285 |
be emitted when the proxy becomes valid or invalid. DBus properties and |
| 286 |
methods on the proxy are not defined until the proxy becomes valid. |
| 287 |
|
| 288 |
[[cockpit-dbus-proxy-data]] |
| 289 |
== proxy.data |
| 290 |
|
| 291 |
A plain javascript object containing all the raw property data that this |
| 292 |
proxy has loaded. This will be updated automatically as the proxy is |
| 293 |
notified of property changes from the DBus service. The |
| 294 |
link:#cockpit-dbus-proxy-onchanged[`+proxy.onchanged+`] event will be |
| 295 |
emitted when it changes. |
| 296 |
|
| 297 |
[[cockpit-dbus-proxy-call]] |
| 298 |
== proxy.call() |
| 299 |
|
| 300 |
.... |
| 301 |
invocation = proxy.call(method, args, [options]) |
| 302 |
.... |
| 303 |
|
| 304 |
Make a DBus method call on this proxy. |
| 305 |
|
| 306 |
For DBus methods that start with an upper case letter, is usually more |
| 307 |
convenient link:#cockpit-dbus-proxy[to call the method directly on the |
| 308 |
proxy]. However if methods that do not follow the usual DBus convention, |
| 309 |
or specify additional options, or the caller cannot be sure that the |
| 310 |
method actually exists, you can use this method. |
| 311 |
|
| 312 |
This function also works on proxies that have are still loading and have |
| 313 |
not become valid yet. |
| 314 |
|
| 315 |
The `+method+` should be a DBus method name, and the `+args+` should be |
| 316 |
an array of arguments to pass to the method. The `+options+` are |
| 317 |
link:#cockpit-dbus-call[described elsewhere]. |
| 318 |
|
| 319 |
The returned value is identical to the one returned from |
| 320 |
link:#cockpit-dbus-call[client.call()]. It is a |
| 321 |
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise[Promise] |
| 322 |
that will complete successfully when the method returns, or fail if an |
| 323 |
error occurs. |
| 324 |
|
| 325 |
[[cockpit-dbus-proxy-wait]] |
| 326 |
== proxy.wait() |
| 327 |
|
| 328 |
.... |
| 329 |
promise = proxy.wait() |
| 330 |
proxy.wait(() => { |
| 331 |
... |
| 332 |
}); |
| 333 |
.... |
| 334 |
|
| 335 |
Wait for a proxy to finish loading. This function returns a promise. If |
| 336 |
a callback function is passed as an argument then that function will be |
| 337 |
invoked when the proxy is ready. If this method is called after a proxy |
| 338 |
has already loaded, then the promise will be resolved immediately, and |
| 339 |
any callback will be invoked immediately. Use the promise or |
| 340 |
`+proxy.valid+` to determine whether the proxy is valid. |
| 341 |
|
| 342 |
[[cockpit-dbus-proxy-onchanged]] |
| 343 |
== proxy.onchanged |
| 344 |
|
| 345 |
.... |
| 346 |
proxy.addEventListener("changed", (event, data) => { |
| 347 |
... |
| 348 |
}); |
| 349 |
.... |
| 350 |
|
| 351 |
This event is emitted when the proxy's properties change. |
| 352 |
|
| 353 |
The `+data+` has the following form, and will only include properties |
| 354 |
that have changed: |
| 355 |
|
| 356 |
.... |
| 357 |
{ |
| 358 |
"Prop1": "value", |
| 359 |
"Prop2": 5 |
| 360 |
} |
| 361 |
.... |
| 362 |
|
| 363 |
[[cockpit-dbus-proxy-signal]] |
| 364 |
== proxy.onsignal |
| 365 |
|
| 366 |
.... |
| 367 |
proxy.addEventListener("signal", (event, name, args) => { |
| 368 |
... |
| 369 |
}); |
| 370 |
.... |
| 371 |
|
| 372 |
This event is emitted when the proxy's emits an event. |
| 373 |
|
| 374 |
For most events, that have names which start with an upper case letter, |
| 375 |
you can just link:#cockpit-dbus-proxy[connect to that event as a signal |
| 376 |
directly]. However if you wish to be notified when any signal is |
| 377 |
emitted, or for signals that do not follow the usual DBus convention, |
| 378 |
you can connect to this event. |
| 379 |
|
| 380 |
The `+name+` is the DBus signal name, and the `+args+` is an array of |
| 381 |
arguments that were emitted with the signal. |
| 382 |
|
| 383 |
[[cockpit-dbus-proxies]] |
| 384 |
== client.proxies() |
| 385 |
|
| 386 |
.... |
| 387 |
proxies = client.proxies([interface], [path_namespace], [options]) |
| 388 |
.... |
| 389 |
|
| 390 |
Create link:#cockpit-dbus-proxy[proxy javascript objects] for a DBus |
| 391 |
interfaces. The proxies will have properties, methods and signals from |
| 392 |
the DBus `+interface+`, and allow for natural interaction. If no |
| 393 |
`+interface+` is specified then the DBus bus name of the client is used. |
| 394 |
If no `+path_namespace+` is provided then `+"/"+` will be used. |
| 395 |
|
| 396 |
Proxies will be automatically created for instances of the `+interface+` |
| 397 |
available at the DBus service. The optional `+path_namespace+` argument |
| 398 |
can be used to restrict the proxies for instances that have DBus paths |
| 399 |
which have the namespace path prefix. |
| 400 |
|
| 401 |
.... |
| 402 |
proxy1 = proxies["/dbus/path1"]; |
| 403 |
proxy2 = proxies["/dbus/path2"]; |
| 404 |
for (proxy in proxies) { |
| 405 |
... |
| 406 |
} |
| 407 |
.... |
| 408 |
|
| 409 |
The returned `+proxies+` object will is used as a dictionary, and will |
| 410 |
have values containing proxies for DBus interface instances, with the |
| 411 |
keys being the DBus paths of those instances. It is possible to |
| 412 |
enumerate over the returned `+proxies+`. |
| 413 |
|
| 414 |
Proxies will be automatically added and removed from the `+proxies+` |
| 415 |
object as they appear and disappear in the service. The |
| 416 |
link:#cockpit-dbus-proxies-onadded[`+proxies.onadded+`] and |
| 417 |
link:#cockpit-dbus-proxies-onremoved[`+proxies.onremoved+`] events will |
| 418 |
be emitted. DBus services may not support notifications of paths |
| 419 |
disappearing. |
| 420 |
|
| 421 |
Use the `+proxies.wait()+` function to be notified when the initial set |
| 422 |
of proxies has been populated. |
| 423 |
|
| 424 |
Usually a proxies ask the `+client+` to watch and be notified of changes |
| 425 |
to the relevant object or path. You can pass an `+options+` argument |
| 426 |
with the `+watch+` field set to `+false+` to prevent this. |
| 427 |
|
| 428 |
[[cockpit-dbus-proxies-wait]] |
| 429 |
== proxies.wait() |
| 430 |
|
| 431 |
.... |
| 432 |
promise = proxies.wait() |
| 433 |
proxies.wait(() => { |
| 434 |
... |
| 435 |
}); |
| 436 |
.... |
| 437 |
|
| 438 |
Wait for a `+proxies+` object to populate its initial set of proxies. |
| 439 |
This function returns a promise. If a callback function is passed as an |
| 440 |
argument then that function will be invoked when the proxies are ready. |
| 441 |
If this method is called after the proxies have populated, then the |
| 442 |
promise will be resolved immediately, and any callback will be invoked |
| 443 |
immediately. |
| 444 |
|
| 445 |
[[cockpit-dbus-proxies-client]] |
| 446 |
== proxies.client |
| 447 |
|
| 448 |
Set to the DBus client of the proxies. Will not change. |
| 449 |
|
| 450 |
[[cockpit-dbus-proxies-iface]] |
| 451 |
== proxies.iface |
| 452 |
|
| 453 |
Set to the DBus interface name of the proxies. Will not change. |
| 454 |
|
| 455 |
[[cockpit-dbus-proxies-path_namespace]] |
| 456 |
== proxies.path_namespace |
| 457 |
|
| 458 |
Set to the DBus path namespace used which the proxies must have as a |
| 459 |
DBus path prefix. Will not change. |
| 460 |
|
| 461 |
[[cockpit-dbus-proxies-onadded]] |
| 462 |
== proxies.onadded |
| 463 |
|
| 464 |
.... |
| 465 |
proxies.addEventListener("added", (event, proxy) => { |
| 466 |
... |
| 467 |
}) |
| 468 |
.... |
| 469 |
|
| 470 |
This event is emitted when a proxy is added to the `+proxies+` object. |
| 471 |
The proxy will already have loaded. |
| 472 |
|
| 473 |
[[cockpit-dbus-proxies-onchanged]] |
| 474 |
== proxies.onchanged |
| 475 |
|
| 476 |
.... |
| 477 |
proxies.addEventListener("changed", (event, proxy) => { |
| 478 |
... |
| 479 |
}) |
| 480 |
.... |
| 481 |
|
| 482 |
This event is emitted when one of the proxy in the `+proxies+` object |
| 483 |
changes its properties. |
| 484 |
|
| 485 |
[[cockpit-dbus-proxies-onremoved]] |
| 486 |
== proxies.onremoved |
| 487 |
|
| 488 |
.... |
| 489 |
proxies.addEventListener("removed", (event, proxy) => { |
| 490 |
... |
| 491 |
}) |
| 492 |
.... |
| 493 |
|
| 494 |
This event is emitted when a proxy is removed to the `+proxies+` object. |
| 495 |
|
| 496 |
[[cockpit-dbus-call]] |
| 497 |
== client.call() |
| 498 |
|
| 499 |
.... |
| 500 |
invocation = client.call(path, interface, method, args, [options]) |
| 501 |
.... |
| 502 |
|
| 503 |
Make a DBus method call. |
| 504 |
|
| 505 |
The `+path+` is the DBus object path to make the call on, `+interface+` |
| 506 |
is the DBus interface for the method and `+method+` is the name of the |
| 507 |
method to call. The `+args+` is an array of arguments to pass to the |
| 508 |
method, each of which must be appropriate for the expected |
| 509 |
link:#cockpit-dbus[DBus type] of that argument. The `+args+` may be |
| 510 |
`+null+` if no arguments are to be sent. |
| 511 |
|
| 512 |
The returned value is a |
| 513 |
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise[Promise] |
| 514 |
that will complete successfully when the method returns, or fail if an |
| 515 |
error occurs. |
| 516 |
|
| 517 |
If `+options+` is specified it should be a plain javascript object, |
| 518 |
which may contain the following properties: |
| 519 |
|
| 520 |
`+flags+`:: |
| 521 |
A string containing DBus message flags. The character `+"i"+` |
| 522 |
indicates to the dbus service that interactive authentication is |
| 523 |
allowed. If the entire `+flags+` field is missing, then `+"i"+` is set |
| 524 |
by default. |
| 525 |
`+type+`:: |
| 526 |
A valid DBus type signature to use when calling the method. In the |
| 527 |
absence of this, the DBus service will be introspected (and the result |
| 528 |
cached) to ask what the method type signature is. |
| 529 |
`+timeout+`:: |
| 530 |
The timeout of the call in milliseconds. The call will fail with the |
| 531 |
`+"timeout"+` problem code. If "timeout" is not given, the call will |
| 532 |
never time out. |
| 533 |
|
| 534 |
[[cockpit-dbus-then]] |
| 535 |
== invocation.then() |
| 536 |
|
| 537 |
.... |
| 538 |
invocation.then((args, options) => { ... }) |
| 539 |
.... |
| 540 |
|
| 541 |
This is a standard |
| 542 |
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise[Promise] |
| 543 |
method. It sets up a handler to be called when the DBus method call |
| 544 |
finishes successfully. |
| 545 |
|
| 546 |
The `+args+` argument is an array of return values from the DBus method. |
| 547 |
Each of them will be converted to an appropriate |
| 548 |
link:#cockpit-dbus[javascript type]. |
| 549 |
|
| 550 |
The `+options+` argument may contain additional information about the |
| 551 |
reply. If the `+type+` option was specified when performing the method |
| 552 |
call, then the `+options+` in the reply here will also contain a |
| 553 |
`+type+` field containing the DBus type signature of the output. If the |
| 554 |
`+flags+` option was specified when performing the call then the |
| 555 |
`+options+` in the reply here will contain message flags. Possible out |
| 556 |
message flags are: |
| 557 |
|
| 558 |
`+>+`:: |
| 559 |
A big endian message. |
| 560 |
`+<+`:: |
| 561 |
A little endian message. |
| 562 |
|
| 563 |
[[cockpit-dbus-catch]] |
| 564 |
== invocation.catch() |
| 565 |
|
| 566 |
.... |
| 567 |
invocation.catch(exception => { ... }) |
| 568 |
.... |
| 569 |
|
| 570 |
This is a standard |
| 571 |
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise[Promise] |
| 572 |
method. It sets up a handler to be called when the DBus method call |
| 573 |
fails. |
| 574 |
|
| 575 |
The `+exception+` object passed to the handler can have the following |
| 576 |
properties: |
| 577 |
|
| 578 |
`+problem+`:: |
| 579 |
A xref:cockpit-error.adoc#cockpit-problems[problem code] string when a problem occurred |
| 580 |
starting or communicating with the DBus service. This is `+null+` in |
| 581 |
the cases where an actual DBus error was occurred. |
| 582 |
`+name+`:: |
| 583 |
The DBus error name. This will be `+null+` in cases where the failure |
| 584 |
was not due to a DBus error. |
| 585 |
`+message+`:: |
| 586 |
A DBus error message. This will be `+null+` in cases where the failure |
| 587 |
was not due to a DBus error. |
| 588 |
|
| 589 |
[[cockpit-dbus-subscribe]] |
| 590 |
== client.subscribe() |
| 591 |
|
| 592 |
.... |
| 593 |
subscription = client.subscribe(match, (path, interface, signal, args) => { ... }) |
| 594 |
.... |
| 595 |
|
| 596 |
Subscribe to signals. The `+match+` argument is a javascript plain |
| 597 |
object which defines what signals to subscribe to. Each property in the |
| 598 |
`+match+` argument restricts signals subscribed to. If a property is not |
| 599 |
present then it is treated as a wildcard, matching anything. If an empty |
| 600 |
object is specified as `+match+` then all signals will be subscribed to. |
| 601 |
The `+match+` argument may contain the following properties: |
| 602 |
|
| 603 |
`+interface+`:: |
| 604 |
A DBus interface to match. |
| 605 |
`+path+`:: |
| 606 |
A DBus object path to match. May not be used together with the |
| 607 |
`+path_namespace+` property. It should be a valid DBus object path, |
| 608 |
that is, it should have no trailing slash. |
| 609 |
`+path_namespace+`:: |
| 610 |
A DBus object path prefix to match. Any paths in the hierarchy below |
| 611 |
this top path will match. May not be used together with the `+path+` |
| 612 |
property. |
| 613 |
`+member+`:: |
| 614 |
The DBus signal name to match. |
| 615 |
`+arg0+`:: |
| 616 |
Matches the first argument of a DBus message, which must be a string. |
| 617 |
|
| 618 |
The handler passed as the second argument will be invoked when the |
| 619 |
signal is received. A `+subscription+` is returned which can be used to |
| 620 |
remove the subscription by calling its `+subscription.remove()+` method. |
| 621 |
|
| 622 |
It is not a problem to subscribe to the same signals more than once, |
| 623 |
with identical or slightly different `+match+` arguments. |
| 624 |
|
| 625 |
[[cockpit-dbus-remove]] |
| 626 |
== subscription.remove() |
| 627 |
|
| 628 |
.... |
| 629 |
subscription.remove() |
| 630 |
.... |
| 631 |
|
| 632 |
Unsubscribe from the DBus signal subscription. |
| 633 |
|
| 634 |
[[cockpit-dbus-watch]] |
| 635 |
== client.watch() |
| 636 |
|
| 637 |
.... |
| 638 |
watch = client.watch(path) |
| 639 |
watch = client.watch({ "path_namespace": path_namespace, "interface": interface }) |
| 640 |
.... |
| 641 |
|
| 642 |
Watch for property and interface changes on the given DBus object |
| 643 |
`+path+` DBus `+path_namespace+`. If `+interface+` is specified only |
| 644 |
properties on that DBus interface will be watched. |
| 645 |
|
| 646 |
The link:#cockpit-dbus-proxy[`+client.proxy()+`] and |
| 647 |
link:#cockpit-dbus-proxies[`+client.proxies()+`] functions and the |
| 648 |
objects they return are high level wrappers around `+client.watch()+`. |
| 649 |
|
| 650 |
The property and interface changes will be available in raw form on the |
| 651 |
link:#cockpit-dbus-onnotify[`+client.onnotify+`] event. |
| 652 |
|
| 653 |
Property and interface changes that are caused by a method call or |
| 654 |
signal will show up before that method call reply is received, or signal |
| 655 |
event is triggered. It should be possible to rely on this guarantee, |
| 656 |
unless the DBus service in question behaves incorrectly. Internally |
| 657 |
these watches work well with code that implements the |
| 658 |
https://dbus.freedesktop.org/doc/dbus-specification.html#standard-interfaces-objectmanager[ObjectManager] |
| 659 |
portion of the DBus specification. If no ObjectManager implementation is |
| 660 |
available, the watch falls back to using DBus |
| 661 |
https://dbus.freedesktop.org/doc/dbus-specification.html#standard-interfaces-introspectable[Introspection] |
| 662 |
along with the usual |
| 663 |
https://dbus.freedesktop.org/doc/dbus-specification.html#standard-interfaces-properties[PropertiesChanged] |
| 664 |
signal. If the DBus service implements none of these, or implements them |
| 665 |
in an inconsistent manner, then this function will provide inconsistent |
| 666 |
or unexpected results. |
| 667 |
|
| 668 |
The parameter is either a DBus `+path+` or a plain javascript object |
| 669 |
with zero or more of the following fields. If an empty javascript object |
| 670 |
is used as an argument, then all paths, interfaces and properties will |
| 671 |
be watched. |
| 672 |
|
| 673 |
`+interface+`:: |
| 674 |
Watch properties on this DBus interface. |
| 675 |
`+path+`:: |
| 676 |
Watch interfaces and properties at this DBus path. May not be used |
| 677 |
together with the `+path_namespace+` property. |
| 678 |
`+path_namespace+`:: |
| 679 |
Watch interfaces and properties under this DBus path. It should be a |
| 680 |
valid DBus object path, that is, it should have no trailing slash. If |
| 681 |
an ObjectManager implementation is available at this interface, then |
| 682 |
it is used. May not be used together with the `+path+` property. |
| 683 |
|
| 684 |
The returned value is a |
| 685 |
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise[Promise] |
| 686 |
that will complete successfully when the watch has populated its initial |
| 687 |
set of properties and interfaces, and these have been notified via |
| 688 |
link:#cockpit-dbus-onnotify[`+client.onnotify+`]. |
| 689 |
|
| 690 |
A watch can be removed by calling the |
| 691 |
link:#cockpit-dbus-watch-remove[`+watch.remove()+`] method on the |
| 692 |
returned value. If identical watches are added more than once, then they |
| 693 |
must also be removed the same number of times before the removal takes |
| 694 |
effect. |
| 695 |
|
| 696 |
[[cockpit-dbus-watch-then]] |
| 697 |
== watch.then() |
| 698 |
|
| 699 |
.... |
| 700 |
watch.then(() => { ... }) |
| 701 |
.... |
| 702 |
|
| 703 |
This is a standard |
| 704 |
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise[Promise] |
| 705 |
method. It sets up a handler to be called when the watch has populated |
| 706 |
its initial properties and interfaces. |
| 707 |
|
| 708 |
[[cockpit-dbus-watch-catch]] |
| 709 |
== watch.catch() |
| 710 |
|
| 711 |
.... |
| 712 |
watch.catch(ex => { ... }) |
| 713 |
.... |
| 714 |
|
| 715 |
This is a standard |
| 716 |
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise[Promise] |
| 717 |
method. It sets up a handler to be called if the watch fails to populate |
| 718 |
its initial properties and interfaces. Note that a watch will only fail |
| 719 |
if the DBus client closes or is somehow disconnected. It does not fail |
| 720 |
in the case of missing interfaces or properties. |
| 721 |
|
| 722 |
[[cockpit-dbus-watch-remove]] |
| 723 |
== watch.remove() |
| 724 |
|
| 725 |
.... |
| 726 |
watch.remove() |
| 727 |
.... |
| 728 |
|
| 729 |
Remove the watch. This may not have any immediate effect if other |
| 730 |
watches are in place. In particular, if identical watches are added more |
| 731 |
than once, then they must also be removed the same number of times |
| 732 |
before the removal takes effect. |
| 733 |
|
| 734 |
[[cockpit-dbus-onnotify]] |
| 735 |
== client.onnotify |
| 736 |
|
| 737 |
.... |
| 738 |
client.addEventListener("notify", data => { ... }) |
| 739 |
.... |
| 740 |
|
| 741 |
An event triggered when link:#cockpit-dbus-watch[watched] properties or |
| 742 |
interfaces change. |
| 743 |
|
| 744 |
The link:#cockpit-dbus-proxy[`+client.proxy()+`] and |
| 745 |
link:#cockpit-dbus-proxies[`+client.proxies()+`] functions and the |
| 746 |
objects they return are high level wrappers around the `+data+` provided |
| 747 |
by this event. |
| 748 |
|
| 749 |
The `+data+` has the following form: |
| 750 |
|
| 751 |
.... |
| 752 |
{ |
| 753 |
"/path1": { |
| 754 |
"org.Interface1": { |
| 755 |
"Prop1": "value", |
| 756 |
"Prop2": 5 |
| 757 |
}, |
| 758 |
"org.Interface2": null |
| 759 |
} |
| 760 |
} |
| 761 |
.... |
| 762 |
|
| 763 |
Multiple paths may be present, each of which may have multiple |
| 764 |
interfaces, each of which may have multiple properties. The first time a |
| 765 |
given path and interface is emitted from this signal, it will have all |
| 766 |
its properties and interfaces. Thereafter only changes are noted. If an |
| 767 |
interface is set to `+null+`, then that interface has disappeared. |
| 768 |
|
| 769 |
[[cockpit-dbus-notify]] |
| 770 |
== client.notify() |
| 771 |
|
| 772 |
.... |
| 773 |
client.notify(data) |
| 774 |
.... |
| 775 |
|
| 776 |
Emits a synthetic link:#cockpit-dbus-onnotify[`+notify+`] event. The |
| 777 |
`+data+` argument should follow the same layout as described for the |
| 778 |
`+notify+` event. |
| 779 |
|
| 780 |
[[cockpit-dbus-onmeta]] |
| 781 |
== client.onmeta |
| 782 |
|
| 783 |
.... |
| 784 |
client.onmeta = (ev, data) => { ... } |
| 785 |
.... |
| 786 |
|
| 787 |
An event triggered when the meta data about |
| 788 |
link:#cockpit-dbus-watch[watched] interfaces is loaded. |
| 789 |
|
| 790 |
The link:#cockpit-dbus-proxy[`+client.proxy()+`] and |
| 791 |
link:#cockpit-dbus-proxies[`+client.proxies()+`] functions and the |
| 792 |
objects they return are high level wrappers around the `+data+` provided |
| 793 |
by this event. |
| 794 |
|
| 795 |
The `+data+` has the following form: |
| 796 |
|
| 797 |
.... |
| 798 |
{ |
| 799 |
"org.Interface": { |
| 800 |
"methods": { |
| 801 |
"Method1": { |
| 802 |
"in": [ "s", "v" ], |
| 803 |
"out": [ "i" ] |
| 804 |
}, |
| 805 |
"Method2": { } |
| 806 |
}, |
| 807 |
"signals": { |
| 808 |
"Signal": { |
| 809 |
"in": [ "b", "s" ] |
| 810 |
} |
| 811 |
}, |
| 812 |
"properties": { |
| 813 |
"Prop1": { |
| 814 |
"flags": "rw", |
| 815 |
"type": "s" |
| 816 |
}, |
| 817 |
"Prop2": { |
| 818 |
"flags": "r", |
| 819 |
"type": "b" |
| 820 |
} |
| 821 |
} |
| 822 |
} |
| 823 |
} |
| 824 |
.... |
| 825 |
|
| 826 |
Multiple interfaces may be present, each of which may have methods and |
| 827 |
properties. This is emitted before the first |
| 828 |
link:#cockpit-dbus-proxy[`+client.onnotify+`] event for the relevant |
| 829 |
interface. |
| 830 |
|
| 831 |
[[cockpit-dbus-variant]] |
| 832 |
== cockpit.variant() |
| 833 |
|
| 834 |
.... |
| 835 |
variant = cockpit.variant(type, value) |
| 836 |
.... |
| 837 |
|
| 838 |
A DBus variant is represented as a plain javascript object with a |
| 839 |
`+"t"+` property represesting the full DBus type of the variant, and a |
| 840 |
`+"v"+` property containing the variant value. |
| 841 |
|
| 842 |
This is a helper function for creating such a variant object. |