From cb3a7c04f3d6dde4f0a1654c88e0fe71e1db7e1e Mon Sep 17 00:00:00 2001 From: Vladimir Krivosheev Date: Tue, 16 May 2017 14:01:59 +0200 Subject: [PATCH] =?UTF-8?q?sign=20requests=20to=20IDE=20=E2=80=94=20fix=20?= =?UTF-8?q?API=20browse(URI)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../intellij/ide/browsers/BrowserLauncher.kt | 4 +- .../ide/browsers/BrowserLauncherAppless.kt | 91 +++++++++---------- .../ide/browsers/BrowserLauncherImpl.kt | 18 ++-- 3 files changed, 51 insertions(+), 62 deletions(-) diff --git a/platform/platform-api/src/com/intellij/ide/browsers/BrowserLauncher.kt b/platform/platform-api/src/com/intellij/ide/browsers/BrowserLauncher.kt index 4fb6efe7a7f5..43df34e95703 100644 --- a/platform/platform-api/src/com/intellij/ide/browsers/BrowserLauncher.kt +++ b/platform/platform-api/src/com/intellij/ide/browsers/BrowserLauncher.kt @@ -33,7 +33,9 @@ abstract class BrowserLauncher { fun browse(uri: URI) = browse(uri, null) - abstract fun browse(uri: URI, project: Project?) + fun browse(uri: URI, project: Project?) { + browse(uri.toString(), project = project) + } abstract fun browse(file: File) 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 09966e8f1a6a..952668bfcbbb 100644 --- a/platform/platform-api/src/com/intellij/ide/browsers/BrowserLauncherAppless.kt +++ b/platform/platform-api/src/com/intellij/ide/browsers/BrowserLauncherAppless.kt @@ -37,7 +37,6 @@ import org.jetbrains.annotations.Contract import java.awt.Desktop import java.io.File import java.io.IOException -import java.net.URI import java.util.* open class BrowserLauncherAppless : BrowserLauncher() { @@ -104,58 +103,24 @@ open class BrowserLauncherAppless : BrowserLauncher() { fun isOpenCommandUsed(command: GeneralCommandLine) = SystemInfo.isMac && ExecUtil.getOpenCommandPath() == command.exePath } - override fun open(url: String) = openOrBrowse(url, false, null) + override fun open(url: String) = openOrBrowse(url, false) override fun browse(file: File) { var path = file.absolutePath if (SystemInfo.isWindows && path[0] != '/') { path = '/' + path } - browse("${StandardFileSystems.FILE_PROTOCOL_PREFIX}$path") + openOrBrowse("${StandardFileSystems.FILE_PROTOCOL_PREFIX}$path", true) } - override fun browse(uri: URI, project: Project?) { - LOG.debug("Launch browser: [$uri]") - - val settings = generalSettings - if (settings.isUseDefaultBrowser) { - var tryToUseCli = true - if (isDesktopActionSupported(Desktop.Action.BROWSE)) { - try { - Desktop.getDesktop().browse(uri) - LOG.debug("Browser launched using JDK 1.6 API") - return - } - catch (e: Exception) { - LOG.warn("Error while using Desktop API, fallback to CLI", e) - // if "No application knows how to open", then we must not try to use OS open - tryToUseCli = !e.message!!.contains("Error code: -10814") - } - } - - if (tryToUseCli) { - defaultBrowserCommand?.let { - doLaunch(uri.toString(), it, null, project) - return - } - } - } - - browseUsingNotSystemDefaultBrowserPolicy(uri, settings, project) + protected open fun browseUsingNotSystemDefaultBrowserPolicy(url: String, settings: GeneralSettings, project: Project?) { + browseUsingPath(url, settings.browserPath, project = project) } - protected open fun browseUsingNotSystemDefaultBrowserPolicy(uri: URI, settings: GeneralSettings, project: Project?) { - browseUsingPath(uri.toString(), settings.browserPath, project = project) - } + private fun openOrBrowse(_url: String, browse: Boolean, project: Project? = null) { + val url = signUrl(_url.trim { it <= ' ' }) - private fun openOrBrowse(_url: String, browse: Boolean, project: Project?) { - val url = _url.trim { it <= ' ' } - - val uri: URI? - if (BrowserUtil.isAbsoluteURL(url)) { - uri = VfsUtil.toUri(url) - } - else { + if (!BrowserUtil.isAbsoluteURL(url)) { val file = File(url) if (!browse && isDesktopActionSupported(Desktop.Action.OPEN)) { if (!file.exists()) { @@ -176,22 +141,50 @@ open class BrowserLauncherAppless : BrowserLauncher() { return } - if (uri == null) { - showError(IdeBundle.message("error.malformed.url", url), project = project) - } - else { - browse(uri, project) + LOG.debug("Launch browser: [$url]") + val settings = generalSettings + if (settings.isUseDefaultBrowser) { + val uri = VfsUtil.toUri(url) + if (uri == null) { + showError(IdeBundle.message("error.malformed.url", url), project = project) + return + } + + var tryToUseCli = true + if (isDesktopActionSupported(Desktop.Action.BROWSE)) { + try { + Desktop.getDesktop().browse(uri) + LOG.debug("Browser launched using JDK 1.6 API") + return + } + catch (e: Exception) { + LOG.warn("Error while using Desktop API, fallback to CLI", e) + // if "No application knows how to open", then we must not try to use OS open + tryToUseCli = !e.message!!.contains("Error code: -10814") + } + } + + if (tryToUseCli) { + defaultBrowserCommand?.let { + doLaunch(url, it, null, project) + return + } + } } + + browseUsingNotSystemDefaultBrowserPolicy(url, settings, project = project) } - override fun browse(url: String, browser: WebBrowser?, project: Project?) { + open protected fun signUrl(url: String): String = url + + override final fun browse(url: String, browser: WebBrowser?, project: Project?) { val effectiveBrowser = getEffectiveBrowser(browser) // if browser is not passed, UrlOpener should be not used for non-http(s) urls if (effectiveBrowser == null || (browser == null && !url.startsWith(URLUtil.HTTP_PROTOCOL))) { openOrBrowse(url, true, project) } else { - UrlOpener.EP_NAME.extensions.any { it.openUrl(effectiveBrowser, url, project) } + UrlOpener.EP_NAME.extensions.any { it.openUrl(effectiveBrowser, signUrl(url), project) } } } diff --git a/xml/impl/src/com/intellij/ide/browsers/BrowserLauncherImpl.kt b/xml/impl/src/com/intellij/ide/browsers/BrowserLauncherImpl.kt index 3863404e000d..c45619bbc073 100644 --- a/xml/impl/src/com/intellij/ide/browsers/BrowserLauncherImpl.kt +++ b/xml/impl/src/com/intellij/ide/browsers/BrowserLauncherImpl.kt @@ -28,17 +28,11 @@ import com.intellij.openapi.ui.showOkNoDialog import com.intellij.openapi.util.SystemInfo import com.intellij.openapi.util.registry.Registry import com.intellij.ui.AppUIUtil -import com.intellij.util.ArrayUtil import com.intellij.util.Urls import org.jetbrains.ide.BuiltInServerManager -import java.net.URI import java.util.concurrent.TimeUnit class BrowserLauncherImpl : BrowserLauncherAppless() { - override fun browse(url: String, browser: WebBrowser?, project: Project?) { - super.browse(signUrl(url), browser, project) - } - override fun getEffectiveBrowser(browser: WebBrowser?): WebBrowser? { var effectiveBrowser = browser if (browser == null) { @@ -51,7 +45,7 @@ class BrowserLauncherImpl : BrowserLauncherAppless() { return effectiveBrowser } - private fun signUrl(url: String): String { + override fun signUrl(url: String): String { @Suppress("NAME_SHADOWING") var url = url @Suppress("NAME_SHADOWING") @@ -67,22 +61,22 @@ class BrowserLauncherImpl : BrowserLauncherAppless() { return url } - override fun browseUsingNotSystemDefaultBrowserPolicy(uri: URI, settings: GeneralSettings, project: Project?) { + override fun browseUsingNotSystemDefaultBrowserPolicy(url: String, settings: GeneralSettings, project: Project?) { val browserManager = WebBrowserManager.getInstance() - if (browserManager.getDefaultBrowserPolicy() == DefaultBrowserPolicy.FIRST || "open" == settings.browserPath) { + if (browserManager.getDefaultBrowserPolicy() == DefaultBrowserPolicy.FIRST) { browserManager.firstActiveBrowser?.let { - browse(uri.toString(), it, project) + browse(url, it, project) return } } else if (SystemInfo.isMac && "open" == settings.browserPath) { browserManager.firstActiveBrowser?.let { - browseUsingPath(uri.toString(), null, it, project, ArrayUtil.EMPTY_STRING_ARRAY) + browseUsingPath(url, null, it, project) return } } - super.browseUsingNotSystemDefaultBrowserPolicy(uri, settings, project) + super.browseUsingNotSystemDefaultBrowserPolicy(url, settings, project) } override fun showError(error: String?, browser: WebBrowser?, project: Project?, title: String?, launchTask: (() -> Unit)?) {