TabManager: one GeckoSession per tab
Browse files
app/src/main/java/dev/nova/browser/TabManager.kt
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
package dev.nova.browser
|
| 2 |
+
|
| 3 |
+
import androidx.compose.runtime.mutableStateListOf
|
| 4 |
+
import androidx.compose.runtime.mutableStateOf
|
| 5 |
+
import org.mozilla.geckoview.GeckoRuntime
|
| 6 |
+
import org.mozilla.geckoview.GeckoSession
|
| 7 |
+
import org.mozilla.geckoview.WebExtension
|
| 8 |
+
|
| 9 |
+
/**
|
| 10 |
+
* Nova's tab model. One GeckoSession per tab; the UI activates a tab by
|
| 11 |
+
* calling GeckoView.setSession() on the active tab's session.
|
| 12 |
+
*/
|
| 13 |
+
class TabManager(private val runtime: GeckoRuntime) {
|
| 14 |
+
|
| 15 |
+
data class Tab(val id: Int, val session: GeckoSession)
|
| 16 |
+
|
| 17 |
+
/** Observable tab list for Compose. */
|
| 18 |
+
val tabs = mutableStateListOf<Tab>()
|
| 19 |
+
val activeTabId = mutableStateOf<Int?>(null)
|
| 20 |
+
|
| 21 |
+
/** Installed built-in extension, wired in once available. */
|
| 22 |
+
var devExtension: WebExtension? = null
|
| 23 |
+
|
| 24 |
+
/** Console lines forwarded from the dev-console extension. */
|
| 25 |
+
val consoleLines = mutableStateListOf<String>()
|
| 26 |
+
|
| 27 |
+
private var nextId = 1
|
| 28 |
+
|
| 29 |
+
/** Per-session content-script message delegate ("browser" native app). */
|
| 30 |
+
private val sessionMessageDelegate = object : WebExtension.MessageDelegate {
|
| 31 |
+
override fun onMessage(
|
| 32 |
+
nativeApp: String,
|
| 33 |
+
message: Any,
|
| 34 |
+
sender: WebExtension.MessageSender
|
| 35 |
+
): GeckoResult<Any>? {
|
| 36 |
+
recordConsoleMessage(message)
|
| 37 |
+
return null
|
| 38 |
+
}
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
fun open(url: String): Tab {
|
| 42 |
+
val session = GeckoSession()
|
| 43 |
+
|
| 44 |
+
session.progressDelegate = object : GeckoSession.ProgressDelegate {
|
| 45 |
+
override fun onProgressChange(session: GeckoSession, progress: Int) {
|
| 46 |
+
// Hook for the loading bar (M2 milestone).
|
| 47 |
+
}
|
| 48 |
+
}
|
| 49 |
+
session.navigationDelegate = object : GeckoSession.NavigationDelegate {
|
| 50 |
+
override fun onLocationChange(session: GeckoSession, url: String?) {
|
| 51 |
+
// Hook for syncing the URL bar (M2 milestone).
|
| 52 |
+
}
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
session.open(runtime)
|
| 56 |
+
|
| 57 |
+
// Content scripts of the dev-console extension can sendNativeMessage to us.
|
| 58 |
+
devExtension?.let { ext ->
|
| 59 |
+
session.webExtensionController.setMessageDelegate(ext, sessionMessageDelegate, "browser")
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
val tab = Tab(nextId++, session)
|
| 63 |
+
tabs.add(tab)
|
| 64 |
+
activeTabId.value = tab.id
|
| 65 |
+
session.loadUri(url)
|
| 66 |
+
return tab
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
fun activate(id: Int) {
|
| 70 |
+
if (tabs.any { it.id == id }) activeTabId.value = id
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
fun close(id: Int) {
|
| 74 |
+
val index = tabs.indexOfFirst { it.id == id }
|
| 75 |
+
if (index == -1) return
|
| 76 |
+
tabs[index].session.close()
|
| 77 |
+
tabs.removeAt(index)
|
| 78 |
+
if (activeTabId.value == id) {
|
| 79 |
+
activeTabId.value = tabs.lastOrNull()?.id
|
| 80 |
+
}
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
fun activeSession(): GeckoSession? =
|
| 84 |
+
activeTabId.value?.let { id -> tabs.firstOrNull { it.id == id }?.session }
|
| 85 |
+
|
| 86 |
+
/** Called once the built-in extension finishes installing. */
|
| 87 |
+
fun wireExtension(ext: WebExtension) {
|
| 88 |
+
devExtension = ext
|
| 89 |
+
// Re-wire existing sessions that opened before the extension was ready.
|
| 90 |
+
tabs.forEach { tab ->
|
| 91 |
+
tab.session.webExtensionController.setMessageDelegate(ext, sessionMessageDelegate, "browser")
|
| 92 |
+
}
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
fun recordConsoleMessage(raw: Any) {
|
| 96 |
+
val line = when (raw) {
|
| 97 |
+
is Map<*, *> -> {
|
| 98 |
+
val type = raw["type"] ?: "log"
|
| 99 |
+
val payload = raw["payload"]?.toString() ?: ""
|
| 100 |
+
"[$type] $payload"
|
| 101 |
+
}
|
| 102 |
+
else -> raw.toString()
|
| 103 |
+
}
|
| 104 |
+
consoleLines.add(line)
|
| 105 |
+
android.util.Log.i("NovaConsole", line)
|
| 106 |
+
}
|
| 107 |
+
}
|