@@ -4,6 +4,7 @@ import android.os.Build
44import android.os.Handler
55import android.os.HandlerThread
66import android.os.Looper
7+ import com.aheaditec.talsec_security.security.api.ExternalIdResult
78import com.aheaditec.talsec_security.security.api.SuspiciousAppInfo
89import com.aheaditec.talsec_security.security.api.Talsec
910import com.aheaditec.talsec_security.security.api.TalsecConfig
@@ -22,6 +23,8 @@ import com.facebook.react.modules.core.DeviceEventManagerModule
2223import com.freeraspreactnative.events.BaseRaspEvent
2324import com.freeraspreactnative.events.RaspExecutionStateEvent
2425import com.freeraspreactnative.events.ThreatEvent
26+ import com.freeraspreactnative.interfaces.PluginExecutionStateListener
27+ import com.freeraspreactnative.interfaces.PluginThreatListener
2528import com.freeraspreactnative.utils.Utils
2629import com.freeraspreactnative.utils.getArraySafe
2730import com.freeraspreactnative.utils.getBooleanSafe
@@ -33,7 +36,6 @@ import com.freeraspreactnative.utils.toEncodedWritableArray
3336class FreeraspReactNativeModule (private val reactContext : ReactApplicationContext ) :
3437 ReactContextBaseJavaModule (reactContext) {
3538
36- private val listener = ThreatListener (FreeraspThreatHandler , FreeraspThreatHandler , FreeraspThreatHandler )
3739 private val lifecycleListener = object : LifecycleEventListener {
3840 override fun onHostResume () {
3941 if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .UPSIDE_DOWN_CAKE ) {
@@ -57,7 +59,6 @@ class FreeraspReactNativeModule(private val reactContext: ReactApplicationContex
5759 }
5860
5961 init {
60- appReactContext = reactContext
6162 reactContext.addLifecycleEventListener(lifecycleListener)
6263 initializeEventKeys()
6364 }
@@ -69,8 +70,7 @@ class FreeraspReactNativeModule(private val reactContext: ReactApplicationContex
6970
7071 try {
7172 val config = buildTalsecConfig(options)
72- FreeraspThreatHandler .listener = ThreatListener
73- listener.registerListener(reactContext)
73+ PluginThreatHandler .registerListener(reactContext)
7474 runOnUiThread {
7575 Talsec .start(reactContext, config, TalsecMode .BACKGROUND )
7676 mainHandler.post {
@@ -147,13 +147,30 @@ class FreeraspReactNativeModule(private val reactContext: ReactApplicationContex
147147 }
148148
149149 @ReactMethod
150- fun addListener (@Suppress(" UNUSED_PARAMETER" ) eventName : String ) {
151- // Set up any upstream listeners or background tasks as necessary
150+ fun addListener (eventName : String ) {
151+ if (eventName == ThreatEvent .CHANNEL_NAME ) {
152+ PluginThreatHandler .threatDispatcher.listener = PluginListener (reactContext)
153+ }
154+ if (eventName == RaspExecutionStateEvent .CHANNEL_NAME ) {
155+ PluginThreatHandler .executionStateDispatcher.listener = PluginListener (reactContext)
156+ }
152157 }
153158
154159 @ReactMethod
155160 fun removeListeners (@Suppress(" UNUSED_PARAMETER" ) count : Int ) {
156- // Remove upstream listeners, stop unnecessary background tasks
161+ // built-in RN method, however it does not suit us as it just tells
162+ // number of un-registered listeners, so we use `removeListenerForEvent`
163+ }
164+
165+ @ReactMethod
166+ fun removeListenerForEvent (eventName : String , promise : Promise ) {
167+ if (eventName == ThreatEvent .CHANNEL_NAME ) {
168+ PluginThreatHandler .threatDispatcher.listener = null
169+ }
170+ if (eventName == RaspExecutionStateEvent .CHANNEL_NAME ) {
171+ PluginThreatHandler .executionStateDispatcher.listener = null
172+ }
173+ promise.resolve(" Listener unregistered" )
157174 }
158175
159176 /* *
@@ -218,17 +235,41 @@ class FreeraspReactNativeModule(private val reactContext: ReactApplicationContex
218235
219236 @ReactMethod
220237 fun storeExternalId (
221- externalId : String , promise : Promise
238+ externalId : String , promise : Promise
222239 ) {
223- try {
224- Talsec .storeExternalId(reactContext, externalId)
225- promise.resolve(" OK - Store external ID" )
226- } catch (e: Exception ) {
240+ try {
241+ when (val result = Talsec .storeExternalId(reactContext, externalId)) {
242+ is ExternalIdResult .Error -> {
227243 promise.reject(
228- " NativePluginError " ,
229- " Error during storeExternalId operation in Talsec Native Plugin "
244+ " ExternalIdError " ,
245+ " Setting up External ID failed - ${result.errorMsg} "
230246 )
247+ }
248+
249+ is ExternalIdResult .Success -> {
250+ promise.resolve(" OK - Store external ID" )
251+ return
252+ }
231253 }
254+ } catch (e: Exception ) {
255+ promise.reject(
256+ " NativePluginError" ,
257+ " Error during storeExternalId operation in Talsec Native Plugin"
258+ )
259+ }
260+ }
261+
262+ @ReactMethod
263+ fun removeExternalId (promise : Promise ) {
264+ try {
265+ Talsec .removeExternalId(reactContext)
266+ promise.resolve(" OK - External ID removed" )
267+ } catch (e: Exception ) {
268+ promise.reject(
269+ " NativePluginError" ,
270+ " Error during storeExternalId operation in Talsec Native Plugin"
271+ )
272+ }
232273 }
233274
234275 private fun buildTalsecConfig (config : ReadableMap ): TalsecConfig {
@@ -260,11 +301,9 @@ class FreeraspReactNativeModule(private val reactContext: ReactApplicationContex
260301 private val backgroundHandler = Handler (backgroundHandlerThread.looper)
261302 private val mainHandler = Handler (Looper .getMainLooper())
262303
263- private lateinit var appReactContext: ReactApplicationContext
264-
265304 internal var talsecStarted = false
266305
267- private fun notifyEvent (event : BaseRaspEvent ) {
306+ private fun notifyEvent (event : BaseRaspEvent , appReactContext : ReactApplicationContext ) {
268307 val params = Arguments .createMap()
269308 params.putInt(event.channelKey, event.value)
270309 appReactContext.getJSModule(DeviceEventManagerModule .RCTDeviceEventEmitter ::class .java)
@@ -274,7 +313,7 @@ class FreeraspReactNativeModule(private val reactContext: ReactApplicationContex
274313 /* *
275314 * Sends malware detected event to React Native
276315 */
277- private fun notifyMalware (suspiciousApps : MutableList <SuspiciousAppInfo >) {
316+ private fun notifyMalware (suspiciousApps : MutableList <SuspiciousAppInfo >, appReactContext : ReactApplicationContext ) {
278317 // Perform the malware encoding on a background thread
279318 backgroundHandler.post {
280319
@@ -294,17 +333,17 @@ class FreeraspReactNativeModule(private val reactContext: ReactApplicationContex
294333 }
295334 }
296335
297- internal object ThreatListener : FreeraspThreatHandler.TalsecReactNative {
336+ internal class PluginListener ( private val reactContext : ReactApplicationContext ) : PluginThreatListener, PluginExecutionStateListener {
298337 override fun threatDetected (threatEventType : ThreatEvent ) {
299- notifyEvent(threatEventType)
338+ notifyEvent(threatEventType, reactContext )
300339 }
301340
302341 override fun malwareDetected (suspiciousApps : MutableList <SuspiciousAppInfo >) {
303- notifyMalware(suspiciousApps)
342+ notifyMalware(suspiciousApps, reactContext )
304343 }
305344
306345 override fun raspExecutionStateChanged (event : RaspExecutionStateEvent ) {
307- notifyEvent(event)
346+ notifyEvent(event, reactContext )
308347 }
309348 }
310349}
0 commit comments