[Release] livebox.web-provider-1.9
[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.0 (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 {
43     LogD("enter");
44     try {
45         m_boxBuffer = m_factory->createBoxRenderBuffer(
46                             boxInfo->boxId,
47                             boxInfo->instanceId,
48                             boxInfo->boxWidth,
49                             boxInfo->boxHeight,
50                             boxInfo->contentInfo);
51         m_boxBuffer->allocate();
52         m_view = m_factory->createRenderView(
53                 boxInfo->boxId, boxInfo->instanceId,
54                 m_boxBuffer->getWindow(),
55                 ewkContext);
56         m_updateTimer = BoxUpdateTimer::create(
57                 boxInfo->period,
58                 Box::updateCallback,
59                 this);
60         BoxSchemeHandler::Instance()->registerBox(boxInfo->instanceId, this);
61
62         // TODO code regarding state needs more testing
63         //m_state = BoxInitState::create(
64         //           IBoxContextPtr(dynamic_cast<IBoxContext*>(this)));
65     } catch (...) {
66         throw;
67     }
68 }
69
70 Box::~Box()
71 {
72     LogD("enter");
73     BoxSchemeHandler::Instance()->unregisterBox(m_boxInfo->instanceId);
74 }
75
76 bool Box::show()
77 {
78     LogD("enter");
79     CHECK_BOX_STATE(m_state, permitShow);
80
81     try {
82         m_updateTimer->start();
83         m_boxBuffer->startCanvasUpdate();
84         RenderInfoPtr renderInfo = makeRenderInfo(renderTypeCreate);
85         m_view->showBox(renderInfo);
86     } catch (...) {
87         return false;
88     }
89
90     SWITCH_BOX_STATE();
91     return true;
92 }
93
94 bool Box::hide()
95 {
96     LogD("enter");
97     CHECK_BOX_STATE(m_state, permitHide);
98
99     try {
100         m_updateTimer->stop();
101         m_view->hideBox();
102         m_boxBuffer->free();
103     } catch (...) {
104         return false;
105     }
106
107     SWITCH_BOX_STATE();
108     return true;
109 }
110
111 bool Box::resize(int width, int height)
112 {
113     LogD("enter");
114     CHECK_BOX_STATE(m_state, permitShow);
115
116     // reset box info to new width, height
117     m_boxInfo->boxWidth = width;
118     m_boxInfo->boxHeight = height;
119
120     try {
121         m_updateTimer->restart();
122         m_boxBuffer->reallocate(
123                 m_boxInfo->boxWidth, m_boxInfo->boxHeight);
124         RenderInfoPtr renderInfo = makeRenderInfo(renderTypeResize);
125         m_view->showBox(renderInfo);
126     } catch (...) {
127         return false;
128     }
129
130     SWITCH_BOX_STATE();
131     return true; 
132 }
133
134 bool Box::resume()
135 {
136     LogD("enter"); 
137     CHECK_BOX_STATE(m_state, permitResume);
138
139     try {
140         m_updateTimer->start();
141         m_view->resumeBox();
142     } catch (...) {
143         return false;
144     }
145
146     SWITCH_BOX_STATE();
147     return true;
148 }
149
150 bool Box::pause()
151 {
152     LogD("enter");
153     CHECK_BOX_STATE(m_state, permitPause);
154
155     try {
156         m_updateTimer->stop();
157         m_view->pauseBox();
158     } catch (...) {
159         return false;
160     }
161
162     SWITCH_BOX_STATE();
163     return true;
164 }
165
166 bool Box::openPd(int width, int height)
167 {
168     LogD("enter");
169     CHECK_BOX_STATE(m_state, permitOpenPd);
170
171     m_boxInfo->pdWidth = width;
172     m_boxInfo->pdHeight = height;
173
174     try {
175         m_updateTimer->stop();
176         m_pdBuffer = RenderBufferFactory::create(
177                             RenderBufferFactory::RENDER_BUFFER_TYPE_PD,
178                             m_boxInfo->boxId,
179                             m_boxInfo->instanceId,
180                             m_boxInfo->pdWidth,
181                             m_boxInfo->pdHeight);
182         m_pdBuffer->allocate();
183         RenderInfoPtr renderInfo = makeRenderInfo(renderTypeOpenPd);
184         m_view->showPd(m_pdBuffer->getWindow(), renderInfo);
185     } catch (...) {
186         return false;
187     }
188
189     SWITCH_BOX_STATE();
190     return true;
191 }
192
193 bool Box::closePd()
194 {
195     LogD("enter");
196     CHECK_BOX_STATE(m_state, permitClosePd);
197
198     try {
199         m_view->hidePd();
200         m_pdBuffer->free();
201         m_updateTimer->start();
202     } catch (...) {
203         return false;
204     }
205
206     SWITCH_BOX_STATE();
207     return true;
208 }
209
210 bool Box::update()
211 {
212     LogD("enter");
213
214     // reload box
215     m_boxBuffer->startCanvasUpdate();
216     RenderInfoPtr renderInfo = makeRenderInfo(renderTypeUpdate);
217     m_view->showBox(renderInfo);
218
219     return true;
220 }
221
222 bool Box::changePeriod(float period)
223 {
224     LogD("enter");
225
226     // reset period
227     m_boxInfo->period = period;
228     m_updateTimer->setPeriod(m_boxInfo->period);
229
230     return true;
231 }
232
233 RenderInfoPtr Box::makeRenderInfo(const std::string& renderType) const
234 {
235     RenderInfoPtr renderInfo(new RenderInfo);
236     // set width, height
237     if (renderType == renderTypeOpenPd) {
238         renderInfo->width = m_boxInfo->pdWidth;
239         renderInfo->height = m_boxInfo->pdHeight;
240     } else {
241         renderInfo->width = m_boxInfo->boxWidth;
242         renderInfo->height = m_boxInfo->boxHeight;
243     }
244     // add width, height, operation type
245     renderInfo->defaultUrlParams = "?type=" + renderType; 
246     char buff[32];
247     sprintf(buff, "&width=%d&height=%d", renderInfo->width, renderInfo->height);
248     renderInfo->defaultUrlParams += buff;
249
250     // add content info
251     if (!m_boxInfo->contentInfo.empty()) {
252         renderInfo->defaultUrlParams += "&" + m_boxInfo->contentInfo;
253     }
254
255     LogD("default url param string: %s", renderInfo->defaultUrlParams.c_str());
256     return renderInfo;
257 }
258
259 void Box::setState(IBoxStatePtr state)
260 {
261     // assign new state
262     //m_state = state;
263 }
264
265 Eina_Bool Box::updateCallback(void* data)
266 {
267     LogD("enter");
268     Box* This = static_cast<Box*>(data);
269
270     This->update();
271     return ECORE_CALLBACK_RENEW;
272 }
273