2 * Copyright (c) 2016 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.Runtime.InteropServices;
19 using Tizen.Internals.Errors;
20 using Tizen.Applications;
22 namespace Tizen.Security
25 /// The class provides the information of the given privilege and API version.
27 /// <since_tizen> 3 </since_tizen>
28 public static class Privilege
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)
34 if (type == PackageType.TPK)
36 return PackageTypeTpk;
38 else if (type == PackageType.WGT)
40 return PackageTypeWgt;
44 Tizen.Log.Error(Interop.Privilege.LogTag, "Invalid Parameter: PackageType doesn't include TPK or WGT");
45 throw new ArgumentException();
50 /// Gets the display name of the given privilege.
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)
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.");
73 /// Gets the display name of the given privilege.
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)
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.");
97 /// Gets the description of the given privilege.
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)
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.");
120 /// Gets the description of the given privilege.
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)
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.");
144 /// Gets the display name of the privacy group in which the given privilege is included.
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)
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.");
166 /// Gets the status of the given privacy related privilege.
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)
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.");
188 internal static class PrivilegeErrorFactory
190 internal static void ThrowException(Exception e, string msg)
192 Tizen.Log.Error(Interop.Privilege.LogTag, "[" + e.ToString() + "] " + msg);
195 internal static void CheckNThrowException(int err, string msg)
197 if (err == (int)ErrorCode.None)
199 string errorMessage = string.Format("[{0}] {1}", ErrorFacts.GetErrorMessage(err), msg);
200 Tizen.Log.Error(Interop.Privilege.LogTag, errorMessage);
203 case (int)ErrorCode.InvalidParameter:
204 throw new ArgumentException();
205 case (int)ErrorCode.OutOfMemory:
206 throw new OutOfMemoryException();
208 throw new InvalidOperationException();