a8eca4c8f96e96a43cd0369def5547b3f335ca59
[platform/core/csapi/tizenfx.git] / src / Tizen.Applications.Common / Tizen.Applications.RPCPort / Port.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 using System.Collections.Generic;
19 using System.Linq;
20
21 namespace Tizen.Applications.RPCPort
22 {
23     /// <summary>
24     /// The class that proxy and stub can use to communicate with each other.
25     /// </summary>
26     /// <since_tizen> 5 </since_tizen>
27     public class Port
28     {
29         /// <summary>
30         /// Enumeration for RPC port types.
31         /// </summary>
32         public enum Type
33         {
34             /// <summary>
35             /// Main channel to communicate.
36             /// </summary>
37             Main,
38
39             /// <summary>
40             /// Sub channel for callbacks.
41             /// </summary>
42             Callback
43         }
44
45         internal IntPtr Handle { get; set; }
46         internal Port() { }
47
48         /// <summary>
49         /// Shares private files with other applications.
50         /// </summary>
51         /// <remarks>
52         /// If all added paths are under the caller application's data path which can be obtained, those will be shared to the target application.
53         /// Platform will grant a temporary permission to the target application for those files and revoke it when the target application is terminated or UnshareFile() is called.
54         /// Paths should be regular files. The target application can just read them.
55         /// Note that the target application doesn't have read permission of the directory that is obtained by caller's data path. You should open the file path with read only mode directly.
56         /// </remarks>
57         /// <param name="paths">The file paths to be shared.</param>
58         /// <exception cref="InvalidIOException">Thrown when an internal IO error occurrs.</exception>
59         /// <since_tizen> 8 </since_tizen>
60         public void ShareFile(IEnumerable<string> paths)
61         {
62             if (paths == null)
63                 throw new InvalidIOException();
64
65             string[] pathArray = paths.ToArray<string>();
66             Interop.LibRPCPort.ErrorCode err = Interop.LibRPCPort.Port.SetPrivateSharingArray(Handle, pathArray, (uint)pathArray.Length);
67             if (err != Interop.LibRPCPort.ErrorCode.None)
68                 throw new InvalidIOException();
69         }
70
71         /// <summary>
72         /// Shares the private file with other applications.
73         /// </summary>
74         /// <seealso cref="ShareFile(IEnumerable{string})"/>
75         /// <param name="path">The file path to be shared.</param>
76         /// <exception cref="InvalidIOException">Thrown when an internal IO error occurrs.</exception>
77         /// <since_tizen> 8 </since_tizen>
78         public void ShareFile(string path)
79         {
80             if (path == null)
81                 throw new InvalidIOException();
82
83             Interop.LibRPCPort.ErrorCode err = Interop.LibRPCPort.Port.SetPrivateSharing(Handle, path);
84             if (err != Interop.LibRPCPort.ErrorCode.None)
85                 throw new InvalidIOException();
86         }
87
88         /// <summary>
89         /// Unshares the private file.
90         /// </summary>
91         /// <exception cref="InvalidIOException">Thrown when an internal IO error occurrs.</exception>
92         /// <since_tizen> 8 </since_tizen>
93         public void UnshareFile()
94         {
95             Interop.LibRPCPort.ErrorCode err = Interop.LibRPCPort.Port.UnsetPrivateSharing(Handle);
96             if (err != Interop.LibRPCPort.ErrorCode.None)
97                 throw new InvalidIOException();
98         }
99     }
100 }