Skip to content

Commit ea5bd26

Browse files
Amejia481pocmo
authored andcommitted
Closes mozilla-mobile#11365: Address breaking changes on GV 96.0.20211204095400
1 parent 43fc046 commit ea5bd26

File tree

5 files changed

+9
-165
lines changed

5 files changed

+9
-165
lines changed

components/browser/engine-gecko/src/main/java/mozilla/components/browser/engine/gecko/GeckoEngine.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class GeckoEngine(
7272
private val runtime: GeckoRuntime = GeckoRuntime.getDefault(context),
7373
executorProvider: () -> GeckoWebExecutor = { GeckoWebExecutor(runtime) },
7474
override val trackingProtectionExceptionStore: TrackingProtectionExceptionStorage =
75-
TrackingProtectionExceptionFileStorage(context, runtime)
75+
TrackingProtectionExceptionFileStorage(runtime)
7676
) : Engine, WebExtensionRuntime {
7777
private val executor by lazy { executorProvider.invoke() }
7878
private val localeUpdater = LocaleSettingUpdater(context, runtime)
@@ -116,7 +116,6 @@ class GeckoEngine(
116116
@Suppress("TooGenericExceptionThrown")
117117
throw RuntimeException("GeckoRuntime is shutting down")
118118
}
119-
trackingProtectionExceptionStore.restore()
120119
}
121120

122121
/**

components/browser/engine-gecko/src/main/java/mozilla/components/browser/engine/gecko/TrackingProtectionExceptionFileStorage.kt

Lines changed: 4 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,21 @@
44

55
package mozilla.components.browser.engine.gecko
66

7-
import android.content.Context
8-
import android.util.AtomicFile
97
import androidx.annotation.VisibleForTesting
108
import kotlinx.coroutines.CoroutineScope
119
import kotlinx.coroutines.Dispatchers
12-
import kotlinx.coroutines.launch
1310
import mozilla.components.browser.engine.gecko.content.blocking.GeckoTrackingProtectionException
1411
import mozilla.components.browser.engine.gecko.ext.geckoTrackingProtectionPermission
1512
import mozilla.components.browser.engine.gecko.ext.isExcludedForTrackingProtection
1613
import mozilla.components.concept.engine.EngineSession
1714
import mozilla.components.concept.engine.content.blocking.TrackingProtectionException
1815
import mozilla.components.concept.engine.content.blocking.TrackingProtectionExceptionStorage
19-
import mozilla.components.support.base.log.logger.Logger
2016
import mozilla.components.support.ktx.kotlin.getOrigin
2117
import mozilla.components.support.ktx.kotlin.stripDefaultPort
22-
import mozilla.components.support.ktx.util.readAndDeserialize
23-
import org.json.JSONArray
24-
import org.mozilla.geckoview.ContentBlockingController
2518
import org.mozilla.geckoview.GeckoRuntime
2619
import org.mozilla.geckoview.GeckoSession.PermissionDelegate.ContentPermission
2720
import org.mozilla.geckoview.GeckoSession.PermissionDelegate.ContentPermission.VALUE_ALLOW
2821
import org.mozilla.geckoview.GeckoSession.PermissionDelegate.ContentPermission.VALUE_DENY
29-
import java.io.File
3022

3123
internal const val STORE_FILE_NAME =
3224
"mozilla_components_tracking_protection_storage_gecko.json"
@@ -35,23 +27,9 @@ internal const val STORE_FILE_NAME =
3527
* A [TrackingProtectionExceptionStorage] implementation to store tracking protection exceptions.
3628
*/
3729
internal class TrackingProtectionExceptionFileStorage(
38-
private val context: Context,
3930
private val runtime: GeckoRuntime
4031
) : TrackingProtectionExceptionStorage {
41-
private val fileLock = Any()
4232
internal var scope = CoroutineScope(Dispatchers.IO)
43-
private val logger = Logger("TrackingProtectionExceptionFileStorage")
44-
45-
/**
46-
* Restore all exceptions from the [STORE_FILE_NAME] file,
47-
* and provides them to the gecko [runtime].
48-
*/
49-
override fun restore() {
50-
if (!isMigrationOver(context)) {
51-
logger.info("Starting tracking protection exceptions migration")
52-
migrateExceptions()
53-
}
54-
}
5533

5634
override fun contains(session: EngineSession, onResult: (Boolean) -> Unit) {
5735
val url = (session as GeckoEngineSession).currentUrl
@@ -77,7 +55,10 @@ internal class TrackingProtectionExceptionFileStorage(
7755

7856
private fun List<ContentPermission>?.filterTrackingProtectionExceptions(url: String) =
7957
this.orEmpty()
80-
.filter { it.isExcludedForTrackingProtection && it.uri.getOrigin().orEmpty().stripDefaultPort() == url }
58+
.filter {
59+
it.isExcludedForTrackingProtection && it.uri.getOrigin().orEmpty()
60+
.stripDefaultPort() == url
61+
}
8162

8263
override fun add(session: EngineSession, persistInPrivateMode: Boolean) {
8364
val geckoEngineSession = (session as GeckoEngineSession)
@@ -161,61 +142,6 @@ internal class TrackingProtectionExceptionFileStorage(
161142
onRemove.invoke()
162143
}
163144
}
164-
165-
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
166-
internal fun getFile(context: Context): AtomicFile {
167-
return AtomicFile(
168-
File(
169-
context.filesDir,
170-
STORE_FILE_NAME
171-
)
172-
)
173-
}
174-
175-
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
176-
internal fun isMigrationOver(context: Context): Boolean {
177-
/*
178-
* We only keep around the exceptions file until
179-
* the migration is over [STORE_FILE_NAME],
180-
* after we migrate exceptions we delete the file.
181-
* */
182-
return File(context.filesDir, STORE_FILE_NAME).exists()
183-
}
184-
185-
/**
186-
* As part of the migration process, we have to load all exceptions from our
187-
* file [STORE_FILE_NAME] into geckoView once, after that we can remove our,
188-
* file [STORE_FILE_NAME].
189-
*/
190-
@Suppress("Deprecation")
191-
internal fun migrateExceptions() {
192-
scope.launch {
193-
synchronized(fileLock) {
194-
getFile(context).readAndDeserialize { json ->
195-
if (json.isNotEmpty()) {
196-
val jsonArray = JSONArray(json)
197-
val exceptionList = (0 until jsonArray.length()).map { index ->
198-
val jsonObject = jsonArray.getJSONObject(index)
199-
ContentBlockingController.ContentBlockingException.fromJson(jsonObject)
200-
}
201-
runtime.contentBlockingController.restoreExceptionList(exceptionList)
202-
}
203-
}
204-
removeFileFromDisk(context)
205-
logger.debug("Finished tracking protection exceptions migration")
206-
}
207-
}
208-
}
209-
210-
@VisibleForTesting
211-
internal fun removeFileFromDisk(context: Context) {
212-
scope.launch {
213-
synchronized(fileLock) {
214-
getFile(context)
215-
.delete()
216-
}
217-
}
218-
}
219145
}
220146

221147
private fun ContentPermission.toTrackingProtectionException(): GeckoTrackingProtectionException {

components/browser/engine-gecko/src/test/java/mozilla/components/browser/engine/gecko/GeckoEngineTest.kt

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import mozilla.components.concept.engine.EngineSession.TrackingProtectionPolicy.
2222
import mozilla.components.concept.engine.EngineSession.TrackingProtectionPolicy.TrackingCategory
2323
import mozilla.components.concept.engine.UnsupportedSettingException
2424
import mozilla.components.concept.engine.content.blocking.TrackerLog
25-
import mozilla.components.concept.engine.content.blocking.TrackingProtectionExceptionStorage
2625
import mozilla.components.concept.engine.mediaquery.PreferredColorScheme
2726
import mozilla.components.concept.engine.webextension.Action
2827
import mozilla.components.concept.engine.webextension.WebExtension
@@ -639,12 +638,12 @@ class GeckoEngineTest {
639638
engine.settings.safeBrowsingPolicy = arrayOf(SafeBrowsingPolicy.PHISHING)
640639
engine.settings.trackingProtectionPolicy =
641640
TrackingProtectionPolicy.select(
642-
trackingCategories = arrayOf(TrackingProtectionPolicy.TrackingCategory.AD),
641+
trackingCategories = arrayOf(TrackingCategory.AD),
643642
cookiePolicy = CookiePolicy.ACCEPT_ONLY_FIRST_PARTY
644643
)
645644

646645
assertEquals(
647-
TrackingProtectionPolicy.TrackingCategory.AD.id,
646+
TrackingCategory.AD.id,
648647
contentBlockingSettings.antiTrackingCategories
649648
)
650649

@@ -1379,7 +1378,7 @@ class GeckoEngineTest {
13791378
metaData = mockNativeWebExtensionMetaData(allowedInPrivateBrowsing = false)
13801379
)
13811380

1382-
val installedExtensions = listOf<GeckoWebExtension>(installedExtension)
1381+
val installedExtensions = listOf(installedExtension)
13831382
val installedExtensionResult = GeckoResult<List<GeckoWebExtension>>()
13841383

13851384
val runtime = mock<GeckoRuntime>()
@@ -1747,15 +1746,6 @@ class GeckoEngineTest {
17471746
assertTrue(version.isAtLeast(69, 0, 0))
17481747
}
17491748

1750-
@Test
1751-
fun `after init is called the trackingProtectionExceptionStore must be restored`() {
1752-
val mockStore: TrackingProtectionExceptionStorage = mock()
1753-
val runtime: GeckoRuntime = mock()
1754-
GeckoEngine(context, runtime = runtime, trackingProtectionExceptionStore = mockStore)
1755-
1756-
verify(mockStore).restore()
1757-
}
1758-
17591749
@Test
17601750
fun `fetch trackers logged successfully`() {
17611751
val runtime = mock<GeckoRuntime>()

components/browser/engine-gecko/src/test/java/mozilla/components/browser/engine/gecko/TrackingProtectionExceptionFileStorageTest.kt

Lines changed: 1 addition & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,27 @@
11
/* This Source Code Form is subject to the terms of the Mozilla Public
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4-
@file:Suppress("DEPRECATION")
54
package mozilla.components.browser.engine.gecko
65

7-
import android.app.Activity
8-
import android.content.Context
9-
import android.util.AtomicFile
106
import androidx.test.ext.junit.runners.AndroidJUnit4
117
import kotlinx.coroutines.CoroutineScope
128
import kotlinx.coroutines.Dispatchers
139
import mozilla.components.browser.engine.gecko.content.blocking.GeckoTrackingProtectionException
1410
import mozilla.components.browser.engine.gecko.permission.geckoContentPermission
1511
import mozilla.components.concept.engine.EngineSession
1612
import mozilla.components.concept.engine.content.blocking.TrackingProtectionException
17-
import mozilla.components.support.ktx.util.readAndDeserialize
18-
import mozilla.components.support.ktx.util.writeString
19-
import mozilla.components.support.test.any
2013
import mozilla.components.support.test.mock
21-
import mozilla.components.support.test.robolectric.testContext
2214
import mozilla.components.support.test.whenever
2315
import org.junit.Assert.assertEquals
2416
import org.junit.Assert.assertFalse
25-
import org.junit.Assert.assertNotNull
26-
import org.junit.Assert.assertNull
2717
import org.junit.Assert.assertTrue
2818
import org.junit.Before
2919
import org.junit.Test
3020
import org.junit.runner.RunWith
3121
import org.mockito.Mockito.anyString
3222
import org.mockito.Mockito.doNothing
3323
import org.mockito.Mockito.spy
34-
import org.mockito.Mockito.times
3524
import org.mockito.Mockito.verify
36-
import org.mozilla.geckoview.ContentBlockingController
37-
import org.mozilla.geckoview.ContentBlockingController.ContentBlockingException
3825
import org.mozilla.geckoview.GeckoResult
3926
import org.mozilla.geckoview.GeckoRuntime
4027
import org.mozilla.geckoview.GeckoSession
@@ -43,8 +30,6 @@ import org.mozilla.geckoview.GeckoSession.PermissionDelegate.ContentPermission.V
4330
import org.mozilla.geckoview.GeckoSession.PermissionDelegate.ContentPermission.VALUE_DENY
4431
import org.mozilla.geckoview.GeckoSession.PermissionDelegate.PERMISSION_TRACKING
4532
import org.mozilla.geckoview.StorageController
46-
import org.robolectric.Robolectric.buildActivity
47-
import java.io.File
4833

4934
@RunWith(AndroidJUnit4::class)
5035
class TrackingProtectionExceptionFileStorageTest {
@@ -53,65 +38,14 @@ class TrackingProtectionExceptionFileStorageTest {
5338

5439
private lateinit var storage: TrackingProtectionExceptionFileStorage
5540

56-
private val context: Context
57-
get() = buildActivity(Activity::class.java).get()
58-
5941
@Before
6042
fun setup() {
6143
runtime = mock()
6244
whenever(runtime.settings).thenReturn(mock())
63-
storage = spy(TrackingProtectionExceptionFileStorage(testContext, runtime))
45+
storage = spy(TrackingProtectionExceptionFileStorage(runtime))
6446
storage.scope = CoroutineScope(Dispatchers.Main)
6547
}
6648

67-
@Test
68-
fun `GIVEN the migration has not been completed WHEN restoring THEN migrate exceptions`() {
69-
70-
whenever(storage.isMigrationOver(testContext)).thenReturn(false)
71-
doNothing().`when`(storage).migrateExceptions()
72-
73-
storage.restore()
74-
75-
verify(storage).migrateExceptions()
76-
}
77-
78-
@Test
79-
fun `GIVEN the migration has been completed WHEN restoring THEN not migrate exceptions`() {
80-
81-
whenever(storage.isMigrationOver(testContext)).thenReturn(true)
82-
83-
storage.restore()
84-
85-
verify(storage, times(0)).migrateExceptions()
86-
}
87-
88-
@Test
89-
@Deprecated("Remove migration code on 96 as GeckoView is going to remove restoreExceptionList on that version")
90-
fun `WHEN migrating exceptions THEN exceptions on disk will be restored on gecko and removed from disk`() {
91-
val exceptionsFile = AtomicFile(
92-
File(context.filesDir, STORE_FILE_NAME)
93-
)
94-
val exceptionStringJSON =
95-
"[{\"principal\":\"eyIxIjp7IjAiOiJodHRwczovL3d3dy5jbm4uY29tLyJ9fQ==\",\"uri\":\"https:\\/\\/www.cnn.com\\/\"}]"
96-
97-
exceptionsFile.writeString { exceptionStringJSON }
98-
99-
val mockContentBlocking = mock<ContentBlockingController>()
100-
101-
whenever(runtime.contentBlockingController).thenReturn(mockContentBlocking)
102-
103-
storage.scope = CoroutineScope(Dispatchers.Main)
104-
105-
assertNotNull(storage.getFile(context).readAndDeserialize { })
106-
107-
storage.migrateExceptions()
108-
109-
verify(mockContentBlocking).restoreExceptionList(any<List<ContentBlockingException>>())
110-
verify(storage).removeFileFromDisk(any())
111-
112-
assertNull(storage.getFile(context).readAndDeserialize { })
113-
}
114-
11549
@Test
11650
fun `GIVEN a new exception WHEN adding THEN the exception is stored on the gecko storage`() {
11751
val storageController = mock<StorageController>()

components/concept/engine/src/main/java/mozilla/components/concept/engine/content/blocking/TrackingProtectionExceptionStorage.kt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,4 @@ interface TrackingProtectionExceptionStorage {
5454
* @param onRemove A callback to inform that the list of active sessions has been removed
5555
*/
5656
fun removeAll(activeSessions: List<EngineSession>? = null, onRemove: () -> Unit = {})
57-
58-
/**
59-
* Restore all domains stored in the storage.
60-
*/
61-
fun restore()
6257
}

0 commit comments

Comments
 (0)