Fix visibility for method 'Dispose' (#606)
[platform/core/csapi/tizenfx.git] / src / Tizen.Applications.Common / Tizen.Applications.RPCPort / ProxyBase.cs
1 /*
2  * Copyright (c) 2018 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.RPCPort
20 {
21     /// <summary>
22     /// Abstract class for creating a proxy class for RPC.
23     /// </summary>
24     /// <since_tizen> 5 </since_tizen>
25     public abstract class ProxyBase : IDisposable
26     {
27         private Interop.LibRPCPort.Proxy.ConnectedEventCallback _connectedEventCallback;
28         private Interop.LibRPCPort.Proxy.DisconnectedEventCallback _disconnectedEventCallback;
29         private Interop.LibRPCPort.Proxy.RejectedEventCallback _rejectedEventCallback;
30         private Interop.LibRPCPort.Proxy.ReceivedEventCallback _receivedEventCallback;
31         private IntPtr _proxy;
32
33         /// <summary>
34         /// Gets Port object.
35         /// </summary>
36         /// <since_tizen> 5 </since_tizen>
37         protected Port Port { get; private set; }
38
39         /// <summary>
40         /// Gets Port object for asynchronous events.
41         /// </summary>
42         /// <since_tizen> 5 </since_tizen>
43         protected Port CallbackPort { get; private set; }
44
45         /// <summary>
46         /// Constructor for this class.
47         /// </summary>
48         /// <exception cref="InvalidIOException">Thrown when internal IO error occurs.</exception>
49         /// <since_tizen> 5 </since_tizen>
50         public ProxyBase()
51         {
52             if (Interop.LibRPCPort.Proxy.Create(out _proxy) != Interop.LibRPCPort.ErrorCode.None)
53                 throw new InvalidIOException();
54             _connectedEventCallback = new Interop.LibRPCPort.Proxy.ConnectedEventCallback(OnConnectedEvent);
55             _disconnectedEventCallback = new Interop.LibRPCPort.Proxy.DisconnectedEventCallback(OnDisconnectedEvent);
56             _rejectedEventCallback = new Interop.LibRPCPort.Proxy.RejectedEventCallback(OnRejectedEvent);
57             _receivedEventCallback = new Interop.LibRPCPort.Proxy.ReceivedEventCallback(OnReceivedEvent);
58             Interop.LibRPCPort.Proxy.AddConnectedEventCb(_proxy, _connectedEventCallback, IntPtr.Zero);
59             Interop.LibRPCPort.Proxy.AddDisconnectedEventCb(_proxy, _disconnectedEventCallback, IntPtr.Zero);
60             Interop.LibRPCPort.Proxy.AddRejectedEventCb(_proxy, _rejectedEventCallback, IntPtr.Zero);
61             Interop.LibRPCPort.Proxy.AddReceivedEventCb(_proxy, _receivedEventCallback, IntPtr.Zero);
62         }
63
64         /// <summary>
65         /// Connects to port.
66         /// </summary>
67         /// <param name="appid">The target stub app ID.</param>
68         /// <param name="port">The name of the RPC port.</param>
69         /// <exception cref="InvalidIDException">Thrown when not available app ID is used.</exception>
70         /// <exception cref="InvalidIOException">Thrown when an internal IO error occurs.</exception>
71         /// <exception cref="PermissionDeniedException">Thrown when the permission is denied.</exception>
72         /// <privilege>http://tizen.org/privilege/datasharing</privilege>
73         /// <privilege>http://tizen.org/privilege/appmanager.launch</privilege>
74         /// <since_tizen> 5 </since_tizen>
75         protected void Connect(string appid, string port)
76         {
77             var err = Interop.LibRPCPort.Proxy.Connect(_proxy, appid, port);
78             switch (err)
79             {
80                 case Interop.LibRPCPort.ErrorCode.InvalidParameter:
81                     throw new InvalidIDException();
82                 case Interop.LibRPCPort.ErrorCode.PermissionDenied:
83                     throw new PermissionDeniedException();
84                 case Interop.LibRPCPort.ErrorCode.IoError:
85                     throw new InvalidIOException();
86             }
87         }
88
89         /// <summary>
90         /// Gets a port.
91         /// </summary>
92         /// <param name="t">The type of port.</param>
93         /// <returns>Port object.</returns>
94         /// <exception cref="InvalidIOException">Thrown when an internal IO error occurs.</exception>
95         /// <since_tizen> 5 </since_tizen>
96         protected Port GetPort(Port.Type t)
97         {
98             var err = Interop.LibRPCPort.Proxy.GetPort(_proxy,
99                 (Interop.LibRPCPort.PortType)t, out IntPtr port);
100             switch (err)
101             {
102                 case Interop.LibRPCPort.ErrorCode.InvalidParameter:
103                 case Interop.LibRPCPort.ErrorCode.IoError:
104                     throw new InvalidIOException();
105             }
106
107             return new Port() { Handle = port };
108         }
109
110         /// <summary>
111         /// Abstract method for receiving connected event.
112         /// </summary>
113         /// <param name="endPoint">The target stub app ID.</param>
114         /// <param name="portName">The name of the RPC port.</param>
115         /// <param name="port">Port object for reading and writing.</param>
116         /// <since_tizen> 5 </since_tizen>
117         protected abstract void OnConnectedEvent(string endPoint, string portName, Port port);
118
119         /// <summary>
120         /// Abstract method for receiving disconnected event.
121         /// </summary>
122         /// <param name="endPoint">The target stub app ID.</param>
123         /// <param name="portName">The name of the port.</param>
124         /// <since_tizen> 5 </since_tizen>
125         protected abstract void OnDisconnectedEvent(string endPoint, string portName);
126
127         /// <summary>
128         /// Abstract method called when the proxy receives data from stub.
129         /// </summary>
130         /// <param name="endPoint">The target stub app ID.</param>
131         /// <param name="portName">The name of the RPC port.</param>
132         /// <since_tizen> 5 </since_tizen>
133         protected abstract void OnReceivedEvent(string endPoint, string portName);
134
135         /// <summary>
136         /// Abstract method for receiving rejected event.
137         /// </summary>
138         /// <param name="endPoint">The target stub app ID.</param>
139         /// <param name="portName">The name of the RPC port.</param>
140         /// <since_tizen> 5 </since_tizen>
141         protected abstract void OnRejectedEvent(string endPoint, string portName);
142
143         private void OnConnectedEvent(string endPoint, string portName, IntPtr port, IntPtr data)
144         {
145             Port = new Port() { Handle = port };
146             CallbackPort = GetPort(Port.Type.Callback);
147             OnConnectedEvent(endPoint, portName, Port);
148         }
149
150         private void OnDisconnectedEvent(string endPoint, string portName, IntPtr data)
151         {
152             Port = null;
153             CallbackPort = null;
154             OnDisconnectedEvent(endPoint, portName);
155         }
156
157         private void OnReceivedEvent(string endPoint, string portName, IntPtr data)
158         {
159             OnReceivedEvent(endPoint, portName);
160         }
161
162         private void OnRejectedEvent(string endPoint, string portName, IntPtr data)
163         {
164             OnRejectedEvent(endPoint, portName);
165         }
166
167         #region IDisposable Support
168         private bool disposedValue = false;
169
170         /// <summary>
171         /// Releases any unmanaged resources used by this object. Can also dispose any other disposable objects.
172         /// </summary>
173         /// <param name="disposing">If true, disposes any disposable objects. If false, does not dispose disposable objects.</param>
174         /// <since_tizen> 6 </since_tizen>
175         protected virtual void Dispose(bool disposing)
176         {
177             if (!disposedValue)
178             {
179                 if (disposing)
180                 {
181                 }
182                 if (_proxy != IntPtr.Zero)
183                     Interop.LibRPCPort.Proxy.Destroy(_proxy);
184                 _proxy = IntPtr.Zero;
185
186                 disposedValue = true;
187             }
188         }
189
190         /// <summary>
191         /// Finalizer of the class ProxyBase.
192         /// </summary>
193         ~ProxyBase()
194         {
195             Dispose(false);
196         }
197
198         /// <summary>
199         /// Release all resources used by the class ProxyBase.
200         /// </summary>
201         /// <since_tizen> 5 </since_tizen>
202         public void Dispose()
203         {
204             Dispose(true);
205             GC.SuppressFinalize(this);
206         }
207         #endregion
208     }
209 }