14aa01a0987ccd3614660ebf38de9e2ebd6c7e41
[platform/framework/web/wrt-plugins-common.git] / src / dispatch-event / dispatch_event_support.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  *
18  * @file       dispatch_event_support.cpp
19  * @author     Tae-Jeong Lee (taejeong.lee@samsung.com)
20  * @version    0.1
21  * @brief
22  */
23
24 #ifndef _DISPATCH_EVENT_SUPPORT_CPP_
25 #define _DISPATCH_EVENT_SUPPORT_CPP_
26
27 #include <string>
28 #include <JavaScriptCore/JavaScript.h>
29 #include <dpl/log/log.h>
30
31 #include "dispatch_event_support.h"
32
33 namespace DispatchEventSupport {
34
35 static std::string getExceptionString(JSContextRef context, JSValueRef exception)
36 {
37     if (exception)
38     {
39         JSStringRef jsString = JSValueToStringCopy(context, exception, NULL);
40         size_t bufSize = JSStringGetMaximumUTF8CStringSize(jsString);
41
42         char* buf = new char[bufSize];
43         JSStringGetUTF8CString(jsString, buf, bufSize);
44         JSStringRelease(jsString);
45
46         std::string ret = buf;
47         delete[] buf;
48
49         return ret;
50     }
51     else
52     {
53         return "NULL";
54     }
55 }
56
57 void dispatchAppServiceEvent(JSContextRef context, const float scale, const std::string bundle)
58 {
59     LogDebug("DispatchTizenServiceEvent(" << context << ", " << scale << ", " << bundle << ")");
60
61     if (context != NULL)
62     {
63         std::stringstream script;
64
65         script << "{\n"
66                << "    var event = document.createEvent(\"CustomEvent\");\n"
67                << "    event.initCustomEvent(\"appservice\", true, true);\n"
68                << "    event.scale = " << scale << ";\n"
69                << "    event.__bundle = \"" << bundle << "\";\n"
70                << "    document.dispatchEvent(event);\n"
71                << "    \n"
72                << "    for (var i=0; i < window.frames.length; i++)\n"
73                << "    { window.frames[i].document.dispatchEvent(event); }\n"
74                << "}";
75
76         // just for debugging
77         // LogDebug("script :\n" << script.str());
78
79         JSValueRef exception = NULL, ret = NULL;
80         JSStringRef jsScript = JSStringCreateWithUTF8CString(script.str().c_str());
81
82         ret = JSEvaluateScript(context, jsScript, NULL, NULL, 1, &exception);
83
84         JSStringRelease(jsScript);
85
86         LogDebug("JSEvaluateScript() - ret:" << ret << ", exception:" << exception << " (" << getExceptionString(context, exception) << ")");
87     }
88 }
89
90 void dispatchSoftKeyboardChangeEvent(JSContextRef context, const std::string state, const int width, const int height)
91 {
92     LogDebug("dispatchSoftKeyboardChangeEvent(" << context << ", " << state << ", " << width << ", " << height << ")");
93
94     if (context != NULL)
95     {
96         std::stringstream script;
97
98         script << "{\n"
99                << "    var event = document.createEvent(\"CustomEvent\");\n"
100                << "    event.initCustomEvent(\"softkeyboardchange\", true, true);\n"
101                << "    event.state = \"" << state << "\";\n"
102                << "    event.width = " << width << ";\n"
103                << "    event.height = " << height << ";\n"
104                << "    document.dispatchEvent(event);\n"
105                << "    \n"
106                << "    for (var i=0; i < window.frames.length; i++)\n"
107                << "    { window.frames[i].document.dispatchEvent(event); }\n"
108                << "}";
109
110         // just for debugging
111         // LogDebug("script :\n" << script.str());
112
113         JSValueRef exception = NULL, ret = NULL;
114         JSStringRef jsScript = JSStringCreateWithUTF8CString(script.str().c_str());
115
116         ret = JSEvaluateScript(context, jsScript, NULL, NULL, 1, &exception);
117
118         JSStringRelease(jsScript);
119
120         LogDebug("JSEvaluateScript() - ret:" << ret << ", exception:" << exception << " (" << getExceptionString(context, exception) << ")");
121     }
122 }
123
124 void dispatchStorageEvent(JSContextRef context, const std::string key, const std::string oldValue, const std::string newValue, const std::string url)
125 {
126     LogDebug("dispatchStorageEvent(" << context << ", " << key << ", " << oldValue << ", " << newValue << ", " << url << ")");
127
128     if (context != NULL)
129     {
130         std::stringstream script;
131
132         script << "{\n"
133                << "    var event = document.createEvent(\"CustomEvent\");\n"
134                << "    event.initCustomEvent(\"storage\", true, true);\n"
135                << "    event.key = \"" << key << "\";\n"
136                << "    event.oldValue = \"" << oldValue << "\";\n"
137                << "    event.newValue = \"" << newValue << "\";\n"
138                << "    event.url = \"" << url << "\";\n"
139                << "    event.storageArea = widget.preferences;\n"
140                << "    document.dispatchEvent(event);\n"
141                << "    \n"
142                << "    for (var i=0; i < window.frames.length; i++)\n"
143                << "    { window.frames[i].document.dispatchEvent(event); }\n"
144                << "}";
145
146         // just for debugging
147         // LogDebug("script :\n" << script.str());
148
149         JSValueRef exception = NULL, ret = NULL;
150         JSStringRef jsScript = JSStringCreateWithUTF8CString(script.str().c_str());
151
152         ret = JSEvaluateScript(context, jsScript, NULL, NULL, 1, &exception);
153
154         JSStringRelease(jsScript);
155
156         LogDebug("JSEvaluateScript() - ret:" << ret << ", exception:" << exception << " (" << getExceptionString(context, exception) << ")");
157     }
158 }
159
160 }
161 #endif //_DISPATCH_EVENT_SUPPORT_CPP_