Skip to content

Commit

Permalink
Allow setting initial load flags when creating tabs
Browse files Browse the repository at this point in the history
  • Loading branch information
csadilek authored and mergify[bot] committed May 6, 2021
1 parent 867def8 commit 9de5d1b
Show file tree
Hide file tree
Showing 9 changed files with 210 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,14 @@ class SessionManager(
/**
* Adds the provided session.
*/
@Suppress("LongParameterList")
fun add(
session: Session,
selected: Boolean = false,
engineSession: EngineSession? = null,
engineSessionState: EngineSessionState? = null,
parent: Session? = null
parent: Session? = null,
initialLoadFlags: EngineSession.LoadUrlFlags = EngineSession.LoadUrlFlags.none()
) {
// Add store to Session so that it can dispatch actions whenever it changes.
session.store = store
Expand All @@ -114,13 +116,13 @@ class SessionManager(
if (session.isCustomTabSession()) {
store?.syncDispatch(
CustomTabListAction.AddCustomTabAction(
session.toCustomTabSessionState()
session.toCustomTabSessionState(initialLoadFlags)
)
)
} else {
store?.syncDispatch(
TabListAction.AddTabAction(
session.toTabSessionState(),
session.toTabSessionState(initialLoadFlags),
select = selected
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,24 +62,30 @@ internal class LinkingMiddleware(
// The parent tab/session is used as a referrer which is not accurate
// for extension pages. The extension page is not loaded by the parent
// tab, but opened by an extension e.g. via browser.tabs.update.
performLoadOnMainThread(action.engineSession, tab.content.url)
performLoadOnMainThread(action.engineSession, tab.content.url, loadFlags = tab.engineState.initialLoadFlags)
} else {
val parentEngineSession = if (tab is TabSessionState) {
tab.parentId?.let { context.state.findTabOrCustomTab(it)?.engineState?.engineSession }
} else {
null
}

performLoadOnMainThread(action.engineSession, tab.content.url, parentEngineSession)
performLoadOnMainThread(
action.engineSession,
tab.content.url,
parentEngineSession,
loadFlags = tab.engineState.initialLoadFlags
)
}
}

private fun performLoadOnMainThread(
engineSession: EngineSession,
url: String,
parent: EngineSession? = null
parent: EngineSession? = null,
loadFlags: EngineSession.LoadUrlFlags
) = scope.launch {
engineSession.loadUrl(url, parent = parent)
engineSession.loadUrl(url, parent = parent, flags = loadFlags)
}

private fun unlink(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,44 @@ package mozilla.components.browser.session.ext
import mozilla.components.browser.session.Session
import mozilla.components.browser.state.state.ContentState
import mozilla.components.browser.state.state.CustomTabSessionState
import mozilla.components.browser.state.state.EngineState
import mozilla.components.browser.state.state.SecurityInfoState
import mozilla.components.browser.state.state.TabSessionState
import mozilla.components.browser.state.state.TrackingProtectionState
import mozilla.components.concept.engine.EngineSession

/**
* Create a matching [TabSessionState] from a [Session].
*/
fun Session.toTabSessionState(): TabSessionState {
fun Session.toTabSessionState(
initialLoadFlags: EngineSession.LoadUrlFlags = EngineSession.LoadUrlFlags.none()
): TabSessionState {
return TabSessionState(
id,
toContentState(),
toTrackingProtectionState(),
parentId = parentId,
contextId = contextId,
source = source
source = source,
engineState = EngineState(initialLoadFlags = initialLoadFlags)
)
}

/**
* Creates a matching [CustomTabSessionState] from a custom tab [Session].
*/
fun Session.toCustomTabSessionState(): CustomTabSessionState {
fun Session.toCustomTabSessionState(
initialLoadFlags: EngineSession.LoadUrlFlags = EngineSession.LoadUrlFlags.none()
): CustomTabSessionState {
val config =
customTabConfig ?: throw IllegalStateException("Session is not a custom tab session")
return CustomTabSessionState(
id,
toContentState(),
toTrackingProtectionState(),
config,
contextId = contextId
contextId = contextId,
engineState = EngineState(initialLoadFlags = initialLoadFlags)
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ import mozilla.components.concept.engine.EngineSessionState
* has recovered its content process), but further action may be needed to restore the last state
* before the session has crashed (if desired).
* @property timestamp Timestamp of when the [EngineSession] was linked.
* @property initialLoadFlags [EngineSession.LoadUrlFlags] to use for the first load of this session.
*/
data class EngineState(
val engineSession: EngineSession? = null,
val engineSessionState: EngineSessionState? = null,
val engineObserver: EngineSession.Observer? = null,
val crashed: Boolean = false,
val timestamp: Long? = null
val timestamp: Long? = null,
val initialLoadFlags: EngineSession.LoadUrlFlags = EngineSession.LoadUrlFlags.none()
)
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import mozilla.components.feature.session.SessionUseCases
import mozilla.components.feature.tabs.CustomTabsUseCases
import mozilla.components.support.test.any
import mozilla.components.support.test.argumentCaptor
import mozilla.components.support.test.eq
import mozilla.components.support.test.libstate.ext.waitUntilIdle
import mozilla.components.support.test.middleware.CaptureActionsMiddleware
import mozilla.components.support.test.mock
Expand All @@ -36,7 +37,6 @@ import org.junit.Assert.assertNotNull
import org.junit.Assert.assertTrue
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.ArgumentMatchers.eq
import org.mockito.Mockito.spy
import org.mockito.Mockito.verify

Expand Down Expand Up @@ -64,7 +64,14 @@ class CustomTabIntentProcessorTest {
handler.process(intent)

val sessionCaptor = argumentCaptor<Session>()
verify(sessionManager).add(sessionCaptor.capture(), eq(false), eq(null), eq(null), eq(null))
verify(sessionManager).add(
sessionCaptor.capture(),
eq(false),
eq(null),
eq(null),
eq(null),
eq(LoadUrlFlags.external())
)

store.waitUntilIdle()
middleware.assertFirstAction(EngineAction.LoadUrlAction::class) { action ->
Expand Down Expand Up @@ -110,7 +117,14 @@ class CustomTabIntentProcessorTest {
handler.process(intent)

val sessionCaptor = argumentCaptor<Session>()
verify(sessionManager).add(sessionCaptor.capture(), eq(false), eq(null), eq(null), eq(null))
verify(sessionManager).add(
sessionCaptor.capture(),
eq(false),
eq(null),
eq(null),
eq(null),
eq(LoadUrlFlags.external())
)

store.waitUntilIdle()
middleware.assertFirstAction(EngineAction.LoadUrlAction::class) { action ->
Expand Down Expand Up @@ -151,7 +165,14 @@ class CustomTabIntentProcessorTest {
handler.process(intent)

val sessionCaptor = argumentCaptor<Session>()
verify(sessionManager).add(sessionCaptor.capture(), eq(false), eq(null), eq(null), eq(null))
verify(sessionManager).add(
sessionCaptor.capture(),
eq(false),
eq(null),
eq(null),
eq(null),
eq(LoadUrlFlags.external())
)

store.waitUntilIdle()
middleware.assertFirstAction(EngineAction.LoadUrlAction::class) { action ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,14 @@ class TabIntentProcessorTest {
handler.process(intent)

val sessionCaptor = argumentCaptor<Session>()
verify(sessionManager).add(sessionCaptor.capture(), eq(true), eq(null), eq(null), eq(null))
verify(sessionManager).add(
sessionCaptor.capture(),
eq(true),
eq(null),
eq(null),
eq(null),
eq(LoadUrlFlags.external())
)
verify(store).dispatch(EngineAction.LoadUrlAction(
sessionCaptor.value.id, "http://mozilla.org", LoadUrlFlags.external())
)
Expand Down Expand Up @@ -131,7 +138,14 @@ class TabIntentProcessorTest {
handler.process(intent)

val sessionCaptor = argumentCaptor<Session>()
verify(sessionManager).add(sessionCaptor.capture(), eq(true), eq(null), eq(null), eq(null))
verify(sessionManager).add(
sessionCaptor.capture(),
eq(true),
eq(null),
eq(null),
eq(null),
eq(LoadUrlFlags.external())
)
verify(store).dispatch(EngineAction.LoadUrlAction(
sessionCaptor.value.id, "http://mozilla.org", LoadUrlFlags.external())
)
Expand Down Expand Up @@ -169,7 +183,14 @@ class TabIntentProcessorTest {
whenever(intent.dataString).thenReturn("http://mozilla.org")
handler.process(intent)
val sessionCaptor = argumentCaptor<Session>()
verify(sessionManager).add(sessionCaptor.capture(), eq(true), eq(null), eq(null), eq(null))
verify(sessionManager).add(
sessionCaptor.capture(),
eq(true),
eq(null),
eq(null),
eq(null),
eq(LoadUrlFlags.external())
)
verify(store).dispatch(EngineAction.LoadUrlAction(
sessionCaptor.value.id, "http://mozilla.org", LoadUrlFlags.external())
)
Expand All @@ -190,35 +211,70 @@ class TabIntentProcessorTest {
whenever(intent.getStringExtra(Intent.EXTRA_TEXT)).thenReturn("http://mozilla.org")
handler.process(intent)
val sessionCaptor = argumentCaptor<Session>()
verify(sessionManager).add(sessionCaptor.capture(), eq(true), eq(null), eq(null), eq(null))
verify(sessionManager).add(
sessionCaptor.capture(),
eq(true),
eq(null),
eq(null),
eq(null),
eq(LoadUrlFlags.external())
)
verify(store).dispatch(EngineAction.LoadUrlAction(
sessionCaptor.value.id, "http://mozilla.org", LoadUrlFlags.external())
)

whenever(intent.getStringExtra(Intent.EXTRA_TEXT)).thenReturn("see http://getpocket.com")
handler.process(intent)
verify(sessionManager, times(2)).add(sessionCaptor.capture(), eq(true), eq(null), eq(null), eq(null))
verify(sessionManager, times(2)).add(
sessionCaptor.capture(),
eq(true),
eq(null),
eq(null),
eq(null),
eq(LoadUrlFlags.external())
)
verify(store).dispatch(EngineAction.LoadUrlAction(
sessionCaptor.allValues.last().id, "http://getpocket.com", LoadUrlFlags.external())
)

whenever(intent.getStringExtra(Intent.EXTRA_TEXT)).thenReturn("see http://mozilla.com and http://getpocket.com")
handler.process(intent)
verify(sessionManager, times(3)).add(sessionCaptor.capture(), eq(true), eq(null), eq(null), eq(null))
verify(sessionManager, times(3)).add(
sessionCaptor.capture(),
eq(true),
eq(null),
eq(null),
eq(null),
eq(LoadUrlFlags.external())
)
verify(store).dispatch(EngineAction.LoadUrlAction(
sessionCaptor.allValues.last().id, "http://mozilla.com", LoadUrlFlags.external())
)

whenever(intent.getStringExtra(Intent.EXTRA_TEXT)).thenReturn("checkout the Tweet: http://tweets.mozilla.com")
handler.process(intent)
verify(sessionManager, times(4)).add(sessionCaptor.capture(), eq(true), eq(null), eq(null), eq(null))
verify(sessionManager, times(4)).add(
sessionCaptor.capture(),
eq(true),
eq(null),
eq(null),
eq(null),
eq(LoadUrlFlags.external())
)
verify(store).dispatch(EngineAction.LoadUrlAction(
sessionCaptor.allValues.last().id, "http://tweets.mozilla.com", LoadUrlFlags.external())
)

whenever(intent.getStringExtra(Intent.EXTRA_TEXT)).thenReturn("checkout the Tweet: HTTP://tweets.mozilla.org")
handler.process(intent)
verify(sessionManager, times(5)).add(sessionCaptor.capture(), eq(true), eq(null), eq(null), eq(null))
verify(sessionManager, times(5)).add(
sessionCaptor.capture(),
eq(true),
eq(null),
eq(null),
eq(null),
eq(LoadUrlFlags.external())
)
verify(store).dispatch(EngineAction.LoadUrlAction(
sessionCaptor.allValues.last().id, "HTTP://tweets.mozilla.org", LoadUrlFlags.external())
)
Expand Down Expand Up @@ -246,7 +302,14 @@ class TabIntentProcessorTest {

handler.process(intent)
val sessionCaptor = argumentCaptor<Session>()
verify(sessionManager).add(sessionCaptor.capture(), eq(true), eq(null), eq(null), eq(null))
verify(sessionManager).add(
sessionCaptor.capture(),
eq(true),
eq(null),
eq(null),
eq(null),
eq(LoadUrlFlags.none())
)
verify(store).dispatch(EngineAction.LoadUrlAction(
sessionCaptor.value.id, searchUrl, LoadUrlFlags.none())
)
Expand Down Expand Up @@ -291,7 +354,14 @@ class TabIntentProcessorTest {
whenever(intent.getStringExtra(SearchManager.QUERY)).thenReturn("http://mozilla.org")
handler.process(intent)
val sessionCaptor = argumentCaptor<Session>()
verify(sessionManager).add(sessionCaptor.capture(), eq(true), eq(null), eq(null), eq(null))
verify(sessionManager).add(
sessionCaptor.capture(),
eq(true),
eq(null),
eq(null),
eq(null),
eq(LoadUrlFlags.external())
)
verify(store).dispatch(EngineAction.LoadUrlAction(
sessionCaptor.value.id, "http://mozilla.org", LoadUrlFlags.external())
)
Expand Down Expand Up @@ -319,7 +389,14 @@ class TabIntentProcessorTest {

handler.process(intent)
val sessionCaptor = argumentCaptor<Session>()
verify(sessionManager).add(sessionCaptor.capture(), eq(true), eq(null), eq(null), eq(null))
verify(sessionManager).add(
sessionCaptor.capture(),
eq(true),
eq(null),
eq(null),
eq(null),
eq(LoadUrlFlags.none())
)
verify(store).dispatch(EngineAction.LoadUrlAction(
sessionCaptor.value.id, searchUrl, LoadUrlFlags.none())
)
Expand Down Expand Up @@ -352,7 +429,14 @@ class TabIntentProcessorTest {
whenever(intent.getStringExtra(SearchManager.QUERY)).thenReturn("http://mozilla.org")
handler.process(intent)
val sessionCaptor = argumentCaptor<Session>()
verify(sessionManager).add(sessionCaptor.capture(), eq(true), eq(null), eq(null), eq(null))
verify(sessionManager).add(
sessionCaptor.capture(),
eq(true),
eq(null),
eq(null),
eq(null),
eq(LoadUrlFlags.external())
)
verify(store).dispatch(EngineAction.LoadUrlAction(
sessionCaptor.value.id, "http://mozilla.org", LoadUrlFlags.external())
)
Expand Down Expand Up @@ -380,7 +464,14 @@ class TabIntentProcessorTest {

handler.process(intent)
val sessionCaptor = argumentCaptor<Session>()
verify(sessionManager).add(sessionCaptor.capture(), eq(true), eq(null), eq(null), eq(null))
verify(sessionManager).add(
sessionCaptor.capture(),
eq(true),
eq(null),
eq(null),
eq(null),
eq(LoadUrlFlags.none())
)
verify(store).dispatch(EngineAction.LoadUrlAction(
sessionCaptor.value.id, searchUrl, LoadUrlFlags.none())
)
Expand Down
Loading

0 comments on commit 9de5d1b

Please sign in to comment.