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 /// <since_tizen> 4 </since_tizen>
27 public static class PrivacyPrivilegeManager
29 private const string LogTag = "Tizen.Privilege";
30 private static Interop.PrivacyPrivilegeManager.RequestResponseCallback s_requestResponseCb;
31 private static IDictionary<string, ResponseContext> s_responseMap = new Dictionary<string, ResponseContext>();
33 static PrivacyPrivilegeManager()
35 s_requestResponseCb = (Interop.PrivacyPrivilegeManager.CallCause cause, Interop.PrivacyPrivilegeManager.RequestResult result, string privilege, IntPtr userData) =>
39 s_responseMap[privilege].FireEvent((CallCause)cause, (RequestResult) result);
43 Log.Error(LogTag, "Exception in callback : " + e.Message);
49 /// Gets the status of a privacy privilege permission.
51 /// <param name="privilege">The privacy privilege to be checked.</param>
52 /// <returns>The permission setting for a respective privilege.</returns>
53 /// <exception cref="ArgumentException">Thrown when an invalid parameter is passed.</exception>
54 /// <exception cref="OutOfMemoryException">Thrown when a memory error occurred.</exception>
55 /// <exception cref="System.IO.IOException">Thrown when the method failed due to an internal I/O error.</exception>
58 /// CheckResult result = PrivacyPrivilegeManager.CheckPermission("http://tizen.org/privilege/account.read");
62 /// // Privilege can be used
65 /// // Privilege can't be used
68 /// // User permission request required
69 /// PrivacyPrivilegeManager.RequestPermission("http://tizen.org/privilege/account.read");
74 /// <since_tizen> 4 </since_tizen>
75 public static CheckResult CheckPermission(string privilege)
77 Interop.PrivacyPrivilegeManager.CheckResult result;
78 int ret = (int)Interop.PrivacyPrivilegeManager.CheckPermission(privilege, out result);
79 if (ret != (int)Interop.PrivacyPrivilegeManager.ErrorCode.None)
81 Log.Error(LogTag, "Failed to check permission");
82 throw PrivacyPrivilegeManagerErrorFactory.GetException(ret);
84 return (CheckResult)result;
88 /// Triggers the permission request for a user.
90 /// <param name="privilege">The privacy privilege to be requested.</param>
91 /// <exception cref="ArgumentException">Thrown when an invalid parameter is passed.</exception>
92 /// <exception cref="OutOfMemoryException">Thrown when a memory error occurred.</exception>
93 /// <exception cref="System.IO.IOException">Thrown when the method failed due to an internal I/O error.</exception>
96 /// CheckResult result = PrivacyPrivilegeManager.CheckPermission("http://tizen.org/privilege/account.read");
100 /// // Privilege can be used
103 /// // Privilege can't be used
106 /// // User permission request required
107 /// PrivacyPrivilegeManager.RequestPermission("http://tizen.org/privilege/account.read");
112 /// <since_tizen> 4 </since_tizen>
113 public static void RequestPermission(string privilege)
115 int ret = (int)Interop.PrivacyPrivilegeManager.RequestPermission(privilege, s_requestResponseCb, IntPtr.Zero);
116 if (ret != (int)Interop.PrivacyPrivilegeManager.ErrorCode.None)
118 Log.Error(LogTag, "Failed to request permission");
119 throw PrivacyPrivilegeManagerErrorFactory.GetException(ret);
124 /// Gets the response context for a given privilege.
126 /// <seealso cref="ResponseContext"/>
127 /// <param name="privilege">The privilege.</param>
128 /// <returns>The response context of a respective privilege.</returns>
129 /// <exception cref="ArgumentException">Thrown if the key is an invalid parameter.</exception>
132 /// private static void PPM_RequestResponse(object sender, RequestResponseEventArgs e)
134 /// if (e.cause == CallCause.Answer)
140 /// case RequestResult.AllowForever:
141 /// Console.WriteLine("User allowed usage of privilege {0} definitely", e.privilege);
143 /// case RequestResult.DenyForever:
144 /// Console.WriteLine("User denied usage of privilege {0} definitely", e.privilege);
146 /// case RequestResult.DenyOnce:
147 /// Console.WriteLine("User denied usage of privilege {0} this time", e.privilege);
153 /// Console.WriteLine("Error occured during requesting permission for {0}", e.privilege);
157 /// PrivacyPrivilegeManager.ResponseContext context = null;
158 /// PrivacyPrivilegeManager.GetResponseContext("http://tizen.org/privilege/account.read").TryGetTarget(out context);
159 /// if(context != null)
161 /// context.ResponseFetched += PPM_RequestResponse;
164 /// PrivacyPrivilegeManager.RequestPermission("http://tizen.org/privilege/account.read");
166 /// PrivacyPrivilegeManager.GetResponseContext("http://tizen.org/privilege/account.read").TryGetTarget(out context);
167 /// if(context != null)
169 /// context.ResponseFetched -= PPM_RequestResponse;
173 /// <since_tizen> 4 </since_tizen>
174 public static WeakReference<ResponseContext> GetResponseContext(string privilege)
176 if (!s_responseMap.ContainsKey(privilege))
178 s_responseMap[privilege] = new ResponseContext(privilege);
180 return new WeakReference<ResponseContext>(s_responseMap[privilege]);
184 /// This class manages event handlers of the privilege permission requests.
185 /// This class enables having event handlers for an individual privilege.
187 /// <since_tizen> 4 </since_tizen>
188 public class ResponseContext
190 private string _privilege;
192 internal ResponseContext(string privilege)
194 _privilege = privilege;
197 /// Occurs when the response for a permission request is fetched.
199 /// <exception cref="System.InvalidOperationException">Thrown when the bundle instance has been disposed.</exception>
200 /// <since_tizen> 4 </since_tizen>
201 public event EventHandler<RequestResponseEventArgs> ResponseFetched
205 _ResponseFetched += value;
210 _ResponseFetched -= value;
211 if (_ResponseFetched == null)
213 s_responseMap.Remove(_privilege);
218 private event EventHandler<RequestResponseEventArgs> _ResponseFetched;
220 internal void FireEvent(CallCause _cause, RequestResult _result)
222 _ResponseFetched?.Invoke(null, new RequestResponseEventArgs() { cause = _cause, result = _result, privilege = _privilege });
227 internal static class PrivacyPrivilegeManagerErrorFactory
229 static internal Exception GetException(int error)
231 Interop.PrivacyPrivilegeManager.ErrorCode errCode = (Interop.PrivacyPrivilegeManager.ErrorCode)error;
234 case Interop.PrivacyPrivilegeManager.ErrorCode.InvalidParameter:
235 return new ArgumentException("Invalid parameter");
236 case Interop.PrivacyPrivilegeManager.ErrorCode.IoError:
237 return new System.IO.IOException("I/O Error");
238 case Interop.PrivacyPrivilegeManager.ErrorCode.OutOfMemory:
239 return new OutOfMemoryException("Out of memory");
240 case Interop.PrivacyPrivilegeManager.ErrorCode.Unknown:
242 return new ArgumentException("Unknown error");