Initialize Tizen 2.3
[framework/web/wrt-plugins-common.git] / src_mobile / 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 "dispatch_event_support.h"
28
29 namespace DispatchEventSupport {
30
31 static std::string getExceptionString(JSContextRef context, JSValueRef exception)
32 {
33     if (exception)
34     {
35         JSStringRef jsString = JSValueToStringCopy(context, exception, NULL);
36         size_t bufSize = JSStringGetMaximumUTF8CStringSize(jsString);
37
38         char* buf = new char[bufSize];
39         JSStringGetUTF8CString(jsString, buf, bufSize);
40         JSStringRelease(jsString);
41
42         std::string ret = buf;
43         delete[] buf;
44
45         return ret;
46     }
47     else
48     {
49         return "NULL";
50     }
51 }
52
53 void dispatchAppServiceEvent(JSContextRef context, const float scale, const std::string bundle)
54 {
55     LogDebug("DispatchTizenServiceEvent(" << context << ", " << scale << ", " << bundle << ")");
56
57     if (context != NULL)
58     {
59         std::stringstream script;
60
61         script << "var __event = document.createEvent(\"CustomEvent\");\n"
62                << "__event.initCustomEvent(\"appservice\", true, true);\n"
63                << "__event.scale = " << scale << ";\n"
64                << "__event.__bundle = \"" << bundle << "\";\n"
65                << "document.dispatchEvent(__event);\n"
66                << "\n"
67                << "for (var i=0; i < window.frames.length; i++)\n"
68                << "{ window.frames[i].document.dispatchEvent(__event); }";
69
70         // just for debugging
71         // LogDebug("script :\n" << script.str());
72
73         JSValueRef exception = NULL, ret = NULL;
74         JSStringRef jsScript = JSStringCreateWithUTF8CString(script.str().c_str());
75
76         ret = JSEvaluateScript(context, jsScript, NULL, NULL, 1, &exception);
77
78         JSStringRelease(jsScript);
79
80         LogDebug("JSEvaluateScript() - ret:" << ret << ", exception:" << exception << " (" << getExceptionString(context, exception) << ")");
81     }
82 }
83
84 void dispatchSoftKeyboardChangeEvent(JSContextRef context, const std::string state, const int width, const int height)
85 {
86     LogDebug("dispatchSoftKeyboardChangeEvent(" << context << ", " << state << ", " << width << ", " << height << ")");
87
88     if (context != NULL)
89     {
90         std::stringstream script;
91
92         script << "var __event = document.createEvent(\"CustomEvent\");\n"
93                << "__event.initCustomEvent(\"softkeyboardchange\", true, true);\n"
94                << "__event.state = \"" << state << "\";\n"
95                << "__event.width = " << width << ";\n"
96                << "__event.height = " << height << ";\n"
97                << "document.dispatchEvent(__event);\n"
98                << "\n"
99                << "for (var i=0; i < window.frames.length; i++)\n"
100                << "{ window.frames[i].document.dispatchEvent(__event); }";
101
102         // just for debugging
103         // LogDebug("script :\n" << script.str());
104
105         JSValueRef exception = NULL, ret = NULL;
106         JSStringRef jsScript = JSStringCreateWithUTF8CString(script.str().c_str());
107
108         ret = JSEvaluateScript(context, jsScript, NULL, NULL, 1, &exception);
109
110         JSStringRelease(jsScript);
111
112         LogDebug("JSEvaluateScript() - ret:" << ret << ", exception:" << exception << " (" << getExceptionString(context, exception) << ")");
113     }
114 }
115
116 void dispatchStorageEvent(JSContextRef context,
117                           const DPL::Optional<std::string>& key,
118                           const DPL::Optional<std::string>& oldValue,
119                           const DPL::Optional<std::string>& newValue,
120                           const std::string& url)
121 {
122     LogDebug("dispatchStorageEvent(" <<
123              context << ", " <<
124              (key.IsNull() ? "null" : *key) << ", " <<
125              (oldValue.IsNull() ? "null" : *oldValue) << ", " <<
126              (newValue.IsNull() ? "null" : *newValue) << ", " <<
127              url << ")");
128
129     if (context != NULL)
130     {
131         std::stringstream script;
132
133         auto toJSValue =
134             [](const DPL::Optional<std::string>& value) -> std::string
135             {
136                 return (value.IsNull() ? "null" : "\"" + *value + "\"");
137             };
138
139         script << "var __event = document.createEvent(\"CustomEvent\");"
140                << "__event.initCustomEvent(\"storage\", true, true);"
141                << "__event.key = " << toJSValue(key) << ";"
142                << "__event.oldValue = " << toJSValue(oldValue) << ";"
143                << "__event.newValue = " << toJSValue(newValue) << ";"
144                << "__event.url = \"" << url << "\";"
145                << "__event.storageArea = widget.preferences;"
146                << "document.dispatchEvent(__event);"
147                << "for (var i=0; i < window.frames.length; i++)"
148                << "{ window.frames[i].document.dispatchEvent(__event); }";
149
150
151         // just for debugging
152         // LogDebug("script :\n" << script.str());
153
154         JSValueRef exception = NULL, ret = NULL;
155         JSStringRef jsScript = JSStringCreateWithUTF8CString(script.str().c_str());
156
157         ret = JSEvaluateScript(context, jsScript, NULL, NULL, 1, &exception);
158
159         JSStringRelease(jsScript);
160
161         LogDebug("JSEvaluateScript() - ret:" << ret << ", exception:" << exception << " (" << getExceptionString(context, exception) << ")");
162     }
163 }
164
165 void dispatchHwKeyEvent(Evas_Object* ewkView, const std::string key)
166 {
167     LogDebug("dispatchHwKeyEvent(" << ewkView << ", " << key << ")");
168
169     if (ewkView != NULL && !key.empty())
170     {
171         std::stringstream script;
172
173         script << "var __event = document.createEvent(\"CustomEvent\");\n"
174                << "__event.initCustomEvent(\"tizenhwkey\", true, true);\n"
175                << "__event.keyName = \"" << key << "\";\n"
176                << "document.dispatchEvent(__event);\n"
177                << "\n"
178                << "for (var i=0; i < window.frames.length; i++)\n"
179                << "{ window.frames[i].document.dispatchEvent(__event); }";
180
181         // just for debugging
182         // LogDebug("script :\n" << script.str());
183
184         if (ewk_view_script_execute(ewkView, script.str().c_str(), NULL, NULL) != EINA_TRUE)
185         {
186             LogWarning("ewk_view_script_execute returned FALSE!");
187         }
188     }
189 }
190
191 }
192 #endif //_DISPATCH_EVENT_SUPPORT_CPP_