[PackageManager][TCSACR-442] Add new APIs for resource control (#3415)
authorjeremy-jang <35089715+jeremy-jang@users.noreply.github.com>
Fri, 10 Sep 2021 05:10:48 +0000 (14:10 +0900)
committerGitHub <noreply@github.com>
Fri, 10 Sep 2021 05:10:48 +0000 (14:10 +0900)
* [PackageManager] Add new APIs for resource control

Signed-off-by: Sangyoon Jang <jeremy.jang@samsung.com>
* [PackageManager] Fix to use IEnumerable at public API

Use IEnumerable<string> instead of List<string>.

Signed-off-by: Sangyoon Jang <jeremy.jang@samsung.com>
* Fix some doxygen comments of Package, PackageFilter class

Signed-off-by: Sangyoon Jang <jeremy.jang@samsung.com>
Co-authored-by: pjh9216 <jh9216.park@samsung.com>
src/Tizen.Applications.PackageManager/Interop/Interop.Package.cs
src/Tizen.Applications.PackageManager/Interop/Interop.PackageManager.cs
src/Tizen.Applications.PackageManager/Tizen.Applications/Package.cs
src/Tizen.Applications.PackageManager/Tizen.Applications/PackageFilter.cs
src/Tizen.Applications.PackageManager/Tizen.Applications/PackageManager.cs

index 05be277..02b03b5 100644 (file)
@@ -37,6 +37,9 @@ internal static partial class Interop
         [UnmanagedFunctionPointerAttribute(CallingConvention.Cdecl)]
         internal delegate bool PackageInfoDependencyInfoCallback(string from, string to, string type, string requiredVersion, IntPtr userData);
 
+        [UnmanagedFunctionPointerAttribute(CallingConvention.Cdecl)]
+        internal delegate bool PackageInfoResAllowedPackageCallback(string allowedPackage, IntPtr requiredPrivileges, IntPtr userData);
+
         // Any change here might require changes in Tizen.Applications.AppType enum
         internal enum AppType
         {
@@ -123,5 +126,11 @@ internal static partial class Interop
 
         [DllImport(Libraries.PackageManager, EntryPoint = "package_info_foreach_dependency_info_depends_on")]
         internal static extern ErrorCode PackageInfoForeachDependencyInfoDependsOn(IntPtr handle, PackageInfoDependencyInfoCallback callback, IntPtr userData);
+
+        [DllImport(Libraries.PackageManager, EntryPoint = "package_info_foreach_res_allowed_package")]
+        internal static extern ErrorCode PackageInfoForeachResAllowedPackage(IntPtr handle, PackageInfoResAllowedPackageCallback callback, IntPtr user_data);
+
+        [DllImport(Libraries.PackageManager, EntryPoint = "package_info_foreach_required_privilege")]
+        internal static extern ErrorCode PackageInfoForeachRequiredPrivilege(IntPtr privilegeHandle, PackageInfoPrivilegeInfoCallback callback, IntPtr user_data);
     }
 }
index ee1683b..2e38a78 100755 (executable)
@@ -150,6 +150,9 @@ internal static partial class Interop
         [DllImport(Libraries.PackageManager, EntryPoint = "package_manager_filter_add_bool")]
         internal static extern ErrorCode PackageManagerFilterAdd(IntPtr handle, string property, bool value);
 
+        [DllImport(Libraries.PackageManager, EntryPoint = "package_manager_filter_add_string")]
+        internal static extern ErrorCode PackageManagerFilterAdd(IntPtr handle, string property, string value);
+
         [DllImport(Libraries.PackageManager, EntryPoint = "package_manager_filter_foreach_package_info")]
         internal static extern ErrorCode PackageManagerFilterForeachPackageInfo(IntPtr handle, PackageManagerPackageInfoCallback callback, IntPtr userData);
 
index 6b19f5a..a51af97 100644 (file)
@@ -50,6 +50,7 @@ namespace Tizen.Applications
         private int _callbackId = 0;
         private List<PackageDependencyInformation> _dependencyTo;
         private List<PackageDependencyInformation> _dependencyFrom;
+        private IReadOnlyDictionary<string, IEnumerable<string>> _allowedPackagesAndPrivilegesList;
 
         private Package(string pkgId)
         {
@@ -288,6 +289,16 @@ namespace Tizen.Applications
             return (CertCompareResultType)compareResult;
         }
 
+        /// <summary>
+        /// The allowed packages and required privileges information.
+        /// </summary>
+        /// <remarks> The dictionary contains application IDs as the keys, and a collection of privileges related to that application as the value.</remarks>
+        /// <since_tizen> 9 </since_tizen>
+        public IReadOnlyDictionary<string, IEnumerable<string>> AllowedPackagesAndPrivileges
+        {
+            get { return _allowedPackagesAndPrivilegesList; }
+        }
+
         // This method assumes that given arguments are already validated and have valid values.
         internal static Package CreatePackage(IntPtr handle, string pkgId)
         {
@@ -376,6 +387,7 @@ namespace Tizen.Applications
             package._privileges = GetPackagePrivilegeInformation(handle);
             package._dependencyTo = GetPackageDependency(handle);
             package._dependencyFrom = GetPackageDependencyDependsOn(handle);
+            package._allowedPackagesAndPrivilegesList = GetAllowedPackagesAndPrivileges(handle);
             return package;
         }
 
@@ -474,5 +486,37 @@ namespace Tizen.Applications
             }
             return dependencies;
         }
+
+        private static Dictionary<string, IEnumerable<string>> GetAllowedPackagesAndPrivileges(IntPtr packageInfoHandle)
+        {
+            Interop.PackageManager.ErrorCode err;
+            Dictionary<string, IEnumerable<string>> allowedPackagesAndPrivileges = new Dictionary<string, IEnumerable<string>>();
+            Interop.Package.PackageInfoResAllowedPackageCallback allowedPackageCallback = (allowedPackage, requiredPrivileges, userData) =>
+            {
+                List<string> requiredPrivilegesList = new List<string>();
+                Interop.Package.PackageInfoPrivilegeInfoCallback requiredPrivCallback = (priv, requiredPrivCbUserData) =>
+                {
+                    requiredPrivilegesList.Add(priv);
+                    return true;
+                };
+                err = Interop.Package.PackageInfoForeachRequiredPrivilege(requiredPrivileges, requiredPrivCallback, IntPtr.Zero);
+                if (err != Interop.PackageManager.ErrorCode.None)
+                {
+                    Log.Warn(LogTag, string.Format("Failed to get required privileges of allowed packages info. err = {0}", err));
+                }
+                else
+                {
+                    allowedPackagesAndPrivileges.Add(allowedPackage, requiredPrivilegesList);
+                }
+                return true;
+            };
+
+            err = Interop.Package.PackageInfoForeachResAllowedPackage(packageInfoHandle, allowedPackageCallback, IntPtr.Zero);
+            if (err != Interop.PackageManager.ErrorCode.None)
+            {
+                Log.Warn(LogTag, string.Format("Failed to get allowed packages info. err = {0}", err));
+            }
+            return allowedPackagesAndPrivileges;
+        }
     }
 }
index 47061c9..36d65f6 100755 (executable)
@@ -25,6 +25,7 @@ namespace Tizen.Applications
     public class PackageFilter
     {
         private IDictionary<string, bool> _filter;
+        private IDictionary<string, string> _stringFilter;
 
         /// <summary>
         /// The default constructor with an empty filter list. All the installed applications will satisfy this filter unless updated with more specific filters.
@@ -33,6 +34,7 @@ namespace Tizen.Applications
         public PackageFilter()
         {
             _filter = new Dictionary<string, bool>();
+            _stringFilter = new Dictionary<string, string>();
         }
 
         /// <summary>
@@ -46,6 +48,29 @@ namespace Tizen.Applications
         }
 
         /// <summary>
+        /// The constructor with specific filters. Using this will filter out the installed packages which do not meet the filter criteria.
+        /// </summary>
+        /// <remarks>The dictionary contains filter keys as the keys, and filter values as the value.</remarks>
+        /// <param name="stringFilter">Package filters using string values.</param>
+        /// <since_tizen> 9 </since_tizen>
+        public PackageFilter(IDictionary<string, string> stringFilter)
+        {
+            _stringFilter = stringFilter;
+        }
+
+        /// <summary>
+        /// The constructor with specific filters. Using this will filter out the installed packages which do not meet the filter criteria.
+        /// </summary>
+        /// <param name="filter">Package filters.</param>
+        /// <param name="stringFilter">Package filters using string values.</param>
+        /// <since_tizen> 9 </since_tizen>
+        public PackageFilter(IDictionary<string, bool> filter, IDictionary<string, string> stringFilter)
+        {
+            _filter = filter;
+            _stringFilter = stringFilter;
+        }
+
+        /// <summary>
         /// Filters to be used in the GetPackages method.
         /// </summary>
         /// <since_tizen> 3 </since_tizen>
@@ -58,6 +83,18 @@ namespace Tizen.Applications
         }
 
         /// <summary>
+        /// String filters to be used in the GetPackages method.
+        /// </summary>
+        /// <since_tizen> 9 </since_tizen>
+        public IDictionary<string, string> StringFilters
+        {
+            get
+            {
+                return _stringFilter;
+            }
+        }
+
+        /// <summary>
         /// This class contains possible keys for the filter to be used in the GetPackages method.
         /// </summary>
         /// <since_tizen> 3 </since_tizen>
@@ -88,6 +125,12 @@ namespace Tizen.Applications
             /// </summary>
             /// <since_tizen> 3 </since_tizen>
             public const string Preload = "PMINFO_PKGINFO_PROP_PACKAGE_PRELOAD";
+
+            /// <summary>
+            /// Key of the string property for filtering the resource type of the package.
+            /// </summary>
+            /// <since_tizen> 9 </since_tizen>
+            public const string ResourceType = "PMINFO_PKGINFO_PROP_PACKAGE_RES_TYPE";
         }
     }
 }
index 6625136..5e24330 100755 (executable)
@@ -393,6 +393,20 @@ namespace Tizen.Applications
                         break;
                     }
                 }
+
+            }
+
+            if (filter != null && filter.StringFilters != null)
+            {
+                foreach (KeyValuePair<string, string> entry in filter?.StringFilters)
+                {
+                    err = Interop.PackageManager.PackageManagerFilterAdd(filterHandle, entry.Key, entry.Value);
+                    if (err != Interop.PackageManager.ErrorCode.None)
+                    {
+                        Log.Warn(LogTag, string.Format("Failed to configure package filter. err = {0}", err));
+                        break;
+                    }
+                }
             }
 
             if (err == Interop.PackageManager.ErrorCode.None)