Skip to content
This repository was archived by the owner on Nov 1, 2022. It is now read-only.

Commit 5d349c9

Browse files
colintheshotscsadilek
authored andcommitted
Fixes #1624: Session: Crash because trackersBlocked is empty
1 parent d9f5fdd commit 5d349c9

File tree

2 files changed

+37
-6
lines changed
  • components/browser/session/src

2 files changed

+37
-6
lines changed

components/browser/session/src/main/java/mozilla/components/browser/session/Session.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ class Session(
207207
* Tracker blocking state, true if blocking trackers is enabled, otherwise false.
208208
*/
209209
var trackerBlockingEnabled: Boolean by Delegates.observable(false) { _, old, new ->
210-
notifyObservers(old, new) { onTrackerBlockingEnabledChanged(this@Session, trackerBlockingEnabled) }
210+
notifyObservers(old, new) { onTrackerBlockingEnabledChanged(this@Session, new) }
211211
}
212212

213213
/**
@@ -216,7 +216,7 @@ class Session(
216216
var trackersBlocked: List<String> by Delegates.observable(emptyList()) { _, old, new ->
217217
notifyObservers(old, new) {
218218
if (new.isNotEmpty()) {
219-
onTrackerBlocked(this@Session, trackersBlocked.last(), trackersBlocked)
219+
onTrackerBlocked(this@Session, new.last(), new)
220220
}
221221
}
222222
}
@@ -227,7 +227,7 @@ class Session(
227227
var findResults: List<FindResult> by Delegates.observable(emptyList()) { _, old, new ->
228228
notifyObservers(old, new) {
229229
if (new.isNotEmpty()) {
230-
onFindResult(this@Session, findResults.last())
230+
onFindResult(this@Session, new.last())
231231
}
232232
}
233233
}

components/browser/session/src/test/java/mozilla/components/browser/session/SessionTest.kt

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
package mozilla.components.browser.session
66

77
import android.graphics.Bitmap
8+
import kotlinx.coroutines.Dispatchers.IO
9+
import kotlinx.coroutines.GlobalScope
10+
import kotlinx.coroutines.async
11+
import kotlinx.coroutines.runBlocking
812
import mozilla.components.browser.session.Session.Source
913
import mozilla.components.browser.session.tab.CustomTabConfig
1014
import mozilla.components.concept.engine.HitResult
@@ -169,7 +173,7 @@ class SessionTest {
169173
session.securityInfo = Session.SecurityInfo(true, "mozilla.org", "issuer")
170174

171175
assertEquals(Session.SecurityInfo(true, "mozilla.org", "issuer"),
172-
session.securityInfo)
176+
session.securityInfo)
173177

174178
assertNotNull(info)
175179
assertEquals(true, info!!.secure)
@@ -403,8 +407,8 @@ class SessionTest {
403407
session.title = "Internet for people, not profit — Mozilla"
404408

405409
verify(observer).onTitleChanged(
406-
eq(session),
407-
eq("Internet for people, not profit — Mozilla"))
410+
eq(session),
411+
eq("Internet for people, not profit — Mozilla"))
408412

409413
assertEquals("Internet for people, not profit — Mozilla", session.title)
410414
}
@@ -616,4 +620,31 @@ class SessionTest {
616620
assertTrue(appPermissionCallbackExecuted)
617621
assertTrue(session.appPermissionRequest.isConsumed())
618622
}
623+
624+
@Test
625+
fun `handle empty blocked trackers list race conditions`() {
626+
val observer = mock(Session.Observer::class.java)
627+
val observer2 = mock(Session.Observer::class.java)
628+
629+
val session = Session("about:blank")
630+
session.register(observer)
631+
session.register(observer2)
632+
633+
runBlocking {
634+
(1..3).map {
635+
val def = GlobalScope.async(IO) {
636+
session.trackersBlocked = emptyList()
637+
session.trackersBlocked += "test"
638+
session.trackersBlocked = emptyList()
639+
}
640+
val def2 = GlobalScope.async(IO) {
641+
session.trackersBlocked = emptyList()
642+
session.trackersBlocked += "test"
643+
session.trackersBlocked = emptyList()
644+
}
645+
def.await()
646+
def2.await()
647+
}
648+
}
649+
}
619650
}

0 commit comments

Comments
 (0)