From d36b1b48be1235c8647b154b2f1c7187e3a0d30e Mon Sep 17 00:00:00 2001 From: Arturo Mejia Date: Tue, 13 Dec 2022 15:45:28 -0500 Subject: [PATCH] [fenix] Bug 1797577 - Clear site data when turning off cookie banner handling. --- .../CookieBannerDetailsController.kt | 22 +++++++ ...efaultCookieBannerDetailsControllerTest.kt | 62 +++++++++++++++---- 2 files changed, 71 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/org/mozilla/fenix/settings/quicksettings/protections/cookiebanners/CookieBannerDetailsController.kt b/app/src/main/java/org/mozilla/fenix/settings/quicksettings/protections/cookiebanners/CookieBannerDetailsController.kt index 1da84b5a4e..1f8ff60cc2 100644 --- a/app/src/main/java/org/mozilla/fenix/settings/quicksettings/protections/cookiebanners/CookieBannerDetailsController.kt +++ b/app/src/main/java/org/mozilla/fenix/settings/quicksettings/protections/cookiebanners/CookieBannerDetailsController.kt @@ -5,6 +5,8 @@ package org.mozilla.fenix.settings.quicksettings.protections.cookiebanners import android.content.Context +import androidx.annotation.VisibleForTesting +import androidx.core.net.toUri import androidx.fragment.app.Fragment import androidx.navigation.NavController import kotlinx.coroutines.CoroutineScope @@ -14,9 +16,11 @@ import kotlinx.coroutines.withContext import mozilla.components.browser.state.selector.findTabOrCustomTab import mozilla.components.browser.state.state.SessionState import mozilla.components.browser.state.store.BrowserStore +import mozilla.components.concept.engine.Engine import mozilla.components.concept.engine.cookiehandling.CookieBannersStorage import mozilla.components.concept.engine.permission.SitePermissions import mozilla.components.feature.session.SessionUseCases +import mozilla.components.lib.publicsuffixlist.PublicSuffixList import mozilla.components.service.glean.private.NoExtras import org.mozilla.fenix.GleanMetrics.CookieBanners import org.mozilla.fenix.browser.BrowserFragmentDirections @@ -60,6 +64,8 @@ class DefaultCookieBannerDetailsController( private val gravity: Int, private val getCurrentTab: () -> SessionState?, private val reload: SessionUseCases.ReloadUrlUseCase, + private val engine: Engine = context.components.core.engine, + private val publicSuffixList: PublicSuffixList = context.components.publicSuffixList, ) : CookieBannerDetailsController { override fun handleBackPressed() { @@ -105,6 +111,7 @@ class DefaultCookieBannerDetailsController( ) CookieBanners.exceptionRemoved.record(NoExtras()) } else { + clearSiteData(tab) cookieBannersStorage.addException(uri = tab.content.url, privateBrowsing = tab.content.private) CookieBanners.exceptionAdded.record(NoExtras()) } @@ -116,4 +123,19 @@ class DefaultCookieBannerDetailsController( reload(tab.id) } } + + @VisibleForTesting + internal suspend fun clearSiteData(tab: SessionState) { + val host = tab.content.url.toUri().host.orEmpty() + val domain = publicSuffixList.getPublicSuffixPlusOne(host).await() + withContext(Dispatchers.Main) { + engine.clearData( + host = domain, + data = Engine.BrowsingData.select( + Engine.BrowsingData.AUTH_SESSIONS, + Engine.BrowsingData.ALL_SITE_DATA, + ), + ) + } + } } diff --git a/app/src/test/java/org/mozilla/fenix/settings/quicksettings/protections/cookiebanners/DefaultCookieBannerDetailsControllerTest.kt b/app/src/test/java/org/mozilla/fenix/settings/quicksettings/protections/cookiebanners/DefaultCookieBannerDetailsControllerTest.kt index 1745a52a34..904b5c28fc 100644 --- a/app/src/test/java/org/mozilla/fenix/settings/quicksettings/protections/cookiebanners/DefaultCookieBannerDetailsControllerTest.kt +++ b/app/src/test/java/org/mozilla/fenix/settings/quicksettings/protections/cookiebanners/DefaultCookieBannerDetailsControllerTest.kt @@ -9,22 +9,28 @@ import androidx.fragment.app.Fragment import androidx.navigation.NavController import androidx.navigation.NavDirections import io.mockk.MockKAnnotations +import io.mockk.Runs +import io.mockk.coEvery import io.mockk.coVerifyOrder import io.mockk.every import io.mockk.impl.annotations.MockK +import io.mockk.just import io.mockk.mockk import io.mockk.slot import io.mockk.spyk import io.mockk.verify +import kotlinx.coroutines.CompletableDeferred import kotlinx.coroutines.test.advanceUntilIdle import mozilla.components.browser.state.state.BrowserState import mozilla.components.browser.state.state.TabSessionState import mozilla.components.browser.state.state.createTab import mozilla.components.browser.state.store.BrowserStore +import mozilla.components.concept.engine.Engine import mozilla.components.concept.engine.cookiehandling.CookieBannersStorage import mozilla.components.concept.engine.permission.SitePermissions import mozilla.components.feature.session.SessionUseCases import mozilla.components.feature.session.TrackingProtectionUseCases +import mozilla.components.lib.publicsuffixlist.PublicSuffixList import mozilla.components.service.glean.testing.GleanTestRule import mozilla.components.support.test.robolectric.testContext import mozilla.components.support.test.rule.MainCoroutineRule @@ -70,6 +76,12 @@ internal class DefaultCookieBannerDetailsControllerTest { @MockK(relaxed = true) private lateinit var reload: SessionUseCases.ReloadUrlUseCase + @MockK(relaxed = true) + private lateinit var engine: Engine + + @MockK(relaxed = true) + private lateinit var publicSuffixList: PublicSuffixList + private var gravity = 54 @get:Rule @@ -86,19 +98,23 @@ internal class DefaultCookieBannerDetailsControllerTest { context = spyk(testContext) tab = createTab("https://mozilla.org") browserStore = BrowserStore(BrowserState(tabs = listOf(tab))) - controller = DefaultCookieBannerDetailsController( - fragment = fragment, - context = context, - ioScope = scope, - cookieBannersStorage = cookieBannersStorage, - navController = { navController }, - sitePermissions = sitePermissions, - gravity = gravity, - getCurrentTab = { tab }, - sessionId = tab.id, - browserStore = browserStore, - protectionsStore = protectionsStore, - reload = reload, + controller = spyk( + DefaultCookieBannerDetailsController( + fragment = fragment, + context = context, + ioScope = scope, + cookieBannersStorage = cookieBannersStorage, + navController = { navController }, + sitePermissions = sitePermissions, + gravity = gravity, + getCurrentTab = { tab }, + sessionId = tab.id, + browserStore = browserStore, + protectionsStore = protectionsStore, + engine = engine, + publicSuffixList = publicSuffixList, + reload = reload, + ), ) every { fragment.context } returns context @@ -159,12 +175,14 @@ internal class DefaultCookieBannerDetailsControllerTest { assertNull(CookieBanners.exceptionRemoved.testGetValue()) every { protectionsStore.dispatch(any()) } returns mockk() + coEvery { controller.clearSiteData(any()) } just Runs controller.handleTogglePressed(isEnabled) advanceUntilIdle() coVerifyOrder { + controller.clearSiteData(tab) cookieBannersStorage.addException( uri = tab.content.url, privateBrowsing = tab.content.private, @@ -179,4 +197,22 @@ internal class DefaultCookieBannerDetailsControllerTest { assertNotNull(CookieBanners.exceptionAdded.testGetValue()) } + + @Test + fun `WHEN clearSiteData THEN delegate the call to the engine`() = + runTestOnMain { + coEvery { publicSuffixList.getPublicSuffixPlusOne(any()) } returns CompletableDeferred("mozilla.org") + + controller.clearSiteData(tab) + + coVerifyOrder { + engine.clearData( + host = "mozilla.org", + data = Engine.BrowsingData.select( + Engine.BrowsingData.AUTH_SESSIONS, + Engine.BrowsingData.ALL_SITE_DATA, + ), + ) + } + } }