sign requests to IDE — fix API browse(URI)

This commit is contained in:
Vladimir Krivosheev
2017-05-16 14:17:35 +02:00
parent 1a8aaf1825
commit cb3a7c04f3
3 changed files with 51 additions and 62 deletions
@@ -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)
@@ -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) }
}
}
@@ -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)?) {