-
Notifications
You must be signed in to change notification settings - Fork 241
/
Privilege.cs
executable file
·198 lines (190 loc) · 11.1 KB
/
Privilege.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
* Copyright (c) 2016-2023 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License
*/
using System;
using System.Runtime.InteropServices;
using Tizen.Internals.Errors;
using Tizen.Applications;
namespace Tizen.Security
{
/// <summary>
/// This class provides the information of the given privilege and the API version.
/// </summary>
/// <since_tizen> 3 </since_tizen>
public static class Privilege
{
internal static readonly string PackageTypeTpk = "PRVINFO_PACKAGE_TYPE_NATIVE";
internal static readonly string PackageTypeWgt = "PRVINFO_PACKAGE_TYPE_WEB";
internal static string ToPackageTypeString(PackageType type)
{
if (type == PackageType.TPK)
{
return PackageTypeTpk;
}
else if (type == PackageType.WGT)
{
return PackageTypeWgt;
}
else
{
Tizen.Log.Error(Interop.Privilege.LogTag, "Invalid Parameter: PackageType doesn't include TPK or WGT");
throw new ArgumentException();
}
}
/// <summary>
/// Gets the display name of the given privilege.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <remarks>If there's no matching privilege, then it returns last token of the given privilege. Since Tizen 6.0, if there's no matching privilege then it returns ArgumentException. </remarks>
/// <param name="apiVersion">The API version.</param>
/// <param name="privilege">The privilege.</param>
/// <returns>The display name of the given privilege at the given API version.</returns>
/// <exception cref="System.ArgumentNullException">Thrown when there is a null parameter.</exception>
/// <exception cref="System.ArgumentException">Thrown when there is an invalid parameter such as a non-existing privilege.</exception>
/// <exception cref="System.OutOfMemoryException">Thrown when out of memory occurs.</exception>
/// <exception cref="System.InvalidOperationException">Thrown when an internal error occurs.</exception>
public static string GetDisplayName(string apiVersion, string privilege)
{
string displayName;
if (apiVersion == null || privilege == null)
PrivilegeErrorFactory.ThrowException(new ArgumentNullException(), "apiVersion and privilege should not be null.");
PrivilegeErrorFactory.CheckNThrowException(
Interop.Privilege.GetDisplayName(apiVersion, privilege, out displayName),
"Failed to Get Privilege's Display Name.");
return displayName;
}
/// <summary>
/// Gets the display name of the given privilege.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <remarks>If there's no matching privilege, then it returns last token of the given privilege. Since Tizen 6.0, if there's no matching privilege then it returns ArgumentException.</remarks>
/// <param name="apiVersion">The API version.</param>
/// <param name="privilege">The privilege.</param>
/// <param name="packageType">The type of application package.</param>
/// <returns>The display name of the given privilege at the given API version and the package type.</returns>
/// <exception cref="System.ArgumentNullException">Thrown when there is a null parameter.</exception>
/// <exception cref="System.ArgumentException">Thrown when there is an invalid parameter such as a non-existing privilege.</exception>
/// <exception cref="System.OutOfMemoryException">Thrown when out of memory occurs.</exception>
/// <exception cref="System.InvalidOperationException">Thrown when an internal error occurs.</exception>
public static string GetDisplayName(string apiVersion, string privilege, PackageType packageType)
{
string displayName;
if (apiVersion == null || privilege == null)
PrivilegeErrorFactory.ThrowException(new ArgumentNullException(), "apiVersion and privilege should not be null.");
PrivilegeErrorFactory.CheckNThrowException(
Interop.Privilege.GetDisplayNameByPkgtype(ToPackageTypeString(packageType), apiVersion, privilege, out displayName),
"Failed to Get Privilege's Display Name.");
return displayName;
}
/// <summary>
/// Gets the description of the given privilege.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <remarks>If there's no matching privilege, then it returns description string for undefined privilege. Since Tizen 6.0, if there's no matching privilege then it returns ArgumentException. </remarks>
/// <param name="apiVersion">The API version.</param>
/// <param name="privilege">The privilege.</param>
/// <returns>The description of given privilege at the given API version</returns>
/// <exception cref="System.ArgumentNullException">Thrown when there is a null parameter.</exception>
/// <exception cref="System.ArgumentException">Thrown when there is an invalid parameter such as a non-existing privilege.</exception>
/// <exception cref="System.OutOfMemoryException">Thrown when out of memory occurs.</exception>
/// <exception cref="System.InvalidOperationException">Thrown when an internal error occurs.</exception>
public static string GetDescription(string apiVersion, string privilege)
{
string description;
if (apiVersion == null || privilege == null)
PrivilegeErrorFactory.ThrowException(new ArgumentNullException(), "apiVersion and privilege should not be null.");
PrivilegeErrorFactory.CheckNThrowException(
Interop.Privilege.GetDescription(apiVersion, privilege, out description),
"Failed to Get Privilege's Description.");
return description;
}
/// <summary>
/// Gets the description of the given privilege.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <remarks>If there's no matching privilege, then it returns description string for undefined privilege. Since Tizen 6.0, if there's no matching privilege then it returns ArgumentException. </remarks>
/// <param name="apiVersion">The API version.</param>
/// <param name="privilege">The privilege.</param>
/// <param name="packageType">The type of application package.</param>
/// <returns>The description of given privilege at the given API version and the package type.</returns>
/// <exception cref="System.ArgumentNullException">Thrown when there is a null parameter.</exception>
/// <exception cref="System.ArgumentException">Thrown when there is an invalid parameter such as a non-existing privilege.</exception>
/// <exception cref="System.OutOfMemoryException">Thrown when out of memory occurs.</exception>
/// <exception cref="System.InvalidOperationException">Thrown when an internal error occurs.</exception>
public static string GetDescription(string apiVersion, string privilege, PackageType packageType)
{
string description;
if (apiVersion == null || privilege == null)
PrivilegeErrorFactory.ThrowException(new ArgumentNullException(), "apiVersion and privilege should not be null.");
PrivilegeErrorFactory.CheckNThrowException(
Interop.Privilege.GetDescriptionByPkgtype(ToPackageTypeString(packageType),apiVersion, privilege, out description),
"Failed to Get Privilege's Description.");
return description;
}
/// <summary>
/// Gets the display name of the privacy group in which the given privilege is included.
/// </summary>
/// <feature>http://tizen.org/feature/security.privacy_privilege</feature>
/// <since_tizen> 3 </since_tizen>
/// <param name="privilege">The privilege.</param>
/// <remarks>The privilege must be privacy related.</remarks>
/// <returns>The privacy group's display name that the given privilege is included in.</returns>
/// <exception cref="System.ArgumentNullException">Thrown when there is a null parameter.</exception>
/// <exception cref="System.ArgumentException">Thrown when there is an invalid parameter such as a non-existing privilege.</exception>
/// <exception cref="System.OutOfMemoryException">Thrown when out of memory occurs.</exception>
/// <exception cref="System.InvalidOperationException">Thrown when an internal error occurs.</exception>
/// <exception cref="System.NotSupportedException">The required feature is not supported.</exception>
[Obsolete("Deprecated since API level 11.")]
public static string GetPrivacyDisplayName(string privilege)
{
string displayName;
if (privilege == null)
PrivilegeErrorFactory.ThrowException(new ArgumentNullException(), "privilege should not be null.");
PrivilegeErrorFactory.CheckNThrowException(
Interop.Privilege.GetPrivacyDisplayName(privilege, out displayName),
"Failed to Get Privacy's Display Name in Which the Given Privilege is included.");
return displayName;
}
}
internal static class PrivilegeErrorFactory
{
internal static void ThrowException(Exception e, string msg)
{
Tizen.Log.Error(Interop.Privilege.LogTag, "[" + e.ToString() + "] " + msg);
throw e;
}
internal static void CheckNThrowException(int err, string msg)
{
if (err == (int)ErrorCode.None)
return;
if (err == (int)Interop.Privilege.ErrorCode.NoMatchingPrivilege)
Tizen.Log.Error(Interop.Privilege.LogTag, "[System.ArgumentException] No such a privilege. " + msg);
else
Tizen.Log.Error(Interop.Privilege.LogTag, "[" + ErrorFacts.GetErrorMessage(err) + "] " + msg);
switch (err)
{
case (int)ErrorCode.NotSupported:
throw new NotSupportedException();
case (int)Interop.Privilege.ErrorCode.NoMatchingPrivilege:
case (int)ErrorCode.InvalidParameter:
throw new ArgumentException();
case (int)ErrorCode.OutOfMemory:
throw new OutOfMemoryException();
default:
throw new InvalidOperationException();
}
}
}
}