Remove temporary code regarding getting valid frame
[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 <Ecore.h>
22 #include <Plugin/IBoxPluginFactory.h>
23 #include "Buffer/IRenderBuffer.h"
24 #include "Buffer/RenderBuffer.h"
25 #include "Buffer/RenderBufferFactory.h"
26 #include "Util/Log.h"
27 #include "BoxData.h"
28 #include "IBoxState.h"
29 #include "BoxState.h"
30 #include "Util/ITimer.h"
31 #include "BoxUpdateTimer.h"
32 #include "BoxSchemeHandler.h"
33 #include "Box.h"
34
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");
40
41 Box::Box(BoxInfoPtr boxInfo, IBoxPluginFactoryPtr factory, EwkContextPtr ewkContext)
42     : m_boxInfo(boxInfo)
43     , m_factory(factory)
44     , m_currentTab(true)
45     , m_paused(false)
46     , m_updateNeeded(false)
47     , m_lastUpdateRequestTime()
48 {
49     LogD("enter");
50     try {
51         m_boxBuffer = m_factory->createBoxRenderBuffer(
52                             boxInfo->boxId,
53                             boxInfo->instanceId,
54                             boxInfo->boxWidth,
55                             boxInfo->boxHeight,
56                             boxInfo->contentInfo);
57         m_boxBuffer->allocate();
58         m_view = m_factory->createRenderView(
59                 boxInfo->boxId,
60                 boxInfo->instanceId,
61                 ewkContext);
62         m_updateTimer = BoxUpdateTimer::create(
63                 boxInfo->period,
64                 Box::updateCallback,
65                 this);
66         BoxSchemeHandler::Instance()->registerBox(boxInfo->instanceId, this);
67
68         // TODO code regarding state needs more testing
69         //m_state = BoxInitState::create(
70         //           IBoxContextPtr(dynamic_cast<IBoxContext*>(this)));
71     } catch (...) {
72         throw;
73     }
74 }
75
76 Box::~Box()
77 {
78     LogD("enter");
79     BoxSchemeHandler::Instance()->unregisterBox(m_boxInfo->instanceId);
80 }
81
82 bool Box::show()
83 {
84     LogD("enter");
85     CHECK_BOX_STATE(m_state, permitShow);
86
87     try {
88         m_updateTimer->start();
89         RenderInfoPtr renderInfo = makeRenderInfo(renderTypeCreate, URL_TYPE_BOX);
90         m_view->showBox(renderInfo);
91     } catch (...) {
92         return false;
93     }
94
95     SWITCH_BOX_STATE();
96     return true;
97 }
98
99 bool Box::hide()
100 {
101     LogD("enter");
102     CHECK_BOX_STATE(m_state, permitHide);
103
104     try {
105         if (m_pdBuffer) {
106             m_pdBuffer->stopCanvasUpdate();
107             m_view->hidePd();
108             m_pdBuffer->free();
109             m_pdBuffer.reset();
110          }
111
112         m_updateTimer->stop();
113         m_boxBuffer->stopCanvasUpdate();
114         m_view->hideBox();
115         m_boxBuffer->free();
116         m_boxBuffer.reset();
117     } catch (...) {
118         return false;
119     }
120
121     SWITCH_BOX_STATE();
122     return true;
123 }
124
125 bool Box::resize(int width, int height)
126 {
127     LogD("enter");
128     CHECK_BOX_STATE(m_state, permitShow);
129
130     // reset box info to new width, height
131     m_boxInfo->boxWidth = width;
132     m_boxInfo->boxHeight = height;
133
134     try {
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);
140     } catch (...) {
141         LogD("resize exception");
142         return false;
143     }
144
145     SWITCH_BOX_STATE();
146     return true; 
147 }
148
149 bool Box::resume()
150 {
151     LogD("enter"); 
152     CHECK_BOX_STATE(m_state, permitResume);
153
154     try {
155         m_currentTab = true;
156         m_paused = false;
157
158         if (m_updateNeeded) {
159             m_updateNeeded = false;
160             updateInternal();
161         } else {
162             m_view->resumeBox();
163         }
164     } catch (...) {
165         return false;
166     }
167
168     SWITCH_BOX_STATE();
169     return true;
170 }
171
172 bool Box::pause(bool background)
173 {
174     LogD("enter");
175     CHECK_BOX_STATE(m_state, permitPause);
176
177     try {
178         if (!background) {
179             m_currentTab = false;
180         }
181         m_paused = true;
182         m_view->pauseBox();
183     } catch (...) {
184         return false;
185     }
186
187     SWITCH_BOX_STATE();
188     return true;
189 }
190
191 bool Box::openPd(int width, int height, double x, double y)
192 {
193     LogD("enter");
194     CHECK_BOX_STATE(m_state, permitOpenPd);
195
196     m_boxInfo->pdWidth = width;
197     m_boxInfo->pdHeight = height;
198     m_boxInfo->pdX = x;
199     m_boxInfo->pdY = y;
200
201     try {
202         m_updateTimer->stop();
203         m_pdBuffer = RenderBufferFactory::create(
204                             RenderBufferFactory::RENDER_BUFFER_TYPE_PD,
205                             m_boxInfo->boxId,
206                             m_boxInfo->instanceId,
207                             m_boxInfo->pdWidth,
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());
215     } catch (...) {
216         return false;
217     }
218
219     SWITCH_BOX_STATE();
220     return true;
221 }
222
223 bool Box::closePd()
224 {
225     LogD("enter");
226     CHECK_BOX_STATE(m_state, permitClosePd);
227
228     try {
229         m_pdBuffer->stopCanvasUpdate();
230         m_view->hidePd();
231         m_pdBuffer->free();
232         m_pdBuffer.reset();
233         m_updateTimer->restart();
234     } catch (...) {
235         return false;
236     }
237
238     // if box update is requested during pd opening
239     if (m_updateNeeded) {
240         updateInternal();
241     }
242
243     SWITCH_BOX_STATE();
244     return true;
245 }
246
247 bool Box::update(time_t requestTime, std::string& contentInfo)
248 {
249     LogD("enter");
250
251     m_lastUpdateRequestTime = requestTime;
252     m_boxInfo->contentInfo = contentInfo;
253     if (m_paused || m_pdBuffer) {
254         // update is dalayed
255         //  until this box goes to current tab or pd is closed
256         m_updateNeeded = true;
257         return true;
258     }
259
260     try {
261         updateInternal();
262     } catch (...) {
263         return false;
264     }
265
266     return true;
267 }
268
269 bool Box::changePeriod(float period)
270 {
271     LogD("enter");
272
273     // reset period
274     m_boxInfo->period = period;
275     m_updateTimer->setPeriod(m_boxInfo->period);
276
277     return true;
278 }
279
280 bool Box::isCurrentTab()
281 {
282     return m_currentTab;
283 }
284
285 time_t Box::getLastUpdateRequestTime()
286 {
287     return m_lastUpdateRequestTime;
288 }
289
290 RenderInfoPtr Box::makeRenderInfo(const std::string& renderType, UrlType urlType) const
291 {
292     LogD("enter");
293     RenderInfoPtr renderInfo(new RenderInfo);
294
295     // add width, height, operation type
296     renderInfo->defaultUrlParams = "?type=" + renderType;
297
298     // set width, height
299     switch (urlType) {
300     case URL_TYPE_BOX:
301         renderInfo->window = m_boxBuffer->getWindow();
302         renderInfo->width = m_boxInfo->boxWidth;
303         renderInfo->height = m_boxInfo->boxHeight;
304         break;
305     case URL_TYPE_PD:
306         renderInfo->window = m_pdBuffer->getWindow();
307         renderInfo->width = m_boxInfo->pdWidth;
308         renderInfo->height = m_boxInfo->pdHeight;
309         break;
310     default:
311         LogD("error url type");
312         return RenderInfoPtr();
313     }
314
315     char buff[32];
316     sprintf(buff, "&width=%d&height=%d", renderInfo->width, renderInfo->height);
317     renderInfo->defaultUrlParams += buff;
318
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";
324         } else {
325             renderInfo->defaultUrlParams += "up";
326         }
327
328         char buff[32];
329         sprintf(buff, "&pdopen-arrow-xpos=%d",
330                 static_cast<int>((m_boxInfo->pdX) * (m_boxInfo->pdWidth)));
331         renderInfo->defaultUrlParams += buff;
332     }
333
334
335     // add content info
336     if (!m_boxInfo->contentInfo.empty()) {
337         renderInfo->defaultUrlParams += "&" + m_boxInfo->contentInfo;
338     }
339
340     LogD("default url param string: %s", renderInfo->defaultUrlParams.c_str());
341     return renderInfo;
342 }
343
344 void Box::updateInternal()
345 {
346     LogD("enter");
347     RenderInfoPtr renderInfo = makeRenderInfo(renderTypeUpdate, URL_TYPE_BOX);
348     m_view->showBox(renderInfo);
349 }
350
351 void Box::setState(IBoxStatePtr state)
352 {
353     // assign new state
354     //m_state = state;
355 }
356
357 Eina_Bool Box::updateCallback(void* data)
358 {
359     LogD("enter");
360     Box* This = static_cast<Box*>(data);
361
362     This->updateInternal();
363     return ECORE_CALLBACK_RENEW;
364 }
365
366 Eina_Bool Box::startUpdateRenderBufferIdlerCallback(void* data)
367 {
368     LogD("enter");
369     RenderBuffer* buffer = static_cast<RenderBuffer*>(data);
370     if (!buffer) {
371         LogD("no buffer");
372     } else {
373         buffer->startCanvasUpdate();
374     }
375
376     return ECORE_CALLBACK_CANCEL;
377 }