[Packagemanager] Fix lifetime of callback delegate (#503)
[platform/core/csapi/tizenfx.git] / src / Tizen.Applications.PackageManager / Tizen.Applications / Package.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.Collections.Generic;
19 using System.Runtime.InteropServices;
20 using System.Threading.Tasks;
21
22 namespace Tizen.Applications
23 {
24     /// <summary>
25     /// This class provides the methods and properties to get information about the packages.
26     /// </summary>
27     /// <since_tizen> 3 </since_tizen>
28     public class Package
29     {
30         private const string LogTag = "Tizen.Applications";
31
32         private string _id = string.Empty;
33         private string _label = string.Empty;
34         private string _iconPath = string.Empty;
35         private string _version = string.Empty;
36         private PackageType _type;
37         private Interop.PackageManager.StorageType _installedStorageType;
38         private string _rootPath = string.Empty;
39         private string _expansionPackageName = string.Empty;
40         private bool _isSystemPackage;
41         private bool _isRemovable;
42         private bool _isPreloaded;
43         private bool _isAccessible;
44         private IReadOnlyDictionary<CertificateType, PackageCertificate> _certificates;
45         private List<string> _privileges;
46         private int _installedTime;
47
48         private Dictionary<IntPtr, Interop.PackageManager.PackageManagerSizeInfoCallback> _packageManagerSizeInfoCallbackDict = new Dictionary<IntPtr, Interop.PackageManager.PackageManagerSizeInfoCallback>();
49         private int _callbackId = 0;
50
51         private Package(string pkgId)
52         {
53             _id = pkgId;
54         }
55
56         /// <summary>
57         /// The package ID.
58         /// </summary>
59         /// <since_tizen> 3 </since_tizen>
60         public string Id { get { return _id; } }
61
62         /// <summary>
63         /// Label of the package.
64         /// </summary>
65         /// <since_tizen> 3 </since_tizen>
66         public string Label { get { return _label; } }
67
68         /// <summary>
69         /// Absolute path to the icon image.
70         /// </summary>
71         /// <since_tizen> 3 </since_tizen>
72         public string IconPath { get { return _iconPath; } }
73
74         /// <summary>
75         /// Version of the package.
76         /// </summary>
77         /// <since_tizen> 3 </since_tizen>
78         public string Version { get { return _version; } }
79
80         /// <summary>
81         /// Type of the package.
82         /// </summary>
83         /// <since_tizen> 3 </since_tizen>
84         public PackageType PackageType { get { return _type; } }
85
86         /// <summary>
87         /// Installed storage type for the package.
88         /// </summary>
89         /// <since_tizen> 3 </since_tizen>
90         public StorageType InstalledStorageType { get { return (StorageType)_installedStorageType; } }
91
92         /// <summary>
93         /// Root path for the package.
94         /// </summary>
95         /// <since_tizen> 3 </since_tizen>
96         public string RootPath { get { return _rootPath; } }
97
98         /// <summary>
99         /// Expansion package name for the package.
100         /// </summary>
101         /// <privilege>http://tizen.org/privilege/packagemanager.admin</privilege>
102         /// <privlevel>platform</privlevel>
103         /// <since_tizen> 3 </since_tizen>
104         public string TizenExpansionPackageName { get { return _expansionPackageName; } }
105
106         /// <summary>
107         /// Checks whether the package is a system package.
108         /// </summary>
109         /// <since_tizen> 3 </since_tizen>
110         public bool IsSystemPackage { get { return _isSystemPackage; } }
111
112         /// <summary>
113         /// Checks whether the package is removable.
114         /// </summary>
115         /// <since_tizen> 3 </since_tizen>
116         public bool IsRemovable { get { return _isRemovable; } }
117
118         /// <summary>
119         /// Checks whether the package is preloaded.
120         /// </summary>
121         /// <since_tizen> 3 </since_tizen>
122         public bool IsPreloaded { get { return _isPreloaded; } }
123
124         /// <summary>
125         /// Checks whether the current package is accessible.
126         /// </summary>
127         /// <since_tizen> 3 </since_tizen>
128         public bool IsAccessible { get { return _isAccessible; } }
129
130         /// <summary>
131         /// Certificate information for the package.
132         /// </summary>
133         /// <since_tizen> 3 </since_tizen>
134         public IReadOnlyDictionary<CertificateType, PackageCertificate> Certificates { get { return _certificates; } }
135
136         /// <summary>
137         /// Requested privilege for the package.
138         /// </summary>
139         /// <since_tizen> 3 </since_tizen>
140         public IEnumerable<string> Privileges { get { return _privileges; } }
141
142         /// <summary>
143         /// Installed time of the package.
144         /// </summary>
145         /// <since_tizen> 3 </since_tizen>
146         public int InstalledTime { get { return _installedTime; } }
147
148         /// <summary>
149         /// Retrieves all the application IDs of this package.
150         /// </summary>
151         /// <returns>Returns a dictionary containing all the application information for a given application type.</returns>
152         /// <since_tizen> 3 </since_tizen>
153         public IEnumerable<ApplicationInfo> GetApplications()
154         {
155             return GetApplications(ApplicationType.All);
156         }
157
158         /// <summary>
159         /// Retrieves all the application IDs of this package.
160         /// </summary>
161         /// <param name="type">Optional: AppType enumeration value.</param>
162         /// <returns>Returns a dictionary containing all the application information for a given application type.</returns>
163         /// <since_tizen> 3 </since_tizen>
164         public IEnumerable<ApplicationInfo> GetApplications(ApplicationType type)
165         {
166             List<ApplicationInfo> appInfoList = new List<ApplicationInfo>();
167             Interop.Package.PackageInfoAppInfoCallback cb = (Interop.Package.AppType appType, string appId, IntPtr userData) =>
168             {
169                 appInfoList.Add(new ApplicationInfo(appId));
170                 return true;
171             };
172
173             IntPtr packageInfoHandle;
174             Interop.PackageManager.ErrorCode err = Interop.Package.PackageInfoCreate(Id, out packageInfoHandle);
175             if (err != Interop.PackageManager.ErrorCode.None)
176             {
177                 Log.Warn(LogTag, string.Format("Failed to create native handle for package info of {0}. err = {1}", Id, err));
178             }
179
180             err = Interop.Package.PackageInfoForeachAppInfo(packageInfoHandle, (Interop.Package.AppType)type, cb, IntPtr.Zero);
181             if (err != Interop.PackageManager.ErrorCode.None)
182             {
183                 Log.Warn(LogTag, string.Format("Failed to application info of {0}. err = {1}", Id, err));
184             }
185
186             err = Interop.Package.PackageInfoDestroy(packageInfoHandle);
187             if (err != Interop.PackageManager.ErrorCode.None)
188             {
189                 Log.Warn(LogTag, string.Format("Failed to destroy native handle for package info of {0}. err = {1}", Id, err));
190             }
191             return appInfoList;
192         }
193
194         /// <summary>
195         /// Gets the package size information.
196         /// </summary>
197         /// <returns>Package size information.</returns>
198         /// <privilege>http://tizen.org/privilege/packagemanager.info</privilege>
199         /// <since_tizen> 3 </since_tizen>
200         public async Task<PackageSizeInformation> GetSizeInformationAsync()
201         {
202             TaskCompletionSource<PackageSizeInformation> tcs = new TaskCompletionSource<PackageSizeInformation>();
203             Interop.PackageManager.PackageManagerSizeInfoCallback sizeInfoCb = (pkgId, sizeInfoHandle, userData) =>
204             {
205                 if (sizeInfoHandle != IntPtr.Zero && Id == pkgId)
206                 {
207                     var pkgSizeInfo = PackageSizeInformation.GetPackageSizeInformation(sizeInfoHandle);
208                     tcs.TrySetResult(pkgSizeInfo);
209                 }
210
211                 lock (_packageManagerSizeInfoCallbackDict)
212                 {
213                     _packageManagerSizeInfoCallbackDict.Remove(userData);
214                 }
215             };
216
217             IntPtr callbackId;
218             lock (_packageManagerSizeInfoCallbackDict)
219             {
220                 callbackId = (IntPtr)_callbackId++;
221                 _packageManagerSizeInfoCallbackDict[callbackId] = sizeInfoCb;
222             }
223
224             Interop.PackageManager.ErrorCode err = Interop.PackageManager.PackageManagerGetSizeInfo(Id, sizeInfoCb, callbackId);
225             if (err != Interop.PackageManager.ErrorCode.None)
226             {
227                 tcs.TrySetException(PackageManagerErrorFactory.GetException(err, "Failed to get total package size info of " + Id));
228             }
229             return await tcs.Task.ConfigureAwait(false);
230         }
231
232         /// <summary>
233         /// Compares the certificate information with the given package ID.
234         /// </summary>
235         /// <param name="packageId">ID of the package.</param>
236         /// <returns>Certificate comparison result.</returns>
237         /// <exception cref="ArgumentException">Thrown when a failed input package ID is invalid.</exception>
238         /// <exception cref="System.IO.IOException">Thrown when the method failed due to an internal I/O error.</exception>
239         /// <since_tizen> 3 </since_tizen>
240         public CertCompareResultType CompareCertInfo(string packageId)
241         {
242             Interop.PackageManager.CertCompareResultType compareResult;
243             Interop.PackageManager.ErrorCode err = Interop.PackageManager.PackageManagerCompareCertInfo(Id, packageId, out compareResult);
244             if (err != Interop.PackageManager.ErrorCode.None)
245             {
246                 throw PackageManagerErrorFactory.GetException(err, "Failed to compare package cert info");
247             }
248
249             return (CertCompareResultType)compareResult;
250         }
251
252         // This method assumes that given arguments are already validated and have valid values.
253         internal static Package CreatePackage(IntPtr handle, string pkgId)
254         {
255             Package package = new Package(pkgId);
256
257             var err = Interop.PackageManager.ErrorCode.None;
258             err = Interop.Package.PackageInfoGetLabel(handle, out package._label);
259             if (err != Interop.PackageManager.ErrorCode.None)
260             {
261                 Log.Warn(LogTag, "Failed to get package label of " + pkgId);
262             }
263             err = Interop.Package.PackageInfoGetIconPath(handle, out package._iconPath);
264             if (err != Interop.PackageManager.ErrorCode.None)
265             {
266                 Log.Warn(LogTag, "Failed to get package icon path of " + pkgId);
267             }
268             err = Interop.Package.PackageInfoGetVersion(handle, out package._version);
269             if (err != Interop.PackageManager.ErrorCode.None)
270             {
271                 Log.Warn(LogTag, "Failed to get package version of " + pkgId);
272             }
273
274             string type;
275             Interop.Package.PackageInfoGetType(handle, out type);
276             if (Enum.TryParse(type, true, out package._type) == false)
277             {
278                 Log.Warn(LogTag, "Failed to get package type of " + pkgId);
279             }
280             err = Interop.Package.PackageInfoGetRootPath(handle, out package._rootPath);
281             if (err != Interop.PackageManager.ErrorCode.None)
282             {
283                 Log.Warn(LogTag, "Failed to get package root directory of " + pkgId);
284             }
285             err = Interop.Package.PackageInfoGetTepName(handle, out package._expansionPackageName);
286             if (err != Interop.PackageManager.ErrorCode.None)
287             {
288                 Log.Warn(LogTag, "Failed to get expansion package name of " + pkgId);
289                 package._expansionPackageName = string.Empty;
290             }
291
292             err = Interop.Package.PackageInfoGetInstalledStorage(handle, out package._installedStorageType);
293             if (err != Interop.PackageManager.ErrorCode.None)
294             {
295                 Log.Warn(LogTag, "Failed to get installed storage type of " + pkgId);
296             }
297             Interop.Package.PackageInfoIsSystemPackage(handle, out package._isSystemPackage);
298             if (err != Interop.PackageManager.ErrorCode.None)
299             {
300                 Log.Warn(LogTag, "Failed to get whether package " + pkgId + " is system package or not");
301             }
302             Interop.Package.PackageInfoIsRemovablePackage(handle, out package._isRemovable);
303             if (err != Interop.PackageManager.ErrorCode.None)
304             {
305                 Log.Warn(LogTag, "Failed to get whether package " + pkgId + " is removable or not");
306             }
307             Interop.Package.PackageInfoIsPreloadPackage(handle, out package._isPreloaded);
308             if (err != Interop.PackageManager.ErrorCode.None)
309             {
310                 Log.Warn(LogTag, "Failed to get whether package " + pkgId + " is preloaded or not");
311             }
312             Interop.Package.PackageInfoIsAccessible(handle, out package._isAccessible);
313             if (err != Interop.PackageManager.ErrorCode.None)
314             {
315                 Log.Warn(LogTag, "Failed to get whether package " + pkgId + " is accessible or not");
316             }
317             try
318             {
319                 Interop.Package.PackageInfoGetInstalledTime(handle, out package._installedTime);
320                 if (err != Interop.PackageManager.ErrorCode.None)
321                 {
322                     Log.Warn(LogTag, "Failed to get installed time of " + pkgId);
323                 }
324             }
325             catch (TypeLoadException)
326             {
327                 // To support in API vesion 3.0
328                 package._installedTime = 0;
329             }
330
331             package._certificates = PackageCertificate.GetPackageCertificates(handle);
332             package._privileges = GetPackagePrivilegeInformation(handle);
333             return package;
334         }
335
336         internal static Package GetPackage(string packageId)
337         {
338             IntPtr packageInfoHandle;
339             Interop.PackageManager.ErrorCode err = Interop.Package.PackageInfoCreate(packageId, out packageInfoHandle);
340             if (err != Interop.PackageManager.ErrorCode.None)
341             {
342                 throw PackageManagerErrorFactory.GetException(err, string.Format("Failed to create native handle for package info of {0}", packageId));
343             }
344
345             Package package = CreatePackage(packageInfoHandle, packageId);
346
347             err = Interop.Package.PackageInfoDestroy(packageInfoHandle);
348             if (err != Interop.PackageManager.ErrorCode.None)
349             {
350                 Log.Warn(LogTag, string.Format("Failed to destroy native handle for package info of {0}. err = {1}", packageId, err));
351             }
352             return package;
353         }
354
355         internal static Package GetPackage(IntPtr packageInfoHandle)
356         {
357             String packageId;
358             Interop.PackageManager.ErrorCode err = Interop.Package.PackageInfoGetPackage(packageInfoHandle, out packageId);
359             if (err != Interop.PackageManager.ErrorCode.None)
360             {
361                 throw PackageManagerErrorFactory.GetException(err, "Failed to get package id for given package handle.");
362             }
363             return CreatePackage(packageInfoHandle, packageId);
364         }
365
366         private static List<string> GetPackagePrivilegeInformation(IntPtr packageInfoHandle)
367         {
368             List<string> privileges = new List<string>();
369             Interop.Package.PackageInfoPrivilegeInfoCallback privilegeInfoCb = (privilege, userData) =>
370             {
371                 privileges.Add(privilege);
372                 return true;
373             };
374
375             Interop.PackageManager.ErrorCode err = Interop.Package.PackageInfoForeachPrivilegeInfo(packageInfoHandle, privilegeInfoCb, IntPtr.Zero);
376             if (err != Interop.PackageManager.ErrorCode.None)
377             {
378                 Log.Warn(LogTag, string.Format("Failed to get privilage info. err = {0}", err));
379             }
380             return privileges;
381         }
382     }
383 }