Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Security / Tizen.Security / Privilege.cs
1 /*
2  *  Copyright (c) 2016 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.Runtime.InteropServices;
19 using Tizen.Internals.Errors;
20 using Tizen.Applications;
21
22 namespace Tizen.Security
23 {
24     /// <summary>
25     /// The class provides the information of the given privilege and API version.
26     /// </summary>
27     /// <since_tizen> 3 </since_tizen>
28     public static class Privilege
29     {
30         internal static readonly string PackageTypeTpk = "PRVINFO_PACKAGE_TYPE_NATIVE";
31         internal static readonly string PackageTypeWgt = "PRVINFO_PACKAGE_TYPE_WEB";
32         internal static string ToPackageTypeString(PackageType type)
33         {
34             if (type == PackageType.TPK)
35             {
36                 return PackageTypeTpk;
37             }
38             else if (type == PackageType.WGT)
39             {
40                 return PackageTypeWgt;
41             }
42             else
43             {
44                 Tizen.Log.Error(Interop.Privilege.LogTag, "Invalid Parameter: PackageType doesn't include TPK or WGT");
45                 throw new ArgumentException();
46             }
47         }
48
49         /// <summary>
50         /// Gets the display name of the given privilege.
51         /// </summary>
52         /// <since_tizen> 3 </since_tizen>
53         /// <remarks>If there's no matching privilege then it returns last token of given privilege.</remarks>
54         /// <param name="apiVersion">The api version</param>
55         /// <param name="privilege">The privilege</param>
56         /// <returns>The display name of given privilege at given api version</returns>
57         /// <exception cref="System.ArgumentNullException">Thrown when there is a null parameter.</exception>
58         /// <exception cref="System.ArgumentException">Thrown when there is an invalid parameter.</exception>
59         /// <exception cref="System.OutOfMemoryException">Thrown when out of memory occurs</exception>
60         /// <exception cref="System.InvalidOperationException">Thrown when internal error occurs.</exception>
61         public static string GetDisplayName(string apiVersion, string privilege)
62         {
63             string displayName;
64             if (apiVersion == null || privilege == null)
65                 PrivilegeErrorFactory.ThrowException(new ArgumentNullException(), "apiVersion and privilege should not be null.");
66             PrivilegeErrorFactory.CheckNThrowException(
67                     Interop.Privilege.GetDisplayName(apiVersion, privilege, out displayName),
68                     "Failed to Get Privilege's Display Name.");
69             return displayName;
70         }
71
72         /// <summary>
73         /// Gets the display name of the given privilege.
74         /// </summary>
75         /// <since_tizen> 3 </since_tizen>
76         /// <remarks>If there's no matching privilege then it returns last token of given privilege.</remarks>
77         /// <param name="apiVersion">The api version</param>
78         /// <param name="privilege">The privilege</param>
79         /// <param name="packageType">The type of application package</param>
80         /// <returns>The display name of given privilege at given api version and the package type</returns>
81         /// <exception cref="System.ArgumentNullException">Thrown when there is a null parameter.</exception>
82         /// <exception cref="System.ArgumentException">Thrown when there is an invalid parameter.</exception>
83         /// <exception cref="System.OutOfMemoryException">Thrown when out of memory occurs</exception>
84         /// <exception cref="System.InvalidOperationException">Thrown when internal error occurs.</exception>
85         public static string GetDisplayName(string apiVersion, string privilege, PackageType packageType)
86         {
87             string displayName;
88             if (apiVersion == null || privilege == null)
89                 PrivilegeErrorFactory.ThrowException(new ArgumentNullException(), "apiVersion and privilege should not be null.");
90             PrivilegeErrorFactory.CheckNThrowException(
91                 Interop.Privilege.GetDisplayNameByPkgtype(ToPackageTypeString(packageType), apiVersion, privilege, out displayName),
92                 "Failed to Get Privilege's Display Name.");
93             return displayName;
94         }
95
96         /// <summary>
97         /// Gets the description of the given privilege.
98         /// </summary>
99         /// <since_tizen> 3 </since_tizen>
100         /// <remarks>If there's no matching privilege then it returns description string for undefined privilege.</remarks>
101         /// <param name="apiVersion">The api version</param>
102         /// <param name="privilege">The privilege</param>
103         /// <returns>The description of given privilege at given api version</returns>
104         /// <exception cref="System.ArgumentNullException">Thrown when there is a null parameter.</exception>
105         /// <exception cref="System.ArgumentException">Thrown when there is an invalid parameter.</exception>
106         /// <exception cref="System.OutOfMemoryException">Thrown when out of memory occurs</exception>
107         /// <exception cref="System.InvalidOperationException">Thrown when internal error occurs.</exception>
108         public static string GetDescription(string apiVersion, string privilege)
109         {
110             string description;
111             if (apiVersion == null || privilege == null)
112                 PrivilegeErrorFactory.ThrowException(new ArgumentNullException(), "apiVersion and privilege should not be null.");
113             PrivilegeErrorFactory.CheckNThrowException(
114                     Interop.Privilege.GetDescription(apiVersion, privilege, out description),
115                     "Failed to Get Privilege's Description.");
116             return description;
117         }
118
119         /// <summary>
120         /// Gets the description of the given privilege.
121         /// </summary>
122         /// <since_tizen> 3 </since_tizen>
123         /// <remarks>If there's no matching privilege then it returns description string for undefined privilege.</remarks>
124         /// <param name="apiVersion">The api version</param>
125         /// <param name="privilege">The privilege</param>
126         /// <param name="packageType">The type of application package</param>
127         /// <returns>The description of given privilege at given api version and the package type</returns>
128         /// <exception cref="System.ArgumentNullException">Thrown when there is a null parameter.</exception>
129         /// <exception cref="System.ArgumentException">Thrown when there is an invalid parameter.</exception>
130         /// <exception cref="System.OutOfMemoryException">Thrown when out of memory occurs</exception>
131         /// <exception cref="System.InvalidOperationException">Thrown when internal error occurs.</exception>
132         public static string GetDescription(string apiVersion, string privilege, PackageType packageType)
133         {
134             string description;
135             if (apiVersion == null || privilege == null)
136                 PrivilegeErrorFactory.ThrowException(new ArgumentNullException(), "apiVersion and privilege should not be null.");
137             PrivilegeErrorFactory.CheckNThrowException(
138                     Interop.Privilege.GetDescriptionByPkgtype(ToPackageTypeString(packageType),apiVersion, privilege, out description),
139                     "Failed to Get Privilege's Description.");
140             return description;
141         }
142
143         /// <summary>
144         /// Gets the display name of the privacy group in which the given privilege is included.
145         /// </summary>
146         /// <since_tizen> 3 </since_tizen>
147         /// <param name="privilege">The privilege</param>
148         /// <remarks>The privilege must be privacy related.</remarks>
149         /// <returns>The privacy group's display name that the given privilege is included in</returns>
150         /// <exception cref="System.ArgumentNullException">Thrown when there is a null parameter.</exception>
151         /// <exception cref="System.ArgumentException">Thrown when there is an invalid parameter.</exception>
152         /// <exception cref="System.OutOfMemoryException">Thrown when out of memory occurs</exception>
153         /// <exception cref="System.InvalidOperationException">Thrown when internal error occurs.</exception>
154         public static string GetPrivacyDisplayName(string privilege)
155         {
156             string displayName;
157             if (privilege == null)
158                 PrivilegeErrorFactory.ThrowException(new ArgumentNullException(), "privilege should not be null.");
159             PrivilegeErrorFactory.CheckNThrowException(
160                     Interop.Privilege.GetPrivacyDisplayName(privilege, out displayName),
161                     "Failed to Get Privacy's Display Name in Which the Given Privilege is included.");
162             return displayName;
163         }
164
165         /// <summary>
166         /// Gets the status of the given privacy related privilege.
167         /// </summary>
168         /// <since_tizen> 3 </since_tizen>
169         /// <param name="privilege">The privilege</param>
170         /// <remarks>The privilege must be privacy related.</remarks>
171         /// <returns>status true if the privilege is on and false if the privilege is off.</returns>
172         /// <exception cref="System.ArgumentNullException">Thrown when there is a null parameter.</exception>
173         /// <exception cref="System.ArgumentException">Thrown when there is an invalid parameter.</exception>
174         /// <exception cref="System.OutOfMemoryException">Thrown when out of memory occurs</exception>
175         /// <exception cref="System.InvalidOperationException">Thrown when internal error occurs.</exception>
176         public static bool GetPrivacyPrivilegeStatus(string privilege)
177         {
178             bool status;
179             if (privilege == null)
180                 PrivilegeErrorFactory.ThrowException(new ArgumentNullException(), "privilege should not be null.");
181             PrivilegeErrorFactory.CheckNThrowException(
182                     Interop.Privilege.GetPrivacyPrivilegeStatus(privilege, out status),
183                     "Failed to Get Privacy Privilege's Status.");
184             return status;
185         }
186     }
187
188     internal static class PrivilegeErrorFactory
189     {
190         internal static void ThrowException(Exception e, string msg)
191         {
192             Tizen.Log.Error(Interop.Privilege.LogTag, "[" + e.ToString() + "] " + msg);
193             throw e;
194         }
195         internal static void CheckNThrowException(int err, string msg)
196         {
197             if (err == (int)ErrorCode.None)
198                 return;
199             string errorMessage = string.Format("[{0}] {1}", ErrorFacts.GetErrorMessage(err), msg);
200             Tizen.Log.Error(Interop.Privilege.LogTag, errorMessage);
201             switch (err)
202             {
203                 case (int)ErrorCode.InvalidParameter:
204                     throw new ArgumentException();
205                 case (int)ErrorCode.OutOfMemory:
206                     throw new OutOfMemoryException();
207                 default:
208                     throw new InvalidOperationException();
209             }
210         }
211     }
212 }