From 002e7f80048f4041749f2af8a27f52e800c7a164 Mon Sep 17 00:00:00 2001 From: Roman Shevchenko Date: Sat, 28 Dec 2019 00:15:00 +0100 Subject: [PATCH] [platform] better `BrowserLauncher*` diagnostic (IDEA-227065) GitOrigin-RevId: c7b0401bc1e17e29017029f057b20f6cccd0ba4f --- .../src/com/intellij/ide/BrowserUtil.java | 4 - .../ide/browsers/BrowserLauncherAppless.kt | 83 ++++++++----------- .../ide/browsers/BrowserLauncherImpl.kt | 17 ++-- 3 files changed, 44 insertions(+), 60 deletions(-) diff --git a/platform/platform-api/src/com/intellij/ide/BrowserUtil.java b/platform/platform-api/src/com/intellij/ide/BrowserUtil.java index 820585ccf2d6..a4b438cd6ec1 100644 --- a/platform/platform-api/src/com/intellij/ide/BrowserUtil.java +++ b/platform/platform-api/src/com/intellij/ide/BrowserUtil.java @@ -102,10 +102,6 @@ public class BrowserUtil { } } - public static boolean isOpenCommandSupportArgs() { - return SystemInfo.isMacOSSnowLeopard; - } - @NotNull public static String getDefaultAlternativeBrowserPath() { if (SystemInfo.isWindows) { diff --git a/platform/platform-api/src/com/intellij/ide/browsers/BrowserLauncherAppless.kt b/platform/platform-api/src/com/intellij/ide/browsers/BrowserLauncherAppless.kt index 6dab33510186..49674eec1245 100644 --- a/platform/platform-api/src/com/intellij/ide/browsers/BrowserLauncherAppless.kt +++ b/platform/platform-api/src/com/intellij/ide/browsers/BrowserLauncherAppless.kt @@ -5,6 +5,7 @@ import com.intellij.CommonBundle import com.intellij.Patches import com.intellij.execution.ExecutionException import com.intellij.execution.configurations.GeneralCommandLine +import com.intellij.execution.process.CapturingProcessHandler import com.intellij.execution.util.ExecUtil import com.intellij.ide.BrowserUtil import com.intellij.ide.GeneralSettings @@ -14,10 +15,8 @@ import com.intellij.openapi.diagnostic.debug import com.intellij.openapi.diagnostic.logger import com.intellij.openapi.project.Project import com.intellij.openapi.util.SystemInfo -import com.intellij.openapi.util.text.StringUtil import com.intellij.openapi.vfs.StandardFileSystems import com.intellij.openapi.vfs.VfsUtil -import com.intellij.util.ArrayUtil import com.intellij.util.PathUtil import com.intellij.util.io.URLUtil import java.awt.Desktop @@ -46,8 +45,8 @@ open class BrowserLauncherAppless : BrowserLauncher() { openOrBrowse("${StandardFileSystems.FILE_PROTOCOL_PREFIX}$path", true) } - protected open fun openWithExplicitBrowser(url: String, settings: GeneralSettings, project: Project?) { - browseUsingPath(url, settings.browserPath, project = project) + protected open fun openWithExplicitBrowser(url: String, browserPath: String?, project: Project?) { + browseUsingPath(url, browserPath, project = project) } private fun openOrBrowse(_url: String, browse: Boolean, project: Project? = null) { @@ -92,7 +91,7 @@ open class BrowserLauncherAppless : BrowserLauncher() { openWithDefaultBrowser(url, project) } else { - openWithExplicitBrowser(url, settings, project = project) + openWithExplicitBrowser(url, settings.browserPath, project = project) } } @@ -122,7 +121,10 @@ open class BrowserLauncherAppless : BrowserLauncher() { showError(IdeBundle.message("browser.default.not.supported"), project = project) return } - doLaunch(url, command, null, project) + + if (url.startsWith("jar:")) return + + doLaunch(GeneralCommandLine(command).withParameters(url), project) } protected open fun signUrl(url: String): String = url @@ -144,6 +146,8 @@ open class BrowserLauncherAppless : BrowserLauncher() { project: Project?, openInNewWindow: Boolean, additionalParameters: Array): Boolean { + if (url != null && url.startsWith("jar:")) return false + val byName = browserPath == null && browser != null val effectivePath = if (byName) PathUtil.toSystemDependentName(browser!!.path) else browserPath val launchTask: (() -> Unit)? = if (byName) { -> browseUsingPath(url, null, browser!!, project, openInNewWindow, additionalParameters) } else null @@ -154,24 +158,12 @@ open class BrowserLauncherAppless : BrowserLauncher() { return false } - return doLaunch(url, BrowserUtil.getOpenBrowserCommand(effectivePath, openInNewWindow), browser, project, additionalParameters, launchTask) - } - - private fun doLaunch(url: String?, - command: List, - browser: WebBrowser?, - project: Project?, - additionalParameters: Array = ArrayUtil.EMPTY_STRING_ARRAY, - launchTask: (() -> Unit)? = null): Boolean { - if (url != null && url.startsWith("jar:")) { - return false - } - - val commandWithUrl = command.toMutableList() + val commandWithUrl = BrowserUtil.getOpenBrowserCommand(effectivePath, openInNewWindow).toMutableList() if (url != null) { if (browser != null) browser.addOpenUrlParameter(commandWithUrl, url) - else commandWithUrl.add(url) + else commandWithUrl += url } + val commandLine = GeneralCommandLine(commandWithUrl) val browserSpecificSettings = browser?.specificSettings @@ -179,23 +171,33 @@ open class BrowserLauncherAppless : BrowserLauncher() { commandLine.environment.putAll(browserSpecificSettings.environmentVariables) } - addArgs(commandLine, browserSpecificSettings, additionalParameters) + val specific = browserSpecificSettings?.additionalParameters ?: emptyList() + if (specific.size + additionalParameters.size > 0) { + if (isOpenCommandUsed(commandLine)) { + commandLine.addParameter("--args") + } + commandLine.addParameters(specific) + commandLine.addParameters(*additionalParameters) + } - return try { - checkCreatedProcess(browser, project, commandLine, commandLine.createProcess(), launchTask) + return doLaunch(commandLine, project, browser, launchTask) + } + + private fun doLaunch(command: GeneralCommandLine, project: Project?, browser: WebBrowser? = null, launchTask: (() -> Unit)? = null) = + try { + LOG.debug { command.commandLineString } + checkCreatedProcess(command, project, browser, launchTask) true } catch (e: ExecutionException) { showError(e.message, browser, project, null, null) false } - } - protected open fun checkCreatedProcess(browser: WebBrowser?, - project: Project?, - commandLine: GeneralCommandLine, - process: Process, - launchTask: (() -> Unit)?) { } + @Throws(ExecutionException::class) + protected open fun checkCreatedProcess(command: GeneralCommandLine, project: Project?, browser: WebBrowser?, launchTask: (() -> Unit)?) { + CapturingProcessHandler.Silent(command).runProcess(10000, false).checkSuccess(LOG) + } protected open fun showError(error: String?, browser: WebBrowser? = null, @@ -221,23 +223,4 @@ private val defaultBrowserCommand: List? SystemInfo.isMac -> listOf(ExecUtil.openCommandPath) SystemInfo.isUnix && SystemInfo.hasXdgOpen() -> listOf("xdg-open") else -> null - } - -private fun addArgs(command: GeneralCommandLine, settings: BrowserSpecificSettings?, additional: Array) { - val specific = settings?.additionalParameters ?: emptyList() - if (specific.size + additional.size > 0) { - if (BrowserLauncherAppless.isOpenCommandUsed(command)) { - if (BrowserUtil.isOpenCommandSupportArgs()) { - command.addParameter("--args") - } - else { - LOG.warn("'open' command doesn't allow passing command-line arguments, so they will be ignored: " + - StringUtil.join(specific, ", ") + " " + additional.contentToString()) - return - } - } - - command.addParameters(specific) - command.addParameters(*additional) - } -} \ No newline at end of file + } \ No newline at end of file diff --git a/xml/impl/src/com/intellij/ide/browsers/BrowserLauncherImpl.kt b/xml/impl/src/com/intellij/ide/browsers/BrowserLauncherImpl.kt index 96e861852a4f..7e95c82e5d81 100644 --- a/xml/impl/src/com/intellij/ide/browsers/BrowserLauncherImpl.kt +++ b/xml/impl/src/com/intellij/ide/browsers/BrowserLauncherImpl.kt @@ -2,9 +2,9 @@ package com.intellij.ide.browsers import com.intellij.concurrency.JobScheduler +import com.intellij.execution.ExecutionException import com.intellij.execution.configurations.GeneralCommandLine import com.intellij.execution.util.ExecUtil -import com.intellij.ide.GeneralSettings import com.intellij.ide.IdeBundle import com.intellij.ide.util.PropertiesComponent import com.intellij.openapi.application.ApplicationManager @@ -48,7 +48,7 @@ class BrowserLauncherImpl : BrowserLauncherAppless() { return url } - override fun openWithExplicitBrowser(url: String, settings: GeneralSettings, project: Project?) { + override fun openWithExplicitBrowser(url: String, browserPath: String?, project: Project?) { val browserManager = WebBrowserManager.getInstance() if (browserManager.getDefaultBrowserPolicy() == DefaultBrowserPolicy.FIRST) { browserManager.firstActiveBrowser?.let { @@ -56,14 +56,14 @@ class BrowserLauncherImpl : BrowserLauncherAppless() { return } } - else if (SystemInfo.isMac && "open" == settings.browserPath) { + else if (SystemInfo.isMac && "open" == browserPath) { browserManager.firstActiveBrowser?.let { browseUsingPath(url, null, it, project) return } } - super.openWithExplicitBrowser(url, settings, project) + super.openWithExplicitBrowser(url, browserPath, project) } override fun showError(error: String?, browser: WebBrowser?, project: Project?, title: String?, launchTask: (() -> Unit)?) { @@ -79,8 +79,10 @@ class BrowserLauncherImpl : BrowserLauncherAppless() { }, project?.disposed) } - override fun checkCreatedProcess(browser: WebBrowser?, project: Project?, commandLine: GeneralCommandLine, process: Process, launchTask: (() -> Unit)?) { - if (isOpenCommandUsed(commandLine)) { + @Throws(ExecutionException::class) + override fun checkCreatedProcess(command: GeneralCommandLine, project: Project?, browser: WebBrowser?, launchTask: (() -> Unit)?) { + if (isOpenCommandUsed(command)) { + val process = command.createProcess() val future = ApplicationManager.getApplication().executeOnPooledThread { try { if (process.waitFor() == 1) { @@ -93,5 +95,8 @@ class BrowserLauncherImpl : BrowserLauncherAppless() { // 10 seconds is enough to start JobScheduler.getScheduler().schedule({ future.cancel(true) }, 10, TimeUnit.SECONDS) } + else { + super.checkCreatedProcess(command, project, browser, launchTask) + } } } \ No newline at end of file