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)
27 #include <dpl/log/log.h>
28 #include <dpl/foreach.h>
29 #include <CommonsJavaScript/Converter.h>
30 #include <js_overlay_addEventListener.h>
31 #include <js_overlay_types.h>
32 #include "JSStorageEvent.h"
33 #include "JSTizenServiceEvent.h"
35 namespace WrtPlugins {
38 AddEventListenerSupport::IFramesListeners
39 AddEventListenerSupport::m_listeners =
40 AddEventListenerSupport::IFramesListeners();
42 JSContextRef AddEventListenerSupport::m_context = NULL;
44 void AddEventListenerSupport::initialize(JSContextRef context)
46 if(!m_context) m_context = context;
49 void AddEventListenerSupport::deinitialize()
52 LogDebug("Not yet initialized");
59 bool AddEventListenerSupport::isInitialized()
61 return m_context!=NULL;
64 JSValueRef AddEventListenerSupport::
65 AddEventListener(JSContextRef context,
67 JSObjectRef thisObject,
69 const JSValueRef arguments[],
70 JSValueRef* exception)
72 LogDebug("Add event listener invoked");
73 LogDebug("This(iframe?):" << thisObject);
74 LogDebug("object:" << object);
76 if (argumentCount < 2 || !JSValueIsString(context, arguments[0])) {
77 LogError("Wrong arguments");
78 return JSValueMakeUndefined(context);
81 std::string eventName = WrtDeviceApis::CommonsJavaScript::
82 Converter(context).toString(arguments[0]);
83 LogDebug("Event name: " << eventName);
85 if(eventName != "storage" &&
86 eventName != "appservice"){
87 LogDebug("Event type not supported");
88 return JSValueMakeUndefined(context);
91 JSValueProtect(m_context, arguments[1]);
92 JSObjectRef objectCb =
93 JSValueToObject(m_context,
96 if(!JSObjectIsFunction(m_context, objectCb))
98 LogError("JS object is not a function");
99 return JSValueMakeUndefined(context);
101 //add object to Listeners
103 //set event information according to each event type
104 if(eventName == "storage") {
105 CallbackData data = {
109 getIFrameListeners(thisObject)->push_back(data);
110 } else if (eventName == "appservice") {
111 CallbackData data = {
115 getIFrameListeners(thisObject)->push_back(data);
118 return JSValueMakeUndefined(context);
121 void AddEventListenerSupport::
122 CallStorageListenersFromDifferentIFrames(
124 const WrtDeviceApis::StorageEvent::Api::IStorageEventPtr& event)
126 LogDebug("Invoked callbacks");
127 LogInfo("Context: " << m_context);
129 FOREACH(it, m_listeners)
131 if(it->first == iframe){
135 auto eventPriv = new JSStorageEventPrivateObject(m_context,
138 JSObjectRef eventObject =
139 JSObjectMake(m_context, JSStorageEvent::getClassRef(), eventPriv);
140 const size_t argc = 1;
141 JSValueRef argv[argc] = {eventObject};
143 JSValueProtect(m_context, eventObject);
145 FOREACH(listener, *it->second)
149 if (listener->eventType ==
152 JSObjectCallAsFunction(
162 JSValueUnprotect(m_context, eventObject);
168 void AddEventListenerSupport::
169 CallServiceListenersFromIFrame(JSObjectRef iframe,
170 const WrtDeviceApis::TizenServiceEvent::Api::ITizenServiceEventPtr& event)
172 using namespace WrtPlugins::Tizen;
173 LogDebug("Invoked callbacks");
175 IFramesListeners::iterator it = m_listeners.find(iframe);
176 if (it == m_listeners.end()) {
177 LogDebug("This frame object not existed");
182 new JSTizenServiceEventPrivateObject(m_context, event);
184 JSObjectRef eventObject =
185 JSObjectMake(m_context, JSTizenServiceEvent::getClassRef(), eventPriv);
186 const size_t argc = 1;
187 JSValueRef argv[argc] = {eventObject};
189 JSValueProtect(m_context, eventObject);
190 FOREACH(listener, *it->second)
192 if (listener->eventType ==
196 JSObjectCallAsFunction(
205 JSValueUnprotect(m_context, eventObject);
209 AddEventListenerSupport::ListenersPtr
210 AddEventListenerSupport::getIFrameListeners(JSObjectRef iframe){
211 auto it = m_listeners.find(iframe);
212 if(it != m_listeners.end()){
216 return m_listeners[iframe] = ListenersPtr(new Listeners());