Skip to content

Commit 824b189

Browse files
Abbondanzometa-codesync[bot]
authored andcommitted
Stop crashing on invalid background url() uris on Android
Summary: Building an image request validates its uri and throws for one the pipeline can't load, such as a `res:` uri without a resource id. That ran in the `backgroundImage` prop setter on the UI thread, so a bad style value crashed the app. The loader now logs a warning and skips that layer, like any other failed load. Also replaces `conic-gradient` as the unknown layer type in a parsing test with a made-up type, since conic gradients are supported now. Changelog: [Internal] Differential Revision: D135152981
1 parent 7924f73 commit 824b189

3 files changed

Lines changed: 33 additions & 4 deletions

File tree

‎packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/drawable/BackgroundImageURLLoader.kt‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,15 @@ internal class BackgroundImageURLLoader(
115115
ImageDecodeOptions.newBuilder().setForceStaticImage(true).build()
116116
)
117117
}
118-
val dataSource =
119-
Fresco.getImagePipeline().fetchDecodedImage(requestBuilder.build(), CALLER_CONTEXT)
118+
val imageRequest =
119+
try {
120+
requestBuilder.build()
121+
} catch (e: RuntimeException) {
122+
// `build()` validates the uri, e.g. a `res:` uri must name a resource id.
123+
FLog.w(ReactConstants.TAG, e, "Invalid background image uri: %s", uri)
124+
return
125+
}
126+
val dataSource = Fresco.getImagePipeline().fetchDecodedImage(imageRequest, CALLER_CONTEXT)
120127
this.dataSource = dataSource
121128
dataSource.subscribe(
122129
object : BaseDataSubscriber<CloseableReference<CloseableImage>>() {

‎packages/react-native/ReactAndroid/src/test/java/com/facebook/react/uimanager/drawable/BackgroundImageURLLoaderTest.kt‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,18 @@
88
package com.facebook.react.uimanager.drawable
99

1010
import android.graphics.Bitmap
11+
import com.facebook.imagepipeline.core.ImagePipelineFactory
12+
import com.facebook.react.bridge.JavaOnlyMap
13+
import com.facebook.react.uimanager.style.BackgroundImageLayer
1114
import org.assertj.core.api.Assertions.*
15+
import org.junit.After
1216
import org.junit.Test
1317
import org.junit.runner.RunWith
1418
import org.robolectric.RobolectricTestRunner
19+
import org.robolectric.RuntimeEnvironment
1520
import org.robolectric.annotation.GraphicsMode
1621

17-
/** Tests for [BackgroundImageURLLoader.LoadedImage] */
22+
/** Tests for [BackgroundImageURLLoader] */
1823
@RunWith(RobolectricTestRunner::class)
1924
@GraphicsMode(GraphicsMode.Mode.NATIVE)
2025
class BackgroundImageURLLoaderTest {
@@ -50,4 +55,21 @@ class BackgroundImageURLLoaderTest {
5055
assertThat(image.height).isEqualTo(40f)
5156
}
5257
}
58+
59+
@After
60+
fun tearDown() {
61+
ImagePipelineFactory.shutDown()
62+
}
63+
64+
@Test
65+
fun testTreatsInvalidResourceUriAsFailedLoad() {
66+
val context = RuntimeEnvironment.getApplication()
67+
ImagePipelineFactory.initialize(context)
68+
val loader = BackgroundImageURLLoader(context) {}
69+
for (uri in listOf("res:///icon", "res:icon")) {
70+
val layer = BackgroundImageLayer.parse(JavaOnlyMap.of("type", "url", "uri", uri), context)
71+
loader.loadBackgroundImages(listOfNotNull(layer))
72+
assertThat(loader.imageForUri(uri)).isNull()
73+
}
74+
}
5375
}

‎packages/react-native/ReactAndroid/src/test/java/com/facebook/react/uimanager/style/BackgroundImageLayerTest.kt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ class BackgroundImageLayerTest {
103103

104104
@Test
105105
fun testRejectsUnknownType() {
106-
assertThat(BackgroundImageLayer.parse(JavaOnlyMap.of("type", "conic-gradient"), context))
106+
assertThat(BackgroundImageLayer.parse(JavaOnlyMap.of("type", "unknown-gradient"), context))
107107
.isNull()
108108
assertThat(BackgroundImageLayer.parse(null, context)).isNull()
109109
}

0 commit comments

Comments
 (0)