2 * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 using System.Collections.Generic;
19 using Tizen.Internals.Errors;
21 namespace Tizen.Security
24 /// The PrivacyPrivilegeManager provides the properties or methods to check and request a permission for privacy privilege.
26 public static class PrivacyPrivilegeManager
28 private const string LogTag = "Tizen.Privilege";
29 private static Interop.PrivacyPrivilegeManager.RequestResponseCallback s_requestResponseCb;
30 private static IDictionary<string, ResponseContext> s_responseMap = new Dictionary<string, ResponseContext>();
32 static PrivacyPrivilegeManager()
34 s_requestResponseCb = (Interop.PrivacyPrivilegeManager.CallCause cause, Interop.PrivacyPrivilegeManager.RequestResult result, string privilege, IntPtr userData) =>
38 s_responseMap[privilege].FireEvent((CallCause)cause, (RequestResult) result);
42 Log.Error(LogTag, "Exception in callback : " + e.Message);
48 /// Gets the status of a privacy privilege permission.
50 /// <param name="privilege">The privacy privilege to be checked.</param>
51 /// <returns>The permission setting for a respective privilege.</returns>
52 /// <exception cref="ArgumentException">Thrown when an invalid parameter is passed.</exception>
53 /// <exception cref="OutOfMemoryException">Thrown when a memory error occurred.</exception>
54 /// <exception cref="System.IO.IOException">Thrown when the method failed due to an internal I/O error.</exception>
57 /// CheckResult result = PrivacyPrivilegeManager.CheckPermission("http://tizen.org/privilege/account.read");
61 /// // Privilege can be used
64 /// // Privilege can't be used
67 /// // User permission request required
68 /// PrivacyPrivilegeManager.RequestPermission("http://tizen.org/privilege/account.read");
73 public static CheckResult CheckPermission(string privilege)
75 Interop.PrivacyPrivilegeManager.CheckResult result;
76 int ret = (int)Interop.PrivacyPrivilegeManager.CheckPermission(privilege, out result);
77 if (ret != (int)Interop.PrivacyPrivilegeManager.ErrorCode.None)
79 Log.Error(LogTag, "Failed to check permission");
80 throw PrivacyPrivilegeManagerErrorFactory.GetException(ret);
82 return (CheckResult)result;
86 /// Triggers the permission request for a user.
88 /// <param name="privilege">The privacy privilege to be requested.</param>
89 /// <exception cref="ArgumentException">Thrown when an invalid parameter is passed.</exception>
90 /// <exception cref="OutOfMemoryException">Thrown when a memory error occurred.</exception>
91 /// <exception cref="System.IO.IOException">Thrown when the method failed due to an internal I/O error.</exception>
94 /// CheckResult result = PrivacyPrivilegeManager.CheckPermission("http://tizen.org/privilege/account.read");
98 /// // Privilege can be used
101 /// // Privilege can't be used
104 /// // User permission request required
105 /// PrivacyPrivilegeManager.RequestPermission("http://tizen.org/privilege/account.read");
110 public static void RequestPermission(string privilege)
112 int ret = (int)Interop.PrivacyPrivilegeManager.RequestPermission(privilege, s_requestResponseCb, IntPtr.Zero);
113 if (ret != (int)Interop.PrivacyPrivilegeManager.ErrorCode.None)
115 Log.Error(LogTag, "Failed to request permission");
116 throw PrivacyPrivilegeManagerErrorFactory.GetException(ret);
121 /// Gets the response context for a given privilege.
123 /// <seealso cref="ResponseContext"/>
124 /// <param name="privilege">The privilege.</param>
125 /// <returns>The response context of a respective privilege.</returns>
126 /// <exception cref="ArgumentException">Thrown if the key is an invalid parameter.</exception>
129 /// private static void PPM_RequestResponse(object sender, RequestResponseEventArgs e)
131 /// if (e.cause == CallCause.Answer)
137 /// case RequestResult.AllowForever:
138 /// Console.WriteLine("User allowed usage of privilege {0} definitely", e.privilege);
140 /// case RequestResult.DenyForever:
141 /// Console.WriteLine("User denied usage of privilege {0} definitely", e.privilege);
143 /// case RequestResult.DenyOnce:
144 /// Console.WriteLine("User denied usage of privilege {0} this time", e.privilege);
150 /// Console.WriteLine("Error occured during requesting permission for {0}", e.privilege);
154 /// PrivacyPrivilegeManager.ResponseContext context = null;
155 /// PrivacyPrivilegeManager.GetResponseContext("http://tizen.org/privilege/account.read").TryGetTarget(out context);
156 /// if(context != null)
158 /// context.ResponseFetched += PPM_RequestResponse;
161 /// PrivacyPrivilegeManager.RequestPermission("http://tizen.org/privilege/account.read");
163 /// PrivacyPrivilegeManager.GetResponseContext("http://tizen.org/privilege/account.read").TryGetTarget(out context);
164 /// if(context != null)
166 /// context.ResponseFetched -= PPM_RequestResponse;
170 public static WeakReference<ResponseContext> GetResponseContext(string privilege)
172 if (!s_responseMap.ContainsKey(privilege))
174 s_responseMap[privilege] = new ResponseContext(privilege);
176 return new WeakReference<ResponseContext>(s_responseMap[privilege]);
180 /// This class manages event handlers of the privilege permission requests.
181 /// This class enables having event handlers for an individual privilege.
183 public class ResponseContext
185 private string _privilege;
187 internal ResponseContext(string privilege)
189 _privilege = privilege;
192 /// Occurs when the response for a permission request is fetched.
194 /// <exception cref="System.InvalidOperationException">Thrown when the bundle instance has been disposed.</exception>
195 public event EventHandler<RequestResponseEventArgs> ResponseFetched
199 _ResponseFetched += value;
204 _ResponseFetched -= value;
205 if (_ResponseFetched == null)
207 s_responseMap.Remove(_privilege);
212 private event EventHandler<RequestResponseEventArgs> _ResponseFetched;
214 internal void FireEvent(CallCause _cause, RequestResult _result)
216 _ResponseFetched?.Invoke(null, new RequestResponseEventArgs() { cause = _cause, result = _result, privilege = _privilege });
221 internal static class PrivacyPrivilegeManagerErrorFactory
223 static internal Exception GetException(int error)
225 Interop.PrivacyPrivilegeManager.ErrorCode errCode = (Interop.PrivacyPrivilegeManager.ErrorCode)error;
228 case Interop.PrivacyPrivilegeManager.ErrorCode.InvalidParameter:
229 return new ArgumentException("Invalid parameter");
230 case Interop.PrivacyPrivilegeManager.ErrorCode.IoError:
231 return new System.IO.IOException("I/O Error");
232 case Interop.PrivacyPrivilegeManager.ErrorCode.OutOfMemory:
233 return new OutOfMemoryException("Out of memory");
234 case Interop.PrivacyPrivilegeManager.ErrorCode.Unknown:
236 return new ArgumentException("Unknown error");