@@ -31,6 +31,13 @@ class UserDecisionRegistry private constructor(
31
31
32
32
}
33
33
34
+ /* *
35
+ * Per-certificate map of pending decisions, which are [Continuation]s that are
36
+ * - resumed when the callback returns a decision and
37
+ * - cancelled when the scope is cancelled.
38
+ *
39
+ * Every call of [check] adds an entry to the [Continuation] list.
40
+ */
34
41
internal val pendingDecisions = mutableMapOf<X509Certificate , MutableList <Continuation <Boolean >>>()
35
42
36
43
/* *
@@ -54,27 +61,28 @@ class UserDecisionRegistry private constructor(
54
61
}
55
62
}
56
63
57
- val requestDecision: Boolean
58
64
synchronized(pendingDecisions) {
59
65
if (pendingDecisions.containsKey(cert)) {
60
66
// There are already pending decisions for this request, just add our request
61
67
pendingDecisions[cert]!! + = cont
62
- requestDecision = false
68
+
63
69
} else {
64
- // First decision for this certificate, show UI
70
+ // First decision for this certificate, add to map and show UI
65
71
pendingDecisions[cert] = mutableListOf (cont)
66
- requestDecision = true
72
+
73
+ scope.launch {
74
+ val userDecision = getUserDecision(cert) // suspends until user decision is made
75
+
76
+ // resume all coroutines that are waiting for a decision
77
+ resumeOnUserDecision(cert, userDecision)
78
+ }
67
79
}
68
80
}
69
81
70
- if (requestDecision)
71
- scope.launch {
72
- val userDecision = getUserDecision(cert) // Suspends until user decision is made
73
- onUserDecision(cert, userDecision)
74
- }
82
+ // Now the coroutine is suspended, and will be resumed when the user has made a decision using cont.resume()
75
83
}
76
84
77
- fun onUserDecision (cert : X509Certificate , trusted : Boolean ) {
85
+ fun resumeOnUserDecision (cert : X509Certificate , trusted : Boolean ) {
78
86
// save decision
79
87
val customCertStore = CustomCertStore .getInstance(context)
80
88
if (trusted)
0 commit comments