Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,14 +222,27 @@ Commands:
state.webSettings.customUserAgentString = "MyApp/1.2.3"
```

Desktop note:
Desktop note on user-agent:

* applied at creation time
* changing it **recreates** the WebView (debounced)
* JS context/history may be lost
* JS context/history may be lostd

👉 Set it early.

### Proxy (desktop only)

```kotlin
// HTTP CONNECT Proxy
state.webSettings.desktopWebSettings.proxyConfig = ProxyConfig.Http("proxy.tld", 8888)
// SOCKS5 Proxy
state.webSettings.desktopWebSettings.proxyConfig = ProxyConfig.Socks5("proxy.tld", 1080)
```

Proxy is only supported on Windows, macOS 14.0+ and Linux.

**NOTE:** You need to set the proxy **before** the WebView gets created.

---

### Logging
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -453,4 +453,4 @@ private fun inlineHtml(): String =
</script>
</body>
</html>
""".trimIndent()
""".trimIndent()
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ sealed class PlatformWebSettings {
var incognito: Boolean = false,
var autoplayWithoutUserInteraction: Boolean = false,
var focused: Boolean = true,
var proxyConfig: ProxyConfig? = null,
) : PlatformWebSettings()

data class IOSWebSettings(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.github.kdroidfilter.webview.setting

sealed class ProxyConfig {
abstract val host: String
abstract val port: Int

data class Http(
override val host: String,
override val port: Int
) : ProxyConfig()

data class Socks5(
override val host: String,
override val port: Int
) : ProxyConfig()

override fun toString() = "$host:$port"
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
package io.github.kdroidfilter.webview.web

import androidx.compose.runtime.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.key
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.awt.SwingPanel
import androidx.compose.ui.graphics.Color
Expand All @@ -10,6 +19,7 @@ import io.github.kdroidfilter.webview.jsbridge.WebViewJsBridge
import io.github.kdroidfilter.webview.jsbridge.parseJsMessage
import io.github.kdroidfilter.webview.request.WebRequest
import io.github.kdroidfilter.webview.request.WebRequestInterceptResult
import io.github.kdroidfilter.webview.setting.ProxyConfig
import io.github.kdroidfilter.webview.wry.Rgba
import kotlinx.coroutines.delay

Expand All @@ -33,7 +43,8 @@ actual fun defaultWebViewFactory(param: WebViewFactoryParam): NativeWebView =
enableNavigationGestures = param.state.webSettings.desktopWebSettings.enableNavigationGestures,
incognito = param.state.webSettings.desktopWebSettings.incognito,
autoplayWithoutUserInteraction = param.state.webSettings.desktopWebSettings.autoplayWithoutUserInteraction,
focused = param.state.webSettings.desktopWebSettings.focused
focused = param.state.webSettings.desktopWebSettings.focused,
proxyConfig = param.state.webSettings.desktopWebSettings.proxyConfig?.toJvmProxyConfig(),
)

else -> NativeWebView(
Expand All @@ -49,7 +60,8 @@ actual fun defaultWebViewFactory(param: WebViewFactoryParam): NativeWebView =
enableNavigationGestures = param.state.webSettings.desktopWebSettings.enableNavigationGestures,
incognito = param.state.webSettings.desktopWebSettings.incognito,
autoplayWithoutUserInteraction = param.state.webSettings.desktopWebSettings.autoplayWithoutUserInteraction,
focused = param.state.webSettings.desktopWebSettings.focused
focused = param.state.webSettings.desktopWebSettings.focused,
proxyConfig = param.state.webSettings.desktopWebSettings.proxyConfig?.toJvmProxyConfig(),
)
}

Expand Down Expand Up @@ -212,11 +224,16 @@ actual fun ActualWebView(
}
}

internal fun ProxyConfig.toJvmProxyConfig(): io.github.kdroidfilter.webview.wry.JvmProxyConfig = when (this) {
is ProxyConfig.Http -> io.github.kdroidfilter.webview.wry.JvmProxyConfig.Http(host, port)
is ProxyConfig.Socks5 -> io.github.kdroidfilter.webview.wry.JvmProxyConfig.Socks5(host, port)
}

private fun Color.toRgba(): Rgba {
val argb: Int = this.toArgb() // 0xAARRGGBB (sRGB)
val a: UByte = ((argb ushr 24) and 0xFF).toUByte()
val r: UByte = ((argb ushr 16) and 0xFF).toUByte()
val g: UByte = ((argb ushr 8) and 0xFF).toUByte()
val b: UByte = (argb and 0xFF).toUByte()
return Rgba(r = r, g = g, b = b, a = a)
}
}
2 changes: 1 addition & 1 deletion wrywebview/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ path = "src/main/rust/lib.rs"
[dependencies]
thiserror = "2.0.11"
uniffi = "0.29.4"
wry = { version = "0.54.2", features = ["devtools"] }
wry = { version = "0.54.2", features = ["mac-proxy", "devtools"] }

[profile.release]
opt-level = "z"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.github.kdroidfilter.webview.wry

/**
* JVM-specific proxy config
*/
sealed class JvmProxyConfig {
abstract val host: String
abstract val port: Int

data class Http(
override val host: String,
override val port: Int
) : JvmProxyConfig() {
override fun toProxy(): Proxy.Http = Proxy.Http(Address(host, port.toUShort()))
}

data class Socks5(
override val host: String,
override val port: Int
) : JvmProxyConfig() {
override fun toProxy(): Proxy.Socks5 = Proxy.Socks5(Address(host, port.toUShort()))
}

internal abstract fun toProxy(): Proxy
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import javax.swing.JPanel
import javax.swing.SwingUtilities
import javax.swing.Timer
import kotlin.concurrent.thread
import kotlin.properties.Delegates


class WryWebViewPanel(
Expand All @@ -25,6 +26,7 @@ class WryWebViewPanel(
private val incognito: Boolean = false,
private val autoplayWithoutUserInteraction: Boolean = false,
private val focused: Boolean = true,
proxyConfig: JvmProxyConfig? = null,
private val bridgeLogger: (String) -> Unit = { System.err.println(it) }
) : JPanel() {
private val host = SkikoInterop.createHost()
Expand All @@ -35,6 +37,7 @@ class WryWebViewPanel(
private val dataDirectory: String? = dataDirectory?.trim()?.takeIf { it.isNotEmpty() }
private val customUserAgent: String? = customUserAgent?.trim()?.takeIf { it.isNotEmpty() }
private val initScript: String? = initScript?.trim()?.takeIf { it.isNotEmpty() }
private val proxy: Proxy? = proxyConfig?.toProxy()
private var pendingUrlWithHeaders: String? = null
private var pendingHeaders: Map<String, String> = emptyMap()
private var pendingHtml: String? = null
Expand Down Expand Up @@ -401,6 +404,7 @@ class WryWebViewPanel(
height = height,
url = initialUrl,
userAgent = userAgent,
proxy = proxy,
dataDirectory = dataDir,
zoom = supportZoom,
transparent = transparent,
Expand All @@ -412,7 +416,7 @@ class WryWebViewPanel(
incognito = incognito,
autoplay = autoplayWithoutUserInteraction,
focused = focused,
navHandler = handler
navHandler = handler,
)
updateBounds()
startGtkPumpIfNeeded()
Expand Down Expand Up @@ -444,7 +448,6 @@ class WryWebViewPanel(
true
}
}

createInFlight = true
stopCreateTimer()
thread(name = "wry-webview-create", isDaemon = true) {
Expand All @@ -455,6 +458,7 @@ class WryWebViewPanel(
height = height,
url = initialUrl,
userAgent = userAgent,
proxy = proxy,
dataDirectory = dataDir,
zoom = supportZoom,
transparent = transparent,
Expand All @@ -466,7 +470,7 @@ class WryWebViewPanel(
incognito = incognito,
autoplay = autoplayWithoutUserInteraction,
focused = focused,
navHandler = handler
navHandler = handler,
)
} catch (e: RuntimeException) {
System.err.println("Failed to create Wry webview: ${e.message}")
Expand Down Expand Up @@ -771,6 +775,7 @@ private object NativeBindings {
height: Int,
url: String,
userAgent: String?,
proxy: Proxy? = null,
dataDirectory: String?,
zoom: Boolean,
transparent: Boolean,
Expand All @@ -782,14 +787,15 @@ private object NativeBindings {
incognito: Boolean,
autoplay: Boolean,
focused: Boolean,
navHandler: NavigationHandler?
navHandler: NavigationHandler?,
): ULong {
return io.github.kdroidfilter.webview.wry.createWebview(
parentHandle = parentHandle,
width = width,
height = height,
url = url,
userAgent = userAgent,
proxy = proxy,
dataDirectory = dataDirectory,
zoom = zoom,
transparent = transparent,
Expand Down
Loading