Initialize Tizen 2.3
[framework/web/wrt-plugins-common.git] / src_wearable / 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 std::string ConvertJSStringToStdString(JSStringRef value)
29 {
30     int nSize = JSStringGetLength(value) + 1;
31     DPL::ScopedArray<char> textStr(new char[nSize]);
32     JSStringGetUTF8CString(value, textStr.Get(), nSize);
33     std::string ret = textStr.Get();
34     return ret;
35 }
36
37 JSValueRef JavaScriptPrintProc(JSContextRef context,
38                                JSObjectRef /*object*/,
39                                JSObjectRef /*thisObject*/,
40                                size_t argumentCount,
41                                const JSValueRef arguments[],
42                                JSValueRef* exception)
43 {
44     if (argumentCount == 0 || !JSValueIsString(context, arguments[0])) {
45         LogError("Argument is not string");
46         return JSValueMakeUndefined(context);
47     }
48
49     JSStringRef textRef = JSValueToStringCopy(context, arguments[0], exception);
50     int nSize = JSStringGetLength(textRef) + 1;
51
52     DPL::ScopedArray<char> textStr(new char[nSize]);
53
54     JSStringGetUTF8CString(textRef, textStr.Get(), nSize);
55     LogDebug("\033[00;35m[jsPrint] " << textStr.Get());
56
57     JSStringRelease(textRef);
58     return JSValueMakeBoolean(context, true);
59 }
60
61 JSValueRef JavaScriptHookProc(
62     JSContextRef context,
63     JSObjectRef /*object*/,
64     JSObjectRef /*thisObject*/,
65     size_t argumentCount,
66     const JSValueRef arguments[],
67     JSValueRef* exception)
68 {
69     bool inError = false;
70     if (argumentCount < 2 ||
71         argumentCount > 3 ||
72         !JSValueIsString(context, arguments[0]) ||
73         !JSValueIsString(context, arguments[1]))
74     {
75         inError = true;
76     }
77
78     if (inError) {
79         LogError("*********************************************");
80         LogError("*********************************************");
81         LogError("Cannot print test Result");
82         LogError("*********************************************");
83         LogError("*********************************************");
84         return JSValueMakeUndefined(context);
85     }
86
87     std::string id, result, message;
88     JSStringRef idRef = JSValueToStringCopy(context, arguments[0], exception);
89     id = ConvertJSStringToStdString(idRef);
90     JSStringRelease(idRef);
91     JSStringRef idResult = JSValueToStringCopy(context,
92                                                arguments[1],
93                                                exception);
94     result = ConvertJSStringToStdString(idResult);
95     JSStringRelease(idResult);
96
97     if (argumentCount == 3 && !JSValueIsString(context, arguments[2])) {
98         JSStringRef idMessage = JSValueToStringCopy(context,
99                                                     arguments[0],
100                                                     exception);
101         message = ConvertJSStringToStdString(idMessage);
102         JSStringRelease(idMessage);
103     }
104
105     LogDebug("\033[00;35m***********************************************");
106     LogDebug("\033[00;35m***********************************************");
107     LogDebug("\033[00;35m TEST ID: " << id);
108     LogDebug("\033[00;35m RESULT: " << result);
109     LogDebug("\033[00;35m MESSAGE: " << message);
110     LogDebug("\033[00;35m***********************************************");
111     LogDebug("\033[00;35m***********************************************");
112
113     return JSValueMakeBoolean(context, true);
114 }
115 }