+/*
+ * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+/**
+ *
+ * @file dispatch_event_support.cpp
+ * @author Tae-Jeong Lee (taejeong.lee@samsung.com)
+ * @version 0.1
+ * @brief
+ */
+
+#ifndef _DISPATCH_EVENT_SUPPORT_CPP_
+#define _DISPATCH_EVENT_SUPPORT_CPP_
+
+#include <string>
+#include <JavaScriptCore/JavaScript.h>
+#include <dpl/log/log.h>
+
+#include "dispatch_event_support.h"
+
+namespace DispatchEventSupport {
+
+static std::string getExceptionString(JSContextRef context, JSValueRef exception)
+{
+ if (exception)
+ {
+ JSStringRef jsString = JSValueToStringCopy(context, exception, NULL);
+ size_t bufSize = JSStringGetMaximumUTF8CStringSize(jsString);
+
+ char* buf = new char[bufSize];
+ JSStringGetUTF8CString(jsString, buf, bufSize);
+ JSStringRelease(jsString);
+
+ std::string ret = buf;
+ delete[] buf;
+
+ return ret;
+ }
+ else
+ {
+ return "NULL";
+ }
+}
+
+void dispatchAppServiceEvent(JSContextRef context, const float scale, const std::string bundle)
+{
+ LogDebug("DispatchTizenServiceEvent(" << context << ", " << scale << ", " << bundle << ")");
+
+ if (context != NULL)
+ {
+ std::stringstream script;
+
+ script << "{\n"
+ << " var event = document.createEvent(\"CustomEvent\");\n"
+ << " event.initCustomEvent(\"appservice\", true, true);\n"
+ << " event.scale = " << scale << ";\n"
+ << " event.__bundle = \"" << bundle << "\";\n"
+ << " document.dispatchEvent(event);\n"
+ << " \n"
+ << " for (var i=0; i < window.frames.length; i++)\n"
+ << " { window.frames[i].document.dispatchEvent(event); }\n"
+ << "}";
+
+ // just for debugging
+ // LogDebug("script :\n" << script.str());
+
+ JSValueRef exception = NULL, ret = NULL;
+ JSStringRef jsScript = JSStringCreateWithUTF8CString(script.str().c_str());
+
+ ret = JSEvaluateScript(context, jsScript, NULL, NULL, 1, &exception);
+
+ JSStringRelease(jsScript);
+
+ LogDebug("JSEvaluateScript() - ret:" << ret << ", exception:" << exception << " (" << getExceptionString(context, exception) << ")");
+ }
+}
+
+void dispatchSoftKeyboardChangeEvent(JSContextRef context, const std::string state, const int width, const int height)
+{
+ LogDebug("dispatchSoftKeyboardChangeEvent(" << context << ", " << state << ", " << width << ", " << height << ")");
+
+ if (context != NULL)
+ {
+ std::stringstream script;
+
+ script << "{\n"
+ << " var event = document.createEvent(\"CustomEvent\");\n"
+ << " event.initCustomEvent(\"softkeyboardchange\", true, true);\n"
+ << " event.state = \"" << state << "\";\n"
+ << " event.width = " << width << ";\n"
+ << " event.height = " << height << ";\n"
+ << " document.dispatchEvent(event);\n"
+ << " \n"
+ << " for (var i=0; i < window.frames.length; i++)\n"
+ << " { window.frames[i].document.dispatchEvent(event); }\n"
+ << "}";
+
+ // just for debugging
+ // LogDebug("script :\n" << script.str());
+
+ JSValueRef exception = NULL, ret = NULL;
+ JSStringRef jsScript = JSStringCreateWithUTF8CString(script.str().c_str());
+
+ ret = JSEvaluateScript(context, jsScript, NULL, NULL, 1, &exception);
+
+ JSStringRelease(jsScript);
+
+ LogDebug("JSEvaluateScript() - ret:" << ret << ", exception:" << exception << " (" << getExceptionString(context, exception) << ")");
+ }
+}
+
+void dispatchStorageEvent(JSContextRef context, const std::string key, const std::string oldValue, const std::string newValue, const std::string url)
+{
+ LogDebug("dispatchStorageEvent(" << context << ", " << key << ", " << oldValue << ", " << newValue << ", " << url << ")");
+
+ if (context != NULL)
+ {
+ std::stringstream script;
+
+ script << "{\n"
+ << " var event = document.createEvent(\"CustomEvent\");\n"
+ << " event.initCustomEvent(\"storage\", true, true);\n"
+ << " event.key = \"" << key << "\";\n"
+ << " event.oldValue = \"" << oldValue << "\";\n"
+ << " event.newValue = \"" << newValue << "\";\n"
+ << " event.url = \"" << url << "\";\n"
+ << " event.storageArea = widget.preferences;\n"
+ << " document.dispatchEvent(event);\n"
+ << " \n"
+ << " for (var i=0; i < window.frames.length; i++)\n"
+ << " { window.frames[i].document.dispatchEvent(event); }\n"
+ << "}";
+
+ // just for debugging
+ // LogDebug("script :\n" << script.str());
+
+ JSValueRef exception = NULL, ret = NULL;
+ JSStringRef jsScript = JSStringCreateWithUTF8CString(script.str().c_str());
+
+ ret = JSEvaluateScript(context, jsScript, NULL, NULL, 1, &exception);
+
+ JSStringRelease(jsScript);
+
+ LogDebug("JSEvaluateScript() - ret:" << ret << ", exception:" << exception << " (" << getExceptionString(context, exception) << ")");
+ }
+}
+
+}
+#endif //_DISPATCH_EVENT_SUPPORT_CPP_