@@ -11,6 +11,30 @@ class FreeraspReactNative: RCTEventEmitter {
1111 FreeraspReactNative . shared = self
1212 }
1313
14+ @objc ( addListener: )
15+ override func addListener( _ eventName: String ! ) {
16+ super. addListener ( eventName)
17+ if eventName == EventIdentifiers . threatChannelName {
18+ ThreatDispatcher . shared. listener = { [ weak self] threat in
19+ self ? . dispatchThreatEvent ( threat: threat)
20+ }
21+ } else if eventName == EventIdentifiers . raspExecutionStateChannelName {
22+ ExecutionStateDispatcher . shared. listener = { [ weak self] event in
23+ self ? . dispatchRaspExecutionStateEvent ( event: event)
24+ }
25+ }
26+ }
27+
28+ @objc ( removeListenerForEvent: withResolver: withRejecter: )
29+ func removeListenerForEvent( eventName: String , resolve: RCTPromiseResolveBlock , rejecter reject: RCTPromiseRejectBlock ) -> Void {
30+ if eventName == EventIdentifiers . threatChannelName {
31+ ThreatDispatcher . shared. listener = nil
32+ } else if eventName == EventIdentifiers . raspExecutionStateChannelName {
33+ ExecutionStateDispatcher . shared. listener = nil
34+ }
35+ resolve ( " Listener unregistered " )
36+ }
37+
1438 @objc ( talsecStart: withResolver: withRejecter: )
1539 func talsecStart( options: NSDictionary , resolve: RCTPromiseResolveBlock , rejecter reject: RCTPromiseRejectBlock ) -> Void {
1640 do {
@@ -72,12 +96,18 @@ class FreeraspReactNative: RCTEventEmitter {
7296 resolve ( " OK - Store external ID " )
7397 }
7498
99+ @objc ( removeExternalId: withRejecter: )
100+ private func removeExternalId( resolve: RCTPromiseResolveBlock , rejecter reject: RCTPromiseRejectBlock ) -> Void {
101+ UserDefaults . standard. removeObject ( forKey: " app.talsec.externalid " )
102+ resolve ( " OK - External ID removed " )
103+ }
104+
75105 /**
76106 * Method to setup the message passing between native and React Native
77107 */
78108 @objc ( getThreatChannelData: withRejecter: )
79109 private func getThreatChannelData( resolve: RCTPromiseResolveBlock , rejecter reject: RCTPromiseRejectBlock ) -> Void {
80- resolve ( [ EventIdentifiers . threatChannelName, EventIdentifiers . threatChannelKey] )
110+ resolve ( [ EventIdentifiers . threatChannelName, EventIdentifiers . threatChannelKey, " 0 " ] )
81111 }
82112
83113 /**
@@ -88,16 +118,16 @@ class FreeraspReactNative: RCTEventEmitter {
88118 resolve ( [ EventIdentifiers . raspExecutionStateChannelName, EventIdentifiers . raspExecutionStateChannelKey] )
89119 }
90120
91- func dispatchEvent ( securityThreat : SecurityThreat ) -> Void {
121+ func dispatchThreatEvent ( threat : SecurityThreat ) {
92122 FreeraspReactNative . shared!. sendEvent ( withName: EventIdentifiers . threatChannelName, body: [
93- EventIdentifiers . threatChannelKey: securityThreat . callbackIdentifier,
123+ EventIdentifiers . threatChannelKey: threat . callbackIdentifier
94124 ] )
95125 }
96126
97- func dispatchRaspExecutionStateEvent( event: RaspExecutionStates ) -> Void {
98- FreeraspReactNative . shared!. sendEvent ( withName: EventIdentifiers . raspExecutionStateChannelName, body: [
99- EventIdentifiers . raspExecutionStateChannelKey: event. callbackIdentifier,
100- ] )
127+ func dispatchRaspExecutionStateEvent( event: RaspExecutionStates ) {
128+ FreeraspReactNative . shared!. sendEvent ( withName: EventIdentifiers . raspExecutionStateChannelName, body: [
129+ EventIdentifiers . raspExecutionStateChannelKey: event. callbackIdentifier,
130+ ] )
101131 }
102132
103133 /**
@@ -139,12 +169,11 @@ extension SecurityThreatCenter: @retroactive SecurityThreatHandler, @retroactive
139169 if ( securityThreat. rawValue == " passcodeChange " ) {
140170 return
141171 }
142- FreeraspReactNative . shared! . dispatchEvent ( securityThreat : securityThreat)
172+ ThreatDispatcher . shared. dispatch ( threat : securityThreat)
143173 }
144174
145175 public func onAllChecksFinished( ) {
146-
147- FreeraspReactNative . shared!. dispatchRaspExecutionStateEvent ( event: RaspExecutionStates . allChecksFinished)
176+ ExecutionStateDispatcher . shared. dispatch ( event: RaspExecutionStates . allChecksFinished)
148177 }
149178}
150179
0 commit comments