|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +@file:Suppress("DEPRECATION_ERROR") // Conflicting okhttp versions |
| 9 | + |
| 10 | +package com.facebook.react.modules.network |
| 11 | + |
| 12 | +import android.os.Bundle |
| 13 | +import com.facebook.react.bridge.Arguments |
| 14 | +import com.facebook.react.bridge.ReactApplicationContext |
| 15 | +import com.facebook.react.bridge.WritableMap |
| 16 | +import com.facebook.react.bridge.buildReadableArray |
| 17 | +import com.facebook.react.internal.featureflags.ReactNativeFeatureFlags |
| 18 | +import java.net.SocketTimeoutException |
| 19 | +import okhttp3.Headers |
| 20 | +import okhttp3.Protocol |
| 21 | +import okhttp3.Request |
| 22 | +import okhttp3.Response |
| 23 | + |
| 24 | +/** |
| 25 | + * Utility class for reporting network lifecycle events to JavaScript and InspectorNetworkReporter. |
| 26 | + */ |
| 27 | +internal object NetworkEventUtil { |
| 28 | + @JvmStatic |
| 29 | + fun onCreateRequest(requestId: Int, request: Request) { |
| 30 | + if (ReactNativeFeatureFlags.enableNetworkEventReporting()) { |
| 31 | + val headersMap = okHttpHeadersToMap(request.headers()) |
| 32 | + InspectorNetworkReporter.reportRequestStart( |
| 33 | + requestId, |
| 34 | + request.url().toString(), |
| 35 | + request.method(), |
| 36 | + headersMap, |
| 37 | + request.body()?.toString().orEmpty(), |
| 38 | + request.body()?.contentLength() ?: 0, |
| 39 | + ) |
| 40 | + InspectorNetworkReporter.reportConnectionTiming(requestId, headersMap) |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + @JvmStatic |
| 45 | + fun onDataSend( |
| 46 | + reactContext: ReactApplicationContext?, |
| 47 | + requestId: Int, |
| 48 | + progress: Long, |
| 49 | + total: Long |
| 50 | + ) { |
| 51 | + reactContext?.emitDeviceEvent( |
| 52 | + "didSendNetworkData", |
| 53 | + buildReadableArray { |
| 54 | + add(requestId) |
| 55 | + add(progress.toInt()) |
| 56 | + add(total.toInt()) |
| 57 | + }) |
| 58 | + } |
| 59 | + |
| 60 | + @JvmStatic |
| 61 | + fun onIncrementalDataReceived( |
| 62 | + reactContext: ReactApplicationContext?, |
| 63 | + requestId: Int, |
| 64 | + data: String?, |
| 65 | + progress: Long, |
| 66 | + total: Long |
| 67 | + ) { |
| 68 | + reactContext?.emitDeviceEvent( |
| 69 | + "didReceiveNetworkIncrementalData", |
| 70 | + buildReadableArray { |
| 71 | + add(requestId) |
| 72 | + add(data) |
| 73 | + add(progress.toInt()) |
| 74 | + add(total.toInt()) |
| 75 | + }) |
| 76 | + } |
| 77 | + |
| 78 | + @JvmStatic |
| 79 | + fun onDataReceivedProgress( |
| 80 | + reactContext: ReactApplicationContext?, |
| 81 | + requestId: Int, |
| 82 | + progress: Long, |
| 83 | + total: Long |
| 84 | + ) { |
| 85 | + reactContext?.emitDeviceEvent( |
| 86 | + "didReceiveNetworkDataProgress", |
| 87 | + buildReadableArray { |
| 88 | + add(requestId) |
| 89 | + add(progress.toInt()) |
| 90 | + add(total.toInt()) |
| 91 | + }) |
| 92 | + } |
| 93 | + |
| 94 | + @JvmStatic |
| 95 | + fun onDataReceived(reactContext: ReactApplicationContext?, requestId: Int, data: String?) { |
| 96 | + reactContext?.emitDeviceEvent( |
| 97 | + "didReceiveNetworkData", |
| 98 | + buildReadableArray { |
| 99 | + add(requestId) |
| 100 | + add(data) |
| 101 | + }) |
| 102 | + } |
| 103 | + |
| 104 | + @JvmStatic |
| 105 | + fun onDataReceived(reactContext: ReactApplicationContext?, requestId: Int, data: WritableMap?) { |
| 106 | + reactContext?.emitDeviceEvent( |
| 107 | + "didReceiveNetworkData", |
| 108 | + Arguments.createArray().apply { |
| 109 | + pushInt(requestId) |
| 110 | + pushMap(data) |
| 111 | + }) |
| 112 | + } |
| 113 | + |
| 114 | + @JvmStatic |
| 115 | + fun onRequestError( |
| 116 | + reactContext: ReactApplicationContext?, |
| 117 | + requestId: Int, |
| 118 | + error: String?, |
| 119 | + e: Throwable? |
| 120 | + ) { |
| 121 | + reactContext?.emitDeviceEvent( |
| 122 | + "didCompleteNetworkResponse", |
| 123 | + buildReadableArray { |
| 124 | + add(requestId) |
| 125 | + add(error) |
| 126 | + if (e?.javaClass == SocketTimeoutException::class.java) { |
| 127 | + add(true) // last argument is a time out boolean |
| 128 | + } |
| 129 | + }) |
| 130 | + } |
| 131 | + |
| 132 | + @JvmStatic |
| 133 | + fun onRequestSuccess( |
| 134 | + reactContext: ReactApplicationContext?, |
| 135 | + requestId: Int, |
| 136 | + encodedDataLength: Long |
| 137 | + ) { |
| 138 | + if (ReactNativeFeatureFlags.enableNetworkEventReporting()) { |
| 139 | + InspectorNetworkReporter.reportResponseEnd(requestId, encodedDataLength) |
| 140 | + } |
| 141 | + reactContext?.emitDeviceEvent( |
| 142 | + "didCompleteNetworkResponse", |
| 143 | + buildReadableArray { |
| 144 | + add(requestId) |
| 145 | + addNull() |
| 146 | + }) |
| 147 | + } |
| 148 | + |
| 149 | + @JvmStatic |
| 150 | + fun onResponseReceived( |
| 151 | + reactContext: ReactApplicationContext?, |
| 152 | + requestId: Int, |
| 153 | + requestUrl: String?, |
| 154 | + response: Response, |
| 155 | + ) { |
| 156 | + val headersMap = okHttpHeadersToMap(response.headers()) |
| 157 | + val headersBundle = Bundle() |
| 158 | + for ((headerName, headerValue) in headersMap) { |
| 159 | + headersBundle.putString(headerName, headerValue) |
| 160 | + } |
| 161 | + |
| 162 | + if (ReactNativeFeatureFlags.enableNetworkEventReporting()) { |
| 163 | + InspectorNetworkReporter.reportResponseStart( |
| 164 | + requestId, |
| 165 | + requestUrl.orEmpty(), |
| 166 | + response.code(), |
| 167 | + headersMap, |
| 168 | + response.body()?.contentLength() ?: 0, |
| 169 | + ) |
| 170 | + } |
| 171 | + reactContext?.emitDeviceEvent( |
| 172 | + "didReceiveNetworkResponse", |
| 173 | + Arguments.createArray().apply { |
| 174 | + pushInt(requestId) |
| 175 | + pushInt(response.code()) |
| 176 | + pushMap(Arguments.fromBundle(headersBundle)) |
| 177 | + pushString(requestUrl) |
| 178 | + }) |
| 179 | + } |
| 180 | + |
| 181 | + @Deprecated("Compatibility overload") |
| 182 | + @JvmStatic |
| 183 | + fun onResponseReceived( |
| 184 | + reactContext: ReactApplicationContext?, |
| 185 | + requestId: Int, |
| 186 | + statusCode: Int, |
| 187 | + headers: WritableMap?, |
| 188 | + url: String? |
| 189 | + ) { |
| 190 | + val headersBuilder = Headers.Builder() |
| 191 | + headers?.let { map -> |
| 192 | + val iterator = map.keySetIterator() |
| 193 | + while (iterator.hasNextKey()) { |
| 194 | + val key = iterator.nextKey() |
| 195 | + val value = map.getString(key) |
| 196 | + if (value != null) { |
| 197 | + headersBuilder.add(key, value) |
| 198 | + } |
| 199 | + } |
| 200 | + } |
| 201 | + onResponseReceived( |
| 202 | + reactContext, |
| 203 | + requestId, |
| 204 | + url, |
| 205 | + Response.Builder() |
| 206 | + .protocol(Protocol.HTTP_1_1) |
| 207 | + .request(Request.Builder().url(url.orEmpty()).build()) |
| 208 | + .headers(headersBuilder.build()) |
| 209 | + .code(statusCode) |
| 210 | + .message("") |
| 211 | + .build()) |
| 212 | + } |
| 213 | + |
| 214 | + private fun okHttpHeadersToMap(headers: Headers): Map<String, String> { |
| 215 | + val responseHeaders = mutableMapOf<String, String>() |
| 216 | + for (i in 0 until headers.size()) { |
| 217 | + val headerName = headers.name(i) |
| 218 | + // multiple values for the same header |
| 219 | + if (responseHeaders.containsKey(headerName)) { |
| 220 | + responseHeaders[headerName] = "${responseHeaders[headerName]}, ${headers.value(i)}" |
| 221 | + } else { |
| 222 | + responseHeaders[headerName] = headers.value(i) |
| 223 | + } |
| 224 | + } |
| 225 | + return responseHeaders |
| 226 | + } |
| 227 | +} |
0 commit comments