AnalyticsServiceInstance.Consent.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. using Unity.Services.Analytics.Internal;
  5. using Unity.Services.Core;
  6. using UnityEngine;
  7. namespace Unity.Services.Analytics
  8. {
  9. partial class AnalyticsServiceInstance
  10. {
  11. internal IConsentTracker ConsentTracker = new ConsentTracker();
  12. internal IAnalyticsForgetter analyticsForgetter;
  13. public async Task<List<string>> CheckForRequiredConsents()
  14. {
  15. var response = await ConsentTracker.CheckGeoIP();
  16. if (response.identifier == Consent.None)
  17. {
  18. return new List<string>();
  19. }
  20. if (ConsentTracker.IsConsentDenied())
  21. {
  22. return new List<string>();
  23. }
  24. if (!ConsentTracker.IsConsentGiven())
  25. {
  26. return new List<string> { response.identifier };
  27. }
  28. return new List<string>();
  29. }
  30. public void ProvideOptInConsent(string identifier, bool consent)
  31. {
  32. if (!ConsentTracker.IsGeoIpChecked())
  33. {
  34. throw new ConsentCheckException(ConsentCheckExceptionReason.ConsentFlowNotKnown,
  35. CommonErrorCodes.Unknown,
  36. "The required consent flow cannot be determined. Make sure CheckForRequiredConsents() method was successfully called.",
  37. null);
  38. }
  39. if (consent == false)
  40. {
  41. if (ConsentTracker.IsConsentGiven(identifier))
  42. {
  43. ConsentTracker.BeginOptOutProcess(identifier);
  44. RevokeWithForgetEvent();
  45. return;
  46. }
  47. Revoke();
  48. }
  49. ConsentTracker.SetUserConsentStatus(identifier, consent);
  50. }
  51. public void OptOut()
  52. {
  53. Debug.Log(ConsentTracker.IsConsentDenied()
  54. ? "This user has opted out. Any cached events have been discarded and no more will be collected."
  55. : "This user has opted out and is in the process of being forgotten...");
  56. if (ConsentTracker.IsConsentGiven())
  57. {
  58. // We have revoked consent but have not yet sent the ForgetMe signal
  59. // Thus we need to keep some of the dispatcher alive until that is done
  60. ConsentTracker.BeginOptOutProcess();
  61. RevokeWithForgetEvent();
  62. return;
  63. }
  64. if (ConsentTracker.IsOptingOutInProgress())
  65. {
  66. RevokeWithForgetEvent();
  67. return;
  68. }
  69. Revoke();
  70. ConsentTracker.SetDenyConsentToAll();
  71. }
  72. void Revoke()
  73. {
  74. // We have already been forgotten and so do not need to send the ForgetMe signal
  75. dataBuffer.ClearDiskCache();
  76. dataBuffer = new BufferRevoked();
  77. dataDispatcher = new Dispatcher(dataBuffer, new WebRequestHelper());
  78. ContainerObject.DestroyContainer();
  79. }
  80. internal void RevokeWithForgetEvent()
  81. {
  82. // Clear everything out of the real buffer and replace it with a dummy
  83. // that will swallow all events and do nothing
  84. dataBuffer.ClearBuffer();
  85. dataBuffer = new BufferRevoked();
  86. dataDispatcher = new Dispatcher(dataBuffer, new WebRequestHelper());
  87. analyticsForgetter = new AnalyticsForgetter(m_CollectURL,
  88. InstallId.GetOrCreateIdentifier(),
  89. Internal.Buffer.SaveDateTime(DateTime.Now),
  90. k_ForgetCallingId,
  91. ForgetMeEventUploaded, ConsentTracker);
  92. analyticsForgetter.AttemptToForget();
  93. }
  94. internal void ForgetMeEventUploaded()
  95. {
  96. ContainerObject.DestroyContainer();
  97. ConsentTracker.FinishOptOutProcess();
  98. #if UNITY_ANALYTICS_EVENT_LOGS
  99. Debug.Log("User opted out successfully and has been forgotten!");
  100. #endif
  101. }
  102. }
  103. }