[Release] livebox.web-provider-1.19
[platform/framework/web/web-provider.git] / src / Core / Box.cpp
1 /*
2  * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://floralicense.org/license/
9  *
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.
15  */
16 /**
17  * @file    Box.cpp
18  * @author  Yunchan Cho (yunchan.cho@samsung.com)
19  */
20 #include <string>
21 #include <Plugin/IBoxPluginFactory.h>
22 #include "Buffer/IRenderBuffer.h"
23 #include "Buffer/RenderBufferFactory.h"
24 #include "Util/Log.h"
25 #include "BoxData.h"
26 #include "IBoxState.h"
27 #include "BoxState.h"
28 #include "Util/ITimer.h"
29 #include "BoxUpdateTimer.h"
30 #include "BoxSchemeHandler.h"
31 #include "Box.h"
32
33 // This is used for informing context of box to web content as value of url parameter
34 static const std::string renderTypeCreate("create");
35 static const std::string renderTypeResize("resize");
36 static const std::string renderTypeOpenPd("pdopen");
37 static const std::string renderTypeUpdate("update");
38
39 Box::Box(BoxInfoPtr boxInfo, IBoxPluginFactoryPtr factory, EwkContextPtr ewkContext)
40     : m_boxInfo(boxInfo)
41     , m_factory(factory)
42     , m_currentTab(true)
43 {
44     LogD("enter");
45     try {
46         m_boxBuffer = m_factory->createBoxRenderBuffer(
47                             boxInfo->boxId,
48                             boxInfo->instanceId,
49                             boxInfo->boxWidth,
50                             boxInfo->boxHeight,
51                             boxInfo->contentInfo);
52         m_boxBuffer->allocate();
53         m_view = m_factory->createRenderView(
54                 boxInfo->boxId, boxInfo->instanceId,
55                 m_boxBuffer->getWindow(),
56                 ewkContext);
57         m_updateTimer = BoxUpdateTimer::create(
58                 boxInfo->period,
59                 Box::updateCallback,
60                 this);
61         BoxSchemeHandler::Instance()->registerBox(boxInfo->instanceId, this);
62
63         // TODO code regarding state needs more testing
64         //m_state = BoxInitState::create(
65         //           IBoxContextPtr(dynamic_cast<IBoxContext*>(this)));
66     } catch (...) {
67         throw;
68     }
69 }
70
71 Box::~Box()
72 {
73     LogD("enter");
74     BoxSchemeHandler::Instance()->unregisterBox(m_boxInfo->instanceId);
75 }
76
77 bool Box::show()
78 {
79     LogD("enter");
80     CHECK_BOX_STATE(m_state, permitShow);
81
82     try {
83         m_updateTimer->start();
84         m_boxBuffer->startCanvasUpdate();
85         RenderInfoPtr renderInfo = makeRenderInfo(renderTypeCreate);
86         m_view->showBox(renderInfo);
87     } catch (...) {
88         return false;
89     }
90
91     SWITCH_BOX_STATE();
92     return true;
93 }
94
95 bool Box::hide()
96 {
97     LogD("enter");
98     CHECK_BOX_STATE(m_state, permitHide);
99
100     try {
101         m_updateTimer->stop();
102         m_view->hideBox();
103         m_boxBuffer->free();
104     } catch (...) {
105         return false;
106     }
107
108     SWITCH_BOX_STATE();
109     return true;
110 }
111
112 bool Box::resize(int width, int height)
113 {
114     LogD("enter");
115     CHECK_BOX_STATE(m_state, permitShow);
116
117     // reset box info to new width, height
118     m_boxInfo->boxWidth = width;
119     m_boxInfo->boxHeight = height;
120
121     try {
122         m_updateTimer->restart();
123         m_boxBuffer->reallocate(
124                 m_boxInfo->boxWidth, m_boxInfo->boxHeight);
125         RenderInfoPtr renderInfo = makeRenderInfo(renderTypeResize);
126         m_view->showBox(renderInfo);
127     } catch (...) {
128         LogD("resize exception");
129         return false;
130     }
131
132     SWITCH_BOX_STATE();
133     return true; 
134 }
135
136 bool Box::resume()
137 {
138     LogD("enter"); 
139     CHECK_BOX_STATE(m_state, permitResume);
140
141     try {
142         m_currentTab = true;
143         m_updateTimer->resume();
144         m_view->resumeBox();
145     } catch (...) {
146         return false;
147     }
148
149     SWITCH_BOX_STATE();
150     return true;
151 }
152
153 bool Box::pause(bool background)
154 {
155     LogD("enter");
156     CHECK_BOX_STATE(m_state, permitPause);
157
158     try {
159         if (!background) {
160             m_currentTab = false;
161         }
162         m_updateTimer->pause();
163         m_view->pauseBox();
164     } catch (...) {
165         return false;
166     }
167
168     SWITCH_BOX_STATE();
169     return true;
170 }
171
172 bool Box::openPd(int width, int height, double x, double y)
173 {
174     LogD("enter");
175     CHECK_BOX_STATE(m_state, permitOpenPd);
176
177     m_boxInfo->pdWidth = width;
178     m_boxInfo->pdHeight = height;
179     m_boxInfo->pdX = x;
180     m_boxInfo->pdY = y;
181
182     try {
183         m_updateTimer->stop();
184         m_pdBuffer = RenderBufferFactory::create(
185                             RenderBufferFactory::RENDER_BUFFER_TYPE_PD,
186                             m_boxInfo->boxId,
187                             m_boxInfo->instanceId,
188                             m_boxInfo->pdWidth,
189                             m_boxInfo->pdHeight);
190         m_pdBuffer->allocate();
191         RenderInfoPtr renderInfo = makeRenderInfo(renderTypeOpenPd);
192         m_view->showPd(m_pdBuffer->getWindow(), renderInfo);
193     } catch (...) {
194         return false;
195     }
196
197     SWITCH_BOX_STATE();
198     return true;
199 }
200
201 bool Box::closePd()
202 {
203     LogD("enter");
204     CHECK_BOX_STATE(m_state, permitClosePd);
205
206     try {
207         m_view->hidePd();
208         m_pdBuffer->free();
209         m_updateTimer->start();
210     } catch (...) {
211         return false;
212     }
213
214     SWITCH_BOX_STATE();
215     return true;
216 }
217
218 bool Box::update()
219 {
220     LogD("enter");
221
222     m_boxBuffer->startCanvasUpdate();
223     RenderInfoPtr renderInfo = makeRenderInfo(renderTypeUpdate);
224     m_view->showBox(renderInfo);
225
226     return true;
227 }
228
229 bool Box::changePeriod(float period)
230 {
231     LogD("enter");
232
233     // reset period
234     m_boxInfo->period = period;
235     m_updateTimer->setPeriod(m_boxInfo->period);
236
237     return true;
238 }
239
240 bool Box::isCurrentTab()
241 {
242     return m_currentTab;
243 }
244
245 RenderInfoPtr Box::makeRenderInfo(const std::string& renderType) const
246 {
247     LogD("enter");
248     RenderInfoPtr renderInfo(new RenderInfo);
249
250     // add width, height, operation type
251     renderInfo->defaultUrlParams = "?type=" + renderType;
252
253     // set width, height
254     if (renderType == renderTypeOpenPd) {
255         renderInfo->width = m_boxInfo->pdWidth;
256         renderInfo->height = m_boxInfo->pdHeight;
257     } else {
258         renderInfo->width = m_boxInfo->boxWidth;
259         renderInfo->height = m_boxInfo->boxHeight;
260     }
261     char buff[32];
262     sprintf(buff, "&width=%d&height=%d", renderInfo->width, renderInfo->height);
263     renderInfo->defaultUrlParams += buff;
264
265     // if needed, set pd information
266     if (renderType == renderTypeOpenPd) {
267         renderInfo->defaultUrlParams += "&pdopen-direction=";
268         if (m_boxInfo->pdY == 0.0f) {
269             renderInfo->defaultUrlParams += "down";
270         } else {
271             renderInfo->defaultUrlParams += "up";
272         }
273
274         char buff[32];
275         sprintf(buff, "&pdopen-arrow-xpos=%d",
276                 static_cast<int>((m_boxInfo->pdX) * (m_boxInfo->pdWidth)));
277         renderInfo->defaultUrlParams += buff;
278     }
279
280
281     // add content info
282     if (!m_boxInfo->contentInfo.empty()) {
283         renderInfo->defaultUrlParams += "&" + m_boxInfo->contentInfo;
284     }
285
286     LogD("default url param string: %s", renderInfo->defaultUrlParams.c_str());
287     return renderInfo;
288 }
289
290 void Box::setState(IBoxStatePtr state)
291 {
292     // assign new state
293     //m_state = state;
294 }
295
296 Eina_Bool Box::updateCallback(void* data)
297 {
298     LogD("enter");
299     Box* This = static_cast<Box*>(data);
300
301     This->update();
302     return ECORE_CALLBACK_RENEW;
303 }
304