using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
-using Tizen.Internals.Errors;
namespace Tizen.Security
{
public static class PrivacyPrivilegeManager
{
private const string LogTag = "Tizen.Privilege";
+ private static Dictionary<int, TaskCompletionSource<RequestMultipleResponseEventArgs>> s_multipleRequestMap = new Dictionary<int, TaskCompletionSource<RequestMultipleResponseEventArgs>>();
+ private static int s_requestId = 0;
private static IDictionary<string, WeakReference<ResponseContext>> s_responseWeakMap = new Dictionary<string, WeakReference<ResponseContext>>();
private static Interop.PrivacyPrivilegeManager.RequestResponseCallback s_requestResponseCb =
(Interop.PrivacyPrivilegeManager.CallCause cause, Interop.PrivacyPrivilegeManager.RequestResult result,
private static IDictionary<string, ResponseContext> s_responseMap = new Dictionary<string, ResponseContext>();
private static HashSet<string> s_PrivilegesInProgress = new HashSet<string>();
+ private static Interop.PrivacyPrivilegeManager.RequestMultipleResponseCallback s_multipleCallback = MultipleRequestHandler;
private static string[] CheckPrivilegesArgument(IEnumerable<string> privileges, string methodName)
{
Log.Info(LogTag, "Sending request for permissions: " + string.Join(" ", privilegesArray));
+ int requestId = 0;
+ lock (s_multipleRequestMap)
+ {
+ requestId = s_requestId++;
+ }
TaskCompletionSource<RequestMultipleResponseEventArgs> permissionResponsesTask = new TaskCompletionSource<RequestMultipleResponseEventArgs>();
- int ret = (int)Interop.PrivacyPrivilegeManager.RequestPermissions(privilegesArray, (uint)privilegesArray.Length,
- (Interop.PrivacyPrivilegeManager.CallCause cause, Interop.PrivacyPrivilegeManager.RequestResult[] results,
- string[] requestedPrivileges, uint privilegesCount, IntPtr userData) =>
- {
- Log.Info(LogTag, "Sending request for permissions: ");
- RequestMultipleResponseEventArgs requestResponse = new RequestMultipleResponseEventArgs();
- PermissionRequestResponse[] permissionResponses = new PermissionRequestResponse[privilegesCount];
-
- for (int iterator = 0; iterator < privilegesCount; ++iterator)
- {
- permissionResponses[iterator] = new PermissionRequestResponse
- {
- Privilege = requestedPrivileges[iterator],
- Result = (RequestResult)results[iterator]
- };
- }
- requestResponse.Cause = (CallCause)cause;
- requestResponse.Responses = permissionResponses;
-
- foreach (string privilege in requestedPrivileges)
- {
- s_PrivilegesInProgress.Remove(privilege);
- }
- permissionResponsesTask.SetResult(requestResponse);
- }, IntPtr.Zero);
+ s_multipleRequestMap[requestId] = permissionResponsesTask;
+ int ret = (int)Interop.PrivacyPrivilegeManager.RequestPermissions(privilegesArray, (uint)privilegesArray.Length, s_multipleCallback, (IntPtr)requestId);
if (ret != (int)Interop.PrivacyPrivilegeManager.ErrorCode.None)
{
{
s_PrivilegesInProgress.Remove(privilege);
}
+ s_multipleRequestMap.Remove(requestId);
throw PrivacyPrivilegeManagerErrorFactory.GetException(ret);
}
else
return s_responseWeakMap[privilege];
}
+ private static void MultipleRequestHandler(Interop.PrivacyPrivilegeManager.CallCause cause, Interop.PrivacyPrivilegeManager.RequestResult[] results,
+ string[] requestedPrivileges, uint privilegesCount, IntPtr userData)
+ {
+ int requestId = (int)userData;
+ if (!s_multipleRequestMap.ContainsKey(requestId))
+ {
+ return;
+ }
+
+ var tcs = s_multipleRequestMap[requestId];
+ RequestMultipleResponseEventArgs requestResponse = new RequestMultipleResponseEventArgs();
+ PermissionRequestResponse[] permissionResponses = new PermissionRequestResponse[privilegesCount];
+
+ for (int iterator = 0; iterator < privilegesCount; ++iterator)
+ {
+ permissionResponses[iterator] = new PermissionRequestResponse
+ {
+ Privilege = requestedPrivileges[iterator],
+ Result = (RequestResult)results[iterator]
+ };
+ }
+ requestResponse.Cause = (CallCause)cause;
+ requestResponse.Responses = permissionResponses;
+
+ foreach (string privilege in requestedPrivileges)
+ {
+ s_PrivilegesInProgress.Remove(privilege);
+ }
+ tcs.SetResult(requestResponse);
+ s_multipleRequestMap.Remove(requestId);
+ }
+
/// <summary>
/// This class manages event handlers of the privilege permission requests.
/// This class enables having event handlers for an individual privilege.