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 #define UPDATE_REQUEST_MIN_TIME 60 // seconds
34 BoxManager::BoxManager(IBoxPluginFactoryPtr factory)
35 : m_boxFactory(factory)
40 BoxManager::~BoxManager()
45 bool BoxManager::doCommand(const request_cmd_type type, const BoxInfoPtr& boxInfo)
50 case REQUEST_CMD_ADD_BOX:
51 result = requestAddBox(boxInfo, m_defaultContext);
53 case REQUEST_CMD_REMOVE_BOX:
54 result = requestRemoveBox(boxInfo->instanceId);
56 case REQUEST_CMD_RESIZE_BOX:
57 result = requestResizeBox(boxInfo->instanceId, boxInfo->boxWidth, boxInfo->boxHeight);
59 case REQUEST_CMD_RESUME_BOX:
60 result = requestResumeBox(boxInfo->instanceId);
62 case REQUEST_CMD_PAUSE_BOX:
63 result = requestPauseBox(boxInfo->instanceId);
65 case REQUEST_CMD_RESUME_ALL:
66 result = requestResumeAll();
68 case REQUEST_CMD_PAUSE_ALL:
69 result = requestPauseAll();
71 case REQUEST_CMD_OPEN_PD:
72 result = requestOpenPd(boxInfo->instanceId,
73 boxInfo->pdWidth, boxInfo->pdHeight,
74 boxInfo->pdX, boxInfo->pdY);
76 case REQUEST_CMD_CLOSE_PD:
77 result = requestClosePd(boxInfo->instanceId);
79 case REQUEST_CMD_CHANGE_PERIOD:
80 result = requestChangePeriod(boxInfo->instanceId, boxInfo->period);
82 case REQUEST_CMD_UPDATE_BOX:
83 result = requestUpdateBox(boxInfo->boxId, boxInfo->contentInfo);
86 LogD("not available request type");
93 bool BoxManager::requestAddBox(BoxInfoPtr boxInfo, EwkContextPtr ewkContext)
100 if (!m_defaultContext) {
101 m_defaultContext = EwkContextPtr(ewk_context_new(), EwkContextDeleter());
103 ewkContext = m_defaultContext;
105 box = Box::create(boxInfo, m_boxFactory, ewkContext);
107 LogD("exection occurs during adding box");
113 LogD("problem occurs during rendering box");
117 insertBoxMap(boxInfo->instanceId, box);
121 bool BoxManager::requestRemoveBox(std::string& instanceId)
123 IBoxPtr box = searchBoxMap(instanceId);
132 eraseBoxMap(instanceId);
136 bool BoxManager::requestResizeBox(std::string& instanceId, int width, int height)
139 IBoxPtr box = searchBoxMap(instanceId);
144 return box->resize(width, height);
147 bool BoxManager::requestResumeBox(std::string& instanceId)
150 IBoxPtr box = searchBoxMap(instanceId);
155 return box->resume();
158 bool BoxManager::requestPauseBox(std::string& instanceId)
161 IBoxPtr box = searchBoxMap(instanceId);
166 // paused by switching other page
167 return box->pause(false);
170 bool BoxManager::requestResumeAll()
173 for (auto it = m_boxMap.begin(); it != m_boxMap.end(); it++) {
174 if (it->second->isCurrentTab()) {
175 it->second->resume();
182 bool BoxManager::requestPauseAll()
185 for (auto it = m_boxMap.begin(); it != m_boxMap.end(); it++) {
186 if (it->second->isCurrentTab()) {
187 // paused by entering background
188 it->second->pause(true);
195 bool BoxManager::requestOpenPd(
196 std::string& instanceId,
197 int width, int height, double x, double y)
200 IBoxPtr box = searchBoxMap(instanceId);
205 return box->openPd(width, height, x, y);
208 bool BoxManager::requestClosePd(std::string& instanceId)
211 IBoxPtr box = searchBoxMap(instanceId);
216 return box->closePd();
219 bool BoxManager::requestChangePeriod(std::string& instanceId, float period)
222 IBoxPtr box = searchBoxMap(instanceId);
227 return box->changePeriod(period);
230 bool BoxManager::requestUpdateBox(std::string& boxId, std::string& contentInfo)
237 time_t requestTime = time(NULL);
239 for (auto it = m_boxMap.begin(); it != m_boxMap.end(); ++it) {
240 if (it->first.find(boxId) == std::string::npos) {
244 int interval = requestTime - box->getLastUpdateRequestTime();
245 if (interval < UPDATE_REQUEST_MIN_TIME) {
246 LogD("this update request is skipped");
249 // set last request time to new one
250 box->update(requestTime, contentInfo);
256 void BoxManager::insertBoxMap(std::string& instanceId, IBoxPtr box)
258 if (!searchBoxMap(instanceId)) {
259 LogD("insert box to map: %s", instanceId.c_str());
260 m_boxMap.insert(BoxMapPair(instanceId, box));
262 LogD("this box was already inserted!");
266 void BoxManager::eraseBoxMap(std::string& instanceId)
268 LogD("erase box to map");
269 if (!searchBoxMap(instanceId)) {
270 LogD("not available box");
274 m_boxMap.erase(instanceId);
277 void BoxManager::updateBoxMap(std::string& instanceId, IBoxPtr box)
279 if (searchBoxMap(instanceId)) {
280 eraseBoxMap(instanceId);
283 insertBoxMap(instanceId, box);
286 IBoxPtr BoxManager::searchBoxMap(std::string& instanceId)
291 auto it = m_boxMap.find(instanceId);
292 if (it != m_boxMap.end()) {
293 LogD("found box: %s (%p)", it->first.c_str(), it->second.get());
300 void BoxManager::clearBoxMap()
305 void BoxManager::EwkContextDeleter::operator()(Ewk_Context* ptr)
307 LogD("ewk context delete");
309 ewk_context_delete(ptr);