Offlin33er commited on
Commit
9ff1941
·
verified ·
1 Parent(s): 0cb70d5

M1: session persistence + restore in TabManager

Browse files
app/src/main/java/dev/nova/browser/TabManager.kt CHANGED
@@ -1,12 +1,17 @@
1
  package dev.nova.browser
2
 
 
 
3
  import androidx.compose.runtime.mutableStateListOf
4
  import androidx.compose.runtime.mutableStateMapOf
5
  import androidx.compose.runtime.mutableStateOf
 
 
6
  import org.mozilla.geckoview.GeckoResult
7
  import org.mozilla.geckoview.GeckoRuntime
8
  import org.mozilla.geckoview.GeckoSession
9
  import org.mozilla.geckoview.WebExtension
 
10
 
11
  /**
12
  * Nova's tab model. One GeckoSession per tab; the UI activates a tab by
@@ -15,8 +20,12 @@ import org.mozilla.geckoview.WebExtension
15
  * GeckoView does not expose canGoBack()/canGoForward() as polling methods —
16
  * navigation state arrives via NavigationDelegate callbacks, so we track it
17
  * here per tab.
 
 
 
 
18
  */
19
- class TabManager(private val runtime: GeckoRuntime) {
20
 
21
  data class Tab(val id: Int, val session: GeckoSession)
22
 
@@ -35,6 +44,10 @@ class TabManager(private val runtime: GeckoRuntime) {
35
  val consoleLines = mutableStateListOf<String>()
36
 
37
  private var nextId = 1
 
 
 
 
38
 
39
  /** Per-session content-script message delegate ("browser" native app). */
40
  private val sessionMessageDelegate = object : WebExtension.MessageDelegate {
@@ -60,7 +73,10 @@ class TabManager(private val runtime: GeckoRuntime) {
60
  perms: List<GeckoSession.PermissionDelegate.ContentPermission>,
61
  hasUserGesture: Boolean,
62
  ) {
63
- if (url != null) lastUrls[tabId] = url
 
 
 
64
  }
65
 
66
  override fun onCanGoBack(session: GeckoSession, canGoBack: Boolean) {
@@ -84,11 +100,15 @@ class TabManager(private val runtime: GeckoRuntime) {
84
  tabs.add(tab)
85
  activeTabId.value = tab.id
86
  session.loadUri(url)
 
87
  return tab
88
  }
89
 
90
  fun activate(id: Int) {
91
- if (tabs.any { it.id == id }) activeTabId.value = id
 
 
 
92
  }
93
 
94
  fun close(id: Int) {
@@ -101,6 +121,7 @@ class TabManager(private val runtime: GeckoRuntime) {
101
  if (activeTabId.value == id) {
102
  activeTabId.value = tabs.lastOrNull()?.id
103
  }
 
104
  }
105
 
106
  fun activeSession(): GeckoSession? =
@@ -110,6 +131,57 @@ class TabManager(private val runtime: GeckoRuntime) {
110
  fun canGoBack(): Boolean =
111
  activeTabId.value?.let { canGoBackStates[it] == true } ?: false
112
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
113
  /** Called once the built-in extension finishes installing. */
114
  fun wireExtension(ext: WebExtension) {
115
  devExtension = ext
@@ -134,6 +206,6 @@ class TabManager(private val runtime: GeckoRuntime) {
134
  }
135
  }
136
  consoleLines.add(line)
137
- android.util.Log.i("NovaConsole", line)
138
  }
139
  }
 
1
  package dev.nova.browser
2
 
3
+ import android.content.Context
4
+ import android.util.Log
5
  import androidx.compose.runtime.mutableStateListOf
6
  import androidx.compose.runtime.mutableStateMapOf
7
  import androidx.compose.runtime.mutableStateOf
8
+ import org.json.JSONArray
9
+ import org.json.JSONObject
10
  import org.mozilla.geckoview.GeckoResult
11
  import org.mozilla.geckoview.GeckoRuntime
12
  import org.mozilla.geckoview.GeckoSession
13
  import org.mozilla.geckoview.WebExtension
14
+ import java.io.File
15
 
16
  /**
17
  * Nova's tab model. One GeckoSession per tab; the UI activates a tab by
 
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
 
30
  data class Tab(val id: Int, val session: GeckoSession)
31
 
 
44
  val consoleLines = mutableStateListOf<String>()
45
 
46
  private var nextId = 1
47
+ private var restoring = false
48
+
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 {
 
73
  perms: List<GeckoSession.PermissionDelegate.ContentPermission>,
74
  hasUserGesture: Boolean,
75
  ) {
76
+ if (url != null) {
77
+ lastUrls[tabId] = url
78
+ if (!restoring) persistSession()
79
+ }
80
  }
81
 
82
  override fun onCanGoBack(session: GeckoSession, canGoBack: Boolean) {
 
100
  tabs.add(tab)
101
  activeTabId.value = tab.id
102
  session.loadUri(url)
103
+ if (!restoring) persistSession()
104
  return tab
105
  }
106
 
107
  fun activate(id: Int) {
108
+ if (tabs.any { it.id == id }) {
109
+ activeTabId.value = id
110
+ if (!restoring) persistSession()
111
+ }
112
  }
113
 
114
  fun close(id: Int) {
 
121
  if (activeTabId.value == id) {
122
  activeTabId.value = tabs.lastOrNull()?.id
123
  }
124
+ persistSession()
125
  }
126
 
127
  fun activeSession(): GeckoSession? =
 
131
  fun canGoBack(): Boolean =
132
  activeTabId.value?.let { canGoBackStates[it] == true } ?: false
133
 
134
+ // ------------------------------------------------------------------
135
+ // Session persistence (M1)
136
+ // ------------------------------------------------------------------
137
+
138
+ /** Writes open-tab URLs + active tab index to disk. Cheap and idempotent. */
139
+ fun persistSession() {
140
+ try {
141
+ val arr = JSONArray()
142
+ var activeIndex = -1
143
+ tabs.forEach { tab ->
144
+ val url = lastUrls[tab.id]
145
+ if (!url.isNullOrBlank()) {
146
+ arr.put(JSONObject().put("url", url))
147
+ if (tab.id == activeTabId.value) activeIndex = arr.length() - 1
148
+ }
149
+ }
150
+ val obj = JSONObject()
151
+ .put("tabs", arr)
152
+ .put("active", activeIndex)
153
+ .put("savedAt", System.currentTimeMillis())
154
+ stateFile.writeText(obj.toString())
155
+ } catch (e: Exception) {
156
+ Log.w("Nova", "persistSession failed", e)
157
+ }
158
+ }
159
+
160
+ /** Reopens persisted tabs on cold start. Returns how many were restored. */
161
+ fun restoreSession(): Int {
162
+ restoring = true
163
+ try {
164
+ if (!stateFile.exists()) return 0
165
+ val obj = JSONObject(stateFile.readText())
166
+ val arr = obj.optJSONArray("tabs") ?: return 0
167
+ val urls = (0 until arr.length())
168
+ .mapNotNull { i -> arr.optJSONObject(i)?.optString("url") }
169
+ .filter { it.isNotBlank() }
170
+ urls.forEach { open(it) }
171
+ val active = obj.optInt("active", -1)
172
+ if (active in urls.indices) {
173
+ tabs.getOrNull(active)?.let { activate(it.id) }
174
+ }
175
+ Log.i("Nova", "Restored ${urls.size} tab(s) from session state")
176
+ return urls.size
177
+ } catch (e: Exception) {
178
+ Log.w("Nova", "restoreSession failed — starting fresh", e)
179
+ return 0
180
+ } finally {
181
+ restoring = false
182
+ }
183
+ }
184
+
185
  /** Called once the built-in extension finishes installing. */
186
  fun wireExtension(ext: WebExtension) {
187
  devExtension = ext
 
206
  }
207
  }
208
  consoleLines.add(line)
209
+ Log.i("NovaConsole", line)
210
  }
211
  }