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.
18 * @author Yunchan Cho (yunchan.cho@samsung.com)
22 #include <Plugin/IBoxPluginFactory.h>
23 #include "Buffer/IRenderBuffer.h"
24 #include "Buffer/RenderBuffer.h"
25 #include "Buffer/RenderBufferFactory.h"
28 #include "IBoxState.h"
30 #include "Util/ITimer.h"
31 #include "BoxUpdateTimer.h"
32 #include "BoxSchemeHandler.h"
35 // This is used for informing context of box to web content as value of url parameter
36 static const std::string renderTypeCreate("create");
37 static const std::string renderTypeResize("resize");
38 static const std::string renderTypeOpenPd("pdopen");
39 static const std::string renderTypeUpdate("update");
41 Box::Box(BoxInfoPtr boxInfo, IBoxPluginFactoryPtr factory, EwkContextPtr ewkContext)
46 , m_updateNeeded(false)
47 , m_lastUpdateRequestTime()
51 m_boxBuffer = m_factory->createBoxRenderBuffer(
56 boxInfo->contentInfo);
57 m_boxBuffer->allocate();
58 m_view = m_factory->createRenderView(
62 m_updateTimer = BoxUpdateTimer::create(
66 BoxSchemeHandler::Instance()->registerBox(boxInfo->instanceId, this);
68 // TODO code regarding state needs more testing
69 //m_state = BoxInitState::create(
70 // IBoxContextPtr(dynamic_cast<IBoxContext*>(this)));
79 BoxSchemeHandler::Instance()->unregisterBox(m_boxInfo->instanceId);
85 CHECK_BOX_STATE(m_state, permitShow);
88 m_updateTimer->start();
89 RenderInfoPtr renderInfo = makeRenderInfo(renderTypeCreate, URL_TYPE_BOX);
90 m_view->showBox(renderInfo);
102 CHECK_BOX_STATE(m_state, permitHide);
106 m_pdBuffer->stopCanvasUpdate();
112 m_updateTimer->stop();
113 m_boxBuffer->stopCanvasUpdate();
125 bool Box::resize(int width, int height)
128 CHECK_BOX_STATE(m_state, permitShow);
130 // reset box info to new width, height
131 m_boxInfo->boxWidth = width;
132 m_boxInfo->boxHeight = height;
135 m_updateTimer->restart();
136 m_boxBuffer->reallocate(
137 m_boxInfo->boxWidth, m_boxInfo->boxHeight);
138 RenderInfoPtr renderInfo = makeRenderInfo(renderTypeResize, URL_TYPE_BOX);
139 m_view->showBox(renderInfo);
141 LogD("resize exception");
152 CHECK_BOX_STATE(m_state, permitResume);
158 if (m_updateNeeded) {
159 m_updateNeeded = false;
172 bool Box::pause(bool background)
175 CHECK_BOX_STATE(m_state, permitPause);
179 m_currentTab = false;
191 bool Box::openPd(int width, int height, double x, double y)
194 CHECK_BOX_STATE(m_state, permitOpenPd);
196 m_boxInfo->pdWidth = width;
197 m_boxInfo->pdHeight = height;
202 m_updateTimer->stop();
203 m_pdBuffer = RenderBufferFactory::create(
204 RenderBufferFactory::RENDER_BUFFER_TYPE_PD,
206 m_boxInfo->instanceId,
208 m_boxInfo->pdHeight);
209 m_pdBuffer->allocate();
210 m_pdBuffer->stopCanvasUpdate();
211 RenderInfoPtr pdRenderInfo = makeRenderInfo(renderTypeOpenPd, URL_TYPE_PD);
212 RenderInfoPtr boxRenderInfo = makeRenderInfo(renderTypeOpenPd, URL_TYPE_BOX);
213 m_view->showPd(pdRenderInfo, boxRenderInfo);
214 ecore_idler_add(startUpdateRenderBufferIdlerCallback, m_pdBuffer.get());
226 CHECK_BOX_STATE(m_state, permitClosePd);
229 m_pdBuffer->stopCanvasUpdate();
233 m_updateTimer->restart();
238 // if box update is requested during pd opening
239 if (m_updateNeeded) {
247 bool Box::update(time_t requestTime, std::string& contentInfo)
251 m_lastUpdateRequestTime = requestTime;
252 m_boxInfo->contentInfo = contentInfo;
253 if (m_paused || m_pdBuffer) {
255 // until this box goes to current tab or pd is closed
256 m_updateNeeded = true;
269 bool Box::changePeriod(float period)
274 m_boxInfo->period = period;
275 m_updateTimer->setPeriod(m_boxInfo->period);
280 bool Box::isCurrentTab()
285 time_t Box::getLastUpdateRequestTime()
287 return m_lastUpdateRequestTime;
290 RenderInfoPtr Box::makeRenderInfo(const std::string& renderType, UrlType urlType) const
293 RenderInfoPtr renderInfo(new RenderInfo);
295 // add width, height, operation type
296 renderInfo->defaultUrlParams = "?type=" + renderType;
301 renderInfo->window = m_boxBuffer->getWindow();
302 renderInfo->width = m_boxInfo->boxWidth;
303 renderInfo->height = m_boxInfo->boxHeight;
306 renderInfo->window = m_pdBuffer->getWindow();
307 renderInfo->width = m_boxInfo->pdWidth;
308 renderInfo->height = m_boxInfo->pdHeight;
311 LogD("error url type");
312 return RenderInfoPtr();
316 sprintf(buff, "&width=%d&height=%d", renderInfo->width, renderInfo->height);
317 renderInfo->defaultUrlParams += buff;
319 // if needed, set pd information
320 if (renderType == renderTypeOpenPd) {
321 renderInfo->defaultUrlParams += "&pdopen-direction=";
322 if (m_boxInfo->pdY == 0.0f) {
323 renderInfo->defaultUrlParams += "down";
325 renderInfo->defaultUrlParams += "up";
329 sprintf(buff, "&pdopen-arrow-xpos=%d",
330 static_cast<int>((m_boxInfo->pdX) * (m_boxInfo->pdWidth)));
331 renderInfo->defaultUrlParams += buff;
336 if (!m_boxInfo->contentInfo.empty()) {
337 renderInfo->defaultUrlParams += "&" + m_boxInfo->contentInfo;
340 LogD("default url param string: %s", renderInfo->defaultUrlParams.c_str());
344 void Box::updateInternal()
347 RenderInfoPtr renderInfo = makeRenderInfo(renderTypeUpdate, URL_TYPE_BOX);
348 m_view->showBox(renderInfo);
351 void Box::setState(IBoxStatePtr state)
357 Eina_Bool Box::updateCallback(void* data)
360 Box* This = static_cast<Box*>(data);
362 This->updateInternal();
363 return ECORE_CALLBACK_RENEW;
366 Eina_Bool Box::startUpdateRenderBufferIdlerCallback(void* data)
369 RenderBuffer* buffer = static_cast<RenderBuffer*>(data);
373 buffer->startCanvasUpdate();
376 return ECORE_CALLBACK_CANCEL;