upload tizen1.0 source
[framework/web/wrt-plugins-common.git] / src / standards / W3C / Widget / AddEventListenerSupport.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  * @version    0.1
21  * @brief
22  */
23
24 #include "AddEventListenerSupport.h"
25
26 #include <string>
27 #include <dpl/log/log.h>
28 #include <dpl/foreach.h>
29 #include <CommonsJavaScript/Converter.h>
30 #include "JSStorageEvent.h"
31
32 namespace WrtPlugins {
33 namespace W3C {
34
35 AddEventListenerSupport::IFramesListeners
36     AddEventListenerSupport::m_listeners =
37         AddEventListenerSupport::IFramesListeners();
38
39 JSContextRef AddEventListenerSupport::m_context = NULL;
40
41 void AddEventListenerSupport::initializeContext(JSContextRef context)
42 {
43     if(!m_context) m_context = context;
44 }
45
46 bool AddEventListenerSupport::isInitialized() {return m_context!=NULL;}
47
48 JSValueRef AddEventListenerSupport::
49 AddEventListener(JSContextRef context,
50                  JSObjectRef object,
51                  JSObjectRef thisObject,
52                  size_t argumentCount,
53                  const JSValueRef arguments[],
54                  JSValueRef* exception)
55 {
56     LogDebug("Add event listener invoked");
57     LogDebug("This(iframe?):" << thisObject);
58     LogDebug("object:" << object);
59
60     if (argumentCount < 2 || !JSValueIsString(context, arguments[0])) {
61         LogError("Wrong arguments");
62         return JSValueMakeUndefined(context);
63     }
64
65     std::string eventName = WrtDeviceApis::CommonsJavaScript::
66         Converter(context).toString(arguments[0]);
67
68     if(eventName != "storage"){
69         LogDebug("Event type not supported");
70         return JSValueMakeUndefined(context);
71     }
72
73     JSValueProtect(m_context, arguments[1]);
74     JSObjectRef objectCb =
75         JSValueToObject(m_context,
76                         arguments[1],
77                         NULL);
78     if(!JSObjectIsFunction(m_context, objectCb))
79     {
80         LogError("JS object is not a function");
81         return JSValueMakeUndefined(context);
82     }
83     //add object to Listeners
84     CallbackData data = {objectCb, thisObject};
85     getIFrameListeners(thisObject)->push_back(data);
86
87     return JSValueMakeUndefined(context);
88 }
89
90 void AddEventListenerSupport::CallListenersFromDifferentIFrames(
91     JSObjectRef iframe,
92     const WrtDeviceApis::StorageEvent::Api::IStorageEventPtr& event)
93 {
94     LogDebug("Invoked callbacks");
95
96     FOREACH(it, m_listeners)
97     {
98         if(it->first == iframe){
99             continue;
100         }
101
102         auto eventPriv = new JSStorageEventPrivateObject(m_context,
103                                                          event);
104
105         JSObjectRef eventObject =
106             JSObjectMake(m_context, JSStorageEvent::getClassRef(), eventPriv);
107         const size_t argc = 1;
108         JSValueRef argv[argc] = {eventObject};
109
110         JSValueProtect(m_context, eventObject);
111
112         FOREACH(listener, *it->second)
113         {
114             LogDebug("Call");
115
116             JSObjectCallAsFunction(
117                 m_context,
118                 listener->object,
119                 NULL,
120                 argc,
121                 argv,
122                 NULL);
123         }
124
125         JSValueUnprotect(m_context, eventObject);
126     }
127
128     LogDebug("Done");
129 };
130
131 AddEventListenerSupport::ListenersPtr
132 AddEventListenerSupport::getIFrameListeners(JSObjectRef iframe){
133     auto it = m_listeners.find(iframe);
134     if(it != m_listeners.end()){
135         return it->second;
136     }
137
138     return m_listeners[iframe] = ListenersPtr(new Listeners());
139 }
140
141 }
142 }