2 * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
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
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 cbs->webCrash = DPL::MakeDelegate(this, &AppBoxRenderView::crashWebProcessCallback);
157 wrt->SetUserDelegates(cbs);
159 // To support transparent background
160 evas_object_color_set(wrt->GetCurrentWebview(), 0, 0, 0, 0);
161 evas_object_layer_set(wrt->GetCurrentWebview(), EVAS_LAYER_MAX);
166 void AppBoxRenderView::destroyBoxWrtCore()
170 m_renderBuffer->stopCanvasUpdate();
172 destroyWrtCore(m_boxWrt);
176 void AppBoxRenderView::destroyPdWrtCore()
180 destroyWrtCore(m_pdWrt);
184 void AppBoxRenderView::destroyWrtCore(WrtCorePtr wrt)
193 void AppBoxRenderView::hideBox()
199 void AppBoxRenderView::pauseBox()
204 void AppBoxRenderView::resumeBox()
209 void AppBoxRenderView::showPd(Evas_Object* pdWin, RenderInfoPtr renderInfo)
214 std::string pdStartUrl = getStartUrl(URL_TYPE_PD, renderInfo->defaultUrlParams);
217 // if needed, last param regarding ewk context can be set to new one.
218 m_pdWrt = createWrtCore(pdStartUrl, pdWin, m_ewkContext);
220 LogD("no wrt core instance");
223 m_pdHelper = AppBoxPdHelper::create(pdWin);
225 // resize webview fitted to width, height of pd
227 m_pdWrt->GetCurrentWebview(),
232 m_pdHelper->finishOpen(m_pdWrt->GetCurrentWebview());
234 m_pdHelper = PdHelper::create(pdWin, pdStartUrl, renderInfo);
238 evas_object_show(pdWin);
239 showBox(m_renderInfo);
242 void AppBoxRenderView::hidePd()
252 // stop box webview after render timer
256 Evas_Object* AppBoxRenderView::getBoxWebView()
259 return m_boxWrt->GetCurrentWebview();
261 // Here, we can't use GetCurrentWebView() of wrt-core to get Box' webview,
262 // because in the non fast-open, GetCurrentWebview() returns PD's webview.
263 return m_pdHelper->getBoxWebView();
267 Evas_Object* AppBoxRenderView::getPdWebView()
273 return m_pdHelper->getPdWebView();
276 std::string AppBoxRenderView::getAppId(std::string& boxId)
280 const char* appId = web_provider_livebox_get_app_id(boxId.c_str());
282 LogD("no appid of %s", boxId.c_str());
283 return std::string();
286 return std::string(appId);
289 std::string AppBoxRenderView::getStartUrl(UrlType type, std::string& defaultParams)
294 url = livebox_service_lb_script_path(m_boxId.c_str());
297 url = livebox_service_pd_script_path(m_boxId.c_str());
300 LogD("no available type");
303 // add default parameters to start url
304 url += defaultParams;
309 Evas_Object* AppBoxRenderView::getCurrentSnapShot()
313 m_snapshot = m_renderBuffer->getSnapshot();
314 //evas_object_layer_set(m_snapshot, EVAS_LAYER_MAX);
319 void AppBoxRenderView::clearSnapShot()
323 evas_object_layer_set(m_snapshot, EVAS_LAYER_MIN);
324 //evas_object_hide(m_snapshot);
325 evas_object_del(m_snapshot);
330 void AppBoxRenderView::addRenderTimer()
333 if (m_fireRenderTimer) {
337 m_fireRenderTimer = ecore_timer_add(
339 fireRenderTimerCallback,
343 void AppBoxRenderView::deleteRenderTimer()
346 if (m_fireRenderTimer) {
347 ecore_timer_del(m_fireRenderTimer);
348 m_fireRenderTimer = NULL;
352 Eina_Bool AppBoxRenderView::fireRenderTimerCallback(void* data)
356 AppBoxRenderView* This = static_cast<AppBoxRenderView*>(data);
357 This->m_fireRenderTimer = NULL;
359 This->m_renderBuffer->stopCanvasUpdate();
360 if (web_provider_livebox_get_mouse_event(This->m_boxId.c_str())) {
362 This->m_renderBuffer->deleteTouchTimer();
363 This->m_boxWrt->Suspend();
365 // Before webview should be removed,
366 // new evas object with last render data should be created
367 // otherwise, after webview is removed, box is white screen.
368 evas_object_show(This->getCurrentSnapShot());
369 This->destroyBoxWrtCore();
372 return ECORE_CALLBACK_CANCEL;
375 Eina_Bool AppBoxRenderView::openPdIdlerCallback(void* data)
378 AppBoxRenderView* This = static_cast<AppBoxRenderView*>(data);
379 if (This && This->m_pdHelper) {
380 This->m_pdHelper->startOpen();
382 return ECORE_CALLBACK_CANCEL;
385 void AppBoxRenderView::executeScriptCallback(
386 Evas_Object* webview, const char* result, void* data)
389 std::string resultStr(result ? result : "null");
390 LogD("result: %s", resultStr.c_str());
393 void AppBoxRenderView::startLoadCallback(Evas_Object* webview)
396 m_renderBuffer->startCanvasUpdate();
397 // execute injection for creating js objects
398 std::ifstream jsFile(injectionFile);
399 std::string script((std::istreambuf_iterator<char>(jsFile)),
400 std::istreambuf_iterator<char>());
402 LogD("injected js code: %s", script.c_str());
403 ewk_view_script_execute(webview, script.c_str(), executeScriptCallback, this);
406 void AppBoxRenderView::finishLoadCallback(Evas_Object* webview)
409 ewk_view_visibility_set(webview, EINA_TRUE);
412 // start render timer
416 if (!(m_pdHelper->isPdOpened()) &&
417 webview == m_pdHelper->getBoxWebView())
420 ecore_idler_add(openPdIdlerCallback, this);
426 void AppBoxRenderView::createWindowBeforeCallback(Evas** canvas, Evas_Object* parent)
431 if (!(m_pdHelper->isPdOpened()) &&
432 parent == m_pdHelper->getBoxWebView())
434 LogD("pd canvas is used");
435 *canvas = m_pdHelper->getPdCanvas();
440 LogD("canvas of this webview is used");
441 *canvas = evas_object_evas_get(parent);
444 void AppBoxRenderView::createWindowAfterCallback(Evas_Object* parent, Evas_Object* child)
448 // To support transparent background
449 evas_object_color_set(child, 0, 0, 0, 0);
452 Evas* parentCanvas = evas_object_evas_get(parent);
453 Evas* childCanvas = evas_object_evas_get(child);
455 if (parentCanvas != childCanvas) {
456 // wrt-core change visibility value to false internally
457 // So plugin should reset this value to true for painting parent webview
458 ewk_view_visibility_set(parent, EINA_TRUE);
459 evas_object_show(parent);
460 m_pdHelper->finishOpen(child);
464 ewk_view_visibility_set(child, EINA_TRUE);
465 evas_object_show(child);
468 void AppBoxRenderView::setBufferCallback(Evas_Object* webview)
471 evas_object_show(webview);
472 evas_object_focus_set(webview, EINA_TRUE);
475 void AppBoxRenderView::unsetBufferCallback(Evas_Object* webview)
478 evas_object_hide(webview);
481 void AppBoxRenderView::decideNavigationCallback(Evas_Object* webview, std::string& uri)
485 // navigation of box scheme should be ignored
486 if(BoxSchemeHandler::Instance()->isBoxScheme(uri)) {
488 BoxSchemeHandler::Instance()->process(m_instanceId, uri);
492 void AppBoxRenderView::crashWebProcessCallback()