[Release] livebox.web-provider-1.9
[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.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://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, boxInfo->pdWidth, boxInfo->pdHeight);
69         break;
70     case REQUEST_CMD_CLOSE_PD:
71         result = requestClosePd(boxInfo->instanceId);
72         break;
73     case REQUEST_CMD_CHANGE_PERIOD:
74         result = requestChangePeriod(boxInfo->instanceId, boxInfo->period);
75         break;
76     default:
77         LogD("not available request type");
78         break;
79     }
80
81     return result;
82 }
83
84 bool BoxManager::requestAddBox(BoxInfoPtr boxInfo, EwkContextPtr ewkContext)
85 {
86     IBoxPtr box;
87
88     // create new box
89     try {
90         if (!ewkContext) {
91             if (!m_defaultContext) {
92                 m_defaultContext = EwkContextPtr(ewk_context_new(), EwkContextDeleter());
93             }
94             ewkContext = m_defaultContext;
95         }
96         box = Box::create(boxInfo, m_boxFactory, ewkContext); 
97     } catch (...) {
98         LogD("exection occurs during adding box");
99         return false;
100     }
101
102     // show new box
103     if (!box->show()) {
104         LogD("problem occurs during rendering box");
105         return false;
106     }
107
108     insertBoxMap(boxInfo->instanceId, box); 
109     return true;
110 }
111
112 bool BoxManager::requestRemoveBox(std::string& instanceId)
113 {
114     IBoxPtr box = searchBoxMap(instanceId);
115     if (!box) {
116         return false;
117     }
118     
119     if (!box->hide()) { 
120         return false;
121     }
122
123     eraseBoxMap(instanceId);
124     return true;
125 }
126
127 bool BoxManager::requestResizeBox(std::string& instanceId, int width, int height)
128 {
129     LogD("enter");
130     IBoxPtr box = searchBoxMap(instanceId);
131     if (!box) {
132         return false;
133     }
134     
135     return box->resize(width, height);
136 }
137
138 bool BoxManager::requestResumeBox(std::string& instanceId)
139 {
140     LogD("enter");
141     IBoxPtr box = searchBoxMap(instanceId);
142     if (!box) {
143         return false;
144     }
145     
146     return box->resume();
147 }
148
149 bool BoxManager::requestPauseBox(std::string& instanceId)
150 {
151     LogD("enter");
152     IBoxPtr box = searchBoxMap(instanceId);
153     if (!box) {
154         return false;
155     }
156     
157     return box->pause();
158 }
159
160 bool BoxManager::requestResumeAll()
161 {
162     LogD("enter");
163     for (auto it = m_boxMap.begin(); it != m_boxMap.end(); it++) {
164         it->second->resume();
165     }
166
167     return true;
168 }
169
170 bool BoxManager::requestPauseAll()
171 {
172     LogD("enter");
173     for (auto it = m_boxMap.begin(); it != m_boxMap.end(); it++) {
174         it->second->pause();
175     }
176
177     return true;
178 }
179
180 bool BoxManager::requestOpenPd(std::string& instanceId, int width, int height)
181 {
182     LogD("enter");
183     IBoxPtr box = searchBoxMap(instanceId);
184     if (!box) {
185         return false;
186     }
187     
188     return box->openPd(width, height);
189 }
190
191 bool BoxManager::requestClosePd(std::string& instanceId)
192 {
193     LogD("enter");
194     IBoxPtr box = searchBoxMap(instanceId);
195     if (!box) {
196         return false;
197     }
198
199     return box->closePd();
200 }
201
202 bool BoxManager::requestChangePeriod(std::string& instanceId, float period)
203 {
204     LogD("enter");
205     IBoxPtr box = searchBoxMap(instanceId);
206     if (!box) {
207         return false;
208     }
209
210     return box->changePeriod(period);
211 }
212
213 void BoxManager::insertBoxMap(std::string& instanceId, IBoxPtr box)
214 {
215     if (!searchBoxMap(instanceId)) {
216         LogD("insert box to map: %s", instanceId.c_str());
217         m_boxMap.insert(BoxMapPair(instanceId, box));
218     } else {
219         LogD("this box was already inserted!");
220     }
221 }
222
223 void BoxManager::eraseBoxMap(std::string& instanceId)
224 {
225     LogD("erase box to map");
226     if (!searchBoxMap(instanceId)) {
227         LogD("not available box");
228         return;
229     }
230
231     m_boxMap.erase(instanceId);
232 }
233
234 void BoxManager::updateBoxMap(std::string& instanceId, IBoxPtr box)
235 {
236     if (searchBoxMap(instanceId)) {
237         eraseBoxMap(instanceId);
238     }
239
240     insertBoxMap(instanceId, box);
241 }
242
243 IBoxPtr BoxManager::searchBoxMap(std::string& instanceId)
244 {
245     LogD("enter");
246     IBoxPtr box;
247     box.reset();
248     auto it = m_boxMap.find(instanceId);
249     if (it != m_boxMap.end()) {
250         LogD("found box: %s (%p)", it->first.c_str(), it->second.get());
251         box = it->second;
252     }
253
254     return box;
255 }
256
257 void BoxManager::clearBoxMap()
258 {
259     m_boxMap.clear();
260 }
261
262 void BoxManager::EwkContextDeleter::operator()(Ewk_Context* ptr)
263 {
264     LogD("ewk context delete");
265     if (ptr) {
266         ewk_context_delete(ptr);
267     }
268 }
269