diff options
Diffstat (limited to 'src/Tizen.Security')
-rw-r--r-- | src/Tizen.Security/.gitignore | 2 | ||||
-rw-r--r-- | src/Tizen.Security/Interop/Interop.Libraries.cs | 23 | ||||
-rwxr-xr-x | src/Tizen.Security/Interop/Interop.Privilege.cs | 46 | ||||
-rw-r--r-- | src/Tizen.Security/Tizen.Security.csproj | 14 | ||||
-rwxr-xr-x | src/Tizen.Security/Tizen.Security/Privilege.cs | 205 |
5 files changed, 290 insertions, 0 deletions
diff --git a/src/Tizen.Security/.gitignore b/src/Tizen.Security/.gitignore new file mode 100644 index 0000000..fba4e61 --- /dev/null +++ b/src/Tizen.Security/.gitignore @@ -0,0 +1,2 @@ +/obj +/bin diff --git a/src/Tizen.Security/Interop/Interop.Libraries.cs b/src/Tizen.Security/Interop/Interop.Libraries.cs new file mode 100644 index 0000000..7188262 --- /dev/null +++ b/src/Tizen.Security/Interop/Interop.Libraries.cs @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2016 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 + */ + +internal static partial class Interop +{ + internal static partial class Libraries + { + public const string Privilege = "libprivilege-info.so.1"; + } +} diff --git a/src/Tizen.Security/Interop/Interop.Privilege.cs b/src/Tizen.Security/Interop/Interop.Privilege.cs new file mode 100755 index 0000000..8b921e8 --- /dev/null +++ b/src/Tizen.Security/Interop/Interop.Privilege.cs @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2016 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.Security; + +internal static partial class Interop +{ + internal static partial class Privilege + { + internal static string LogTag = "Tizen.Security.Privilege"; + + [DllImport(Libraries.Privilege, EntryPoint = "privilege_info_get_display_name")] + internal static extern int GetDisplayName(string apiVersion, string privilege, out string displayName); + + [DllImport(Libraries.Privilege, EntryPoint = "privilege_info_get_description")] + internal static extern int GetDescription(string apiVersion, string privilege, out string description); + + [DllImport(Libraries.Privilege, EntryPoint = "privilege_info_get_display_name_by_pkgtype")] + internal static extern int GetDisplayNameByPkgtype(string packageType, string apiVersion, string privilege, out string displayName); + + [DllImport(Libraries.Privilege, EntryPoint = "privilege_info_get_description_by_pkgtype")] + internal static extern int GetDescriptionByPkgtype(string packageType, string apiVersion, string privilege, out string description); + + [DllImport(Libraries.Privilege, EntryPoint = "privilege_info_get_privacy_display_name")] + internal static extern int GetPrivacyDisplayName(string privilege, out string displayName); + + [DllImport(Libraries.Privilege, EntryPoint = "privilege_info_get_privacy_privilege_status")] + internal static extern int GetPrivacyPrivilegeStatus(string privilege, out bool status); + } +} diff --git a/src/Tizen.Security/Tizen.Security.csproj b/src/Tizen.Security/Tizen.Security.csproj new file mode 100644 index 0000000..b877776 --- /dev/null +++ b/src/Tizen.Security/Tizen.Security.csproj @@ -0,0 +1,14 @@ +<Project Sdk="Microsoft.NET.Sdk"> + <Import Project="../../build/common.props" /> + + <PropertyGroup> + <TargetFramework>netstandard1.6</TargetFramework> + </PropertyGroup> + + <ItemGroup> + <ProjectReference Include="..\Tizen.Applications.PackageManager\Tizen.Applications.PackageManager.csproj" /> + </ItemGroup> + + <Import Project="../../build/common.targets" /> +</Project> + diff --git a/src/Tizen.Security/Tizen.Security/Privilege.cs b/src/Tizen.Security/Tizen.Security/Privilege.cs new file mode 100755 index 0000000..a59cfd5 --- /dev/null +++ b/src/Tizen.Security/Tizen.Security/Privilege.cs @@ -0,0 +1,205 @@ +/* + * Copyright (c) 2016 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> + /// The class provides the information of the given privilege and API version. + /// </summary> + 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> + /// <remarks>If there's no matching privilege then it returns last token of given privilege.</remarks> + /// <param name="apiVersion">The api version</param> + /// <param name="privilege">The privilege</param> + /// <returns>The display name of given privilege at 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.</exception> + /// <exception cref="System.OutOfMemoryException">Thrown when out of memory occurs</exception> + /// <exception cref="System.InvalidOperationException">Thrown when 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> + /// <remarks>If there's no matching privilege then it returns last token of given privilege.</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 given privilege at 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.</exception> + /// <exception cref="System.OutOfMemoryException">Thrown when out of memory occurs</exception> + /// <exception cref="System.InvalidOperationException">Thrown when 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> + /// <remarks>If there's no matching privilege then it returns description string for undefined privilege.</remarks> + /// <param name="apiVersion">The api version</param> + /// <param name="privilege">The privilege</param> + /// <returns>The description of given privilege at 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.</exception> + /// <exception cref="System.OutOfMemoryException">Thrown when out of memory occurs</exception> + /// <exception cref="System.InvalidOperationException">Thrown when 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> + /// <remarks>If there's no matching privilege then it returns description string for undefined privilege.</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 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.</exception> + /// <exception cref="System.OutOfMemoryException">Thrown when out of memory occurs</exception> + /// <exception cref="System.InvalidOperationException">Thrown when 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> + /// <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.</exception> + /// <exception cref="System.OutOfMemoryException">Thrown when out of memory occurs</exception> + /// <exception cref="System.InvalidOperationException">Thrown when internal error occurs.</exception> + 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; + } + + /// <summary> + /// Gets the status of the given privacy related privilege. + /// </summary> + /// <param name="privilege">The privilege</param> + /// <remarks>The privilege must be privacy related.</remarks> + /// <returns>status true if the privilege is on and false if the privilege is off.</returns> + /// <exception cref="System.ArgumentNullException">Thrown when there is a null parameter.</exception> + /// <exception cref="System.ArgumentException">Thrown when there is an invalid parameter.</exception> + /// <exception cref="System.OutOfMemoryException">Thrown when out of memory occurs</exception> + /// <exception cref="System.InvalidOperationException">Thrown when internal error occurs.</exception> + public static bool GetPrivacyPrivilegeStatus(string privilege) + { + bool status; + if (privilege == null) + PrivilegeErrorFactory.ThrowException(new ArgumentNullException(), "privilege should not be null."); + PrivilegeErrorFactory.CheckNThrowException( + Interop.Privilege.GetPrivacyPrivilegeStatus(privilege, out status), + "Failed to Get Privacy Privilege's Status."); + return status; + } + } + + 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; + string errorMessage = string.Format("[{0}] {1}", ErrorFacts.GetErrorMessage(err), msg); + Tizen.Log.Error(Interop.Privilege.LogTag, errorMessage); + switch (err) + { + case (int)ErrorCode.InvalidParameter: + throw new ArgumentException(); + case (int)ErrorCode.OutOfMemory: + throw new OutOfMemoryException(); + default: + throw new InvalidOperationException(); + } + } + } +} |