Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.WebView / Tizen.WebView / JavaScriptMessage.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 using System.Text;
20
21 namespace Tizen.WebView
22 {
23     public delegate void JavaScriptMessageHandler(JavaScriptMessage message);
24
25     /// <summary>
26     /// A Script message contains information that sent from JavaScript runtime.
27     /// </summary>
28     public class JavaScriptMessage
29     {
30         private string _name;
31         private IntPtr _body;
32
33         internal JavaScriptMessage(Interop.ChromiumEwk.ScriptMessage message)
34         {
35             _name = Marshal.PtrToStringAnsi(message.name);
36             _body = message.body;
37         }
38
39         /// <summary>
40         /// Obect name in JavaScript.
41         /// </summary>
42         public string Name
43         {
44             get
45             {
46                 return _name;
47             }
48         }
49
50         /// <summary>
51         /// Gets the value of body as integer type.
52         /// </summary>
53         /// <returns>The value of body as integer type</returns>
54         public int GetBodyAsInteger()
55         {
56             if (_body == IntPtr.Zero)
57             {
58                 return 0;
59             }
60             return Marshal.ReadInt32(_body, 0);
61         }
62
63         /// <summary>
64         /// Gets the value of body as double type.
65         /// </summary>
66         /// <returns>The value of body as double type</returns>
67         public double GetBodyAsDouble()
68         {
69             if (_body == IntPtr.Zero)
70             {
71                 return 0d;
72             }
73             double[] ret = new double[1] ;
74             Marshal.Copy(_body, ret, 0, 1);
75             return ret[0];
76         }
77
78         /// <summary>
79         /// Gets the value of body as boolean type.
80         /// </summary>
81         /// <returns>The value of body as boolean type</returns>
82         public bool GetBodyAsBoolean()
83         {
84             if (_body == IntPtr.Zero)
85             {
86                 return false;
87             }
88             return Marshal.ReadByte(_body) != (byte)0;
89         }
90
91         /// <summary>
92         /// Gets the value of body as string type.
93         /// </summary>
94         /// <returns>The value of body as string type</returns>
95         public string GetBodyAsString()
96         {
97             if (_body == IntPtr.Zero)
98             {
99                 return string.Empty;
100             }
101             return Marshal.PtrToStringAnsi(_body);
102         }
103     }
104 }