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"
27 #include "Util/Util.h"
29 #include "IBoxState.h"
31 #include "Util/ITimer.h"
32 #include "BoxUpdateTimer.h"
33 #include "BoxSchemeHandler.h"
36 // This is used for informing context of box to web content as value of url parameter
37 static const std::string renderTypeCreate("create");
38 static const std::string renderTypeResize("resize");
39 static const std::string renderTypeOpenPd("pdopen");
40 static const std::string renderTypeUpdate("update");
42 Box::Box(BoxInfoPtr boxInfo, IBoxPluginFactoryPtr factory, EwkContextPtr ewkContext)
47 , m_updateNeeded(false)
48 , m_lastUpdateRequestTime()
52 m_boxBuffer = m_factory->createBoxRenderBuffer(
57 boxInfo->contentInfo);
58 m_boxBuffer->allocate();
59 m_view = m_factory->createRenderView(
63 m_updateTimer = BoxUpdateTimer::create(
67 BoxSchemeHandler::Instance()->registerBox(boxInfo->instanceId, this);
69 // TODO code regarding state needs more testing
70 //m_state = BoxInitState::create(
71 // IBoxContextPtr(dynamic_cast<IBoxContext*>(this)));
80 BoxSchemeHandler::Instance()->unregisterBox(m_boxInfo->instanceId);
86 CHECK_BOX_STATE(m_state, permitShow);
89 m_updateTimer->start();
90 RenderInfoPtr renderInfo = makeRenderInfo(renderTypeCreate, URL_TYPE_BOX);
91 m_view->showBox(renderInfo);
103 CHECK_BOX_STATE(m_state, permitHide);
107 m_pdBuffer->stopCanvasUpdate();
113 m_updateTimer->stop();
114 m_boxBuffer->stopCanvasUpdate();
126 bool Box::resize(int width, int height)
129 CHECK_BOX_STATE(m_state, permitShow);
131 // reset box info to new width, height
132 m_boxInfo->boxWidth = width;
133 m_boxInfo->boxHeight = height;
136 m_updateTimer->restart();
137 m_boxBuffer->reallocate(
138 m_boxInfo->boxWidth, m_boxInfo->boxHeight);
139 RenderInfoPtr renderInfo = makeRenderInfo(renderTypeResize, URL_TYPE_BOX);
140 m_view->showBox(renderInfo);
142 LogD("resize exception");
153 CHECK_BOX_STATE(m_state, permitResume);
159 if (m_updateNeeded) {
160 m_updateNeeded = false;
173 bool Box::pause(bool background)
176 CHECK_BOX_STATE(m_state, permitPause);
180 m_currentTab = false;
192 bool Box::openPd(int width, int height, double x, double y)
195 CHECK_BOX_STATE(m_state, permitOpenPd);
197 m_boxInfo->pdWidth = width;
198 m_boxInfo->pdHeight = height;
203 m_updateTimer->stop();
204 m_pdBuffer = RenderBufferFactory::create(
205 RenderBufferFactory::RENDER_BUFFER_TYPE_PD,
207 m_boxInfo->instanceId,
209 m_boxInfo->pdHeight);
210 m_pdBuffer->allocate();
211 m_pdBuffer->stopCanvasUpdate();
212 RenderInfoPtr pdRenderInfo = makeRenderInfo(renderTypeOpenPd, URL_TYPE_PD);
213 RenderInfoPtr boxRenderInfo = makeRenderInfo(renderTypeOpenPd, URL_TYPE_BOX);
214 m_view->showPd(pdRenderInfo, boxRenderInfo);
215 ecore_idler_add(startUpdateRenderBufferIdlerCallback, m_pdBuffer.get());
227 CHECK_BOX_STATE(m_state, permitClosePd);
231 m_pdBuffer->stopCanvasUpdate();
236 m_updateTimer->restart();
241 // if box update is requested during pd opening
242 if (m_updateNeeded) {
250 bool Box::update(time_t requestTime, std::string& contentInfo)
254 m_lastUpdateRequestTime = requestTime;
255 m_boxInfo->contentInfo = contentInfo;
256 if (m_paused || m_pdBuffer) {
258 // until this box goes to current tab or pd is closed
259 m_updateNeeded = true;
272 bool Box::changePeriod(float period)
277 m_boxInfo->period = period;
278 m_updateTimer->setPeriod(m_boxInfo->period);
283 bool Box::isCurrentTab()
288 time_t Box::getLastUpdateRequestTime()
290 return m_lastUpdateRequestTime;
293 RenderInfoPtr Box::makeRenderInfo(const std::string& renderType, UrlType urlType) const
296 RenderInfoPtr renderInfo(new RenderInfo);
298 // add width, height, operation type
299 renderInfo->defaultUrlParams = "?type=" + renderType;
304 renderInfo->window = m_boxBuffer->getWindow();
305 renderInfo->width = m_boxInfo->boxWidth;
306 renderInfo->height = m_boxInfo->boxHeight;
309 renderInfo->window = m_pdBuffer->getWindow();
310 renderInfo->width = m_boxInfo->pdWidth;
311 renderInfo->height = m_boxInfo->pdHeight;
314 LogD("error url type");
315 return RenderInfoPtr();
319 sprintf(buff, "&width=%d&height=%d", renderInfo->width, renderInfo->height);
320 renderInfo->defaultUrlParams += buff;
322 // if needed, set pd information
323 if (renderType == renderTypeOpenPd) {
324 renderInfo->defaultUrlParams += "&pdopen-direction=";
325 if (m_boxInfo->pdY == 0.0f) {
326 renderInfo->defaultUrlParams += "down";
328 renderInfo->defaultUrlParams += "up";
332 sprintf(buff, "&pdopen-arrow-xpos=%d",
333 static_cast<int>((m_boxInfo->pdX) * (m_boxInfo->pdWidth)));
334 renderInfo->defaultUrlParams += buff;
339 if (!m_boxInfo->contentInfo.empty()) {
340 renderInfo->defaultUrlParams += "&" + m_boxInfo->contentInfo;
343 LogD("default url param string: %s", renderInfo->defaultUrlParams.c_str());
347 void Box::updateInternal()
350 RenderInfoPtr renderInfo = makeRenderInfo(renderTypeUpdate, URL_TYPE_BOX);
351 m_view->showBox(renderInfo);
354 void Box::setState(IBoxStatePtr state)
361 Eina_Bool Box::updateCallback(void* data)
364 Box* This = static_cast<Box*>(data);
366 This->updateInternal();
367 return ECORE_CALLBACK_RENEW;
370 Eina_Bool Box::startUpdateRenderBufferIdlerCallback(void* data)
373 RenderBuffer* buffer = static_cast<RenderBuffer*>(data);
377 buffer->startCanvasUpdate();
380 return ECORE_CALLBACK_CANCEL;
383 BoxInfoPtr Box::getBoxInfo()