Skip to content

Commit 36d5a60

Browse files
preset JS_QUERY_POOL_SIZE to reserve JS query pool (aws#4385)
* preset JS_QUERY_POOL_SIZE to reserve JS query pool * revert dedupe * lint * more lint --------- Co-authored-by: manodnyab <[email protected]>
1 parent 7c924e3 commit 36d5a60

File tree

4 files changed

+40
-27
lines changed

4 files changed

+40
-27
lines changed

plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/QLoginWebview.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,10 @@ class QWebviewBrowser(val project: Project, private val parentDisposable: Dispos
102102
) {
103103
// TODO: confirm if we need such configuration or the default is fine
104104
override val jcefBrowser = createBrowser(parentDisposable)
105-
override val query = JBCefJSQuery.create(jcefBrowser)
105+
private val query = JBCefJSQuery.create(jcefBrowser)
106106
private val objectMapper = jacksonObjectMapper()
107107

108-
override val handler = Function<String, JBCefJSQuery.Response> {
108+
private val handler = Function<String, JBCefJSQuery.Response> {
109109
val jsonTree = objectMapper.readTree(it)
110110
val command = jsonTree.get("command").asText()
111111
LOG.debug { "Data received from Q browser: ${jsonTree.asText()}" }
@@ -185,7 +185,7 @@ class QWebviewBrowser(val project: Project, private val parentDisposable: Dispos
185185
),
186186
)
187187

188-
loadWebView()
188+
loadWebView(query)
189189

190190
query.addHandler(handler)
191191
}
@@ -252,6 +252,10 @@ class QWebviewBrowser(val project: Project, private val parentDisposable: Dispos
252252
return
253253
}
254254

255+
override fun loadWebView(query: JBCefJSQuery) {
256+
jcefBrowser.loadHTML(getWebviewHTML(webScriptUri, query))
257+
}
258+
255259
companion object {
256260
private val LOG = getLogger<QWebviewBrowser>()
257261
private const val WEB_SCRIPT_URI = "http://webview/js/getStart.js"

plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/util/JcefBrowserUtil.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@ import com.intellij.openapi.util.Disposer
88
import com.intellij.ui.jcef.JBCefApp
99
import com.intellij.ui.jcef.JBCefBrowserBase
1010
import com.intellij.ui.jcef.JBCefBrowserBuilder
11+
import com.intellij.ui.jcef.JBCefClient
1112

1213
fun createBrowser(parent: Disposable): JBCefBrowserBase {
13-
val client = JBCefApp.getInstance().createClient()
14+
val client = JBCefApp.getInstance().createClient().apply {
15+
setProperty(JBCefClient.Properties.JS_QUERY_POOL_SIZE, 5)
16+
}
1417

1518
Disposer.register(parent, client)
1619

plugins/core/jetbrains-community/src/software/aws/toolkits/jetbrains/core/webview/LoginBrowser.kt

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import software.aws.toolkits.jetbrains.utils.pollFor
2525
import software.aws.toolkits.resources.message
2626
import software.aws.toolkits.telemetry.FeatureId
2727
import java.util.concurrent.Future
28-
import java.util.function.Function
2928

3029
data class BrowserState(val feature: FeatureId, val browserCancellable: Boolean = false, val requireReauth: Boolean = false)
3130

@@ -35,12 +34,11 @@ abstract class LoginBrowser(
3534
val webScriptUri: String
3635
) {
3736
abstract val jcefBrowser: JBCefBrowserBase
38-
abstract val query: JBCefJSQuery
39-
abstract val handler: Function<String, JBCefJSQuery.Response>
4037

4138
protected var currentAuthorization: PendingAuthorization? = null
4239

4340
protected data class BearerConnectionSelectionSettings(val currentSelection: AwsBearerTokenConnection, val onChange: (AwsBearerTokenConnection) -> Unit)
41+
4442
protected val selectionSettings = mutableMapOf<String, BearerConnectionSelectionSettings>()
4543

4644
protected val onPendingToken: (InteractiveBearerTokenProvider) -> Unit = { provider ->
@@ -104,7 +102,7 @@ abstract class LoginBrowser(
104102
}
105103
}
106104

107-
protected fun<T> loginWithBackgroundContext(action: () -> T): Future<T> =
105+
protected fun <T> loginWithBackgroundContext(action: () -> T): Future<T> =
108106
ApplicationManager.getApplication().executeOnPooledThread<T> {
109107
runBlocking {
110108
withBackgroundProgress(project, message("credentials.pending.title")) {
@@ -115,6 +113,12 @@ abstract class LoginBrowser(
115113
}
116114
}
117115

116+
abstract fun loadWebView(query: JBCefJSQuery)
117+
118+
protected suspend fun pollForAuthorization(provider: InteractiveBearerTokenProvider): PendingAuthorization? = pollFor {
119+
provider.pendingAuthorization
120+
}
121+
118122
protected fun reauth(connection: ToolkitConnection?) {
119123
if (connection is AwsBearerTokenConnection) {
120124
loginWithBackgroundContext {
@@ -123,15 +127,12 @@ abstract class LoginBrowser(
123127
}
124128
}
125129

126-
protected fun loadWebView() {
127-
jcefBrowser.loadHTML(getWebviewHTML())
128-
}
130+
companion object {
131+
fun getWebviewHTML(webScriptUri: String, query: JBCefJSQuery): String {
132+
val colorMode = if (JBColor.isBright()) "jb-light" else "jb-dark"
133+
val postMessageToJavaJsCode = query.inject("JSON.stringify(message)")
129134

130-
protected fun getWebviewHTML(): String {
131-
val colorMode = if (JBColor.isBright()) "jb-light" else "jb-dark"
132-
val postMessageToJavaJsCode = query.inject("JSON.stringify(message)")
133-
134-
val jsScripts = """
135+
val jsScripts = """
135136
<script>
136137
(function() {
137138
window.ideApi = {
@@ -142,9 +143,9 @@ abstract class LoginBrowser(
142143
}())
143144
</script>
144145
<script type="text/javascript" src="$webScriptUri"></script>
145-
""".trimIndent()
146+
""".trimIndent()
146147

147-
return """
148+
return """
148149
<!DOCTYPE html>
149150
<html>
150151
<head>
@@ -155,10 +156,7 @@ abstract class LoginBrowser(
155156
$jsScripts
156157
</body>
157158
</html>
158-
""".trimIndent()
159-
}
160-
161-
protected suspend fun pollForAuthorization(provider: InteractiveBearerTokenProvider): PendingAuthorization? = pollFor {
162-
provider.pendingAuthorization
159+
""".trimIndent()
160+
}
163161
}
164162
}

plugins/toolkit/jetbrains-core/src/software/aws/toolkits/jetbrains/core/explorer/webview/ToolkitLoginWebview.kt

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import com.intellij.ui.dsl.gridLayout.VerticalAlign
2323
import com.intellij.ui.jcef.JBCefApp
2424
import com.intellij.ui.jcef.JBCefBrowserBase
2525
import com.intellij.ui.jcef.JBCefBrowserBuilder
26+
import com.intellij.ui.jcef.JBCefClient
2627
import com.intellij.ui.jcef.JBCefJSQuery
2728
import kotlinx.coroutines.CoroutineScope
2829
import kotlinx.coroutines.channels.awaitClose
@@ -145,18 +146,21 @@ class ToolkitWebviewBrowser(val project: Project, private val parentDisposable:
145146
ToolkitWebviewBrowser.WEB_SCRIPT_URI
146147
) {
147148
// TODO: confirm if we need such configuration or the default is fine
149+
// TODO: move JcefBrowserUtils to core
148150
override val jcefBrowser: JBCefBrowserBase by lazy {
149-
val client = JBCefApp.getInstance().createClient()
151+
val client = JBCefApp.getInstance().createClient().apply {
152+
setProperty(JBCefClient.Properties.JS_QUERY_POOL_SIZE, 5)
153+
}
150154
Disposer.register(parentDisposable, client)
151155
JBCefBrowserBuilder()
152156
.setClient(client)
153157
.setOffScreenRendering(true)
154158
.build()
155159
}
156-
override val query: JBCefJSQuery = JBCefJSQuery.create(jcefBrowser)
160+
private val query: JBCefJSQuery = JBCefJSQuery.create(jcefBrowser)
157161
private val objectMapper = jacksonObjectMapper()
158162

159-
override val handler = Function<String, JBCefJSQuery.Response> {
163+
private val handler = Function<String, JBCefJSQuery.Response> {
160164
val jsonTree = objectMapper.readTree(it)
161165
val command = jsonTree.get("command").asText()
162166
LOG.debug { "Data received from Toolkit browser: ${jsonTree.toPrettyString()}" }
@@ -246,7 +250,7 @@ class ToolkitWebviewBrowser(val project: Project, private val parentDisposable:
246250
),
247251
)
248252

249-
loadWebView()
253+
loadWebView(query)
250254

251255
query.addHandler(handler)
252256
}
@@ -335,6 +339,10 @@ class ToolkitWebviewBrowser(val project: Project, private val parentDisposable:
335339
}
336340
}
337341

342+
override fun loadWebView(query: JBCefJSQuery) {
343+
jcefBrowser.loadHTML(getWebviewHTML(webScriptUri, query))
344+
}
345+
338346
fun component(): JComponent? = jcefBrowser.component
339347

340348
companion object {

0 commit comments

Comments
 (0)