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