-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.go
169 lines (148 loc) · 5.42 KB
/
context.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package dbus
import (
"context"
"errors"
"os"
)
func getCtx[T any](ctx context.Context, key any) (ret T, ok bool) {
v := ctx.Value(key)
if v == nil {
return ret, false
}
if ret, ok := v.(T); ok {
return ret, true
}
return ret, false
}
func withContextHeader(ctx context.Context, conn *Conn, hdr *header) context.Context {
if hdr.Sender != "" {
ctx = context.WithValue(ctx, senderContextKey{}, conn.Peer(hdr.Sender))
if hdr.Type == msgTypeSignal && hdr.Path != "" && hdr.Interface != "" {
ctx = context.WithValue(ctx, emitterContextKey{}, conn.Peer(hdr.Sender).Object(hdr.Path).Interface(hdr.Interface))
}
}
if hdr.Destination != "" {
ctx = context.WithValue(ctx, destContextKey{}, conn.Peer(hdr.Destination))
}
return ctx
}
func withContextEmitter(ctx context.Context, emitter Interface) context.Context {
return context.WithValue(ctx, emitterContextKey{}, emitter)
}
// emitterContextKey is the context key that carries the emitter of a
// DBus signal.
type emitterContextKey struct{}
// ContextEmitter returns the notification's emitter value from ctx,
// and reports whether an emitter was found.
//
// Emitter information is only available in the context of
// [Unmarshaler]'s UnmarshalDBus method, when unmarshaling a signal or
// property change type.
func ContextEmitter(ctx context.Context) (Interface, bool) {
return getCtx[Interface](ctx, emitterContextKey{})
}
// senderContextKey is the context key that carries the sender of a
// DBus message.
type senderContextKey struct{}
// ContextSender returns the sender found in ctx, and reports whether
// a sender was found.
//
// Sender information is available in the context of [Unmarshaler]'s
// UnmarshalDBus method.
func ContextSender(ctx context.Context) (Peer, bool) {
return getCtx[Peer](ctx, senderContextKey{})
}
// destContextKey is the context key that carries the destination of a
// DBus message.
type destContextKey struct{}
// ContextDestination returns the destination found in ctx, and
// reports whether a destination was found.
//
// Destination information is available in the context of
// [Marshaler]'s MarshalDBus method when sending method calls, and in
// the context of [Unmarshaler]'s UnmarshalDBus method when receiving
// method calls.
func ContextDestination(ctx context.Context) (Peer, bool) {
return getCtx[Peer](ctx, destContextKey{})
}
// filesContextKey is the context key that carries file descriptors
// received with a DBus message.
type filesContextKey struct{}
// withContextFiles augments ctx with message files.
func withContextFiles(ctx context.Context, files *[]*os.File) context.Context {
return context.WithValue(ctx, filesContextKey{}, files)
}
// contextFile returns the idx-th message file in ctx.
//
// [File] is the only consumer of this API, being the only way to
// interact with DBus file descriptors.
func contextFile(ctx context.Context, idx uint32) *os.File {
v := ctx.Value(filesContextKey{})
if v == nil {
return nil
}
fs, ok := v.(*[]*os.File)
if !ok {
return nil
}
if int(idx) >= len(*fs) {
return nil
}
return (*fs)[int(idx)]
}
// contextFile adds file to the context's outgoing files buffer.
//
// [File] is the only consumer of this API, being the only way to
// interact with DBus file descriptors.
func contextPutFile(ctx context.Context, file *os.File) (idx uint32, err error) {
v := ctx.Value(filesContextKey{})
if v == nil {
return 0, errors.New("cannot send file descriptor: invalid context")
}
fsp, ok := v.(*[]*os.File)
if !ok || fsp == nil {
return 0, errors.New("cannot send file descriptor: invalid context")
}
*fsp = append(*fsp, file)
return uint32(len(*fsp) - 1), nil
}
type allowInteractionContextKey struct{}
// WithContextUserInteraction returns a copy of the parent context
// with the DBus user interation policy set according to allow.
//
// Allowing user interaction prompts the user for permission to
// proceed when a call is made to a privileged method, instead of
// returning an access denied error immediately.
//
// Interaction is disabled by default because it can cause calls to
// block for an extended period of time, until the user responds to
// the authorization prompt, or indefinitely on servers where users
// aren't expected to be available for interactive prompting.
func WithContextUserInteraction(parent context.Context, allow bool) context.Context {
return context.WithValue(parent, allowInteractionContextKey{}, allow)
}
type blockAutostartContextKey struct{}
// WithContextAutostart returns a copy of the parent context with the
// DBus auto-start policy set according to allow.
//
// Services that provide DBus APIs can elect to be "bus
// activated". Bus activated peers are present on the bus even when
// their underlying service isn't running, and the bus arranges to
// start them seamlessly when something communicates with them.
//
// By default, method calls trigger bus activation, and clients don't
// need to be aware of this feature. If auto-starting services is
// undesirable, WithContextAutostart can be used to make calls fail
// with a [CallError] if they would trigger a bus activation.
func WithContextAutostart(ctx context.Context, allow bool) context.Context {
return context.WithValue(ctx, blockAutostartContextKey{}, !allow)
}
func contextCallFlags(ctx context.Context) (flags byte) {
if v, ok := ctx.Value(allowInteractionContextKey{}).(bool); ok && v {
flags |= 0x4
}
if v, ok := ctx.Value(blockAutostartContextKey{}).(bool); ok && v {
flags |= 0x2
}
return flags
}