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 AddEventListenerSupport.cpp
19 * @author Grzegorz Krawczyk (g.krawczyk@samsung.com)
20 * @author Yunchan Cho (yunchan.cho@samsung.com)
26 #include <dpl/log/log.h>
27 #include <dpl/foreach.h>
28 #include <CommonsJavaScript/Converter.h>
29 #include <js_overlay_addEventListener.h>
30 #include <js_overlay_types.h>
31 #include <JSStorageEvent.h>
33 namespace WrtPlugins {
35 const std::string storageEventName = "storage";
36 const std::string appServiceEventName = "appservice";
37 const std::string softKeyboardChangeEventName = "softkeyboardchange";
39 AddEventListenerSupport::IFramesListeners
40 AddEventListenerSupport::m_listeners =
41 AddEventListenerSupport::IFramesListeners();
43 JSContextRef AddEventListenerSupport::m_context = NULL;
45 void AddEventListenerSupport::initialize(JSContextRef context)
52 void AddEventListenerSupport::deinitialize()
55 LogDebug("Not yet initialized");
62 bool AddEventListenerSupport::isInitialized()
64 return m_context != NULL;
67 JSValueRef AddEventListenerSupport::
68 AddEventListener(JSContextRef context,
70 JSObjectRef thisObject,
72 const JSValueRef arguments[],
73 JSValueRef* exception)
75 LogDebug("Add event listener invoked");
76 LogDebug("This(iframe?):" << thisObject);
77 LogDebug("object:" << object);
79 if (argumentCount < 2 || !JSValueIsString(context, arguments[0])) {
80 LogError("Wrong arguments");
81 return JSValueMakeUndefined(context);
84 std::string eventName = WrtDeviceApis::CommonsJavaScript::
85 Converter(context).toString(arguments[0]);
86 LogDebug("Event name: " << eventName);
88 if (eventName != storageEventName &&
89 eventName != appServiceEventName &&
90 eventName != softKeyboardChangeEventName)
92 LogDebug("Event type not supported");
93 return JSValueMakeUndefined(context);
96 JSValueProtect(m_context, arguments[1]);
97 JSObjectRef objectCb =
98 JSValueToObject(m_context,
101 if (!JSObjectIsFunction(m_context, objectCb)) {
102 LogError("JS object is not a function");
103 return JSValueMakeUndefined(context);
105 //add object to Listeners
106 //set event information according to each event type
108 data.object = objectCb;
109 data.thisObject = thisObject;
111 if (eventName == storageEventName) {
112 data.eventType = StorageCustomEvent;
113 } else if (eventName == appServiceEventName) {
114 data.eventType = ServiceCustomEvent;
115 } else if (eventName == softKeyboardChangeEventName) {
116 data.eventType = SoftKeyboardChangeCustomEvent;
119 getIFrameListeners(thisObject)->push_back(data);
121 return JSValueMakeUndefined(context);
124 void AddEventListenerSupport::
125 CallStorageListenersFromDifferentIFrames(
127 const WrtDeviceApis::StorageEvent::Api::IStorageEventPtr& event)
129 LogDebug("Invoked callbacks");
130 LogInfo("Context: " << m_context);
132 FOREACH(it, m_listeners)
134 if (it->first == iframe) {
138 auto eventPriv = new JSStorageEventPrivateObject(m_context,
141 JSObjectRef eventObject =
142 JSObjectMake(m_context, JSStorageEvent::getClassRef(), eventPriv);
143 const size_t argc = 1;
144 JSValueRef argv[argc] = { eventObject };
146 JSValueProtect(m_context, eventObject);
148 FOREACH(listener, *it->second)
152 if (listener->eventType ==
155 JSObjectCallAsFunction(
165 JSValueUnprotect(m_context, eventObject);
171 void AddEventListenerSupport::
172 CallCustomEventListenersFromIFrame(
174 CustomEventType eventType,
175 JSObjectRef eventObject)
177 LogDebug("Invoked callbacks");
179 IFramesListeners::iterator it = m_listeners.find(iframe);
180 if (it == m_listeners.end()) {
181 LogDebug("This frame object not existed");
185 const size_t argc = 1;
186 JSValueRef argv[argc] = { eventObject };
188 JSValueProtect(m_context, eventObject);
189 FOREACH(listener, *it->second)
191 if (listener->eventType == eventType) {
193 JSObjectCallAsFunction(
202 JSValueUnprotect(m_context, eventObject);
206 AddEventListenerSupport::ListenersPtr
207 AddEventListenerSupport::getIFrameListeners(JSObjectRef iframe)
209 auto it = m_listeners.find(iframe);
210 if (it != m_listeners.end()) {
214 return m_listeners[iframe] = ListenersPtr(new Listeners());