Skip to content

Commit

Permalink
Merge pull request #29 from SourcePointUSA/DIA-3423-DIA-3349-crashes
Browse files Browse the repository at this point in the history
DIA-3423 DIA-3349 fix the crashes
  • Loading branch information
Nevazhnovu authored Jan 22, 2024
2 parents 32efb82 + 9c9d714 commit 9fdf6f5
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using ConsentManagementProviderLib.EventHandlerInterface;
using UnityEngine.EventSystems;

namespace ConsentManagementProviderLib.Observer
{
internal static class BroadcastEventDispatcher
{
public static Queue<Action> actions = new Queue<Action>();
public static ConcurrentQueue<Action> actions = new ConcurrentQueue<Action>();

public static void Execute<T>(BaseEventData eventData, ExecuteEvents.EventFunction<T> functor) where T : IConsentEventHandler
{
var handlers = BroadcastReceivers.GetHandlersForEvent<T>();
if (handlers == null) return;
CmpDebugUtil.Log($"{typeof(T).Name} has {handlers.Count} invokable instances");
CmpDebugUtil.Log($"{typeof(T).Name} has {handlers.Length} invokable instances");
foreach (var handler in handlers)
{
actions.Enqueue(delegate { ExecuteEvents.Execute<T>(handler, eventData, functor); });
}
}

public static void Execute(Action action)
{
actions.Enqueue(action);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ private void Awake()

private void Update()
{
while (BroadcastEventDispatcher.actions.Count > 0)
while (BroadcastEventDispatcher.actions.TryDequeue(out var action))
{
BroadcastEventDispatcher.actions.Dequeue()?.Invoke();
action?.Invoke();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using ConsentManagementProviderLib.EventHandlerInterface;
using UnityEngine;

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

public static IList<GameObject> GetHandlersForEvent<T>() where T : IConsentEventHandler
public static GameObject[] GetHandlersForEvent<T>() where T : IConsentEventHandler
{
if (!BroadcastsReceivers.ContainsKey(typeof(T)))
lock (BroadcastsReceivers)
{
return null;
if (!BroadcastsReceivers.ContainsKey(typeof(T)))
{
return null;
}

return BroadcastsReceivers[typeof(T)].ToArray();
}
return BroadcastsReceivers[typeof(T)];
}

public static void RegisterBroadcastReceiver<T>(GameObject go) where T : IConsentEventHandler
{
if (BroadcastsReceivers.ContainsKey(typeof(T)))
{
BroadcastsReceivers[typeof(T)].Add(go);
}
else
lock (BroadcastsReceivers)
{
BroadcastsReceivers.Add(typeof(T), new List<GameObject>());
BroadcastsReceivers[typeof(T)].Add(go);
if (BroadcastsReceivers.ContainsKey(typeof(T)))
{
BroadcastsReceivers[typeof(T)].Add(go);
}
else
{
BroadcastsReceivers.Add(typeof(T), new List<GameObject>());
BroadcastsReceivers[typeof(T)].Add(go);
}
}
}

public static void UnregisterBroadcastReceiver<T>(GameObject go) where T : IConsentEventHandler
{
if (BroadcastsReceivers.ContainsKey(typeof(T)) && BroadcastsReceivers[typeof(T)].Contains(go))
{
BroadcastsReceivers[typeof(T)].Remove(go);
}
else
lock (BroadcastsReceivers)
{
CmpDebugUtil.LogWarning($"{go.name} is not subscribed to handle {typeof(T)}");
if (BroadcastsReceivers.ContainsKey(typeof(T)) && BroadcastsReceivers[typeof(T)].Contains(go))
{
BroadcastsReceivers[typeof(T)].Remove(go);
}
else
{
CmpDebugUtil.LogWarning($"{go.name} is not subscribed to handle {typeof(T)}");
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using ConsentManagementProviderLib.Json;
using ConsentManagementProviderLib.Observer;
using UnityEngine;

namespace ConsentManagementProviderLib.Android
Expand Down Expand Up @@ -58,16 +59,20 @@ AndroidJavaObject onAction(AndroidJavaObject view, AndroidJavaObject actionType)
void onConsentReady(string spConsents)
{
CmpDebugUtil.Log("I've reached the C# onConsentReady with json string: " + spConsents);
try
{
SpConsents consents = JsonUnwrapper.UnwrapSpConsentsAndroid(spConsents);
_spConsents = consents;
ConsentMessenger.Broadcast<IOnConsentReady>(consents);
}
catch (Exception e)

BroadcastEventDispatcher.Execute(() =>
{
ConsentMessenger.Broadcast<IOnConsentError>(e);
}
try
{
SpConsents consents = JsonUnwrapper.UnwrapSpConsentsAndroid(spConsents);
_spConsents = consents;
ConsentMessenger.Broadcast<IOnConsentReady>(consents);
}
catch (Exception e)
{
ConsentMessenger.Broadcast<IOnConsentError>(e);
}
});
}

/**
Expand All @@ -78,16 +83,20 @@ void onSpFinished(string spConsents)
CmpDebugUtil.ForceEnableNextCmpLog();
CmpDebugUtil.LogWarning($"I've reached the C# onSpFinished with JSON spConsents={spConsents}");
Console.WriteLine($"spConsents= `{spConsents}");
try
{
SpConsents consents = JsonUnwrapper.UnwrapSpConsentsAndroid(spConsents);
_spConsents = consents;
ConsentMessenger.Broadcast<IOnConsentSpFinished>(consents);
}
catch (Exception e)

BroadcastEventDispatcher.Execute(() =>
{
ConsentMessenger.Broadcast<IOnConsentError>(e);
}
try
{
SpConsents consents = JsonUnwrapper.UnwrapSpConsentsAndroid(spConsents);
_spConsents = consents;
ConsentMessenger.Broadcast<IOnConsentSpFinished>(consents);
}
catch (Exception e)
{
ConsentMessenger.Broadcast<IOnConsentError>(e);
}
});
}

void onError(AndroidJavaObject rawThrowableObject)
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# 2.1.6
* [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)

# 2.1.5
* [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)

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "com.sourcepoint.unitycmp",
"displayName": "Sourcepoint Consent Message Plugin",
"version": "2.1.5",
"version": "2.1.6",
"unity": "2021.3",
"description": "Native UI Privacy Manager for both GDPR and CCPA legislations.",
"author": {
Expand Down
Binary file modified unitypackage/ConsentManagementProvider.unitypackage
Binary file not shown.

0 comments on commit 9fdf6f5

Please sign in to comment.