Fix description (#3619)
[platform/core/csapi/tizenfx.git] / src / Tizen.Applications.Cion / Tizen.Applications / PayloadAsyncResult.cs
1 /*
2  * Copyright (c) 2021 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
19 namespace Tizen.Applications
20 {
21     /// <summary>
22     /// A class to represent result of payload.
23     /// </summary>
24     /// <since_tizen> 9 </since_tizen>
25     public class PayloadAsyncResult : IDisposable
26     {
27         private PayloadAsyncResult(PayloadAsyncResultCode result, PeerInfo peer, string payloadId)
28         {
29             Result = result;
30             PeerInfo = peer;
31             PayloadId = payloadId;
32         }
33
34         internal static PayloadAsyncResult CreateFromHandle(IntPtr handle)
35         {
36             Interop.Cion.ErrorCode ret = Interop.CionPayloadAsyncResult.CionPayloadAsyncResultGetResult(handle, out int code);
37             if (ret != Interop.Cion.ErrorCode.None)
38             {
39                 throw CionErrorFactory.GetException(ret, "Fail to get result code from the AsyncResult");
40             }
41
42             ret = Interop.CionPayloadAsyncResult.CionPayloadAsyncResultGetPayloadID(handle, out string payloadId);
43             if (ret != Interop.Cion.ErrorCode.None)
44             {
45                 throw CionErrorFactory.GetException(ret, "Fail to get payload id from the AsyncResult");
46             }
47
48             ret = Interop.CionPayloadAsyncResult.CionPayloadAsyncResultGetPeerInfo(handle, out IntPtr peer);
49             if (ret != Interop.Cion.ErrorCode.None)
50             {
51                 throw CionErrorFactory.GetException(ret, "Fail to get peerinfo from the AsyncResult");
52             }
53             ret = Interop.CionPeerInfo.CionPeerInfoClone(peer, out PeerInfoSafeHandle clone);
54             if (ret != Interop.Cion.ErrorCode.None)
55             {
56                 throw CionErrorFactory.GetException(ret, "Failed to clone peer info.");
57             }
58
59             return new PayloadAsyncResult((PayloadAsyncResultCode)code, new PeerInfo(clone), payloadId);
60         }
61
62         /// <summary>
63         /// Gets the result of payload.
64         /// </summary>
65         /// <since_tizen> 9 </since_tizen>
66         public PayloadAsyncResultCode Result { get; }
67
68         /// <summary>
69         /// Gets the peer info of payload.
70         /// </summary>
71         /// <since_tizen> 9 </since_tizen>
72         public PeerInfo PeerInfo { get; }
73
74         /// <summary>
75         /// Gets the payload id.
76         /// </summary>
77         /// <since_tizen> 9 </since_tizen>
78         public string PayloadId { get; }
79
80         #region IDisposable Support
81         private bool disposedValue = false;
82
83         /// <summary>
84         /// Releases any unmanaged resources used by this object. Can also dispose any other disposable objects.
85         /// </summary>
86         /// <param name="disposing">If true, disposes any disposable objects. If false, does not dispose disposable objects.</param>
87         /// <since_tizen> 9 </since_tizen>
88         protected virtual void Dispose(bool disposing)
89         {
90             if (!disposedValue)
91             {
92                 if (disposing)
93                 {
94                     PeerInfo?.Dispose();
95                 }
96                 disposedValue = true;
97             }
98         }
99
100         /// <summary>
101         /// Releases all resources used by the PayloadAsyncResult class.
102         /// </summary>
103         /// <since_tizen> 9 </since_tizen>
104         public void Dispose()
105         {
106             Dispose(true);
107             GC.SuppressFinalize(this);
108         }
109         #endregion
110     }
111 }