Setting since_tizen 3/4 on Tizen.NET API
[platform/core/csapi/tizenfx.git] / src / Tizen.Security.PrivacyPrivilegeManager / Tizen.Security / PrivacyPrivilegeManager.cs
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 using System;
18 using System.Collections.Generic;
19 using Tizen.Internals.Errors;
20
21 namespace Tizen.Security
22 {
23     /// <summary>
24     /// The PrivacyPrivilegeManager provides the properties or methods to check and request a permission for privacy privilege.
25     /// </summary>
26     /// <since_tizen> 4 </since_tizen>
27     public static class PrivacyPrivilegeManager
28     {
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>();
32
33         static PrivacyPrivilegeManager()
34         {
35             s_requestResponseCb = (Interop.PrivacyPrivilegeManager.CallCause cause, Interop.PrivacyPrivilegeManager.RequestResult result, string privilege, IntPtr userData) =>
36             {
37                 try
38                 {
39                     s_responseMap[privilege].FireEvent((CallCause)cause, (RequestResult) result);
40                 }
41                 catch (Exception e)
42                 {
43                     Log.Error(LogTag, "Exception in callback : " + e.Message);
44                 }
45             };
46         }
47
48         /// <summary>
49         /// Gets the status of a privacy privilege permission.
50         /// </summary>
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>
56         /// <example>
57         /// <code>
58         ///     CheckResult result = PrivacyPrivilegeManager.CheckPermission("http://tizen.org/privilege/account.read");
59         ///     switch (result)
60         ///     {
61         ///         case Allow:
62         ///             // Privilege can be used
63         ///             break;
64         ///         case Deny:
65         ///             // Privilege can't be used
66         ///             break;
67         ///         case Ask:
68         ///             // User permission request required
69         ///             PrivacyPrivilegeManager.RequestPermission("http://tizen.org/privilege/account.read");
70         ///             break;
71         ///     }
72         /// </code>
73         /// </example>
74         /// <since_tizen> 4 </since_tizen>
75         public static CheckResult CheckPermission(string privilege)
76         {
77             Interop.PrivacyPrivilegeManager.CheckResult result;
78             int ret = (int)Interop.PrivacyPrivilegeManager.CheckPermission(privilege, out result);
79             if (ret != (int)Interop.PrivacyPrivilegeManager.ErrorCode.None)
80             {
81                 Log.Error(LogTag, "Failed to check permission");
82                 throw PrivacyPrivilegeManagerErrorFactory.GetException(ret);
83             }
84             return (CheckResult)result;
85         }
86
87         /// <summary>
88         /// Triggers the permission request for a user.
89         /// </summary>
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>
94         /// <example>
95         /// <code>
96         ///     CheckResult result = PrivacyPrivilegeManager.CheckPermission("http://tizen.org/privilege/account.read");
97         ///     switch (result)
98         ///     {
99         ///         case Allow:
100         ///             // Privilege can be used
101         ///             break;
102         ///         case Deny:
103         ///             // Privilege can't be used
104         ///             break;
105         ///         case Ask:
106         ///             // User permission request required
107         ///             PrivacyPrivilegeManager.RequestPermission("http://tizen.org/privilege/account.read");
108         ///             break;
109         ///     }
110         /// </code>
111         /// </example>
112         /// <since_tizen> 4 </since_tizen>
113         public static void RequestPermission(string privilege)
114         {
115             int ret = (int)Interop.PrivacyPrivilegeManager.RequestPermission(privilege, s_requestResponseCb, IntPtr.Zero);
116             if (ret != (int)Interop.PrivacyPrivilegeManager.ErrorCode.None)
117             {
118                 Log.Error(LogTag, "Failed to request permission");
119                 throw PrivacyPrivilegeManagerErrorFactory.GetException(ret);
120             }
121         }
122
123         /// <summary>
124         /// Gets the response context for a given privilege.
125         /// </summary>
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>
130         /// <example>
131         /// <code>
132         /// private static void PPM_RequestResponse(object sender, RequestResponseEventArgs e)
133         /// {
134         ///     if (e.cause == CallCause.Answer)
135         ///     {
136         ///        switch(e.result)
137         ///
138         ///        {
139         ///
140         ///         case RequestResult.AllowForever:
141         ///             Console.WriteLine("User allowed usage of privilege {0} definitely", e.privilege);
142         ///             break;
143         ///         case RequestResult.DenyForever:
144         ///             Console.WriteLine("User denied usage of privilege {0} definitely", e.privilege);
145         ///             break;
146         ///         case RequestResult.DenyOnce:
147         ///             Console.WriteLine("User denied usage of privilege {0} this time", e.privilege);
148         ///             break;
149         ///         };
150         ///     }
151         ///     else
152         ///     {
153         ///         Console.WriteLine("Error occured during requesting permission for {0}", e.privilege);
154         ///     }
155         ///}
156         ///
157         ///     PrivacyPrivilegeManager.ResponseContext context = null;
158         ///     PrivacyPrivilegeManager.GetResponseContext("http://tizen.org/privilege/account.read").TryGetTarget(out context);
159         ///     if(context != null)
160         ///     {
161         ///         context.ResponseFetched += PPM_RequestResponse;
162         ///     }
163         ///
164         ///     PrivacyPrivilegeManager.RequestPermission("http://tizen.org/privilege/account.read");
165         ///
166         ///     PrivacyPrivilegeManager.GetResponseContext("http://tizen.org/privilege/account.read").TryGetTarget(out context);
167         ///     if(context != null)
168         ///     {
169         ///         context.ResponseFetched -= PPM_RequestResponse;
170         ///     }
171         /// </code>
172         /// </example>
173         /// <since_tizen> 4 </since_tizen>
174         public static WeakReference<ResponseContext> GetResponseContext(string privilege)
175         {
176             if (!s_responseMap.ContainsKey(privilege))
177             {
178                 s_responseMap[privilege] = new ResponseContext(privilege);
179             }
180             return new WeakReference<ResponseContext>(s_responseMap[privilege]);
181         }
182
183         /// <summary>
184         /// This class manages event handlers of the privilege permission requests.
185         /// This class enables having event handlers for an individual privilege.
186         /// </summary>
187         /// <since_tizen> 4 </since_tizen>
188         public class ResponseContext
189         {
190             private string _privilege;
191
192             internal ResponseContext(string privilege)
193             {
194                 _privilege = privilege;
195             }
196             /// <summary>
197             /// Occurs when the response for a permission request is fetched.
198             /// </summary>
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
202             {
203                 add
204                 {
205                     _ResponseFetched += value;
206                 }
207
208                 remove
209                 {
210                     _ResponseFetched -= value;
211                     if (_ResponseFetched == null)
212                     {
213                         s_responseMap.Remove(_privilege);
214                     }
215                 }
216             }
217
218             private event EventHandler<RequestResponseEventArgs> _ResponseFetched;
219
220             internal void FireEvent(CallCause _cause, RequestResult _result)
221             {
222                 _ResponseFetched?.Invoke(null, new RequestResponseEventArgs() { cause = _cause, result = _result, privilege = _privilege });
223             }
224         }
225     }
226
227     internal static class PrivacyPrivilegeManagerErrorFactory
228     {
229         static internal Exception GetException(int error)
230         {
231             Interop.PrivacyPrivilegeManager.ErrorCode errCode = (Interop.PrivacyPrivilegeManager.ErrorCode)error;
232             switch(errCode)
233             {
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:
241                 default:
242                     return new ArgumentException("Unknown error");
243             }
244         }
245     }
246 }