@@ -9,7 +9,6 @@ import com.aheaditec.talsec_security.security.api.SuspiciousAppInfo
99import com.aheaditec.talsec_security.security.api.Talsec
1010import com.aheaditec.talsec_security.security.api.TalsecConfig
1111import com.aheaditec.talsec_security.security.api.TalsecMode
12- import com.aheaditec.talsec_security.security.api.ThreatListener
1312import com.facebook.react.bridge.Arguments
1413import com.facebook.react.bridge.LifecycleEventListener
1514import com.facebook.react.bridge.Promise
@@ -19,8 +18,8 @@ import com.facebook.react.bridge.ReactMethod
1918import com.facebook.react.bridge.ReadableMap
2019import com.facebook.react.bridge.UiThreadUtil.runOnUiThread
2120import com.facebook.react.bridge.WritableArray
21+ import com.facebook.react.bridge.WritableMap
2222import com.facebook.react.modules.core.DeviceEventManagerModule
23- import com.freeraspreactnative.events.BaseRaspEvent
2423import com.freeraspreactnative.events.RaspExecutionStateEvent
2524import com.freeraspreactnative.events.ThreatEvent
2625import com.freeraspreactnative.interfaces.PluginExecutionStateListener
@@ -38,15 +37,24 @@ class FreeraspReactNativeModule(private val reactContext: ReactApplicationContex
3837
3938 private val lifecycleListener = object : LifecycleEventListener {
4039 override fun onHostResume () {
40+ PluginThreatHandler .threatDispatcher.onResume()
41+ PluginThreatHandler .executionStateDispatcher.onResume()
4142 if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .UPSIDE_DOWN_CAKE ) {
4243 reactContext.currentActivity?.let { ScreenProtector .register(it) }
4344 }
4445 }
4546
4647 override fun onHostPause () {
48+ PluginThreatHandler .threatDispatcher.onPause()
49+ PluginThreatHandler .executionStateDispatcher.onPause()
4750 if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .UPSIDE_DOWN_CAKE ) {
4851 reactContext.currentActivity?.let { ScreenProtector .unregister(it) }
4952 }
53+ if (reactContext.currentActivity?.isFinishing == true ) {
54+ PluginThreatHandler .threatDispatcher.unregisterListener()
55+ PluginThreatHandler .executionStateDispatcher.unregisterListener()
56+ PluginThreatHandler .unregisterSDKListener(reactContext)
57+ }
5058 }
5159
5260 override fun onHostDestroy () {
@@ -61,16 +69,21 @@ class FreeraspReactNativeModule(private val reactContext: ReactApplicationContex
6169 init {
6270 reactContext.addLifecycleEventListener(lifecycleListener)
6371 initializeEventKeys()
72+ PluginThreatHandler .initializeDispatchers(PluginListener (reactContext))
6473 }
6574
6675 @ReactMethod
6776 fun talsecStart (
6877 options : ReadableMap , promise : Promise
6978 ) {
79+ if (talsecStarted) {
80+ promise.resolve(" freeRASP started" )
81+ return
82+ }
7083
7184 try {
7285 val config = buildTalsecConfig(options)
73- PluginThreatHandler .registerListener (reactContext)
86+ PluginThreatHandler .registerSDKListener (reactContext)
7487 runOnUiThread {
7588 Talsec .start(reactContext, config, TalsecMode .BACKGROUND )
7689 mainHandler.post {
@@ -149,10 +162,10 @@ class FreeraspReactNativeModule(private val reactContext: ReactApplicationContex
149162 @ReactMethod
150163 fun addListener (eventName : String ) {
151164 if (eventName == ThreatEvent .CHANNEL_NAME ) {
152- PluginThreatHandler .threatDispatcher.listener = PluginListener (reactContext )
165+ PluginThreatHandler .threatDispatcher.registerListener( )
153166 }
154167 if (eventName == RaspExecutionStateEvent .CHANNEL_NAME ) {
155- PluginThreatHandler .executionStateDispatcher.listener = PluginListener (reactContext )
168+ PluginThreatHandler .executionStateDispatcher.registerListener( )
156169 }
157170 }
158171
@@ -165,10 +178,10 @@ class FreeraspReactNativeModule(private val reactContext: ReactApplicationContex
165178 @ReactMethod
166179 fun removeListenerForEvent (eventName : String , promise : Promise ) {
167180 if (eventName == ThreatEvent .CHANNEL_NAME ) {
168- PluginThreatHandler .threatDispatcher.listener = null
181+ PluginThreatHandler .threatDispatcher.unregisterListener()
169182 }
170183 if (eventName == RaspExecutionStateEvent .CHANNEL_NAME ) {
171- PluginThreatHandler .executionStateDispatcher.listener = null
184+ PluginThreatHandler .executionStateDispatcher.unregisterListener()
172185 }
173186 promise.resolve(" Listener unregistered" )
174187 }
@@ -302,48 +315,40 @@ class FreeraspReactNativeModule(private val reactContext: ReactApplicationContex
302315 private val mainHandler = Handler (Looper .getMainLooper())
303316
304317 internal var talsecStarted = false
318+ }
319+
320+ internal class PluginListener (private val reactContext : ReactApplicationContext ) :
321+ PluginThreatListener , PluginExecutionStateListener {
305322
306- private fun notifyEvent ( event : BaseRaspEvent , appReactContext : ReactApplicationContext ) {
323+ override fun threatDetected ( threatEventType : ThreatEvent ) {
307324 val params = Arguments .createMap()
308- params.putInt(event.channelKey, event.value)
309- appReactContext.getJSModule(DeviceEventManagerModule .RCTDeviceEventEmitter ::class .java)
310- .emit(event.channelName, params)
325+ params.putInt(threatEventType.channelKey, threatEventType.value)
326+ notifyEvent(ThreatEvent .CHANNEL_NAME , params)
311327 }
312328
313- /* *
314- * Sends malware detected event to React Native
315- */
316- private fun notifyMalware (suspiciousApps : MutableList <SuspiciousAppInfo >, appReactContext : ReactApplicationContext ) {
317- // Perform the malware encoding on a background thread
329+ override fun malwareDetected (suspiciousApps : MutableList <SuspiciousAppInfo >) {
318330 backgroundHandler.post {
319-
320- val encodedSuspiciousApps = suspiciousApps.toEncodedWritableArray(appReactContext)
321-
331+ val encodedSuspiciousApps = suspiciousApps.toEncodedWritableArray(reactContext)
322332 mainHandler.post {
323333 val params = Arguments .createMap()
324334 params.putInt(ThreatEvent .CHANNEL_KEY , ThreatEvent .Malware .value)
325335 params.putArray(
326336 ThreatEvent .MALWARE_CHANNEL_KEY , encodedSuspiciousApps
327337 )
328-
329- appReactContext.getJSModule(DeviceEventManagerModule .RCTDeviceEventEmitter ::class .java)
330- .emit(ThreatEvent .CHANNEL_NAME , params)
338+ notifyEvent(ThreatEvent .CHANNEL_NAME , params)
331339 }
332340 }
333341 }
334- }
335-
336- internal class PluginListener (private val reactContext : ReactApplicationContext ) : PluginThreatListener, PluginExecutionStateListener {
337- override fun threatDetected (threatEventType : ThreatEvent ) {
338- notifyEvent(threatEventType, reactContext)
339- }
340342
341- override fun malwareDetected (suspiciousApps : MutableList <SuspiciousAppInfo >) {
342- notifyMalware(suspiciousApps, reactContext)
343+ override fun raspExecutionStateChanged (event : RaspExecutionStateEvent ) {
344+ val params = Arguments .createMap()
345+ params.putInt(event.channelKey, event.value)
346+ notifyEvent(event.channelName, params)
343347 }
344348
345- override fun raspExecutionStateChanged (event : RaspExecutionStateEvent ) {
346- notifyEvent(event, reactContext)
349+ private fun notifyEvent (eventName : String , params : WritableMap ) {
350+ reactContext.getJSModule(DeviceEventManagerModule .RCTDeviceEventEmitter ::class .java)
351+ .emit(eventName, params)
347352 }
348353 }
349354}
0 commit comments