tizen 2.3.1 release
[framework/web/mobile/wrt.git] / src / domain / permission_popup_manager.cpp
1 /*
2  * Copyright (c) 2013 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    permission_popup_manager.cpp
18  * @author  Jihoon Chung (jihoon.chung@samsung.com)
19  */
20
21 #include "permission_popup_manager.h"
22
23 #include <dpl/availability.h>
24 #include <dpl/foreach.h>
25 #include <dpl/log/wrt_log.h>
26 #include <dpl/singleton_safe_impl.h>
27 #include <Evas.h>
28
29 IMPLEMENT_SAFE_SINGLETON(PermissionPopupManager)
30
31 PermissionPopupManager::PermissionPopupManager()
32 {}
33
34 PermissionPopupManager::~PermissionPopupManager()
35 {}
36
37 void PermissionPopupManager::registerPopup(Evas_Object* webview, Evas_Object* popup)
38 {
39     WrtLogD("register");
40
41     if (!webview || !popup)
42     {
43         WrtLogW("Wrong input argument");
44         return;
45     }
46     addWebview(webview);
47     addPopup(popup);
48     m_pairList.push_back(Pair(webview, popup));
49 }
50
51 void PermissionPopupManager::unregisterWebview(Evas_Object* webview)
52 {
53     WrtLogD("unegister");
54
55     FOREACH(it, m_pairList)
56     {
57         if (it->first == webview)
58         {
59             removePopup(it->second);
60             evas_object_del(it->second);
61
62             // erase iterator
63             PairList::iterator next = it;
64             ++next;
65             m_pairList.erase(it);
66             it = next;
67         }
68     }
69     removeWebview(webview);
70 }
71
72 void PermissionPopupManager::unregisterPopup(Evas_Object* popup)
73 {
74     WrtLogD("unegister popup");
75
76     Evas_Object* webview = NULL;
77
78     FOREACH(it, m_pairList)
79     {
80         if (it->second == popup)
81         {
82             webview = it->first;
83             removePopup(it->second);
84
85             // erase iterator
86             PairList::iterator next = it;
87             ++next;
88             m_pairList.erase(it);
89             it = next;
90         }
91     }
92
93     if (webview)
94     {
95         FOREACH(it, m_pairList)
96         {
97             if (it->first == webview)
98             {
99                 // PairList still has webview usage.
100                 // Do not clean-up webview data.
101                 return;
102             }
103         }
104         removeWebview(webview);
105     }
106 }
107
108 void PermissionPopupManager::addWebview(Evas_Object* webview)
109 {
110     evas_object_event_callback_add(webview, EVAS_CALLBACK_DEL, deleteWebviewCallback, this);
111 }
112
113 void PermissionPopupManager::addPopup(Evas_Object* popup)
114 {
115     evas_object_event_callback_add(popup, EVAS_CALLBACK_DEL, deletePopupCallback, this);
116 }
117
118 void PermissionPopupManager::removeWebview(Evas_Object* webview)
119 {
120     evas_object_event_callback_del(webview, EVAS_CALLBACK_DEL, deleteWebviewCallback);
121 }
122
123 void PermissionPopupManager::removePopup(Evas_Object* popup)
124 {
125     evas_object_event_callback_del(popup, EVAS_CALLBACK_DEL, deletePopupCallback);
126 }
127
128 void PermissionPopupManager::deleteWebviewCallback(void* data, Evas* e, Evas_Object* obj, void* eventInfo)
129 {
130     Assert(data);
131     Assert(obj);
132
133     DPL_UNUSED_PARAM(e);
134     DPL_UNUSED_PARAM(eventInfo);
135
136     PermissionPopupManager* This = static_cast<PermissionPopupManager*>(data);
137     This->unregisterWebview(obj);
138 }
139
140 void PermissionPopupManager::deletePopupCallback(void* data, Evas* e, Evas_Object* obj, void* eventInfo)
141 {
142     Assert(data);
143     Assert(obj);
144
145     DPL_UNUSED_PARAM(e);
146     DPL_UNUSED_PARAM(eventInfo);
147
148     PermissionPopupManager* This = static_cast<PermissionPopupManager*>(data);
149     This->unregisterPopup(obj);
150 }
151