Update wrt-plugins-common_0.3.53
[framework/web/wrt-plugins-common.git] / src / js-overlay / js_overlay_functions.cpp
1 /*
2  * Copyright (c) 2011 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  * @file    javascript_functions.cpp
18  * @author  Grzegorz Krawczyk (g.krawczyk@samgsung.com)
19  * @version
20  * @brief
21  */
22
23 #include <dpl/log/log.h>
24 #include <dpl/scoped_array.h>
25 #include <js_overlay_functions.h>
26
27 namespace JSCFunctions {
28
29 std::string ConvertJSStringToStdString(JSStringRef value)
30 {
31     int nSize = JSStringGetLength(value) + 1;
32     DPL::ScopedArray<char> textStr(new char[nSize]);
33     JSStringGetUTF8CString(value, textStr.Get(), nSize);
34     std::string ret = textStr.Get();
35     return ret;
36 }
37
38 JSValueRef JavaScriptPrintProc(JSContextRef context,
39                                JSObjectRef /*object*/,
40                                JSObjectRef /*thisObject*/,
41                                size_t argumentCount,
42                                const JSValueRef arguments[],
43                                JSValueRef* exception)
44 {
45     if (argumentCount == 0 || !JSValueIsString(context, arguments[0])) {
46         LogError("Argument is not string");
47         return JSValueMakeUndefined(context);
48     }
49
50     JSStringRef textRef = JSValueToStringCopy(context, arguments[0], exception);
51     int nSize = JSStringGetLength(textRef) + 1;
52
53     DPL::ScopedArray<char> textStr(new char[nSize]);
54
55     JSStringGetUTF8CString(textRef, textStr.Get(), nSize);
56     LogDebug("\033[00;35m[jsPrint] " << textStr.Get());
57
58     JSStringRelease(textRef);
59     return JSValueMakeBoolean(context, true);
60 }
61
62 JSValueRef JavaScriptHookProc(
63         JSContextRef context,
64         JSObjectRef /*object*/,
65         JSObjectRef /*thisObject*/,
66         size_t argumentCount,
67         const JSValueRef arguments[],
68         JSValueRef* exception)
69 {
70     bool inError = false;
71     if (argumentCount < 2 ||
72         argumentCount > 3 ||
73         !JSValueIsString(context, arguments[0]) ||
74         !JSValueIsString(context, arguments[1])) {
75
76         inError = true;
77     }
78
79     if (inError) {
80         LogError("*********************************************");
81         LogError("*********************************************");
82         LogError("Cannot print test Result");
83         LogError("*********************************************");
84         LogError("*********************************************");
85         return JSValueMakeUndefined(context);
86     }
87
88     std::string id, result, message;
89     JSStringRef idRef = JSValueToStringCopy(context, arguments[0], exception);
90     id = ConvertJSStringToStdString(idRef);
91     JSStringRelease(idRef);
92     JSStringRef idResult = JSValueToStringCopy(context,
93                                                arguments[1],
94                                                exception);
95     result = ConvertJSStringToStdString(idResult);
96     JSStringRelease(idResult);
97
98     if (argumentCount == 3 && !JSValueIsString(context, arguments[2])) {
99         JSStringRef idMessage = JSValueToStringCopy(context,
100                                                     arguments[0],
101                                                     exception);
102         message = ConvertJSStringToStdString(idMessage);
103         JSStringRelease(idMessage);
104     }
105
106     LogDebug("\033[00;35m***********************************************");
107     LogDebug("\033[00;35m***********************************************");
108     LogDebug("\033[00;35m TEST ID: " << id);
109     LogDebug("\033[00;35m RESULT: " << result);
110     LogDebug("\033[00;35m MESSAGE: " << message);
111     LogDebug("\033[00;35m***********************************************");
112     LogDebug("\033[00;35m***********************************************");
113
114     return JSValueMakeBoolean(context, true);
115 }
116 }