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_
28 #include <JavaScriptCore/JavaScript.h>
29 #include <dpl/log/log.h>
31 #include "dispatch_event_support.h"
33 namespace DispatchEventSupport {
35 static std::string getExceptionString(JSContextRef context, JSValueRef exception)
39 JSStringRef jsString = JSValueToStringCopy(context, exception, NULL);
40 size_t bufSize = JSStringGetMaximumUTF8CStringSize(jsString);
42 char* buf = new char[bufSize];
43 JSStringGetUTF8CString(jsString, buf, bufSize);
44 JSStringRelease(jsString);
46 std::string ret = buf;
57 void dispatchAppServiceEvent(JSContextRef context, const float scale, const std::string bundle)
59 LogDebug("DispatchTizenServiceEvent(" << context << ", " << scale << ", " << bundle << ")");
63 std::stringstream script;
65 script << "var __event = document.createEvent(\"CustomEvent\");\n"
66 << "__event.initCustomEvent(\"appservice\", true, true);\n"
67 << "__event.scale = " << scale << ";\n"
68 << "__event.__bundle = \"" << bundle << "\";\n"
69 << "document.dispatchEvent(__event);\n"
71 << "for (var i=0; i < window.frames.length; i++)\n"
72 << "{ window.frames[i].document.dispatchEvent(__event); }";
75 // LogDebug("script :\n" << script.str());
77 JSValueRef exception = NULL, ret = NULL;
78 JSStringRef jsScript = JSStringCreateWithUTF8CString(script.str().c_str());
80 ret = JSEvaluateScript(context, jsScript, NULL, NULL, 1, &exception);
82 JSStringRelease(jsScript);
84 LogDebug("JSEvaluateScript() - ret:" << ret << ", exception:" << exception << " (" << getExceptionString(context, exception) << ")");
88 void dispatchSoftKeyboardChangeEvent(JSContextRef context, const std::string state, const int width, const int height)
90 LogDebug("dispatchSoftKeyboardChangeEvent(" << context << ", " << state << ", " << width << ", " << height << ")");
94 std::stringstream script;
96 script << "var __event = document.createEvent(\"CustomEvent\");\n"
97 << "__event.initCustomEvent(\"softkeyboardchange\", true, true);\n"
98 << "__event.state = \"" << state << "\";\n"
99 << "__event.width = " << width << ";\n"
100 << "__event.height = " << height << ";\n"
101 << "document.dispatchEvent(__event);\n"
103 << "for (var i=0; i < window.frames.length; i++)\n"
104 << "{ window.frames[i].document.dispatchEvent(__event); }";
106 // just for debugging
107 // LogDebug("script :\n" << script.str());
109 JSValueRef exception = NULL, ret = NULL;
110 JSStringRef jsScript = JSStringCreateWithUTF8CString(script.str().c_str());
112 ret = JSEvaluateScript(context, jsScript, NULL, NULL, 1, &exception);
114 JSStringRelease(jsScript);
116 LogDebug("JSEvaluateScript() - ret:" << ret << ", exception:" << exception << " (" << getExceptionString(context, exception) << ")");
120 void dispatchStorageEvent(JSContextRef context,
121 const DPL::Optional<std::string>& key,
122 const DPL::Optional<std::string>& oldValue,
123 const DPL::Optional<std::string>& newValue,
124 const std::string& url)
126 LogDebug("dispatchStorageEvent(" <<
128 (key.IsNull() ? "null" : *key) << ", " <<
129 (oldValue.IsNull() ? "null" : *oldValue) << ", " <<
130 (newValue.IsNull() ? "null" : *newValue) << ", " <<
135 std::stringstream script;
138 [](const DPL::Optional<std::string>& value) -> std::string
140 return (value.IsNull() ? "null" : "\"" + *value + "\"");
143 script << "var __event = document.createEvent(\"CustomEvent\");"
144 << "__event.initCustomEvent(\"storage\", true, true);"
145 << "__event.key = " << toJSValue(key) << ";"
146 << "__event.oldValue = " << toJSValue(oldValue) << ";"
147 << "__event.newValue = " << toJSValue(newValue) << ";"
148 << "__event.url = \"" << url << "\";"
149 << "__event.storageArea = widget.preferences;"
150 << "document.dispatchEvent(__event);"
151 << "for (var i=0; i < window.frames.length; i++)"
152 << "{ window.frames[i].document.dispatchEvent(__event); }";
155 // just for debugging
156 // LogDebug("script :\n" << script.str());
158 JSValueRef exception = NULL, ret = NULL;
159 JSStringRef jsScript = JSStringCreateWithUTF8CString(script.str().c_str());
161 ret = JSEvaluateScript(context, jsScript, NULL, NULL, 1, &exception);
163 JSStringRelease(jsScript);
165 LogDebug("JSEvaluateScript() - ret:" << ret << ", exception:" << exception << " (" << getExceptionString(context, exception) << ")");
169 void dispatchHwKeyEvent(Evas_Object* ewkView, const std::string key)
171 LogDebug("dispatchHwKeyEvent(" << ewkView << ", " << key << ")");
173 if (ewkView != NULL && !key.empty())
175 std::stringstream script;
177 script << "var __event = document.createEvent(\"CustomEvent\");\n"
178 << "__event.initCustomEvent(\"tizenhwkey\", true, true);\n"
179 << "__event.keyName = \"" << key << "\";\n"
180 << "document.dispatchEvent(__event);\n"
182 << "for (var i=0; i < window.frames.length; i++)\n"
183 << "{ window.frames[i].document.dispatchEvent(__event); }";
185 // just for debugging
186 // LogDebug("script :\n" << script.str());
188 ewk_view_script_execute(ewkView, script.str().c_str(), NULL, NULL);
193 #endif //_DISPATCH_EVENT_SUPPORT_CPP_