Introduce Tizen.WebView
[platform/core/csapi/tizenfx.git] / src / Tizen.WebView / Tizen.WebView / JavaScriptMessage.cs
1 /*\r
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved\r
3  *\r
4  * Licensed under the Apache License, Version 2.0 (the License);\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  * http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an AS IS BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 \r
17 using System;\r
18 using System.Runtime.InteropServices;\r
19 using System.Text;\r
20 \r
21 namespace Tizen.WebView\r
22 {\r
23     public delegate void JavaScriptMessageHandler(JavaScriptMessage message);\r
24 \r
25     /// <summary>\r
26     /// A Script message contains information that sent from JavaScript runtime.\r
27     /// </summary>\r
28     public class JavaScriptMessage\r
29     {\r
30         private string _name;\r
31         private IntPtr _body;\r
32 \r
33         internal JavaScriptMessage(Interop.ChromiumEwk.ScriptMessage message)\r
34         {\r
35             _name = Marshal.PtrToStringAnsi(message.name);\r
36             _body = message.body;\r
37         }\r
38 \r
39         /// <summary>\r
40         /// Obect name in JavaScript.\r
41         /// </summary>\r
42         public string Name\r
43         {\r
44             get\r
45             {\r
46                 return _name;\r
47             }\r
48         }\r
49 \r
50         /// <summary>\r
51         /// Gets the value of body as integer type.\r
52         /// </summary>\r
53         /// <returns>The value of body as integer type</returns>\r
54         public int GetBodyAsInteger()\r
55         {\r
56             if (_body == IntPtr.Zero)\r
57             {\r
58                 return 0;\r
59             }\r
60             return Marshal.ReadInt32(_body, 0);\r
61         }\r
62 \r
63         /// <summary>\r
64         /// Gets the value of body as double type.\r
65         /// </summary>\r
66         /// <returns>The value of body as double type</returns>\r
67         public double GetBodyAsDouble()\r
68         {\r
69             if (_body == IntPtr.Zero)\r
70             {\r
71                 return 0d;\r
72             }\r
73             double[] ret = new double[1] ;\r
74             Marshal.Copy(_body, ret, 0, 1);\r
75             return ret[0];\r
76         }\r
77 \r
78         /// <summary>\r
79         /// Gets the value of body as boolean type.\r
80         /// </summary>\r
81         /// <returns>The value of body as boolean type</returns>\r
82         public bool GetBodyAsBoolean()\r
83         {\r
84             if (_body == IntPtr.Zero)\r
85             {\r
86                 return false;\r
87             }\r
88             return Marshal.ReadByte(_body) != (byte)0;\r
89         }\r
90 \r
91         /// <summary>\r
92         /// Gets the value of body as string type.\r
93         /// </summary>\r
94         /// <returns>The value of body as string type</returns>\r
95         public string GetBodyAsString()\r
96         {\r
97             if (_body == IntPtr.Zero)\r
98             {\r
99                 return string.Empty;\r
100             }\r
101             return Marshal.PtrToStringAnsi(_body);\r
102         }\r
103     }\r
104 }\r