M2: progress/loading/forward tracking + persisted history store
Browse files
app/src/main/java/dev/nova/browser/TabManager.kt
CHANGED
|
@@ -19,11 +19,14 @@ import java.io.File
|
|
| 19 |
*
|
| 20 |
* GeckoView does not expose canGoBack()/canGoForward() as polling methods —
|
| 21 |
* navigation state arrives via NavigationDelegate callbacks, so we track it
|
| 22 |
-
* here per tab.
|
| 23 |
*
|
| 24 |
* M1 session restore: open-tab URLs + the active tab index are persisted to
|
| 25 |
* a JSON file on every tab change and on activity stop, and reloaded on cold
|
| 26 |
* start (URL-level restore; full session-state capture is a later milestone).
|
|
|
|
|
|
|
|
|
|
| 27 |
*/
|
| 28 |
class TabManager(private val runtime: GeckoRuntime, private val context: Context) {
|
| 29 |
|
|
@@ -35,8 +38,16 @@ class TabManager(private val runtime: GeckoRuntime, private val context: Context
|
|
| 35 |
|
| 36 |
/** Navigation state per tab id, fed by NavigationDelegate callbacks. */
|
| 37 |
val canGoBackStates = mutableStateMapOf<Int, Boolean>()
|
|
|
|
| 38 |
val lastUrls = mutableStateMapOf<Int, String>()
|
| 39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
/** Installed built-in extension, wired in once available. */
|
| 41 |
var devExtension: WebExtension? = null
|
| 42 |
|
|
@@ -49,6 +60,13 @@ class TabManager(private val runtime: GeckoRuntime, private val context: Context
|
|
| 49 |
private val stateFile: File
|
| 50 |
get() = File(context.filesDir, "session_state.json")
|
| 51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
/** Per-session content-script message delegate ("browser" native app). */
|
| 53 |
private val sessionMessageDelegate = object : WebExtension.MessageDelegate {
|
| 54 |
override fun onMessage(
|
|
@@ -82,10 +100,29 @@ class TabManager(private val runtime: GeckoRuntime, private val context: Context
|
|
| 82 |
override fun onCanGoBack(session: GeckoSession, canGoBack: Boolean) {
|
| 83 |
canGoBackStates[tabId] = canGoBack
|
| 84 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
}
|
| 86 |
session.progressDelegate = object : GeckoSession.ProgressDelegate {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
override fun onProgressChange(session: GeckoSession, progress: Int) {
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
}
|
| 90 |
}
|
| 91 |
|
|
@@ -117,7 +154,10 @@ class TabManager(private val runtime: GeckoRuntime, private val context: Context
|
|
| 117 |
tabs[index].session.close()
|
| 118 |
tabs.removeAt(index)
|
| 119 |
canGoBackStates.remove(id)
|
|
|
|
| 120 |
lastUrls.remove(id)
|
|
|
|
|
|
|
| 121 |
if (activeTabId.value == id) {
|
| 122 |
activeTabId.value = tabs.lastOrNull()?.id
|
| 123 |
}
|
|
@@ -127,10 +167,59 @@ class TabManager(private val runtime: GeckoRuntime, private val context: Context
|
|
| 127 |
fun activeSession(): GeckoSession? =
|
| 128 |
activeTabId.value?.let { id -> tabs.firstOrNull { it.id == id }?.session }
|
| 129 |
|
| 130 |
-
/** Whether the active tab can navigate back (delegate-tracked). */
|
| 131 |
fun canGoBack(): Boolean =
|
| 132 |
activeTabId.value?.let { canGoBackStates[it] == true } ?: false
|
| 133 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
// ------------------------------------------------------------------
|
| 135 |
// Session persistence (M1)
|
| 136 |
// ------------------------------------------------------------------
|
|
@@ -208,4 +297,9 @@ class TabManager(private val runtime: GeckoRuntime, private val context: Context
|
|
| 208 |
consoleLines.add(line)
|
| 209 |
Log.i("NovaConsole", line)
|
| 210 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
}
|
|
|
|
| 19 |
*
|
| 20 |
* GeckoView does not expose canGoBack()/canGoForward() as polling methods —
|
| 21 |
* navigation state arrives via NavigationDelegate callbacks, so we track it
|
| 22 |
+
* here per tab. Loading state and progress arrive via ProgressDelegate.
|
| 23 |
*
|
| 24 |
* M1 session restore: open-tab URLs + the active tab index are persisted to
|
| 25 |
* a JSON file on every tab change and on activity stop, and reloaded on cold
|
| 26 |
* start (URL-level restore; full session-state capture is a later milestone).
|
| 27 |
+
*
|
| 28 |
+
* M2 history: committed visits are recorded (most-recent-first, deduped,
|
| 29 |
+
* capped) into history.json and power the URL bar autocomplete.
|
| 30 |
*/
|
| 31 |
class TabManager(private val runtime: GeckoRuntime, private val context: Context) {
|
| 32 |
|
|
|
|
| 38 |
|
| 39 |
/** Navigation state per tab id, fed by NavigationDelegate callbacks. */
|
| 40 |
val canGoBackStates = mutableStateMapOf<Int, Boolean>()
|
| 41 |
+
val canGoForwardStates = mutableStateMapOf<Int, Boolean>()
|
| 42 |
val lastUrls = mutableStateMapOf<Int, String>()
|
| 43 |
|
| 44 |
+
/** Loading state per tab id, fed by ProgressDelegate. */
|
| 45 |
+
val loadingStates = mutableStateMapOf<Int, Boolean>()
|
| 46 |
+
val progressStates = mutableStateMapOf<Int, Int>()
|
| 47 |
+
|
| 48 |
+
/** Visited URLs, most recent first (deduplicated, capped). */
|
| 49 |
+
val history = mutableStateListOf<String>()
|
| 50 |
+
|
| 51 |
/** Installed built-in extension, wired in once available. */
|
| 52 |
var devExtension: WebExtension? = null
|
| 53 |
|
|
|
|
| 60 |
private val stateFile: File
|
| 61 |
get() = File(context.filesDir, "session_state.json")
|
| 62 |
|
| 63 |
+
private val historyFile: File
|
| 64 |
+
get() = File(context.filesDir, "history.json")
|
| 65 |
+
|
| 66 |
+
init {
|
| 67 |
+
loadHistory()
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
/** Per-session content-script message delegate ("browser" native app). */
|
| 71 |
private val sessionMessageDelegate = object : WebExtension.MessageDelegate {
|
| 72 |
override fun onMessage(
|
|
|
|
| 100 |
override fun onCanGoBack(session: GeckoSession, canGoBack: Boolean) {
|
| 101 |
canGoBackStates[tabId] = canGoBack
|
| 102 |
}
|
| 103 |
+
|
| 104 |
+
override fun onCanGoForward(session: GeckoSession, canGoForward: Boolean) {
|
| 105 |
+
canGoForwardStates[tabId] = canGoForward
|
| 106 |
+
}
|
| 107 |
}
|
| 108 |
session.progressDelegate = object : GeckoSession.ProgressDelegate {
|
| 109 |
+
override fun onPageStarted(session: GeckoSession, url: String) {
|
| 110 |
+
loadingStates[tabId] = true
|
| 111 |
+
progressStates[tabId] = 0
|
| 112 |
+
}
|
| 113 |
+
|
| 114 |
override fun onProgressChange(session: GeckoSession, progress: Int) {
|
| 115 |
+
progressStates[tabId] = progress
|
| 116 |
+
}
|
| 117 |
+
|
| 118 |
+
override fun onPageStop(session: GeckoSession, success: Boolean) {
|
| 119 |
+
loadingStates[tabId] = false
|
| 120 |
+
if (success) {
|
| 121 |
+
progressStates[tabId] = 100
|
| 122 |
+
// Committed visits go into history (skips about: pages etc.)
|
| 123 |
+
val url = lastUrls[tabId]
|
| 124 |
+
if (!restoring && url != null) recordVisit(url)
|
| 125 |
+
}
|
| 126 |
}
|
| 127 |
}
|
| 128 |
|
|
|
|
| 154 |
tabs[index].session.close()
|
| 155 |
tabs.removeAt(index)
|
| 156 |
canGoBackStates.remove(id)
|
| 157 |
+
canGoForwardStates.remove(id)
|
| 158 |
lastUrls.remove(id)
|
| 159 |
+
loadingStates.remove(id)
|
| 160 |
+
progressStates.remove(id)
|
| 161 |
if (activeTabId.value == id) {
|
| 162 |
activeTabId.value = tabs.lastOrNull()?.id
|
| 163 |
}
|
|
|
|
| 167 |
fun activeSession(): GeckoSession? =
|
| 168 |
activeTabId.value?.let { id -> tabs.firstOrNull { it.id == id }?.session }
|
| 169 |
|
| 170 |
+
/** Whether the active tab can navigate back/forward (delegate-tracked). */
|
| 171 |
fun canGoBack(): Boolean =
|
| 172 |
activeTabId.value?.let { canGoBackStates[it] == true } ?: false
|
| 173 |
|
| 174 |
+
fun canGoForward(): Boolean =
|
| 175 |
+
activeTabId.value?.let { canGoForwardStates[it] == true } ?: false
|
| 176 |
+
|
| 177 |
+
// ------------------------------------------------------------------
|
| 178 |
+
// History (M2)
|
| 179 |
+
// ------------------------------------------------------------------
|
| 180 |
+
|
| 181 |
+
fun recordVisit(url: String) {
|
| 182 |
+
if (url.startsWith("about:") || url.startsWith("data:") || url.startsWith("javascript:")) return
|
| 183 |
+
history.remove(url)
|
| 184 |
+
history.add(0, url)
|
| 185 |
+
while (history.size > MAX_HISTORY) history.removeAt(history.lastIndex)
|
| 186 |
+
persistHistory()
|
| 187 |
+
}
|
| 188 |
+
|
| 189 |
+
/** Substring matches against history, most recent first. */
|
| 190 |
+
fun suggestionsFor(query: String): List<String> {
|
| 191 |
+
val q = query.trim()
|
| 192 |
+
if (q.isBlank() || q.startsWith("about:")) return emptyList()
|
| 193 |
+
return history
|
| 194 |
+
.filter { it != q && it.contains(q, ignoreCase = true) }
|
| 195 |
+
.take(MAX_SUGGESTIONS)
|
| 196 |
+
}
|
| 197 |
+
|
| 198 |
+
private fun persistHistory() {
|
| 199 |
+
try {
|
| 200 |
+
val arr = JSONArray()
|
| 201 |
+
history.forEach { arr.put(it) }
|
| 202 |
+
File(context.filesDir, "history.json").writeText(arr.toString())
|
| 203 |
+
} catch (e: Exception) {
|
| 204 |
+
Log.w("Nova", "persistHistory failed", e)
|
| 205 |
+
}
|
| 206 |
+
}
|
| 207 |
+
|
| 208 |
+
private fun loadHistory() {
|
| 209 |
+
try {
|
| 210 |
+
val f = File(context.filesDir, "history.json")
|
| 211 |
+
if (!f.exists()) return
|
| 212 |
+
val arr = JSONArray(f.readText())
|
| 213 |
+
history.clear()
|
| 214 |
+
for (i in 0 until arr.length()) {
|
| 215 |
+
val url = arr.optString(i)
|
| 216 |
+
if (url.isNotBlank()) history.add(url)
|
| 217 |
+
}
|
| 218 |
+
} catch (e: Exception) {
|
| 219 |
+
Log.w("Nova", "loadHistory failed — starting with empty history", e)
|
| 220 |
+
}
|
| 221 |
+
}
|
| 222 |
+
|
| 223 |
// ------------------------------------------------------------------
|
| 224 |
// Session persistence (M1)
|
| 225 |
// ------------------------------------------------------------------
|
|
|
|
| 297 |
consoleLines.add(line)
|
| 298 |
Log.i("NovaConsole", line)
|
| 299 |
}
|
| 300 |
+
|
| 301 |
+
companion object {
|
| 302 |
+
private const val MAX_HISTORY = 500
|
| 303 |
+
private const val MAX_SUGGESTIONS = 5
|
| 304 |
+
}
|
| 305 |
}
|