@@ -220,6 +220,10 @@ class QWebviewBrowser(val project: Project, private val parentDisposable: Dispos
220
220
)
221
221
}
222
222
223
+ is BrowserMessage .ListProfiles -> {
224
+ handleListProfilesMessage()
225
+ }
226
+
223
227
is BrowserMessage .PublishWebviewTelemetry -> {
224
228
publishTelemetry(message)
225
229
}
@@ -262,60 +266,41 @@ class QWebviewBrowser(val project: Project, private val parentDisposable: Dispos
262
266
writeValueAsString(it)
263
267
}
264
268
265
- // TODO: pass "REAUTH" if connection expires
266
- // Perform the potentially blocking AWS call outside the EDT to fetch available region profiles.
267
- ApplicationManager .getApplication().executeOnPooledThread {
268
- val stage = if (isQExpired(project)) {
269
- " REAUTH"
270
- } else if (isQConnected(project) && QRegionProfileManager .getInstance().isPendingProfileSelection(project)) {
271
- " PROFILE_SELECT"
272
- } else {
273
- " START"
274
- }
275
-
276
- var errorMessage: String? = null
277
- var profiles: List <QRegionProfile > = emptyList()
269
+ val stage = if (isQExpired(project)) {
270
+ " REAUTH"
271
+ } else if (isQConnected(project) && QRegionProfileManager .getInstance().isPendingProfileSelection(project)) {
272
+ " PROFILE_SELECT"
273
+ } else {
274
+ " START"
275
+ }
278
276
279
- if (stage == " PROFILE_SELECT" ) {
280
- try {
281
- profiles = QRegionProfileManager .getInstance().listRegionProfiles(project).orEmpty()
282
- if (profiles.size == 1 ) {
283
- LOG .debug { " User only have access to 1 Q profile, auto-selecting profile ${profiles.first().profileName} for ${project.name} " }
284
- QRegionProfileManager .getInstance().switchProfile(project, profiles.first(), QProfileSwitchIntent .Update )
285
- }
286
- } catch (e: Exception ) {
287
- errorMessage = e.message
288
- LOG .warn { " Failed to call listRegionProfiles API" }
289
- val qConn = ToolkitConnectionManager .getInstance(project).activeConnectionForFeature(QConnection .getInstance())
290
- Telemetry .amazonq.didSelectProfile.use { span ->
291
- span.source(QProfileSwitchIntent .Auth .value)
292
- .amazonQProfileRegion(QRegionProfileManager .getInstance().activeProfile(project)?.region ? : " not-set" )
293
- .ssoRegion((qConn as ? AwsBearerTokenConnection )?.region)
294
- .credentialStartUrl((qConn as ? AwsBearerTokenConnection )?.startUrl)
295
- .result(MetricResult .Failed )
296
- .reason(e.message)
277
+ when (stage) {
278
+ " PROFILE_SELECT" -> {
279
+ val jsonData = """
280
+ {
281
+ stage: '$stage ',
282
+ status: 'pending'
297
283
}
298
- }
284
+ """ .trimIndent()
285
+ executeJS(" window.ideClient.prepareUi($jsonData )" )
299
286
}
300
287
301
- val jsonData = """
302
- {
303
- stage: '$stage ',
304
- regions: $regions ,
305
- idcInfo: {
306
- profileName: '${lastLoginIdcInfo.profileName} ',
307
- startUrl: '${lastLoginIdcInfo.startUrl} ',
308
- region: '${lastLoginIdcInfo.region} '
309
- },
310
- cancellable: ${state.browserCancellable} ,
311
- feature: '${state.feature} ',
312
- existConnections: ${writeValueAsString(selectionSettings.values.map { it.currentSelection }.toList())} ,
313
- profiles: ${writeValueAsString(profiles)} ,
314
- errorMessage: ${errorMessage?.let { " \" $it \" " } ? : " null" }
315
- }
316
- """ .trimIndent()
288
+ else -> {
289
+ val jsonData = """
290
+ {
291
+ stage: '$stage ',
292
+ regions: $regions ,
293
+ idcInfo: {
294
+ profileName: '${lastLoginIdcInfo.profileName} ',
295
+ startUrl: '${lastLoginIdcInfo.startUrl} ',
296
+ region: '${lastLoginIdcInfo.region} '
297
+ },
298
+ cancellable: ${state.browserCancellable} ,
299
+ feature: '${state.feature} ',
300
+ existConnections: ${writeValueAsString(selectionSettings.values.map { it.currentSelection }.toList())} ,
301
+ }
302
+ """ .trimIndent()
317
303
318
- runInEdt {
319
304
executeJS(" window.ideClient.prepareUi($jsonData )" )
320
305
}
321
306
}
@@ -330,6 +315,52 @@ class QWebviewBrowser(val project: Project, private val parentDisposable: Dispos
330
315
jcefBrowser.loadHTML(getWebviewHTML(webScriptUri, query))
331
316
}
332
317
318
+ private fun handleListProfilesMessage () {
319
+ ApplicationManager .getApplication().executeOnPooledThread {
320
+ var errorMessage = " "
321
+ val profiles = try {
322
+ QRegionProfileManager .getInstance().listRegionProfiles(project)
323
+ } catch (e: Exception ) {
324
+ e.message?.let {
325
+ errorMessage = it
326
+ }
327
+ LOG .warn { " Failed to call listRegionProfiles API: $errorMessage " }
328
+ val qConn = ToolkitConnectionManager .getInstance(project).activeConnectionForFeature(QConnection .getInstance())
329
+ Telemetry .amazonq.didSelectProfile.use { span ->
330
+ span.source(QProfileSwitchIntent .Auth .value)
331
+ .amazonQProfileRegion(QRegionProfileManager .getInstance().activeProfile(project)?.region ? : " not-set" )
332
+ .ssoRegion((qConn as ? AwsBearerTokenConnection )?.region)
333
+ .credentialStartUrl((qConn as ? AwsBearerTokenConnection )?.startUrl)
334
+ .result(MetricResult .Failed )
335
+ .reason(e.message)
336
+ }
337
+
338
+ null
339
+ }
340
+
341
+ // auto-select the profile if users only have 1 and don't show the UI
342
+ if (profiles?.size == 1 ) {
343
+ LOG .debug { " User only have access to 1 Q profile, auto-selecting profile ${profiles.first().profileName} for ${project.name} " }
344
+ QRegionProfileManager .getInstance().switchProfile(project, profiles.first(), QProfileSwitchIntent .Update )
345
+ return @executeOnPooledThread
346
+ }
347
+
348
+ // required EDT as this entire block is executed on thread pool
349
+ runInEdt {
350
+ val jsonData = """
351
+ {
352
+ stage: 'PROFILE_SELECT',
353
+ status: '${if (profiles != null ) " succeeded" else " failed" } ',
354
+ profiles: ${writeValueAsString(profiles ? : " " )} ,
355
+ errorMessage: '$errorMessage '
356
+ }
357
+ """ .trimIndent()
358
+
359
+ executeJS(" window.ideClient.prepareUi($jsonData )" )
360
+ }
361
+ }
362
+ }
363
+
333
364
companion object {
334
365
private val LOG = getLogger<QWebviewBrowser >()
335
366
private const val WEB_SCRIPT_URI = " http://webview/js/getStart.js"
0 commit comments