Skip to content

Commit 47b9aca

Browse files
committed
Minor changes in structure, comments
1 parent 106c60a commit 47b9aca

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

lib/src/main/java/at/bitfire/cert4android/UserDecisionRegistry.kt

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ class UserDecisionRegistry private constructor(
3131

3232
}
3333

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+
*/
3441
internal val pendingDecisions = mutableMapOf<X509Certificate, MutableList<Continuation<Boolean>>>()
3542

3643
/**
@@ -54,27 +61,28 @@ class UserDecisionRegistry private constructor(
5461
}
5562
}
5663

57-
val requestDecision: Boolean
5864
synchronized(pendingDecisions) {
5965
if (pendingDecisions.containsKey(cert)) {
6066
// There are already pending decisions for this request, just add our request
6167
pendingDecisions[cert]!! += cont
62-
requestDecision = false
68+
6369
} else {
64-
// First decision for this certificate, show UI
70+
// First decision for this certificate, add to map and show UI
6571
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+
}
6779
}
6880
}
6981

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()
7583
}
7684

77-
fun onUserDecision(cert: X509Certificate, trusted: Boolean) {
85+
fun resumeOnUserDecision(cert: X509Certificate, trusted: Boolean) {
7886
// save decision
7987
val customCertStore = CustomCertStore.getInstance(context)
8088
if (trusted)

0 commit comments

Comments
 (0)