Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.WebView / Tizen.WebView / SmartCallbackArgs.cs
1 /*
2  * Copyright (c) 2017 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.Runtime.InteropServices;
19
20 namespace Tizen.WebView
21 {
22     /// <summary>
23     /// Argument from the SmartCallback.
24     /// </summary>
25     public class SmartCallbackArgs : EventArgs
26     {
27         private IntPtr _arg;
28
29         internal SmartCallbackArgs(IntPtr arg)
30         {
31             _arg = arg;
32         }
33
34         /// <summary>
35         /// Gets argument as integer type.
36         /// </summary>
37         /// <returns>Argument as integer type</returns>
38         public int GetAsInteger()
39         {
40             if (_arg == IntPtr.Zero)
41             {
42                 return 0;
43             }
44             return Marshal.ReadInt32(_arg, 0);
45         }
46
47         /// <summary>
48         /// Gets argument as double type.
49         /// </summary>
50         /// <returns>Argument as double type</returns>
51         public double GetAsDouble()
52         {
53             if (_arg == IntPtr.Zero)
54             {
55                 return 0d;
56             }
57             double[] ret = new double[1];
58             Marshal.Copy(_arg, ret, 0, 1);
59             return ret[0];
60         }
61
62         /// <summary>
63         /// Gets argument as boolean type.
64         /// </summary>
65         /// <returns>Argument as boolean type</returns>
66         public bool GetAsBoolean()
67         {
68             if (_arg == IntPtr.Zero)
69             {
70                 return false;
71             }
72             return Marshal.ReadByte(_arg) != (byte)0;
73         }
74
75         /// <summary>
76         /// Gets argument as string type.
77         /// </summary>
78         /// <returns>Argument as string type</returns>
79         public string GetAsString()
80         {
81             if (_arg == IntPtr.Zero)
82             {
83                 return string.Empty;
84             }
85             return Marshal.PtrToStringAnsi(_arg);
86         }
87
88         internal static SmartCallbackArgs CreateFromSmartEvent(IntPtr data, IntPtr obj, IntPtr info)
89         {
90             return new SmartCallbackArgs(info);
91         }
92     }
93 }