Support external request regarding updating content of Box
[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     return box->pause();
163 }
164
165 bool BoxManager::requestResumeAll()
166 {
167     LogD("enter");
168     for (auto it = m_boxMap.begin(); it != m_boxMap.end(); it++) {
169         it->second->resume();
170     }
171
172     return true;
173 }
174
175 bool BoxManager::requestPauseAll()
176 {
177     LogD("enter");
178     for (auto it = m_boxMap.begin(); it != m_boxMap.end(); it++) {
179         it->second->pause();
180     }
181
182     return true;
183 }
184
185 bool BoxManager::requestOpenPd(
186         std::string& instanceId,
187         int width, int height, double x, double y)
188 {
189     LogD("enter");
190     IBoxPtr box = searchBoxMap(instanceId);
191     if (!box) {
192         return false;
193     }
194     
195     return box->openPd(width, height, x, y);
196 }
197
198 bool BoxManager::requestClosePd(std::string& instanceId)
199 {
200     LogD("enter");
201     IBoxPtr box = searchBoxMap(instanceId);
202     if (!box) {
203         return false;
204     }
205
206     return box->closePd();
207 }
208
209 bool BoxManager::requestChangePeriod(std::string& instanceId, float period)
210 {
211     LogD("enter");
212     IBoxPtr box = searchBoxMap(instanceId);
213     if (!box) {
214         return false;
215     }
216
217     return box->changePeriod(period);
218 }
219
220 bool BoxManager::requestUpdateBox(std::string& instanceId)
221 {
222     LogD("enter");
223     IBoxPtr box = searchBoxMap(instanceId);
224     if (!box) {
225         return false;
226     }
227
228     return box->update();
229 }
230
231 void BoxManager::insertBoxMap(std::string& instanceId, IBoxPtr box)
232 {
233     if (!searchBoxMap(instanceId)) {
234         LogD("insert box to map: %s", instanceId.c_str());
235         m_boxMap.insert(BoxMapPair(instanceId, box));
236     } else {
237         LogD("this box was already inserted!");
238     }
239 }
240
241 void BoxManager::eraseBoxMap(std::string& instanceId)
242 {
243     LogD("erase box to map");
244     if (!searchBoxMap(instanceId)) {
245         LogD("not available box");
246         return;
247     }
248
249     m_boxMap.erase(instanceId);
250 }
251
252 void BoxManager::updateBoxMap(std::string& instanceId, IBoxPtr box)
253 {
254     if (searchBoxMap(instanceId)) {
255         eraseBoxMap(instanceId);
256     }
257
258     insertBoxMap(instanceId, box);
259 }
260
261 IBoxPtr BoxManager::searchBoxMap(std::string& instanceId)
262 {
263     LogD("enter");
264     IBoxPtr box;
265     box.reset();
266     auto it = m_boxMap.find(instanceId);
267     if (it != m_boxMap.end()) {
268         LogD("found box: %s (%p)", it->first.c_str(), it->second.get());
269         box = it->second;
270     }
271
272     return box;
273 }
274
275 void BoxManager::clearBoxMap()
276 {
277     m_boxMap.clear();
278 }
279
280 void BoxManager::EwkContextDeleter::operator()(Ewk_Context* ptr)
281 {
282     LogD("ewk context delete");
283     if (ptr) {
284         ewk_context_delete(ptr);
285     }
286 }
287