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)
24 #include "AddEventListenerSupport.h"
27 #include <dpl/log/log.h>
28 #include <dpl/foreach.h>
29 #include <CommonsJavaScript/Converter.h>
30 #include "JSStorageEvent.h"
32 namespace WrtPlugins {
35 AddEventListenerSupport::IFramesListeners
36 AddEventListenerSupport::m_listeners =
37 AddEventListenerSupport::IFramesListeners();
39 JSContextRef AddEventListenerSupport::m_context = NULL;
41 void AddEventListenerSupport::initializeContext(JSContextRef context)
43 if(!m_context) m_context = context;
46 bool AddEventListenerSupport::isInitialized() {return m_context!=NULL;}
48 JSValueRef AddEventListenerSupport::
49 AddEventListener(JSContextRef context,
51 JSObjectRef thisObject,
53 const JSValueRef arguments[],
54 JSValueRef* exception)
56 LogDebug("Add event listener invoked");
57 LogDebug("This(iframe?):" << thisObject);
58 LogDebug("object:" << object);
60 if (argumentCount < 2 || !JSValueIsString(context, arguments[0])) {
61 LogError("Wrong arguments");
62 return JSValueMakeUndefined(context);
65 std::string eventName = WrtDeviceApis::CommonsJavaScript::
66 Converter(context).toString(arguments[0]);
68 if(eventName != "storage"){
69 LogDebug("Event type not supported");
70 return JSValueMakeUndefined(context);
73 JSValueProtect(m_context, arguments[1]);
74 JSObjectRef objectCb =
75 JSValueToObject(m_context,
78 if(!JSObjectIsFunction(m_context, objectCb))
80 LogError("JS object is not a function");
81 return JSValueMakeUndefined(context);
83 //add object to Listeners
84 CallbackData data = {objectCb, thisObject};
85 getIFrameListeners(thisObject)->push_back(data);
87 return JSValueMakeUndefined(context);
90 void AddEventListenerSupport::CallListenersFromDifferentIFrames(
92 const WrtDeviceApis::StorageEvent::Api::IStorageEventPtr& event)
94 LogDebug("Invoked callbacks");
96 FOREACH(it, m_listeners)
98 if(it->first == iframe){
102 auto eventPriv = new JSStorageEventPrivateObject(m_context,
105 JSObjectRef eventObject =
106 JSObjectMake(m_context, JSStorageEvent::getClassRef(), eventPriv);
107 const size_t argc = 1;
108 JSValueRef argv[argc] = {eventObject};
110 JSValueProtect(m_context, eventObject);
112 FOREACH(listener, *it->second)
116 JSObjectCallAsFunction(
125 JSValueUnprotect(m_context, eventObject);
131 AddEventListenerSupport::ListenersPtr
132 AddEventListenerSupport::getIFrameListeners(JSObjectRef iframe){
133 auto it = m_listeners.find(iframe);
134 if(it != m_listeners.end()){
138 return m_listeners[iframe] = ListenersPtr(new Listeners());