Fix not web liveboxes of other tabs to be rendered when web-provider goes to foreground
[platform/framework/web/web-provider.git] / src / Core / BoxManager.cpp
1 /*
2  * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Flora License, Version 1.1 (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://floralicense.org/license/
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    BoxManager.cpp
18  * @author  Yunchan Cho (yunchan.cho@samsung.com)
19  */
20 #include <map>
21 #include <ewk_context.h>
22 #include <Plugin/IBoxPluginFactory.h>
23 #include <Plugin/box_plugin_interface.h>
24 #include "Util/Log.h"
25 #include "IBox.h"
26 #include "Box.h"
27 #include "BoxData.h"
28 #include "BoxManager.h"
29
30 BoxManager::BoxManager(IBoxPluginFactoryPtr factory)
31     : m_boxFactory(factory)
32 {
33     LogD("enter");
34 }
35
36 BoxManager::~BoxManager()
37 {
38     LogD("enter");
39 }
40
41 bool BoxManager::doCommand(const request_cmd_type type, const BoxInfoPtr& boxInfo)
42 {
43     bool result = false;
44
45     switch (type) {
46     case REQUEST_CMD_ADD_BOX:
47         result = requestAddBox(boxInfo, m_defaultContext);
48         break;
49     case REQUEST_CMD_REMOVE_BOX:
50         result = requestRemoveBox(boxInfo->instanceId);
51         break;
52     case REQUEST_CMD_RESIZE_BOX:
53         result = requestResizeBox(boxInfo->instanceId, boxInfo->boxWidth, boxInfo->boxHeight);
54         break;
55     case REQUEST_CMD_RESUME_BOX:
56         result = requestResumeBox(boxInfo->instanceId);
57         break;
58     case REQUEST_CMD_PAUSE_BOX:
59         result = requestPauseBox(boxInfo->instanceId);
60         break;
61     case REQUEST_CMD_RESUME_ALL:
62         result = requestResumeAll();
63         break;
64     case REQUEST_CMD_PAUSE_ALL:
65         result = requestPauseAll();
66         break;
67     case REQUEST_CMD_OPEN_PD:
68         result = requestOpenPd(boxInfo->instanceId,
69                     boxInfo->pdWidth, boxInfo->pdHeight,
70                     boxInfo->pdX, boxInfo->pdY);
71         break;
72     case REQUEST_CMD_CLOSE_PD:
73         result = requestClosePd(boxInfo->instanceId);
74         break;
75     case REQUEST_CMD_CHANGE_PERIOD:
76         result = requestChangePeriod(boxInfo->instanceId, boxInfo->period);
77         break;
78     case REQUEST_CMD_UPDATE_BOX:
79         result = requestUpdateBox(boxInfo->instanceId);
80         break;
81     default:
82         LogD("not available request type");
83         break;
84     }
85
86     return result;
87 }
88
89 bool BoxManager::requestAddBox(BoxInfoPtr boxInfo, EwkContextPtr ewkContext)
90 {
91     IBoxPtr box;
92
93     // create new box
94     try {
95         if (!ewkContext) {
96             if (!m_defaultContext) {
97                 m_defaultContext = EwkContextPtr(ewk_context_new(), EwkContextDeleter());
98             }
99             ewkContext = m_defaultContext;
100         }
101         box = Box::create(boxInfo, m_boxFactory, ewkContext); 
102     } catch (...) {
103         LogD("exection occurs during adding box");
104         return false;
105     }
106
107     // show new box
108     if (!box->show()) {
109         LogD("problem occurs during rendering box");
110         return false;
111     }
112
113     insertBoxMap(boxInfo->instanceId, box); 
114     return true;
115 }
116
117 bool BoxManager::requestRemoveBox(std::string& instanceId)
118 {
119     IBoxPtr box = searchBoxMap(instanceId);
120     if (!box) {
121         return false;
122     }
123     
124     if (!box->hide()) { 
125         return false;
126     }
127
128     eraseBoxMap(instanceId);
129     return true;
130 }
131
132 bool BoxManager::requestResizeBox(std::string& instanceId, int width, int height)
133 {
134     LogD("enter");
135     IBoxPtr box = searchBoxMap(instanceId);
136     if (!box) {
137         return false;
138     }
139     
140     return box->resize(width, height);
141 }
142
143 bool BoxManager::requestResumeBox(std::string& instanceId)
144 {
145     LogD("enter");
146     IBoxPtr box = searchBoxMap(instanceId);
147     if (!box) {
148         return false;
149     }
150     
151     return box->resume();
152 }
153
154 bool BoxManager::requestPauseBox(std::string& instanceId)
155 {
156     LogD("enter");
157     IBoxPtr box = searchBoxMap(instanceId);
158     if (!box) {
159         return false;
160     }
161     
162     // paused by switching other page
163     return box->pause(false);
164 }
165
166 bool BoxManager::requestResumeAll()
167 {
168     LogD("enter");
169     for (auto it = m_boxMap.begin(); it != m_boxMap.end(); it++) {
170         if (it->second->isCurrentTab()) {
171             it->second->resume();
172         }
173     }
174
175     return true;
176 }
177
178 bool BoxManager::requestPauseAll()
179 {
180     LogD("enter");
181     for (auto it = m_boxMap.begin(); it != m_boxMap.end(); it++) {
182         if (it->second->isCurrentTab()) {
183             // paused by entering background
184             it->second->pause(true);
185         }
186     }
187
188     return true;
189 }
190
191 bool BoxManager::requestOpenPd(
192         std::string& instanceId,
193         int width, int height, double x, double y)
194 {
195     LogD("enter");
196     IBoxPtr box = searchBoxMap(instanceId);
197     if (!box) {
198         return false;
199     }
200     
201     return box->openPd(width, height, x, y);
202 }
203
204 bool BoxManager::requestClosePd(std::string& instanceId)
205 {
206     LogD("enter");
207     IBoxPtr box = searchBoxMap(instanceId);
208     if (!box) {
209         return false;
210     }
211
212     return box->closePd();
213 }
214
215 bool BoxManager::requestChangePeriod(std::string& instanceId, float period)
216 {
217     LogD("enter");
218     IBoxPtr box = searchBoxMap(instanceId);
219     if (!box) {
220         return false;
221     }
222
223     return box->changePeriod(period);
224 }
225
226 bool BoxManager::requestUpdateBox(std::string& instanceId)
227 {
228     LogD("enter");
229     IBoxPtr box = searchBoxMap(instanceId);
230     if (!box) {
231         return false;
232     }
233
234     return box->update();
235 }
236
237 void BoxManager::insertBoxMap(std::string& instanceId, IBoxPtr box)
238 {
239     if (!searchBoxMap(instanceId)) {
240         LogD("insert box to map: %s", instanceId.c_str());
241         m_boxMap.insert(BoxMapPair(instanceId, box));
242     } else {
243         LogD("this box was already inserted!");
244     }
245 }
246
247 void BoxManager::eraseBoxMap(std::string& instanceId)
248 {
249     LogD("erase box to map");
250     if (!searchBoxMap(instanceId)) {
251         LogD("not available box");
252         return;
253     }
254
255     m_boxMap.erase(instanceId);
256 }
257
258 void BoxManager::updateBoxMap(std::string& instanceId, IBoxPtr box)
259 {
260     if (searchBoxMap(instanceId)) {
261         eraseBoxMap(instanceId);
262     }
263
264     insertBoxMap(instanceId, box);
265 }
266
267 IBoxPtr BoxManager::searchBoxMap(std::string& instanceId)
268 {
269     LogD("enter");
270     IBoxPtr box;
271     box.reset();
272     auto it = m_boxMap.find(instanceId);
273     if (it != m_boxMap.end()) {
274         LogD("found box: %s (%p)", it->first.c_str(), it->second.get());
275         box = it->second;
276     }
277
278     return box;
279 }
280
281 void BoxManager::clearBoxMap()
282 {
283     m_boxMap.clear();
284 }
285
286 void BoxManager::EwkContextDeleter::operator()(Ewk_Context* ptr)
287 {
288     LogD("ewk context delete");
289     if (ptr) {
290         ewk_context_delete(ptr);
291     }
292 }
293