-
Notifications
You must be signed in to change notification settings - Fork 60
[FEAT] WebView: Full support for new architecture #1060
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
marco-saia-datadog
merged 2 commits into
develop
from
marcosaia/feat/full-new-arch-webview-support
Dec 11, 2025
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
...iew/android/src/newarch/com/datadog/reactnative/webview/DdSdkReactNativeWebViewManager.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| /* | ||
| * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
| * This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
| * Copyright 2016-Present Datadog, Inc. | ||
| */ | ||
|
|
||
| package com.datadog.reactnative.webview | ||
|
|
||
| import android.annotation.SuppressLint | ||
| import com.datadog.android.api.SdkCore | ||
| import com.datadog.android.webview.WebViewTracking | ||
| import com.datadog.reactnative.DatadogSDKWrapperStorage | ||
| import com.facebook.react.bridge.ReactContext | ||
| import com.facebook.react.uimanager.ThemedReactContext | ||
| import com.reactnativecommunity.webview.RNCWebView | ||
| import com.reactnativecommunity.webview.RNCWebViewClient | ||
| import com.reactnativecommunity.webview.RNCWebViewManager | ||
| import com.reactnativecommunity.webview.RNCWebViewWrapper | ||
| import org.json.JSONArray | ||
|
|
||
| /** | ||
| * The entry point to use Datadog auto-instrumented WebView feature. | ||
| */ | ||
| class DdSdkReactNativeWebViewManager( | ||
| private val reactContext: ReactContext | ||
| ) : RNCWebViewManager() { | ||
| // The name used to reference this custom View from React Native. | ||
| override fun getName(): String { | ||
| return VIEW_NAME | ||
| } | ||
|
|
||
| /** | ||
| * The instance of Datadog SDK Core. | ||
| */ | ||
| @Volatile private var _datadogCore: SdkCore? = null | ||
| val datadogCore: SdkCore? | ||
| get() = _datadogCore | ||
|
|
||
| init { | ||
| DatadogSDKWrapperStorage.addOnInitializedListener { core -> | ||
| _datadogCore = core | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Intercepts the WebView wrapper instance before it is returned and ensures that | ||
| * JavaScript is enabled on the underlying WebView. JavaScript must be enabled | ||
| * for Datadog WebView tracking to function correctly. | ||
| */ | ||
| @SuppressLint("SetJavaScriptEnabled") | ||
| override fun createViewInstance(context: ThemedReactContext): RNCWebViewWrapper { | ||
| val viewInstance = super.createViewInstance(context) | ||
| viewInstance.webView.settings.javaScriptEnabled = true | ||
| return viewInstance | ||
| } | ||
|
|
||
| /** | ||
| * Intercepts the JavaScript injected before the WebView loads. | ||
| * | ||
| * In the New Architecture, WebView props from React Native are ignored, | ||
| * so this callback is the only reliable place to extract the | ||
| * `// #allowedHosts=<JSON>` configuration and apply Datadog WebView tracking. | ||
| */ | ||
| override fun setInjectedJavaScriptBeforeContentLoaded( | ||
| view: RNCWebViewWrapper?, | ||
| value: String? | ||
| ) { | ||
| val allowedHosts = value?.let { extractAllowedHosts(it) } | ||
| val webView = view?.webView | ||
|
|
||
| if (allowedHosts != null && webView != null) { | ||
| configureWebViewTracking(webView, allowedHosts) | ||
| } | ||
|
|
||
| super.setInjectedJavaScriptBeforeContentLoaded(view, value) | ||
| } | ||
|
|
||
| private fun configureWebViewTracking(webView: RNCWebView, allowedHosts: List<String>) { | ||
| val datadogCore = _datadogCore | ||
| if (datadogCore != null) { | ||
| WebViewTracking.enable( | ||
| webView, | ||
| allowedHosts = allowedHosts, | ||
| sdkCore = datadogCore | ||
| ) | ||
| } else { | ||
| DatadogSDKWrapperStorage.addOnInitializedListener { core -> | ||
| reactContext.runOnUiQueueThread { | ||
| WebViewTracking.enable( | ||
| webView, | ||
| allowedHosts = allowedHosts, | ||
| sdkCore = core | ||
| ) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| override fun addEventEmitters( | ||
| reactContext: ThemedReactContext, | ||
| view: RNCWebViewWrapper | ||
| ) { | ||
| view.webView.webViewClient = RNCWebViewClient() | ||
| } | ||
|
|
||
| companion object { | ||
| // The name used to reference this custom View from React Native. | ||
| const val VIEW_NAME = "DdReactNativeWebView" | ||
|
|
||
| private fun extractAllowedHosts(input: String): List<String>? { | ||
| // Regex that captures everything after "// #allowedHosts=" | ||
| val regex = Regex("""//\s*#allowedHosts\s*=\s*(.+)""") | ||
|
|
||
| val match = regex.find(input) ?: return null | ||
| val jsonString = match.groupValues[1].trim() | ||
|
|
||
| return try { | ||
| val jsonArray = JSONArray(jsonString) | ||
| (0 until jsonArray.length()).map { jsonArray.getString(it) } | ||
| } catch (e: Exception) { | ||
| null | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
...view/android/src/testNewArch/kotlin/com/datadog/reactnative/webview/DatadogWebViewTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| /* | ||
| * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
| * This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
| * Copyright 2016-Present Datadog, Inc. | ||
| */ | ||
|
|
||
| package com.datadog.reactnative.webview | ||
|
|
||
| import com.datadog.android.api.SdkCore | ||
| import com.datadog.android.core.InternalSdkCore | ||
| import com.datadog.android.webview.WebViewTracking | ||
| import com.datadog.reactnative.DatadogSDKWrapperStorage | ||
| import main.reactnative.tools.unit.GenericAssert.Companion.assertThat | ||
| import com.facebook.react.bridge.JavaOnlyArray | ||
| import com.facebook.react.uimanager.ThemedReactContext | ||
| import com.reactnativecommunity.webview.RNCWebView | ||
| import com.reactnativecommunity.webview.RNCWebViewWrapper | ||
| import org.junit.jupiter.api.AfterEach | ||
| import org.junit.jupiter.api.BeforeEach | ||
| import org.junit.jupiter.api.Test | ||
| import org.junit.jupiter.api.extension.ExtendWith | ||
| import org.junit.jupiter.api.extension.Extensions | ||
| import org.mockito.Mock | ||
| import org.mockito.MockedStatic | ||
| import org.mockito.Mockito | ||
| import org.mockito.Mockito.mock | ||
| import org.mockito.junit.jupiter.MockitoExtension | ||
| import org.mockito.junit.jupiter.MockitoSettings | ||
| import org.mockito.kotlin.any | ||
| import org.mockito.kotlin.doReturn | ||
| import org.mockito.kotlin.verify | ||
| import org.mockito.kotlin.whenever | ||
| import org.mockito.quality.Strictness | ||
|
|
||
| @Extensions( | ||
| ExtendWith(MockitoExtension::class) | ||
| ) | ||
| @MockitoSettings(strictness = Strictness.LENIENT) | ||
| internal class DatadogWebViewTest { | ||
|
|
||
| @Mock | ||
| lateinit var themedReactContext: ThemedReactContext | ||
|
|
||
| @Mock | ||
| lateinit var datadogCore: InternalSdkCore | ||
|
|
||
| private lateinit var webViewTrackingMockedStatic: MockedStatic<WebViewTracking> | ||
|
|
||
| @BeforeEach | ||
| fun `set up`() { | ||
| whenever(themedReactContext.runOnUiQueueThread(any())).thenAnswer { answer -> | ||
| answer.getArgument<Runnable>(0).run() | ||
| true | ||
| } | ||
|
|
||
| webViewTrackingMockedStatic = Mockito.mockStatic(WebViewTracking::class.java) | ||
| webViewTrackingMockedStatic.`when`<Unit> { | ||
| WebViewTracking.enable( | ||
| webView = any(), // Mock the WebView parameter | ||
| allowedHosts = any(), // Mock the list of allowed hosts | ||
| logsSampleRate = any(), // Mock the logsSampleRate parameter | ||
| sdkCore = any() // Mock the SdkCore parameter | ||
| ) | ||
| }.then {} // Return Unit as the function has no return value | ||
| } | ||
|
|
||
| @AfterEach | ||
| fun `tear down`() { | ||
| webViewTrackingMockedStatic.close() | ||
| } | ||
|
|
||
| @Test | ||
| fun `Datadog Core is set once initialized`() { | ||
| val manager = DdSdkReactNativeWebViewManager(themedReactContext) | ||
| assertThat(manager.datadogCore).isNull() | ||
|
|
||
| DatadogSDKWrapperStorage.notifyOnInitializedListeners(datadogCore) | ||
|
|
||
| assertThat(manager.datadogCore).isNotNull() | ||
| assertThat(manager.datadogCore).isInstanceOf(SdkCore::class.java) | ||
| } | ||
|
|
||
| @Test | ||
| fun `Registers to SdkCore listener if the SDK is not initialized`() { | ||
| // ========= | ||
| // Given | ||
| // ========= | ||
| val manager = DdSdkReactNativeWebViewManager(themedReactContext) | ||
|
|
||
| // When first initialized, the WebView manager core should be null | ||
| assertThat(manager.datadogCore).isNull() | ||
|
|
||
| // ========= | ||
| // When | ||
| // ========= | ||
| val rncWebView = mock(RNCWebView::class.java) | ||
| val rncWebViewWrapper = mock(RNCWebViewWrapper::class.java) | ||
| whenever(rncWebViewWrapper.webView) doReturn rncWebView | ||
|
|
||
| // When JS sends allowedHosts through 'injectedJavaScriptBeforeContentLoaded' prop | ||
| manager.setInjectedJavaScriptBeforeContentLoaded( | ||
| rncWebViewWrapper, | ||
| "// #allowedHosts=[\"example.com\",\"test.com\"]" | ||
| ) | ||
|
|
||
| // ========= | ||
| // Then | ||
| // ========= | ||
|
|
||
| // When we notify listeners that the core is available... | ||
| DatadogSDKWrapperStorage.notifyOnInitializedListeners(datadogCore) | ||
|
|
||
| // ...the WebView should enable WebView tracking in the UI Thread. | ||
| verify(themedReactContext).runOnUiQueueThread(any()) | ||
|
|
||
| // Native WebView tracking should be called | ||
| val arrayList = ArrayList(listOf("example.com", "test.com")) | ||
| webViewTrackingMockedStatic.verify { | ||
| WebViewTracking.enable(rncWebView, arrayList, 100.0f, datadogCore) | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.