@@ -9,6 +9,7 @@ import com.facebook.react.bridge.ReactApplicationContext
99import com.facebook.react.bridge.ReactContextBaseJavaModule
1010import com.facebook.react.bridge.ReactMethod
1111import com.facebook.react.bridge.ReadableMap
12+ import com.facebook.react.bridge.UiThreadUtil.runOnUiThread
1213import com.facebook.react.bridge.WritableArray
1314import com.facebook.react.modules.core.DeviceEventManagerModule
1415
@@ -32,17 +33,18 @@ class FreeraspReactNativeModule(val reactContext: ReactApplicationContext) :
3233 ) {
3334
3435 try {
35- val config = parseTalsecConfig (options)
36+ val config = buildTalsecConfig (options)
3637 FreeraspThreatHandler .listener = ThreatListener
3738 listener.registerListener(reactContext)
38- Talsec .start(reactContext, config)
39+ runOnUiThread { // Your code to run on a different thread or UI thread
40+ Talsec .start(reactContext, config)
41+ }
42+
3943 promise.resolve(" freeRASP started" )
4044
41- } catch (e: Exception ) {
42- val params = Arguments .createMap().apply {
43- putString(" message" , e.message)
44- }
45- promise.reject(" initializationError" , params)
45+ }
46+ catch (e: Exception ) {
47+ promise.reject(" TalsecInitializationError" , e.message, e)
4648 }
4749 }
4850
@@ -85,42 +87,17 @@ class FreeraspReactNativeModule(val reactContext: ReactApplicationContext) :
8587 // Remove upstream listeners, stop unnecessary background tasks
8688 }
8789
88- private fun parseTalsecConfig (config : ReadableMap ): TalsecConfig {
89- val androidConfig = config.getMap(" androidConfig" )!!
90- val packageName = androidConfig.getString(" packageName" )!!
91- val certificateHashes = mutableListOf<String >()
92- val hashes = androidConfig.getArray(" certificateHashes" )!!
93- for (i in 0 until hashes.size()) {
94- // in RN versions < 0.63, getString is nullable
95- @Suppress(" UNNECESSARY_SAFE_CALL" )
96- hashes.getString(i)?.let {
97- certificateHashes.add(it)
98- }
99- }
100- val watcherMail = config.getString(" watcherMail" )
101- val alternativeStores = mutableListOf<String >()
102- if (androidConfig.hasKey(" supportedAlternativeStores" )) {
103- val stores = androidConfig.getArray(" supportedAlternativeStores" )!!
104- for (i in 0 until stores.size()) {
105- // in RN versions < 0.63, getString is nullable
106- @Suppress(" UNNECESSARY_SAFE_CALL" )
107- stores.getString(i)?.let {
108- alternativeStores.add(it)
109- }
110- }
111- }
112- var isProd = true
113- if (config.hasKey(" isProd" )) {
114- isProd = config.getBoolean(" isProd" )
115- }
90+ private fun buildTalsecConfig (config : ReadableMap ): TalsecConfig {
91+ val androidConfig = config.getMapThrowing(" androidConfig" )
92+ val packageName = androidConfig.getStringThrowing(" packageName" )
93+ val certificateHashes = androidConfig.getArraySafe(" certificateHashes" )
94+
95+ val talsecBuilder = TalsecConfig .Builder (packageName, certificateHashes)
96+ .watcherMail(config.getString(" watcherMail" ))
97+ .supportedAlternativeStores(androidConfig.getArraySafe(" supportedAlternativeStores" ))
98+ .prod(config.getBooleanSafe(" isProd" ))
11699
117- return TalsecConfig (
118- packageName,
119- certificateHashes.toTypedArray(),
120- watcherMail,
121- alternativeStores.toTypedArray(),
122- isProd
123- )
100+ return talsecBuilder.build()
124101 }
125102
126103 companion object {
0 commit comments