tizen 2.3.1 release
[framework/web/mobile/wrt.git] / src / wrt-launchpad-daemon / src / web_internal_eventloop.cpp
1 /*
2  * Copyright (c) 2014 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  * @file    web_internal_eventloop.cpp
18  * @author  Prathmesh (prathmesh.m@samsung.com)
19  */
20
21 #include "web_internal_eventloop.h"
22 #include <dpl/singleton_safe_impl.h>
23
24 IMPLEMENT_SAFE_SINGLETON(WebInternalEvent)
25
26 WebInternalEvent::WebInternalEvent()
27 {
28 }
29
30 #ifdef SUPPORT_MULTIPLE_REQUEST
31 WebInternalEvent::~WebInternalEvent()
32 {
33     for (auto it = m_webInternalEventMap.begin(); it != m_webInternalEventMap.end(); ++it) {
34         delete it->second;
35     }
36     m_webInternalEventMap.clear();
37 }
38
39 void WebInternalEvent::addListner(webInternalEventType type, Ecore_Event_Handler_Cb cbFunc, void *data)
40 {
41     auto it = m_webInternalEventMap.find(type);
42     if (it != m_webInternalEventMap.end())
43         return;
44
45     WebInternalUserCBData* cbData = new WebInternalUserCBData();
46     cbData->m_event = ecore_event_type_new();
47     cbData->m_callerCallback = cbFunc;
48     cbData->m_data = data;
49     m_customEventHandler = ecore_event_handler_add(cbData->m_event, eventTriggeredCallback, this);
50     m_webInternalEventMap.insert(WebInternalEventPair(type, cbData));
51 }
52
53 void WebInternalEvent::deleteListner(webInternalEventType type)
54 {
55     auto it = m_webInternalEventMap.find(type);
56     if (it != m_webInternalEventMap.end()) {
57         delete it->second;
58         m_webInternalEventMap.erase(it);
59     }
60 }
61
62 void WebInternalEvent::sendEvent(webInternalEventType type, void *eventInfo)
63 {
64     auto it = m_webInternalEventMap.find(type);
65     if (it != m_webInternalEventMap.end()) {
66         WebInternalUserCBData* cbData = it->second;
67         ecore_event_add(cbData->m_event, eventInfo, freeEventInfo, NULL);
68     }
69 }
70
71 void WebInternalEvent::freeEventInfo(void *user_data, void *func_data)
72 {
73     // Do nothing. Added here else the default free is called.
74     // Need to override the default free and insteed free after
75     // its done for us
76 }
77
78 Eina_Bool WebInternalEvent::eventTriggeredCallback(void *data, int type, void *eventInfo)
79 {
80     WebInternalEvent* This = (WebInternalEvent*)data;
81
82     for (auto it = This->m_webInternalEventMap.begin(); it != This->m_webInternalEventMap.end(); ++it) {
83         WebInternalUserCBData* cbData = it->second;
84         if (cbData->m_event == type) {
85             cbData->m_callerCallback(cbData->m_data, it->first, eventInfo);
86             break;
87         }
88     }
89     return ECORE_CALLBACK_PASS_ON;
90 }
91
92 #else // SUPPORT_MULTIPLE_REQUEST
93
94 WebInternalEvent::~WebInternalEvent()
95 {
96 }
97
98 void WebInternalEvent::addListner(webInternalEventType type, Ecore_Event_Handler_Cb cbFunc, void *data)
99 {
100     if (WEB_APP_TERMINATE == type) {
101         m_backEvent = ecore_event_type_new();
102         m_customEventHandler = ecore_event_handler_add(m_backEvent, eventTriggeredCallback, this);
103         m_callerData = data;
104         m_callerCallback = cbFunc;
105     }
106 }
107
108 void WebInternalEvent::deleteListner(webInternalEventType type)
109 {
110
111 }
112
113 void WebInternalEvent::sendEvent(webInternalEventType type, void *eventInfo)
114 {
115     if (WEB_APP_TERMINATE == type) {
116          ecore_event_add(m_backEvent, eventInfo, NULL, NULL);
117     }
118 }
119
120 Eina_Bool WebInternalEvent::eventTriggeredCallback(void *data, int type, void *eventInfo)
121 {
122     WebInternalEvent* This = (WebInternalEvent*)data;
123     if (This->m_backEvent == type) {
124         Ecore_Event_Handler_Cb cb = This->m_callerCallback;
125         cb(This->m_callerData, WEB_APP_TERMINATE, eventInfo);
126     }
127     return ECORE_CALLBACK_PASS_ON;
128 }
129 #endif // SUPPORT_MULTIPLE_REQUEST