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;
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"
72 << " for (var i=0; i < window.frames.length; i++)\n"
73 << " { window.frames[i].document.dispatchEvent(event); }\n"
77 // LogDebug("script :\n" << script.str());
79 JSValueRef exception = NULL, ret = NULL;
80 JSStringRef jsScript = JSStringCreateWithUTF8CString(script.str().c_str());
82 ret = JSEvaluateScript(context, jsScript, NULL, NULL, 1, &exception);
84 JSStringRelease(jsScript);
86 LogDebug("JSEvaluateScript() - ret:" << ret << ", exception:" << exception << " (" << getExceptionString(context, exception) << ")");
90 void dispatchSoftKeyboardChangeEvent(JSContextRef context, const std::string state, const int width, const int height)
92 LogDebug("dispatchSoftKeyboardChangeEvent(" << context << ", " << state << ", " << width << ", " << height << ")");
96 std::stringstream script;
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"
106 << " for (var i=0; i < window.frames.length; i++)\n"
107 << " { window.frames[i].document.dispatchEvent(event); }\n"
110 // just for debugging
111 // LogDebug("script :\n" << script.str());
113 JSValueRef exception = NULL, ret = NULL;
114 JSStringRef jsScript = JSStringCreateWithUTF8CString(script.str().c_str());
116 ret = JSEvaluateScript(context, jsScript, NULL, NULL, 1, &exception);
118 JSStringRelease(jsScript);
120 LogDebug("JSEvaluateScript() - ret:" << ret << ", exception:" << exception << " (" << getExceptionString(context, exception) << ")");
124 void dispatchStorageEvent(JSContextRef context, const std::string key, const std::string oldValue, const std::string newValue, const std::string url)
126 LogDebug("dispatchStorageEvent(" << context << ", " << key << ", " << oldValue << ", " << newValue << ", " << url << ")");
130 std::stringstream script;
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"
142 << " for (var i=0; i < window.frames.length; i++)\n"
143 << " { window.frames[i].document.dispatchEvent(event); }\n"
146 // just for debugging
147 // LogDebug("script :\n" << script.str());
149 JSValueRef exception = NULL, ret = NULL;
150 JSStringRef jsScript = JSStringCreateWithUTF8CString(script.str().c_str());
152 ret = JSEvaluateScript(context, jsScript, NULL, NULL, 1, &exception);
154 JSStringRelease(jsScript);
156 LogDebug("JSEvaluateScript() - ret:" << ret << ", exception:" << exception << " (" << getExceptionString(context, exception) << ")");
161 #endif //_DISPATCH_EVENT_SUPPORT_CPP_