2 * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
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
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.
17 * @file AppBoxRenderView.cpp
18 * @author Yunchan Cho (yunchan.cho@samsung.com)
27 #include <ewk_context.h>
28 #include <livebox-service.h>
29 #include <i_runnable_widget_object.h>
30 #include <core_module.h>
31 #include <dpl/fast_delegate.h>
32 #include <Core/BoxSchemeHandler.h>
33 #include <Core/View/IRenderView.h>
34 #include <Core/View/IPdHelper.h>
35 #include <Core/View/PdHelper.h>
36 #include <API/web_provider_livebox_info.h>
37 #include <Core/Util/Log.h>
38 #include "AppBoxObserver.h"
39 #include "AppBoxRenderBuffer.h"
40 #include "AppBoxPdHelper.h"
41 #include "AppBoxRenderView.h"
43 #define RENDER_MAX_TIME 10.0
45 // injection javascript file regarding creating js object used by box and pd
46 static const std::string injectionFile("/usr/share/web-provider/injection.js");
48 AppBoxRenderView::AppBoxRenderView(
49 std::string boxId, std::string instanceId,
50 Evas_Object* boxWin, EwkContextPtr ewkContext)
53 , m_instanceId(instanceId)
62 , m_ewkContext(ewkContext)
66 m_appId = getAppId(m_boxId);
67 if (m_appId.empty()) {
68 throw; //exception throw!
71 evas_object_show(m_boxWin);
72 m_renderBuffer = AppBoxObserver::Instance()->getRenderBuffer(m_instanceId);
73 m_pdFastOpen = web_provider_livebox_get_pd_fast_open(m_boxId.c_str()) ? true : false;
74 AppBoxObserver::Instance()->registerRenderView(m_instanceId, this);
77 AppBoxRenderView::~AppBoxRenderView()
80 evas_object_hide(m_boxWin);
81 AppBoxObserver::Instance()->unregisterRenderView(m_instanceId);
84 void AppBoxRenderView::showBox(RenderInfoPtr renderInfo)
88 // delete already running timer
92 if (web_provider_livebox_get_mouse_event(m_boxId.c_str())) {
93 m_renderBuffer->deleteTouchTimer();
97 std::string boxStartUrl = getStartUrl(URL_TYPE_BOX, renderInfo->defaultUrlParams);
99 LogD("existing wrt core is removed");
100 destroyWrtCore(m_boxWrt);
103 m_boxWrt = createWrtCore(boxStartUrl, m_boxWin, m_ewkContext);
105 // in case of showing box by request of pd open
107 m_pdHelper->setBoxWebView(m_boxWrt->GetCurrentWebview());
110 // resize webview fitted to width, height of Box
112 m_boxWrt->GetCurrentWebview(),
117 m_renderInfo = renderInfo;
120 AppBoxRenderView::WrtCorePtr AppBoxRenderView::createWrtCore(
121 std::string& startUrl, Evas_Object* win, EwkContextPtr ewkContext)
126 #ifdef MULTIPROCESS_SERVICE_SUPPORT
127 wrt = WRT::CoreModuleSingleton::
128 Instance().getRunnableWidgetObject(m_appId, DPL::Optional<unsigned>());
130 wrt = WRT::CoreModuleSingleton::
131 Instance().getRunnableWidgetObject(m_appId);
134 if (startUrl.empty()) {
135 LogD("no start url");
138 wrt->PrepareView(startUrl, win, ewkContext.get());
139 wrt->CheckBeforeLaunch();
141 // set callback functions of RunnableWidgetObject
142 WRT::UserDelegatesPtr cbs(new WRT::UserDelegates);
143 cbs->loadStart = DPL::MakeDelegate(this, &AppBoxRenderView::startLoadCallback);
144 cbs->loadFinish = DPL::MakeDelegate(this, &AppBoxRenderView::finishLoadCallback);
145 cbs->bufferSet = DPL::MakeDelegate(this, &AppBoxRenderView::setBufferCallback);
146 cbs->bufferUnset = DPL::MakeDelegate(this, &AppBoxRenderView::unsetBufferCallback);
148 cbs->windowCreateBefore =
149 DPL::MakeDelegate(this, &AppBoxRenderView::createWindowBeforeCallback);
150 cbs->windowCreateAfter =
151 DPL::MakeDelegate(this, &AppBoxRenderView::createWindowAfterCallback);
154 cbs->navigationDecide =
155 DPL::MakeDelegate(this, &AppBoxRenderView::decideNavigationCallback);
156 wrt->SetUserDelegates(cbs);
158 // To support transparent background
159 evas_object_color_set(wrt->GetCurrentWebview(), 0, 0, 0, 0);
160 evas_object_layer_set(wrt->GetCurrentWebview(), EVAS_LAYER_MAX);
165 void AppBoxRenderView::destroyBoxWrtCore()
169 m_renderBuffer->stopCanvasUpdate();
171 destroyWrtCore(m_boxWrt);
175 void AppBoxRenderView::destroyPdWrtCore()
179 destroyWrtCore(m_pdWrt);
183 void AppBoxRenderView::destroyWrtCore(WrtCorePtr wrt)
192 void AppBoxRenderView::hideBox()
198 void AppBoxRenderView::pauseBox()
203 void AppBoxRenderView::resumeBox()
208 void AppBoxRenderView::showPd(Evas_Object* pdWin, RenderInfoPtr renderInfo)
213 std::string pdStartUrl = getStartUrl(URL_TYPE_PD, renderInfo->defaultUrlParams);
216 // if needed, last param regarding ewk context can be set to new one.
217 m_pdWrt = createWrtCore(pdStartUrl, pdWin, m_ewkContext);
219 LogD("no wrt core instance");
222 m_pdHelper = AppBoxPdHelper::create(pdWin);
224 // resize webview fitted to width, height of pd
226 m_pdWrt->GetCurrentWebview(),
231 m_pdHelper->finishOpen(m_pdWrt->GetCurrentWebview());
233 m_pdHelper = PdHelper::create(pdWin, pdStartUrl, renderInfo);
237 evas_object_show(pdWin);
238 showBox(m_renderInfo);
241 void AppBoxRenderView::hidePd()
251 // stop box webview after render timer
255 Evas_Object* AppBoxRenderView::getBoxWebView()
257 return m_boxWrt->GetCurrentWebview();
260 Evas_Object* AppBoxRenderView::getPdWebView()
266 return m_pdHelper->getPdWebView();
269 std::string AppBoxRenderView::getAppId(std::string& boxId)
273 const char* appId = web_provider_livebox_get_app_id(boxId.c_str());
275 LogD("no appid of %s", boxId.c_str());
276 return std::string();
279 return std::string(appId);
282 std::string AppBoxRenderView::getStartUrl(UrlType type, std::string& defaultParams)
287 url = livebox_service_lb_script_path(m_boxId.c_str());
290 url = livebox_service_pd_script_path(m_boxId.c_str());
293 LogD("no available type");
296 // add default parameters to start url
297 url += defaultParams;
302 Evas_Object* AppBoxRenderView::getCurrentSnapShot()
306 m_snapshot = m_renderBuffer->getSnapshot();
307 //evas_object_layer_set(m_snapshot, EVAS_LAYER_MAX);
312 void AppBoxRenderView::clearSnapShot()
316 evas_object_layer_set(m_snapshot, EVAS_LAYER_MIN);
317 //evas_object_hide(m_snapshot);
318 evas_object_del(m_snapshot);
323 void AppBoxRenderView::addRenderTimer()
326 if (m_fireRenderTimer) {
330 m_fireRenderTimer = ecore_timer_add(
332 fireRenderTimerCallback,
336 void AppBoxRenderView::deleteRenderTimer()
339 if (m_fireRenderTimer) {
340 ecore_timer_del(m_fireRenderTimer);
341 m_fireRenderTimer = NULL;
345 Eina_Bool AppBoxRenderView::fireRenderTimerCallback(void* data)
349 AppBoxRenderView* This = static_cast<AppBoxRenderView*>(data);
350 This->m_fireRenderTimer = NULL;
352 This->m_renderBuffer->stopCanvasUpdate();
353 if (web_provider_livebox_get_mouse_event(This->m_boxId.c_str())) {
355 This->m_renderBuffer->deleteTouchTimer();
356 This->m_boxWrt->Suspend();
358 // Before webview should be removed,
359 // new evas object with last render data should be created
360 // otherwise, after webview is removed, box is white screen.
361 evas_object_show(This->getCurrentSnapShot());
362 This->destroyBoxWrtCore();
365 return ECORE_CALLBACK_CANCEL;
368 Eina_Bool AppBoxRenderView::openPdIdlerCallback(void* data)
371 AppBoxRenderView* This = static_cast<AppBoxRenderView*>(data);
372 if (This && This->m_pdHelper) {
373 This->m_pdHelper->startOpen();
375 return ECORE_CALLBACK_CANCEL;
378 void AppBoxRenderView::executeScriptCallback(
379 Evas_Object* webview, const char* result, void* data)
382 std::string resultStr(result ? result : "null");
383 LogD("result: %s", resultStr.c_str());
386 void AppBoxRenderView::startLoadCallback(Evas_Object* webview)
389 m_renderBuffer->startCanvasUpdate();
390 // execute injection for creating js objects
391 std::ifstream jsFile(injectionFile);
392 std::string script((std::istreambuf_iterator<char>(jsFile)),
393 std::istreambuf_iterator<char>());
395 LogD("injected js code: %s", script.c_str());
396 ewk_view_script_execute(webview, script.c_str(), executeScriptCallback, this);
399 void AppBoxRenderView::finishLoadCallback(Evas_Object* webview)
402 ewk_view_visibility_set(webview, EINA_TRUE);
405 // start render timer
409 if (!(m_pdHelper->isPdOpened()) &&
410 webview == m_pdHelper->getBoxWebView())
413 ecore_idler_add(openPdIdlerCallback, this);
419 void AppBoxRenderView::createWindowBeforeCallback(Evas** canvas, Evas_Object* parent)
424 if (!(m_pdHelper->isPdOpened()) &&
425 parent == m_pdHelper->getBoxWebView())
427 LogD("pd canvas is used");
428 *canvas = m_pdHelper->getPdCanvas();
433 LogD("canvas of this webview is used");
434 *canvas = evas_object_evas_get(parent);
437 void AppBoxRenderView::createWindowAfterCallback(Evas_Object* parent, Evas_Object* child)
441 // To support transparent background
442 evas_object_color_set(child, 0, 0, 0, 0);
445 Evas* parentCanvas = evas_object_evas_get(parent);
446 Evas* childCanvas = evas_object_evas_get(child);
448 if (parentCanvas != childCanvas) {
449 // wrt-core change visibility value to false internally
450 // So plugin should reset this value to true for painting parent webview
451 ewk_view_visibility_set(parent, EINA_TRUE);
452 evas_object_show(parent);
453 m_pdHelper->finishOpen(child);
457 ewk_view_visibility_set(child, EINA_TRUE);
458 evas_object_show(child);
461 void AppBoxRenderView::setBufferCallback(Evas_Object* webview)
464 evas_object_show(webview);
465 evas_object_focus_set(webview, EINA_TRUE);
468 void AppBoxRenderView::unsetBufferCallback(Evas_Object* webview)
471 evas_object_hide(webview);
474 void AppBoxRenderView::decideNavigationCallback(Evas_Object* webview, std::string& uri)
478 // navigation of box scheme should be ignored
479 if(BoxSchemeHandler::Instance()->isBoxScheme(uri)) {
481 BoxSchemeHandler::Instance()->process(m_instanceId, uri);