Skip to content

Commit

Permalink
Persist history metadata tab state
Browse files Browse the repository at this point in the history
  • Loading branch information
csadilek authored and mergify[bot] committed Jun 1, 2021
1 parent b5080fd commit c039cce
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import mozilla.components.browser.state.state.TabSessionState
import mozilla.components.browser.state.state.recover.RecoverableTab
import mozilla.components.concept.engine.Engine
import mozilla.components.concept.engine.EngineSessionState
import mozilla.components.concept.storage.HistoryMetadataKey
import mozilla.components.support.ktx.android.util.nextBooleanOrNull
import mozilla.components.support.ktx.android.util.nextStringOrNull
import mozilla.components.support.ktx.util.readJSON
Expand Down Expand Up @@ -167,6 +168,10 @@ private fun JsonReader.tabSession(): RecoverableTab? {
var readerStateActive: Boolean? = null
var readerActiveUrl: String? = null

var historyMetadataUrl: String? = null
var historyMetadataSearchTerm: String? = null
var historyMetadataReferrerUrl: String? = null

beginObject()

while (hasNext()) {
Expand All @@ -178,6 +183,9 @@ private fun JsonReader.tabSession(): RecoverableTab? {
Keys.SESSION_TITLE -> title = nextStringOrNull() ?: ""
Keys.SESSION_READER_MODE_KEY -> readerStateActive = nextBooleanOrNull()
Keys.SESSION_READER_MODE_ACTIVE_URL_KEY -> readerActiveUrl = nextStringOrNull()
Keys.SESSION_HISTORY_METADATA_URL -> historyMetadataUrl = nextStringOrNull()
Keys.SESSION_HISTORY_METADATA_SEARCH_TERM -> historyMetadataSearchTerm = nextStringOrNull()
Keys.SESSION_HISTORY_METADATA_REFERRER_URL -> historyMetadataReferrerUrl = nextStringOrNull()
Keys.SESSION_LAST_ACCESS -> lastAccess = nextLong()
Keys.SESSION_SOURCE_KEY -> nextString()
else -> throw IllegalArgumentException("Unknown session key: $name")
Expand All @@ -197,6 +205,15 @@ private fun JsonReader.tabSession(): RecoverableTab? {
active = readerStateActive ?: false,
activeUrl = readerActiveUrl
),
historyMetadata = if (historyMetadataUrl != null) {
HistoryMetadataKey(
historyMetadataUrl,
historyMetadataSearchTerm,
historyMetadataReferrerUrl
)
} else {
null
},
private = false, // We never serialize private sessions
lastAccess = lastAccess ?: 0
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,18 @@ private fun JsonWriter.tab(
value(tab.readerState.activeUrl)
}

val metadata = tab.historyMetadata
if (metadata != null) {
name(Keys.SESSION_HISTORY_METADATA_URL)
value(metadata.url)

name(Keys.SESSION_HISTORY_METADATA_SEARCH_TERM)
value(metadata.searchTerm)

name(Keys.SESSION_HISTORY_METADATA_REFERRER_URL)
value(metadata.referrerUrl)
}

endObject()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ internal object Keys {
const val SESSION_LAST_ACCESS = "lastAccess"
const val SESSION_SOURCE_KEY = "source"

const val SESSION_HISTORY_METADATA_URL = "historyMetadataUrl"
const val SESSION_HISTORY_METADATA_SEARCH_TERM = "historyMetadataSearchTerm"
const val SESSION_HISTORY_METADATA_REFERRER_URL = "historyMetadataReferrerUrl"

const val SESSION_KEY = "session"
const val ENGINE_SESSION_KEY = "engineSession"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import mozilla.components.browser.state.state.TabSessionState
import mozilla.components.browser.state.state.createTab
import mozilla.components.concept.engine.Engine
import mozilla.components.concept.engine.EngineSessionState
import mozilla.components.concept.storage.HistoryMetadataKey
import mozilla.components.support.ktx.util.streamJSON
import mozilla.components.support.test.any
import mozilla.components.support.test.mock
Expand Down Expand Up @@ -112,6 +113,38 @@ class BrowserStateWriterReaderTest {
assertEquals("https://www.mozilla.org", restoredTab.url)
assertEquals("Mozilla", restoredTab.title)
}

@Test
fun `Read and write tab with history metadata`() {
val engineState = createFakeEngineState()
val engine = createFakeEngine(engineState)

val tab = createTab(
url = "https://www.mozilla.org",
title = "Mozilla",
contextId = "work",
historyMetadata = HistoryMetadataKey(
"https://www.mozilla.org",
searchTerm = "test",
referrerUrl = "https://firefox.com"
)
)

val writer = BrowserStateWriter()
val reader = BrowserStateReader()

val file = AtomicFile(
File.createTempFile(UUID.randomUUID().toString(), UUID.randomUUID().toString())
)

assertTrue(writer.writeTab(tab, file))

val restoredTab = reader.readTab(engine, file)
assertNotNull(restoredTab!!)

assertNotNull(restoredTab.historyMetadata)
assertEquals(tab.content.url, restoredTab.historyMetadata!!.url)
}
}

private fun createFakeEngineState(): EngineSessionState {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import mozilla.components.browser.state.store.BrowserStore
import mozilla.components.concept.engine.Engine
import mozilla.components.concept.engine.EngineSession
import mozilla.components.concept.engine.EngineSessionState
import mozilla.components.concept.storage.HistoryMetadataKey
import mozilla.components.support.base.Component
import mozilla.components.support.base.facts.Action
import mozilla.components.support.base.facts.Fact
Expand Down Expand Up @@ -365,6 +366,7 @@ class SessionManager(
val session: Session,
val engineSessionState: EngineSessionState? = null,
val readerState: ReaderState? = null,
val historyMetadata: HistoryMetadataKey? = null,
val lastAccess: Long = 0
)

Expand Down Expand Up @@ -458,7 +460,8 @@ private fun Session.toRestoredTabSessionState(snapshot: SessionManager.Snapshot.
return toTabSessionState().copy(
engineState = engineState,
readerState = snapshot.readerState ?: ReaderState(),
lastAccess = snapshot.lastAccess
lastAccess = snapshot.lastAccess,
historyMetadata = snapshot.historyMetadata
)
}

Expand All @@ -476,6 +479,7 @@ private fun RecoverableTab.toSnapshotItem(): SessionManager.Snapshot.Item {
},
engineSessionState = state,
readerState = readerState,
historyMetadata = historyMetadata,
lastAccess = lastAccess
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ fun createTab(
engineSession: EngineSession? = null,
engineSessionState: EngineSessionState? = null,
crashed: Boolean = false,
mediaSessionState: MediaSessionState? = null
mediaSessionState: MediaSessionState? = null,
historyMetadata: HistoryMetadataKey? = null
): TabSessionState {
return TabSessionState(
id = id,
Expand All @@ -100,6 +101,7 @@ fun createTab(
engineSessionState = engineSessionState,
crashed = crashed
),
mediaSessionState = mediaSessionState
mediaSessionState = mediaSessionState,
historyMetadata = historyMetadata
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package mozilla.components.browser.state.state.recover
import mozilla.components.browser.state.state.ReaderState
import mozilla.components.browser.state.state.TabSessionState
import mozilla.components.concept.engine.EngineSessionState
import mozilla.components.concept.storage.HistoryMetadataKey

/**
* A tab that is no longer open and in the list of tabs, but that can be restored (recovered) at
Expand Down Expand Up @@ -35,7 +36,8 @@ data class RecoverableTab(
val state: EngineSessionState? = null,
val readerState: ReaderState = ReaderState(),
val lastAccess: Long = 0,
val private: Boolean = false
val private: Boolean = false,
val historyMetadata: HistoryMetadataKey? = null
)

/**
Expand All @@ -50,7 +52,8 @@ fun TabSessionState.toRecoverableTab() = RecoverableTab(
state = engineState.engineSessionState,
readerState = readerState,
lastAccess = lastAccess,
private = content.private
private = content.private,
historyMetadata = historyMetadata
)

/**
Expand Down

0 comments on commit c039cce

Please sign in to comment.