[Release] wrt-plugins-common_0.3.94
[platform/framework/web/wrt-plugins-common.git] / src / dispatch-event / dispatch_event_support.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 /**
17  *
18  * @file       dispatch_event_support.cpp
19  * @author     Tae-Jeong Lee (taejeong.lee@samsung.com)
20  * @version    0.1
21  * @brief
22  */
23
24 #ifndef _DISPATCH_EVENT_SUPPORT_CPP_
25 #define _DISPATCH_EVENT_SUPPORT_CPP_
26
27 #include <string>
28 #include <JavaScriptCore/JavaScript.h>
29 #include <dpl/log/log.h>
30
31 #include "dispatch_event_support.h"
32
33 namespace DispatchEventSupport {
34
35 static std::string getExceptionString(JSContextRef context, JSValueRef exception)
36 {
37     if (exception)
38     {
39         JSStringRef jsString = JSValueToStringCopy(context, exception, NULL);
40         size_t bufSize = JSStringGetMaximumUTF8CStringSize(jsString);
41
42         char* buf = new char[bufSize];
43         JSStringGetUTF8CString(jsString, buf, bufSize);
44         JSStringRelease(jsString);
45
46         std::string ret = buf;
47         delete[] buf;
48
49         return ret;
50     }
51     else
52     {
53         return "NULL";
54     }
55 }
56
57 void dispatchAppServiceEvent(JSContextRef context, const float scale, const std::string bundle)
58 {
59     LogDebug("DispatchTizenServiceEvent(" << context << ", " << scale << ", " << bundle << ")");
60
61     if (context != NULL)
62     {
63         std::stringstream script;
64
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"
70                << "\n"
71                << "for (var i=0; i < window.frames.length; i++)\n"
72                << "{ window.frames[i].document.dispatchEvent(__event); }";
73
74         // just for debugging
75         // LogDebug("script :\n" << script.str());
76
77         JSValueRef exception = NULL, ret = NULL;
78         JSStringRef jsScript = JSStringCreateWithUTF8CString(script.str().c_str());
79
80         ret = JSEvaluateScript(context, jsScript, NULL, NULL, 1, &exception);
81
82         JSStringRelease(jsScript);
83
84         LogDebug("JSEvaluateScript() - ret:" << ret << ", exception:" << exception << " (" << getExceptionString(context, exception) << ")");
85     }
86 }
87
88 void dispatchSoftKeyboardChangeEvent(JSContextRef context, const std::string state, const int width, const int height)
89 {
90     LogDebug("dispatchSoftKeyboardChangeEvent(" << context << ", " << state << ", " << width << ", " << height << ")");
91
92     if (context != NULL)
93     {
94         std::stringstream script;
95
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"
102                << "\n"
103                << "for (var i=0; i < window.frames.length; i++)\n"
104                << "{ window.frames[i].document.dispatchEvent(__event); }";
105
106         // just for debugging
107         // LogDebug("script :\n" << script.str());
108
109         JSValueRef exception = NULL, ret = NULL;
110         JSStringRef jsScript = JSStringCreateWithUTF8CString(script.str().c_str());
111
112         ret = JSEvaluateScript(context, jsScript, NULL, NULL, 1, &exception);
113
114         JSStringRelease(jsScript);
115
116         LogDebug("JSEvaluateScript() - ret:" << ret << ", exception:" << exception << " (" << getExceptionString(context, exception) << ")");
117     }
118 }
119
120 void dispatchStorageEvent(JSContextRef context, const std::string key, const std::string oldValue, const std::string newValue, const std::string url)
121 {
122     LogDebug("dispatchStorageEvent(" << context << ", " << key << ", " << oldValue << ", " << newValue << ", " << url << ")");
123
124     if (context != NULL)
125     {
126         std::stringstream script;
127
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"
136                << "\n"
137                << "for (var i=0; i < window.frames.length; i++)\n"
138                << "{ window.frames[i].document.dispatchEvent(__event); }";
139
140
141         // just for debugging
142         // LogDebug("script :\n" << script.str());
143
144         JSValueRef exception = NULL, ret = NULL;
145         JSStringRef jsScript = JSStringCreateWithUTF8CString(script.str().c_str());
146
147         ret = JSEvaluateScript(context, jsScript, NULL, NULL, 1, &exception);
148
149         JSStringRelease(jsScript);
150
151         LogDebug("JSEvaluateScript() - ret:" << ret << ", exception:" << exception << " (" << getExceptionString(context, exception) << ")");
152     }
153 }
154
155 }
156 #endif //_DISPATCH_EVENT_SUPPORT_CPP_