Fix various build warnings
[platform/framework/web/web-provider.git] / src / Core / Box.cpp
index 1f62aa5..526309f 100644 (file)
@@ -1,11 +1,11 @@
 /*
  * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
  *
- *    Licensed under the Apache License, Version 2.0 (the "License");
+ *    Licensed under the Flora License, Version 1.1 (the "License");
  *    you may not use this file except in compliance with the License.
  *    You may obtain a copy of the License at
  *
- *        http://www.apache.org/licenses/LICENSE-2.0
+ *        http://floralicense.org/license/
  *
  *    Unless required by applicable law or agreed to in writing, software
  *    distributed under the License is distributed on an "AS IS" BASIS,
  * @author  Yunchan Cho (yunchan.cho@samsung.com)
  */
 #include <string>
+#include <Ecore.h>
 #include <Plugin/IBoxPluginFactory.h>
 #include "Buffer/IRenderBuffer.h"
+#include "Buffer/RenderBuffer.h"
 #include "Buffer/RenderBufferFactory.h"
 #include "Util/Log.h"
+#include "Util/Util.h"
 #include "BoxData.h"
 #include "IBoxState.h"
 #include "BoxState.h"
@@ -39,6 +42,10 @@ static const std::string renderTypeUpdate("update");
 Box::Box(BoxInfoPtr boxInfo, IBoxPluginFactoryPtr factory, EwkContextPtr ewkContext)
     : m_boxInfo(boxInfo)
     , m_factory(factory)
+    , m_currentTab(true)
+    , m_paused(false)
+    , m_updateNeeded(false)
+    , m_lastUpdateRequestTime()
 {
     LogD("enter");
     try {
@@ -50,8 +57,8 @@ Box::Box(BoxInfoPtr boxInfo, IBoxPluginFactoryPtr factory, EwkContextPtr ewkCont
                             boxInfo->contentInfo);
         m_boxBuffer->allocate();
         m_view = m_factory->createRenderView(
-                boxInfo->boxId, boxInfo->instanceId,
-                m_boxBuffer->getWindow(),
+                boxInfo->boxId,
+                boxInfo->instanceId,
                 ewkContext);
         m_updateTimer = BoxUpdateTimer::create(
                 boxInfo->period,
@@ -80,8 +87,7 @@ bool Box::show()
 
     try {
         m_updateTimer->start();
-        m_boxBuffer->startCanvasUpdate();
-        RenderInfoPtr renderInfo = makeRenderInfo(renderTypeCreate);
+        RenderInfoPtr renderInfo = makeRenderInfo(renderTypeCreate, URL_TYPE_BOX);
         m_view->showBox(renderInfo);
     } catch (...) {
         return false;
@@ -97,9 +103,18 @@ bool Box::hide()
     CHECK_BOX_STATE(m_state, permitHide);
 
     try {
+        if (m_pdBuffer) {
+            m_pdBuffer->stopCanvasUpdate();
+            m_view->hidePd();
+            m_pdBuffer->free();
+            m_pdBuffer.reset();
+         }
+
         m_updateTimer->stop();
+        m_boxBuffer->stopCanvasUpdate();
         m_view->hideBox();
         m_boxBuffer->free();
+        m_boxBuffer.reset();
     } catch (...) {
         return false;
     }
@@ -121,24 +136,32 @@ bool Box::resize(int width, int height)
         m_updateTimer->restart();
         m_boxBuffer->reallocate(
                 m_boxInfo->boxWidth, m_boxInfo->boxHeight);
-        RenderInfoPtr renderInfo = makeRenderInfo(renderTypeResize);
+        RenderInfoPtr renderInfo = makeRenderInfo(renderTypeResize, URL_TYPE_BOX);
         m_view->showBox(renderInfo);
     } catch (...) {
+        LogD("resize exception");
         return false;
     }
 
     SWITCH_BOX_STATE();
-    return true; 
+    return true;
 }
 
 bool Box::resume()
 {
-    LogD("enter"); 
+    LogD("enter");
     CHECK_BOX_STATE(m_state, permitResume);
 
     try {
-        m_updateTimer->start();
-        m_view->resumeBox();
+        m_currentTab = true;
+        m_paused = false;
+
+        if (m_updateNeeded) {
+            m_updateNeeded = false;
+            updateInternal();
+        } else {
+            m_view->resumeBox();
+        }
     } catch (...) {
         return false;
     }
@@ -147,13 +170,16 @@ bool Box::resume()
     return true;
 }
 
-bool Box::pause()
+bool Box::pause(bool background)
 {
     LogD("enter");
     CHECK_BOX_STATE(m_state, permitPause);
 
     try {
-        m_updateTimer->stop();
+        if (!background) {
+            m_currentTab = false;
+        }
+        m_paused = true;
         m_view->pauseBox();
     } catch (...) {
         return false;
@@ -163,13 +189,15 @@ bool Box::pause()
     return true;
 }
 
-bool Box::openPd(int width, int height)
+bool Box::openPd(int width, int height, double x, double y)
 {
     LogD("enter");
     CHECK_BOX_STATE(m_state, permitOpenPd);
 
     m_boxInfo->pdWidth = width;
     m_boxInfo->pdHeight = height;
+    m_boxInfo->pdX = x;
+    m_boxInfo->pdY = y;
 
     try {
         m_updateTimer->stop();
@@ -180,8 +208,11 @@ bool Box::openPd(int width, int height)
                             m_boxInfo->pdWidth,
                             m_boxInfo->pdHeight);
         m_pdBuffer->allocate();
-        RenderInfoPtr renderInfo = makeRenderInfo(renderTypeOpenPd);
-        m_view->showPd(m_pdBuffer->getWindow(), renderInfo);
+        m_pdBuffer->stopCanvasUpdate();
+        RenderInfoPtr pdRenderInfo = makeRenderInfo(renderTypeOpenPd, URL_TYPE_PD);
+        RenderInfoPtr boxRenderInfo = makeRenderInfo(renderTypeOpenPd, URL_TYPE_BOX);
+        m_view->showPd(pdRenderInfo, boxRenderInfo);
+        ecore_idler_add(startUpdateRenderBufferIdlerCallback, m_pdBuffer.get());
     } catch (...) {
         return false;
     }
@@ -196,25 +227,42 @@ bool Box::closePd()
     CHECK_BOX_STATE(m_state, permitClosePd);
 
     try {
+        m_pdBuffer->stopCanvasUpdate();
         m_view->hidePd();
         m_pdBuffer->free();
-        m_updateTimer->start();
+        m_pdBuffer.reset();
+        m_updateTimer->restart();
     } catch (...) {
         return false;
     }
 
+    // if box update is requested during pd opening
+    if (m_updateNeeded) {
+        updateInternal();
+    }
+
     SWITCH_BOX_STATE();
     return true;
 }
 
-bool Box::update()
+bool Box::update(time_t requestTime, std::string& contentInfo)
 {
     LogD("enter");
 
-    // reload box
-    m_boxBuffer->startCanvasUpdate();
-    RenderInfoPtr renderInfo = makeRenderInfo(renderTypeUpdate);
-    m_view->showBox(renderInfo);
+    m_lastUpdateRequestTime = requestTime;
+    m_boxInfo->contentInfo = contentInfo;
+    if (m_paused || m_pdBuffer) {
+        // update is dalayed
+        //  until this box goes to current tab or pd is closed
+        m_updateNeeded = true;
+        return true;
+    }
+
+    try {
+        updateInternal();
+    } catch (...) {
+        return false;
+    }
 
     return true;
 }
@@ -230,23 +278,61 @@ bool Box::changePeriod(float period)
     return true;
 }
 
-RenderInfoPtr Box::makeRenderInfo(const std::string& renderType) const
+bool Box::isCurrentTab()
 {
+    return m_currentTab;
+}
+
+time_t Box::getLastUpdateRequestTime()
+{
+    return m_lastUpdateRequestTime;
+}
+
+RenderInfoPtr Box::makeRenderInfo(const std::string& renderType, UrlType urlType) const
+{
+    LogD("enter");
     RenderInfoPtr renderInfo(new RenderInfo);
+
+    // add width, height, operation type
+    renderInfo->defaultUrlParams = "?type=" + renderType;
+
     // set width, height
-    if (renderType == renderTypeOpenPd) {
-        renderInfo->width = m_boxInfo->pdWidth;
-        renderInfo->height = m_boxInfo->pdHeight;
-    } else {
+    switch (urlType) {
+    case URL_TYPE_BOX:
+        renderInfo->window = m_boxBuffer->getWindow();
         renderInfo->width = m_boxInfo->boxWidth;
         renderInfo->height = m_boxInfo->boxHeight;
+        break;
+    case URL_TYPE_PD:
+        renderInfo->window = m_pdBuffer->getWindow();
+        renderInfo->width = m_boxInfo->pdWidth;
+        renderInfo->height = m_boxInfo->pdHeight;
+        break;
+    default:
+        LogD("error url type");
+        return RenderInfoPtr();
     }
-    // add width, height, operation type
-    renderInfo->defaultUrlParams = "?type=" + renderType; 
+
     char buff[32];
     sprintf(buff, "&width=%d&height=%d", renderInfo->width, renderInfo->height);
     renderInfo->defaultUrlParams += buff;
 
+    // if needed, set pd information
+    if (renderType == renderTypeOpenPd) {
+        renderInfo->defaultUrlParams += "&pdopen-direction=";
+        if (m_boxInfo->pdY == 0.0f) {
+            renderInfo->defaultUrlParams += "down";
+        } else {
+            renderInfo->defaultUrlParams += "up";
+        }
+
+        char buff[32];
+        sprintf(buff, "&pdopen-arrow-xpos=%d",
+                static_cast<int>((m_boxInfo->pdX) * (m_boxInfo->pdWidth)));
+        renderInfo->defaultUrlParams += buff;
+    }
+
+
     // add content info
     if (!m_boxInfo->contentInfo.empty()) {
         renderInfo->defaultUrlParams += "&" + m_boxInfo->contentInfo;
@@ -256,8 +342,16 @@ RenderInfoPtr Box::makeRenderInfo(const std::string& renderType) const
     return renderInfo;
 }
 
+void Box::updateInternal()
+{
+    LogD("enter");
+    RenderInfoPtr renderInfo = makeRenderInfo(renderTypeUpdate, URL_TYPE_BOX);
+    m_view->showBox(renderInfo);
+}
+
 void Box::setState(IBoxStatePtr state)
 {
+    UNUSED_PARAM(state);
     // assign new state
     //m_state = state;
 }
@@ -267,7 +361,25 @@ Eina_Bool Box::updateCallback(void* data)
     LogD("enter");
     Box* This = static_cast<Box*>(data);
 
-    This->update();
+    This->updateInternal();
     return ECORE_CALLBACK_RENEW;
 }
 
+Eina_Bool Box::startUpdateRenderBufferIdlerCallback(void* data)
+{
+    LogD("enter");
+    RenderBuffer* buffer = static_cast<RenderBuffer*>(data);
+    if (!buffer) {
+        LogD("no buffer");
+    } else {
+        buffer->startCanvasUpdate();
+    }
+
+    return ECORE_CALLBACK_CANCEL;
+}
+
+BoxInfoPtr Box::getBoxInfo()
+{
+    LogD("enter");
+    return m_boxInfo;
+}