2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @file dispatch_event_support.cpp
19 * @author Tae-Jeong Lee (taejeong.lee@samsung.com)
24 #ifndef _DISPATCH_EVENT_SUPPORT_CPP_
25 #define _DISPATCH_EVENT_SUPPORT_CPP_
27 #include "dispatch_event_support.h"
29 namespace DispatchEventSupport {
31 static std::string getExceptionString(JSContextRef context, JSValueRef exception)
35 JSStringRef jsString = JSValueToStringCopy(context, exception, NULL);
36 size_t bufSize = JSStringGetMaximumUTF8CStringSize(jsString);
38 char* buf = new char[bufSize];
39 JSStringGetUTF8CString(jsString, buf, bufSize);
40 JSStringRelease(jsString);
42 std::string ret = buf;
53 void dispatchAppServiceEvent(JSContextRef context, const float scale, const std::string bundle)
55 LogDebug("DispatchTizenServiceEvent(" << context << ", " << scale << ", " << bundle << ")");
59 std::stringstream script;
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"
67 << "for (var i=0; i < window.frames.length; i++)\n"
68 << "{ window.frames[i].document.dispatchEvent(__event); }";
71 // LogDebug("script :\n" << script.str());
73 JSValueRef exception = NULL, ret = NULL;
74 JSStringRef jsScript = JSStringCreateWithUTF8CString(script.str().c_str());
76 ret = JSEvaluateScript(context, jsScript, NULL, NULL, 1, &exception);
78 JSStringRelease(jsScript);
80 LogDebug("JSEvaluateScript() - ret:" << ret << ", exception:" << exception << " (" << getExceptionString(context, exception) << ")");
84 void dispatchSoftKeyboardChangeEvent(JSContextRef context, const std::string state, const int width, const int height)
86 LogDebug("dispatchSoftKeyboardChangeEvent(" << context << ", " << state << ", " << width << ", " << height << ")");
90 std::stringstream script;
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"
99 << "for (var i=0; i < window.frames.length; i++)\n"
100 << "{ window.frames[i].document.dispatchEvent(__event); }";
102 // just for debugging
103 // LogDebug("script :\n" << script.str());
105 JSValueRef exception = NULL, ret = NULL;
106 JSStringRef jsScript = JSStringCreateWithUTF8CString(script.str().c_str());
108 ret = JSEvaluateScript(context, jsScript, NULL, NULL, 1, &exception);
110 JSStringRelease(jsScript);
112 LogDebug("JSEvaluateScript() - ret:" << ret << ", exception:" << exception << " (" << getExceptionString(context, exception) << ")");
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)
122 LogDebug("dispatchStorageEvent(" <<
124 (key.IsNull() ? "null" : *key) << ", " <<
125 (oldValue.IsNull() ? "null" : *oldValue) << ", " <<
126 (newValue.IsNull() ? "null" : *newValue) << ", " <<
131 std::stringstream script;
134 [](const DPL::Optional<std::string>& value) -> std::string
136 return (value.IsNull() ? "null" : "\"" + *value + "\"");
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); }";
151 // just for debugging
152 // LogDebug("script :\n" << script.str());
154 JSValueRef exception = NULL, ret = NULL;
155 JSStringRef jsScript = JSStringCreateWithUTF8CString(script.str().c_str());
157 ret = JSEvaluateScript(context, jsScript, NULL, NULL, 1, &exception);
159 JSStringRelease(jsScript);
161 LogDebug("JSEvaluateScript() - ret:" << ret << ", exception:" << exception << " (" << getExceptionString(context, exception) << ")");
165 void dispatchHwKeyEvent(Evas_Object* ewkView, const std::string key)
167 LogDebug("dispatchHwKeyEvent(" << ewkView << ", " << key << ")");
169 if (ewkView != NULL && !key.empty())
171 std::stringstream script;
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"
178 << "for (var i=0; i < window.frames.length; i++)\n"
179 << "{ window.frames[i].document.dispatchEvent(__event); }";
181 // just for debugging
182 // LogDebug("script :\n" << script.str());
184 if (ewk_view_script_execute(ewkView, script.str().c_str(), NULL, NULL) != EINA_TRUE)
186 LogWarning("ewk_view_script_execute returned FALSE!");
192 #endif //_DISPATCH_EVENT_SUPPORT_CPP_