2 * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://floralicense.org/license/
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.
17 * @file BoxManager.cpp
18 * @author Yunchan Cho (yunchan.cho@samsung.com)
23 #include <ewk_context.h>
24 #include <Plugin/IBoxPluginFactory.h>
25 #include <Plugin/box_plugin_interface.h>
30 #include "BoxManager.h"
32 BoxManager::BoxManager(IBoxPluginFactoryPtr factory)
33 : m_boxFactory(factory)
38 BoxManager::~BoxManager()
43 bool BoxManager::doCommand(const request_cmd_type type, const BoxInfoPtr& boxInfo)
48 case REQUEST_CMD_ADD_BOX:
49 result = requestAddBox(boxInfo, m_defaultContext);
51 case REQUEST_CMD_REMOVE_BOX:
52 result = requestRemoveBox(boxInfo->instanceId);
54 case REQUEST_CMD_RESIZE_BOX:
55 result = requestResizeBox(boxInfo->instanceId, boxInfo->boxWidth, boxInfo->boxHeight);
57 case REQUEST_CMD_RESUME_BOX:
58 result = requestResumeBox(boxInfo->instanceId);
60 case REQUEST_CMD_PAUSE_BOX:
61 result = requestPauseBox(boxInfo->instanceId);
63 case REQUEST_CMD_RESUME_ALL:
64 result = requestResumeAll();
66 case REQUEST_CMD_PAUSE_ALL:
67 result = requestPauseAll();
69 case REQUEST_CMD_OPEN_PD:
70 result = requestOpenPd(boxInfo->instanceId,
71 boxInfo->pdWidth, boxInfo->pdHeight,
72 boxInfo->pdX, boxInfo->pdY);
74 case REQUEST_CMD_CLOSE_PD:
75 result = requestClosePd(boxInfo->instanceId);
77 case REQUEST_CMD_CHANGE_PERIOD:
78 result = requestChangePeriod(boxInfo->instanceId, boxInfo->period);
80 case REQUEST_CMD_UPDATE_BOX:
81 result = requestUpdateBox(boxInfo->boxId, boxInfo->contentInfo);
84 LogD("not available request type");
91 bool BoxManager::requestAddBox(BoxInfoPtr boxInfo, EwkContextPtr ewkContext)
98 if (!m_defaultContext) {
99 m_defaultContext = EwkContextPtr(ewk_context_new(), EwkContextDeleter());
101 ewkContext = m_defaultContext;
103 box = Box::create(boxInfo, m_boxFactory, ewkContext);
105 LogD("exection occurs during adding box");
111 LogD("problem occurs during rendering box");
115 insertBoxMap(boxInfo->instanceId, box);
119 bool BoxManager::requestRemoveBox(std::string& instanceId)
121 IBoxPtr box = searchBoxMap(instanceId);
130 eraseBoxMap(instanceId);
134 bool BoxManager::requestResizeBox(std::string& instanceId, int width, int height)
137 IBoxPtr box = searchBoxMap(instanceId);
142 return box->resize(width, height);
145 bool BoxManager::requestResumeBox(std::string& instanceId)
148 IBoxPtr box = searchBoxMap(instanceId);
153 return box->resume();
156 bool BoxManager::requestPauseBox(std::string& instanceId)
159 IBoxPtr box = searchBoxMap(instanceId);
164 // paused by switching other page
165 return box->pause(false);
168 bool BoxManager::requestResumeAll()
171 for (auto it = m_boxMap.begin(); it != m_boxMap.end(); it++) {
172 if (it->second->isCurrentTab()) {
173 it->second->resume();
180 bool BoxManager::requestPauseAll()
183 for (auto it = m_boxMap.begin(); it != m_boxMap.end(); it++) {
184 if (it->second->isCurrentTab()) {
185 // paused by entering background
186 it->second->pause(true);
193 bool BoxManager::requestOpenPd(
194 std::string& instanceId,
195 int width, int height, double x, double y)
198 IBoxPtr box = searchBoxMap(instanceId);
203 return box->openPd(width, height, x, y);
206 bool BoxManager::requestClosePd(std::string& instanceId)
209 IBoxPtr box = searchBoxMap(instanceId);
214 return box->closePd();
217 bool BoxManager::requestChangePeriod(std::string& instanceId, float period)
220 IBoxPtr box = searchBoxMap(instanceId);
225 return box->changePeriod(period);
228 bool BoxManager::requestUpdateBox(std::string& boxId, std::string& contentInfo)
235 time_t requestTime = time(NULL);
237 for (auto it = m_boxMap.begin(); it != m_boxMap.end(); ++it) {
238 if (it->first.find(boxId) == std::string::npos) {
242 box->update(requestTime, contentInfo);
248 void BoxManager::insertBoxMap(std::string& instanceId, IBoxPtr box)
250 if (!searchBoxMap(instanceId)) {
251 LogD("insert box to map: %s", instanceId.c_str());
252 m_boxMap.insert(BoxMapPair(instanceId, box));
254 LogD("this box was already inserted!");
258 void BoxManager::eraseBoxMap(std::string& instanceId)
260 LogD("erase box to map");
261 if (!searchBoxMap(instanceId)) {
262 LogD("not available box");
266 m_boxMap.erase(instanceId);
269 void BoxManager::updateBoxMap(std::string& instanceId, IBoxPtr box)
271 if (searchBoxMap(instanceId)) {
272 eraseBoxMap(instanceId);
275 insertBoxMap(instanceId, box);
278 IBoxPtr BoxManager::searchBoxMap(std::string& instanceId)
283 auto it = m_boxMap.find(instanceId);
284 if (it != m_boxMap.end()) {
285 LogD("found box: %s (%p)", it->first.c_str(), it->second.get());
292 void BoxManager::clearBoxMap()
297 void BoxManager::EwkContextDeleter::operator()(Ewk_Context* ptr)
299 LogD("ewk context delete");
301 ewk_context_delete(ptr);