Skip to content

Commit 9fdf6f5

Browse files
authored
Merge pull request #29 from SourcePointUSA/DIA-3423-DIA-3349-crashes
DIA-3423 DIA-3349 fix the crashes
2 parents 32efb82 + 9c9d714 commit 9fdf6f5

File tree

7 files changed

+69
-41
lines changed

7 files changed

+69
-41
lines changed
Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
11
using System;
2-
using System.Collections.Generic;
2+
using System.Collections.Concurrent;
33
using ConsentManagementProviderLib.EventHandlerInterface;
44
using UnityEngine.EventSystems;
55

66
namespace ConsentManagementProviderLib.Observer
77
{
88
internal static class BroadcastEventDispatcher
99
{
10-
public static Queue<Action> actions = new Queue<Action>();
10+
public static ConcurrentQueue<Action> actions = new ConcurrentQueue<Action>();
1111

1212
public static void Execute<T>(BaseEventData eventData, ExecuteEvents.EventFunction<T> functor) where T : IConsentEventHandler
1313
{
1414
var handlers = BroadcastReceivers.GetHandlersForEvent<T>();
1515
if (handlers == null) return;
16-
CmpDebugUtil.Log($"{typeof(T).Name} has {handlers.Count} invokable instances");
16+
CmpDebugUtil.Log($"{typeof(T).Name} has {handlers.Length} invokable instances");
1717
foreach (var handler in handlers)
1818
{
1919
actions.Enqueue(delegate { ExecuteEvents.Execute<T>(handler, eventData, functor); });
2020
}
2121
}
22+
23+
public static void Execute(Action action)
24+
{
25+
actions.Enqueue(action);
26+
}
2227
}
2328
}

Assets/ConsentManagementProvider/Scripts/observer/BroadcastEventsExecutor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ private void Awake()
1212

1313
private void Update()
1414
{
15-
while (BroadcastEventDispatcher.actions.Count > 0)
15+
while (BroadcastEventDispatcher.actions.TryDequeue(out var action))
1616
{
17-
BroadcastEventDispatcher.actions.Dequeue()?.Invoke();
17+
action?.Invoke();
1818
}
1919
}
2020
}

Assets/ConsentManagementProvider/Scripts/observer/BroadcastReceivers.cs

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
34
using ConsentManagementProviderLib.EventHandlerInterface;
45
using UnityEngine;
56

@@ -9,37 +10,47 @@ internal static class BroadcastReceivers
910
{
1011
private static readonly IDictionary<Type, IList<GameObject>> BroadcastsReceivers = new Dictionary<Type, IList<GameObject>>();
1112

12-
public static IList<GameObject> GetHandlersForEvent<T>() where T : IConsentEventHandler
13+
public static GameObject[] GetHandlersForEvent<T>() where T : IConsentEventHandler
1314
{
14-
if (!BroadcastsReceivers.ContainsKey(typeof(T)))
15+
lock (BroadcastsReceivers)
1516
{
16-
return null;
17+
if (!BroadcastsReceivers.ContainsKey(typeof(T)))
18+
{
19+
return null;
20+
}
21+
22+
return BroadcastsReceivers[typeof(T)].ToArray();
1723
}
18-
return BroadcastsReceivers[typeof(T)];
1924
}
2025

2126
public static void RegisterBroadcastReceiver<T>(GameObject go) where T : IConsentEventHandler
2227
{
23-
if (BroadcastsReceivers.ContainsKey(typeof(T)))
24-
{
25-
BroadcastsReceivers[typeof(T)].Add(go);
26-
}
27-
else
28+
lock (BroadcastsReceivers)
2829
{
29-
BroadcastsReceivers.Add(typeof(T), new List<GameObject>());
30-
BroadcastsReceivers[typeof(T)].Add(go);
30+
if (BroadcastsReceivers.ContainsKey(typeof(T)))
31+
{
32+
BroadcastsReceivers[typeof(T)].Add(go);
33+
}
34+
else
35+
{
36+
BroadcastsReceivers.Add(typeof(T), new List<GameObject>());
37+
BroadcastsReceivers[typeof(T)].Add(go);
38+
}
3139
}
3240
}
3341

3442
public static void UnregisterBroadcastReceiver<T>(GameObject go) where T : IConsentEventHandler
3543
{
36-
if (BroadcastsReceivers.ContainsKey(typeof(T)) && BroadcastsReceivers[typeof(T)].Contains(go))
37-
{
38-
BroadcastsReceivers[typeof(T)].Remove(go);
39-
}
40-
else
44+
lock (BroadcastsReceivers)
4145
{
42-
CmpDebugUtil.LogWarning($"{go.name} is not subscribed to handle {typeof(T)}");
46+
if (BroadcastsReceivers.ContainsKey(typeof(T)) && BroadcastsReceivers[typeof(T)].Contains(go))
47+
{
48+
BroadcastsReceivers[typeof(T)].Remove(go);
49+
}
50+
else
51+
{
52+
CmpDebugUtil.LogWarning($"{go.name} is not subscribed to handle {typeof(T)}");
53+
}
4354
}
4455
}
4556
}

Assets/ConsentManagementProvider/Scripts/wrapper/Android/SpClientProxy.cs

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using ConsentManagementProviderLib.Json;
3+
using ConsentManagementProviderLib.Observer;
34
using UnityEngine;
45

56
namespace ConsentManagementProviderLib.Android
@@ -58,16 +59,20 @@ AndroidJavaObject onAction(AndroidJavaObject view, AndroidJavaObject actionType)
5859
void onConsentReady(string spConsents)
5960
{
6061
CmpDebugUtil.Log("I've reached the C# onConsentReady with json string: " + spConsents);
61-
try
62-
{
63-
SpConsents consents = JsonUnwrapper.UnwrapSpConsentsAndroid(spConsents);
64-
_spConsents = consents;
65-
ConsentMessenger.Broadcast<IOnConsentReady>(consents);
66-
}
67-
catch (Exception e)
62+
63+
BroadcastEventDispatcher.Execute(() =>
6864
{
69-
ConsentMessenger.Broadcast<IOnConsentError>(e);
70-
}
65+
try
66+
{
67+
SpConsents consents = JsonUnwrapper.UnwrapSpConsentsAndroid(spConsents);
68+
_spConsents = consents;
69+
ConsentMessenger.Broadcast<IOnConsentReady>(consents);
70+
}
71+
catch (Exception e)
72+
{
73+
ConsentMessenger.Broadcast<IOnConsentError>(e);
74+
}
75+
});
7176
}
7277

7378
/**
@@ -78,16 +83,20 @@ void onSpFinished(string spConsents)
7883
CmpDebugUtil.ForceEnableNextCmpLog();
7984
CmpDebugUtil.LogWarning($"I've reached the C# onSpFinished with JSON spConsents={spConsents}");
8085
Console.WriteLine($"spConsents= `{spConsents}");
81-
try
82-
{
83-
SpConsents consents = JsonUnwrapper.UnwrapSpConsentsAndroid(spConsents);
84-
_spConsents = consents;
85-
ConsentMessenger.Broadcast<IOnConsentSpFinished>(consents);
86-
}
87-
catch (Exception e)
86+
87+
BroadcastEventDispatcher.Execute(() =>
8888
{
89-
ConsentMessenger.Broadcast<IOnConsentError>(e);
90-
}
89+
try
90+
{
91+
SpConsents consents = JsonUnwrapper.UnwrapSpConsentsAndroid(spConsents);
92+
_spConsents = consents;
93+
ConsentMessenger.Broadcast<IOnConsentSpFinished>(consents);
94+
}
95+
catch (Exception e)
96+
{
97+
ConsentMessenger.Broadcast<IOnConsentError>(e);
98+
}
99+
});
91100
}
92101

93102
void onError(AndroidJavaObject rawThrowableObject)

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# 2.1.6
2+
* [DIA-3423](https://sourcepoint.atlassian.net/browse/DIA-3423)[DIA-3349](https://sourcepoint.atlassian.net/browse/DIA-3349) DIA-3423, DIA-3349 Fix Unity racing conditions [#28](https://github.com/SourcePointUSA/unity-sdk/pull/28)
3+
14
# 2.1.5
25
* [DIA-2811](https://sourcepoint.atlassian.net/browse/DIA-2811) DIA-2811 Removed code signing for IOS [#25](https://github.com/SourcePointUSA/unity-sdk/pull/25)
36

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "com.sourcepoint.unitycmp",
33
"displayName": "Sourcepoint Consent Message Plugin",
4-
"version": "2.1.5",
4+
"version": "2.1.6",
55
"unity": "2021.3",
66
"description": "Native UI Privacy Manager for both GDPR and CCPA legislations.",
77
"author": {
-141 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)