Commit 2d873071 authored by Christian Sadilek's avatar Christian Sadilek
Browse files

Remove remaining usages of Session[Manager] in BrowserFragment

parent c9b8f57f
Loading
Loading
Loading
Loading
+13 −29
Original line number Diff line number Diff line
@@ -38,13 +38,15 @@ import kotlinx.coroutines.flow.mapNotNull
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import mozilla.appservices.places.BookmarkRoot
import mozilla.components.browser.session.Session
import mozilla.components.browser.state.action.ContentAction
import mozilla.components.browser.state.selector.findCustomTab
import mozilla.components.browser.state.selector.findCustomTabOrSelectedTab
import mozilla.components.browser.state.selector.findTab
import mozilla.components.browser.state.selector.findTabOrCustomTab
import mozilla.components.browser.state.selector.findTabOrCustomTabOrSelectedTab
import mozilla.components.browser.state.selector.getNormalOrPrivateTabs
import mozilla.components.browser.state.selector.selectedTab
import mozilla.components.browser.state.state.CustomTabSessionState
import mozilla.components.browser.state.state.SessionState
import mozilla.components.browser.state.state.TabSessionState
import mozilla.components.browser.state.state.content.DownloadState
@@ -254,7 +256,6 @@ abstract class BaseBrowserFragment : Fragment(), UserInteractionHandler, Activit
    @CallSuper
    internal open fun initializeUI(view: View, tab: SessionState) {
        val context = requireContext()
        val sessionManager = context.components.core.sessionManager
        val store = context.components.core.store
        val activity = requireActivity() as HomeActivity

@@ -350,7 +351,7 @@ abstract class BaseBrowserFragment : Fragment(), UserInteractionHandler, Activit
            container = view.browserLayout,
            toolbarPosition = context.settings().toolbarPosition,
            interactor = browserInteractor,
            customTabSession = customTabSessionId?.let { sessionManager.findSessionById(it) },
            customTabSession = customTabSessionId?.let { store.state.findCustomTab(it) },
            lifecycleOwner = viewLifecycleOwner
        )

@@ -551,7 +552,7 @@ abstract class BaseBrowserFragment : Fragment(), UserInteractionHandler, Activit
                        val directions = NavGraphDirections.actionGlobalShareFragment(
                            data = arrayOf(shareData),
                            showPage = true,
                            sessionId = getSessionById()?.id
                            sessionId = getCurrentTab()?.id
                        )
                        findNavController().navigate(directions)
                    }
@@ -585,14 +586,14 @@ abstract class BaseBrowserFragment : Fragment(), UserInteractionHandler, Activit

        searchFeature.set(
            feature = SearchFeature(store, customTabSessionId) { request, tabId ->
                val parentSession = sessionManager.findSessionById(tabId)
                val parentSession = store.state.findTabOrCustomTab(tabId)
                val useCase = if (request.isPrivate) {
                    requireComponents.useCases.searchUseCases.newPrivateTabSearch
                } else {
                    requireComponents.useCases.searchUseCases.newTabSearch
                }

                if (parentSession?.isCustomTabSession() == true) {
                if (parentSession is CustomTabSessionState) {
                    useCase.invoke(request.query)
                    requireActivity().startActivity(openInFenixIntent)
                } else {
@@ -1016,7 +1017,7 @@ abstract class BaseBrowserFragment : Fragment(), UserInteractionHandler, Activit
    final override fun onViewStateRestored(savedInstanceState: Bundle?) {
        super.onViewStateRestored(savedInstanceState)
        savedInstanceState?.getString(KEY_CUSTOM_TAB_SESSION_ID)?.let {
            if (requireComponents.core.sessionManager.findSessionById(it)?.customTabConfig != null) {
            if (requireComponents.core.store.state.findCustomTab(it) != null) {
                customTabSessionId = it
            }
        }
@@ -1054,22 +1055,18 @@ abstract class BaseBrowserFragment : Fragment(), UserInteractionHandler, Activit
     * or if it has a parent session and no more history
     */
    protected open fun removeSessionIfNeeded(): Boolean {
        getSessionById()?.let { session ->
        getCurrentTab()?.let { session ->
            return if (session.source == SessionState.Source.ACTION_VIEW) {
                activity?.finish()
                requireComponents.useCases.tabsUseCases.removeTab(session.id)
                true
            } else {
                if (session.hasParentSession) {
                    // The removeTab use case does not currently select a parent session, so
                    // we are using sessionManager.remove
                    requireComponents.core.sessionManager.remove(
                        session,
                        selectParentIfExists = true
                    )
                val hasParentSession = session is TabSessionState && session.parentId != null
                if (hasParentSession) {
                    requireComponents.useCases.tabsUseCases.removeTab(session.id, selectParentIfExists = true)
                }
                // We want to return to home if this session didn't have a parent session to select.
                val goToOverview = !session.hasParentSession
                val goToOverview = !hasParentSession
                !goToOverview
            }
        }
@@ -1134,19 +1131,6 @@ abstract class BaseBrowserFragment : Fragment(), UserInteractionHandler, Activit
        (activity as HomeActivity).browsingModeManager.mode = sessionMode
    }

    /**
     * Returns the current session.
     */
    protected fun getSessionById(): Session? {
        val sessionManager = requireComponents.core.sessionManager
        val localCustomTabId = customTabSessionId
        return if (localCustomTabId != null) {
            sessionManager.findSessionById(localCustomTabId)
        } else {
            sessionManager.selectedSession
        }
    }

    @VisibleForTesting
    internal fun getCurrentTab(): SessionState? {
        return requireComponents.core.store.state.findCustomTabOrSelectedTab(customTabSessionId)
+1 −1
Original line number Diff line number Diff line
@@ -84,7 +84,7 @@ class BrowserFragment : BaseBrowserFragment(), UserInteractionHandler {
                visible = {
                    readerModeAvailable
                },
                selected = getSessionById()?.let {
                selected = getCurrentTab()?.let {
                        activity?.components?.core?.store?.state?.findTab(it.id)?.readerState?.active
                    } ?: false,
                listener = browserInteractor::onReaderModePressed
+5 −5
Original line number Diff line number Diff line
@@ -16,8 +16,8 @@ import androidx.lifecycle.LifecycleOwner
import kotlinx.android.extensions.LayoutContainer
import kotlinx.coroutines.ExperimentalCoroutinesApi
import mozilla.components.browser.domains.autocomplete.ShippedDomainsProvider
import mozilla.components.browser.session.Session
import mozilla.components.browser.state.selector.selectedTab
import mozilla.components.browser.state.state.CustomTabSessionState
import mozilla.components.browser.state.state.ExternalAppType
import mozilla.components.browser.toolbar.BrowserToolbar
import mozilla.components.browser.toolbar.behavior.BrowserToolbarBehavior
@@ -52,7 +52,7 @@ class BrowserToolbarView(
    private val container: ViewGroup,
    private val toolbarPosition: ToolbarPosition,
    private val interactor: BrowserToolbarViewInteractor,
    private val customTabSession: Session?,
    private val customTabSession: CustomTabSessionState?,
    private val lifecycleOwner: LifecycleOwner
) : LayoutContainer {

@@ -78,8 +78,8 @@ class BrowserToolbarView(

    @VisibleForTesting
    internal val isPwaTabOrTwaTab: Boolean
        get() = customTabSession?.customTabConfig?.externalAppType == ExternalAppType.PROGRESSIVE_WEB_APP ||
                customTabSession?.customTabConfig?.externalAppType == ExternalAppType.TRUSTED_WEB_ACTIVITY
        get() = customTabSession?.config?.externalAppType == ExternalAppType.PROGRESSIVE_WEB_APP ||
                customTabSession?.config?.externalAppType == ExternalAppType.TRUSTED_WEB_ACTIVITY

    init {
        val isCustomTabSession = customTabSession != null
@@ -185,7 +185,7 @@ class BrowserToolbarView(
                    view,
                    menuToolbar,
                    customTabSession.id,
                    isPrivate = customTabSession.private
                    isPrivate = customTabSession.content.private
                )
            } else {
                DefaultToolbarIntegration(
+4 −4
Original line number Diff line number Diff line
@@ -15,8 +15,8 @@ import androidx.annotation.VisibleForTesting
import androidx.core.view.isVisible
import com.google.android.material.snackbar.Snackbar
import kotlinx.android.synthetic.main.browser_toolbar_popup_window.view.*
import mozilla.components.browser.session.Session
import mozilla.components.browser.state.selector.selectedTab
import mozilla.components.browser.state.state.CustomTabSessionState
import mozilla.components.browser.state.store.BrowserStore
import org.mozilla.fenix.R
import org.mozilla.fenix.components.FenixSnackbar
@@ -27,7 +27,7 @@ import java.lang.ref.WeakReference
object ToolbarPopupWindow {
    fun show(
        view: WeakReference<View>,
        customTabSession: Session? = null,
        customTabSession: CustomTabSessionState? = null,
        handlePasteAndGo: (String) -> Unit,
        handlePaste: (String) -> Unit,
        copyVisible: Boolean = true
@@ -101,10 +101,10 @@ object ToolbarPopupWindow {
    @VisibleForTesting
    internal fun getUrlForClipboard(
        store: BrowserStore,
        customTabSession: Session? = null
        customTabSession: CustomTabSessionState? = null
    ): String? {
        return if (customTabSession != null) {
            customTabSession.url
            customTabSession.content.url
        } else {
            val selectedTab = store.state.selectedTab
            selectedTab?.readerState?.activeUrl ?: selectedTab?.content?.url
+2 −4
Original line number Diff line number Diff line
@@ -4,11 +4,10 @@

package org.mozilla.fenix.utils

import io.mockk.every
import io.mockk.mockk
import mozilla.components.browser.session.Session
import mozilla.components.browser.state.state.BrowserState
import mozilla.components.browser.state.state.ReaderState
import mozilla.components.browser.state.state.createCustomTab
import mozilla.components.browser.state.state.createTab
import mozilla.components.browser.state.store.BrowserStore
import org.junit.Assert.assertEquals
@@ -21,8 +20,7 @@ class ToolbarPopupWindowTest {

    @Test
    fun getUrlForClipboard() {
        val customTabSession: Session = mockk()
        every { customTabSession.url } returns "https://mozilla.org"
        val customTabSession = createCustomTab("https://mozilla.org")

        // Custom tab
        assertEquals(