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, const std::string key, const std::string oldValue, const std::string newValue, const std::string url)
122 LogDebug("dispatchStorageEvent(" << context << ", " << key << ", " << oldValue << ", " << newValue << ", " << url << ")");
126 std::stringstream script;
128 script << "var __event = document.createEvent(\"CustomEvent\");\n"
129 << "__event.initCustomEvent(\"storage\", true, true);\n"
130 << "__event.key = \"" << key << "\";\n"
131 << "__event.oldValue = \"" << oldValue << "\";\n"
132 << "__event.newValue = \"" << newValue << "\";\n"
133 << "__event.url = \"" << url << "\";\n"
134 << "__event.storageArea = widget.preferences;\n"
135 << "document.dispatchEvent(__event);\n"
137 << "for (var i=0; i < window.frames.length; i++)\n"
138 << "{ window.frames[i].document.dispatchEvent(__event); }";
141 // just for debugging
142 // LogDebug("script :\n" << script.str());
144 JSValueRef exception = NULL, ret = NULL;
145 JSStringRef jsScript = JSStringCreateWithUTF8CString(script.str().c_str());
147 ret = JSEvaluateScript(context, jsScript, NULL, NULL, 1, &exception);
149 JSStringRelease(jsScript);
151 LogDebug("JSEvaluateScript() - ret:" << ret << ", exception:" << exception << " (" << getExceptionString(context, exception) << ")");
156 #endif //_DISPATCH_EVENT_SUPPORT_CPP_