/
ui_router.go
209 lines (183 loc) · 5.31 KB
/
ui_router.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package service
import (
"fmt"
"sync"
libkb "github.com/keybase/client/go/libkb"
keybase1 "github.com/keybase/client/go/protocol/keybase1"
"github.com/keybase/go-framed-msgpack-rpc/rpc"
context "golang.org/x/net/context"
)
type transporterAndConnectionID struct {
transporter rpc.Transporter
connectionID libkb.ConnectionID
}
type getObj struct {
ui libkb.UIKind
retCh chan<- transporterAndConnectionID
}
type uiWrapper struct {
cid libkb.ConnectionID
sessionID int
}
type setObj struct {
cid libkb.ConnectionID
ui libkb.UIKind
}
type UIRouter struct {
sync.Mutex
libkb.Contextified
cm *libkb.ConnectionManager
uis map[libkb.UIKind]libkb.ConnectionID
}
func NewUIRouter(g *libkb.GlobalContext) *UIRouter {
return &UIRouter{
Contextified: libkb.NewContextified(g),
cm: g.ConnectionManager,
uis: make(map[libkb.UIKind]libkb.ConnectionID),
}
}
func (u *UIRouter) Shutdown() {}
func (u *UIRouter) SetUI(c libkb.ConnectionID, k libkb.UIKind) {
u.Lock()
defer u.Unlock()
u.G().Log.Debug("UIRouter: connection %v registering UI %s [%p]", c, k, u)
u.uis[k] = c
}
func (u *UIRouter) getUI(k libkb.UIKind) (rpc.Transporter, libkb.ConnectionID) {
u.Lock()
defer u.Unlock()
var ret rpc.Transporter
cid, ok := u.uis[k]
if ok {
if ret = u.cm.LookupConnection(cid); ret == nil {
u.G().Log.Debug("UIRouter: connection %v inactive, deleting registered UI %s", cid, k)
delete(u.uis, k)
}
}
return ret, cid
}
func (u *UIRouter) GetIdentifyUI() (libkb.IdentifyUI, error) {
x, _ := u.getUI(libkb.IdentifyUIKind)
if x == nil {
return nil, nil
}
cli := rpc.NewClient(x, libkb.NewContextifiedErrorUnwrapper(u.G()), nil)
iuicli := keybase1.IdentifyUiClient{Cli: cli}
sessionID, err := iuicli.DelegateIdentifyUI(context.TODO())
if err != nil {
return nil, err
}
ret := &RemoteIdentifyUI{
sessionID: sessionID,
uicli: iuicli,
logUI: &LogUI{
sessionID,
&keybase1.LogUiClient{Cli: cli},
},
Contextified: libkb.NewContextified(u.G()),
}
return ret, nil
}
func (u *UIRouter) GetIdentifyUICtx(ctx context.Context) (int, libkb.IdentifyUI, error) {
x, _ := u.getUI(libkb.IdentifyUIKind)
if x == nil {
return 0, nil, nil
}
cli := rpc.NewClient(x, libkb.NewContextifiedErrorUnwrapper(u.G()), nil)
iuicli := keybase1.IdentifyUiClient{Cli: cli}
sessionID, err := iuicli.DelegateIdentifyUI(ctx)
if err != nil {
return 0, nil, err
}
ret := &RemoteIdentifyUI{
sessionID: sessionID,
uicli: iuicli,
logUI: &LogUI{
sessionID,
&keybase1.LogUiClient{Cli: cli},
},
Contextified: libkb.NewContextified(u.G()),
}
return sessionID, ret, nil
}
func (u *UIRouter) GetSecretUI(sessionID int) (ui libkb.SecretUI, err error) {
defer u.G().Trace("UIRouter#GetSecretUI", func() error { return err })()
x, _ := u.getUI(libkb.SecretUIKind)
if x == nil {
u.G().Log.Debug("| getUI(libkb.SecretUIKind) returned nil")
return nil, nil
}
cli := rpc.NewClient(x, libkb.NewContextifiedErrorUnwrapper(u.G()), nil)
scli := keybase1.SecretUiClient{Cli: cli}
u.G().Log.Debug("| returning delegated SecretUI with sessionID = %d", sessionID)
ret := &SecretUI{
cli: &scli,
sessionID: sessionID,
Contextified: libkb.NewContextified(u.G()),
}
return ret, nil
}
func (u *UIRouter) GetHomeUI() (keybase1.HomeUIInterface, error) {
var err error
defer u.G().Trace(fmt.Sprintf("UIRouter#GetHomeUI [%p]", u), func() error { return err })()
x, _ := u.getUI(libkb.HomeUIKind)
if x == nil {
u.G().Log.Debug("| getUI(libkb.HomeUIKind) returned nil")
return nil, nil
}
cli := rpc.NewClient(x, libkb.NewContextifiedErrorUnwrapper(u.G()), nil)
uicli := keybase1.HomeUIClient{Cli: cli}
return uicli, nil
}
func (u *UIRouter) GetRekeyUI() (keybase1.RekeyUIInterface, int, error) {
var err error
defer u.G().Trace("UIRouter#GetRekeyUI", func() error { return err })()
x, cid := u.getUI(libkb.RekeyUIKind)
if x == nil {
u.G().Log.Debug("| getUI(libkb.RekeyUIKind) returned nil")
return nil, 0, nil
}
cli := rpc.NewClient(x, libkb.NewContextifiedErrorUnwrapper(u.G()), nil)
uicli := keybase1.RekeyUIClient{Cli: cli}
sessionID, err := uicli.DelegateRekeyUI(context.TODO())
if err != nil {
return nil, 0, err
}
ret := &RekeyUI{
Contextified: libkb.NewContextified(u.G()),
sessionID: sessionID,
cli: &uicli,
connectionID: cid,
}
return ret, sessionID, nil
}
func (u *UIRouter) getOrReuseRekeyUI(prev *RekeyUI) (ret *RekeyUI, err error) {
defer u.G().Trace("UIRouter#GetOrReuseRekeyUI", func() error { return err })()
x, cid := u.getUI(libkb.RekeyUIKind)
if x == nil {
return nil, nil
}
if prev != nil && prev.connectionID == cid {
return prev, nil
}
cli := rpc.NewClient(x, libkb.NewContextifiedErrorUnwrapper(u.G()), nil)
uicli := keybase1.RekeyUIClient{Cli: cli}
var sessionID int
sessionID, err = uicli.DelegateRekeyUI(context.TODO())
if err != nil {
return nil, err
}
ret = &RekeyUI{
Contextified: libkb.NewContextified(u.G()),
sessionID: sessionID,
cli: &uicli,
connectionID: cid,
}
return ret, nil
}
func (u *UIRouter) GetRekeyUINoSessionID() (ret keybase1.RekeyUIInterface, err error) {
defer u.G().Trace("UIRouter#GetRekeyUINoSessionID", func() error { return err })()
return u.getOrReuseRekeyUI(nil)
}