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