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