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