Update wrt-plugins-common_0.3.53
[framework/web/wrt-plugins-common.git] / src / js-overlay / js_overlay_addEventListener.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       AddEventListenerSupport.cpp
19  * @author     Grzegorz Krawczyk (g.krawczyk@samsung.com)
20  * @author     Yunchan Cho (yunchan.cho@samsung.com)
21  * @version    0.1
22  * @brief
23  */
24
25
26 #include <string>
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"
34
35 namespace WrtPlugins {
36 namespace W3C {
37
38 AddEventListenerSupport::IFramesListeners
39     AddEventListenerSupport::m_listeners =
40         AddEventListenerSupport::IFramesListeners();
41
42 JSContextRef AddEventListenerSupport::m_context = NULL;
43
44 void AddEventListenerSupport::initialize(JSContextRef context)
45 {
46     if(!m_context) m_context = context;
47 }
48
49 void AddEventListenerSupport::deinitialize()
50 {
51     if(!m_context) {
52         LogDebug("Not yet initialized");
53     }
54
55     m_listeners.clear();
56     m_context = NULL;
57 }
58
59 bool AddEventListenerSupport::isInitialized() 
60 {
61     return m_context!=NULL;
62 }
63
64 JSValueRef AddEventListenerSupport::
65 AddEventListener(JSContextRef context,
66                  JSObjectRef object,
67                  JSObjectRef thisObject,
68                  size_t argumentCount,
69                  const JSValueRef arguments[],
70                  JSValueRef* exception)
71 {
72     LogDebug("Add event listener invoked");
73     LogDebug("This(iframe?):" << thisObject);
74     LogDebug("object:" << object);
75
76     if (argumentCount < 2 || !JSValueIsString(context, arguments[0])) {
77         LogError("Wrong arguments");
78         return JSValueMakeUndefined(context);
79     }
80
81     std::string eventName = WrtDeviceApis::CommonsJavaScript::
82         Converter(context).toString(arguments[0]);
83     LogDebug("Event name: " << eventName);
84
85     if(eventName != "storage" &&
86             eventName != "appservice"){
87         LogDebug("Event type not supported");
88         return JSValueMakeUndefined(context);
89     }
90
91     JSValueProtect(m_context, arguments[1]);
92     JSObjectRef objectCb =
93         JSValueToObject(m_context,
94                         arguments[1],
95                         NULL);
96     if(!JSObjectIsFunction(m_context, objectCb))
97     {
98         LogError("JS object is not a function");
99         return JSValueMakeUndefined(context);
100     }
101     //add object to Listeners
102     
103     //set event information according to each event type
104     if(eventName == "storage") {
105         CallbackData data = {
106             StorageCustomEvent,
107             objectCb, 
108             thisObject};
109         getIFrameListeners(thisObject)->push_back(data);
110     } else if (eventName == "appservice") {
111         CallbackData data = {
112             ServiceCustomEvent,
113             objectCb, 
114             thisObject};
115         getIFrameListeners(thisObject)->push_back(data);
116     }
117
118     return JSValueMakeUndefined(context);
119 }
120
121 void AddEventListenerSupport::
122 CallStorageListenersFromDifferentIFrames(
123     JSObjectRef iframe,
124     const WrtDeviceApis::StorageEvent::Api::IStorageEventPtr& event)
125 {
126     LogDebug("Invoked callbacks");
127     LogInfo("Context: " << m_context);
128
129     FOREACH(it, m_listeners)
130     {
131         if(it->first == iframe){
132             continue;
133         }
134
135         auto eventPriv = new JSStorageEventPrivateObject(m_context,
136                                                          event);
137
138         JSObjectRef eventObject =
139             JSObjectMake(m_context, JSStorageEvent::getClassRef(), eventPriv);
140         const size_t argc = 1;
141         JSValueRef argv[argc] = {eventObject};
142
143         JSValueProtect(m_context, eventObject);
144
145         FOREACH(listener, *it->second)
146         {
147             LogDebug("Call");
148
149             if (listener->eventType ==
150                     StorageCustomEvent)
151             {
152                 JSObjectCallAsFunction(
153                     m_context,
154                     listener->object,
155                     NULL,
156                     argc,
157                     argv,
158                     NULL);
159             }
160         }
161
162         JSValueUnprotect(m_context, eventObject);
163     }
164
165     LogDebug("Done");
166 };
167
168 void AddEventListenerSupport::
169 CallServiceListenersFromIFrame(JSObjectRef iframe,
170     const WrtDeviceApis::TizenServiceEvent::Api::ITizenServiceEventPtr& event)
171 {
172     using namespace WrtPlugins::Tizen;
173     LogDebug("Invoked callbacks");
174
175     IFramesListeners::iterator it = m_listeners.find(iframe);
176     if (it == m_listeners.end()) {
177         LogDebug("This frame object not existed");
178         return;
179     }
180
181     auto eventPriv = 
182         new JSTizenServiceEventPrivateObject(m_context, event);
183
184     JSObjectRef eventObject =
185         JSObjectMake(m_context, JSTizenServiceEvent::getClassRef(), eventPriv);
186     const size_t argc = 1;
187     JSValueRef argv[argc] = {eventObject};
188
189     JSValueProtect(m_context, eventObject);
190     FOREACH(listener, *it->second)
191     {
192         if (listener->eventType ==
193                 ServiceCustomEvent)
194         {
195             LogDebug("Call");
196             JSObjectCallAsFunction(
197                 m_context,
198                 listener->object,
199                 iframe,
200                 argc,
201                 argv,
202                 NULL);
203         }
204     }
205     JSValueUnprotect(m_context, eventObject);
206     LogDebug("Done");
207 };
208
209 AddEventListenerSupport::ListenersPtr
210 AddEventListenerSupport::getIFrameListeners(JSObjectRef iframe){
211     auto it = m_listeners.find(iframe);
212     if(it != m_listeners.end()){
213         return it->second;
214     }
215
216     return m_listeners[iframe] = ListenersPtr(new Listeners());
217 }
218
219 }
220 }