2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.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://www.apache.org/licenses/LICENSE-2.0
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.
16 #include "wrt-client.h"
20 #include <dpl/log/log.h>
21 #include <dpl/optional_typedefs.h>
22 #include <common/application_data.h>
23 #include <core_module.h>
24 #include <global_logic.h>
25 #include <widget_localize_model.h>
26 #include <localization_setting.h>
27 #include <widget_deserialize_model.h>
29 #include <dpl/localization/w3c_file_localization.h>
30 #include <dpl/localization/LanguageTagsProvider.h>
32 //W3C PACKAGING enviroment variable name
33 #define W3C_DEBUG_ENV_VARIABLE "DEBUG_LOAD_FINISH"
35 // window signal callback
36 const std::string VIEWMODE_TYPE_FULLSCREEN = "fullscreen";
37 const std::string VIEWMODE_TYPE_MAXIMIZED = "maximized";
38 char const* const ELM_SWALLOW_CONTENT = "elm.swallow.content";
40 WrtClient::WrtClient(int argc, char **argv) :
41 Application(argc, argv, "wrt-client", false),
42 DPL::TaskDecl<WrtClient>(this),
44 m_initializing(false),
49 m_returnStatus(ReturnStatus::Succeeded),
50 m_widgetState(WidgetState::WidgetState_Stopped)
53 LogDebug("App Created");
56 WrtClient::~WrtClient()
58 LogDebug("App Finished");
61 WrtClient::ReturnStatus::Type WrtClient::getReturnStatus() const
63 return m_returnStatus;
66 void WrtClient::OnStop()
68 LogInfo("Stopping Dummy Client");
72 void WrtClient::OnCreate()
75 ADD_PROFILING_POINT("OnCreate callback", "point");
79 void WrtClient::OnResume()
81 if (m_widgetState != WidgetState_Suspended) {
82 LogWarning("Widget is not suspended, resuming was skipped");
86 evas_object_focus_set(m_widget->GetCurrentWebview(), EINA_TRUE);
87 m_widgetState = WidgetState_Running;
91 void WrtClient::OnPause()
95 if (m_widgetState != WidgetState_Running) {
96 LogWarning("Widget is not running to be suspended");
100 evas_object_focus_set(m_widget->GetCurrentWebview(), EINA_FALSE);
101 m_widgetState = WidgetState_Suspended;
104 void WrtClient::OnReset(bundle *b)
107 // bundle argument is freed after OnReset() is returned
108 // So bundle duplication is needed
109 ApplicationDataSingleton::Instance().setBundle(bundle_dup(b));
111 if (true == m_initializing) {
112 LogDebug("can not handle reset event");
115 if (true == m_launched) {
116 if (m_widgetState == WidgetState_Stopped) {
117 LogError("Widget is not running to be reset");
121 elm_win_raise(m_windowData->m_win);
122 evas_object_focus_set(m_widget->GetCurrentWebview(), EINA_TRUE);
123 m_widgetState = WidgetState_Running;
125 if (true == checkArgument())
136 void WrtClient::OnTerminate()
138 LogDebug("Wrt Shutdown now");
142 void WrtClient::showHelpAndQuit()
144 printf("Usage: wrt-client [OPTION]... [WIDGET: ID]...\n"
146 "Mandatory arguments to long options are mandatory for short "
148 " -h, --help show this help\n"
150 "launch widget with given tizen ID\n"
152 "launch widget with given tizen ID\n"
158 bool WrtClient::checkArgument()
160 LogInfo("checkArgument");
162 std::string arg = m_argv[0];
168 if (arg.find("wrt-client") != std::string::npos)
176 if (arg == "-h" || arg == "--help") {
179 } else if (arg == "-l" || arg == "--launch" ||
180 arg == "-t" || arg == "--tizen") {
184 m_tizenId = std::string(m_argv[2]);
189 size_t pos = arg.find_last_of('/');
191 if (pos != std::string::npos) {
192 arg = arg.erase(0, pos + 1);
195 // Launch widget based on application basename
197 LogDebug("Tizen id: " << m_tizenId);
203 void WrtClient::setStep()
207 AddStep(&WrtClient::initStep);
209 setSdkLauncherDebugData();
211 AddStep(&WrtClient::launchStep);
212 AddStep(&WrtClient::shutdownStep);
214 m_initializing = true;
216 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(NextStepEvent());
219 void WrtClient::setSdkLauncherDebugData()
221 LogDebug("setSdkLauncherDebugData");
223 /* check bundle from sdk launcher */
224 bundle *bundleFromSdkLauncher;
225 bundleFromSdkLauncher = bundle_import_from_argv(m_argc, m_argv);
226 const char *bundle_debug = bundle_get_val(bundleFromSdkLauncher, "debug");
227 const char *bundle_pid = bundle_get_val(bundleFromSdkLauncher, "pid");
228 if (bundle_debug != NULL && bundle_pid != NULL) {
229 if (strcmp(bundle_debug, "true") == 0) {
231 m_sdkLauncherPid = atoi(bundle_pid);
236 bundle_free(bundleFromSdkLauncher);
239 bool WrtClient::checkDebugMode(SDKDebugData* debugData)
241 LogError("Checking for debug mode");
244 bool debugMode = debugData->debugMode;
246 LogInfo("[DEBUG_MODE] Widget is launched in " <<
247 (debugMode ? "DEBUG" : "RETAIL") <<
250 if (debugMode == true) {
251 // In WAC widget, only test widgets can use web inspector.
253 // every launched widgets as debug mode can use it.
254 if (m_dao->getWidgetType().appType == WrtDB::APP_TYPE_WAC20)
257 GlobalLogicSingleton::Instance().GetGlobalModel()
258 ->DeveloperMode.Get();
259 //This code will be activated
260 //after WAC test certificate is used by SDK
261 //bool isTestWidget = view->m_widgetModel->IsTestWidget.Get();
264 // LogInfo("This is not WAC Test Widget");
267 if (!developerMode) {
268 LogInfo("This is not WAC Developer Mode");
276 void WrtClient::OnEventReceived(const NextStepEvent& /*event*/)
278 LogDebug("Executing next step");
282 void WrtClient::initStep()
285 if (WRT::CoreModuleSingleton::Instance().Init()) {
286 m_initialized = true;
288 m_returnStatus = ReturnStatus::Failed;
289 SwitchToStep(&WrtClient::shutdownStep);
292 // ecore_event_jobs are processed sequentially without concession to
293 // other type events. To give a chance of execute to other events,
294 // ecore_timer_add was used.
295 DPL::Event::ControllerEventHandler<NextStepEvent>::PostTimedEvent(
296 NextStepEvent(),0.001);
299 bool WrtClient::checkWACTestCertififedWidget()
301 // WAC Waikiki Beta Release Core Specification: Widget Runtime
304 // WR-4710 The WRT MUST enable debug functions only for WAC test widgets
305 // i.e. the functions must not be usable for normal WAC widgets, even when
306 // a WAC test widget is executing.
307 ADD_PROFILING_POINT("DeveloperModeCheck", "start");
310 // A widget signed with a WAC-issued test certificate as described in
313 bool developerWidget = m_dao->isTestWidget();
315 GlobalLogicSingleton::Instance().GetGlobalModel()->DeveloperMode.Get();
317 LogDebug("Is WAC test widget: " << developerWidget);
318 LogDebug("Is developer Mode: " << developerMode);
320 if (developerWidget) {
323 LogError("WAC test certified developer widget is needed for " <<
327 //TODO: WR-4660 (show popup about developer widget
329 LogInfo("POPUP: THIS IS TEST WIDGET!");
332 ADD_PROFILING_POINT("DeveloperModeCheck", "stop");
336 void WrtClient::loadFinishCallback(bool success)
338 ADD_PROFILING_POINT("loadFinishCallback", "start");
339 SDKDebugData* debug = new SDKDebugData;
340 debug->debugMode = m_debugMode;
341 debug->pid = new unsigned long(getpid());
343 LogInfo("Post result of launch");
345 // Start inspector server, if current mode is debugger mode.
346 // In the WK2 case, ewk_view_inspector_server_start should
347 // be called after WebProcess is created.
348 if (checkDebugMode(debug))
351 ewk_view_inspector_server_start(m_widget->GetCurrentWebview(), 0);
352 if (debug->portnum == 0) {
353 LogWarning("Failed to get portnum");
355 LogInfo("Assigned port number for inspector : "
359 LogDebug("Debug mode is disabled");
362 //w3c packaging test debug (message on 4>)
363 const char * makeScreen = getenv(W3C_DEBUG_ENV_VARIABLE);
364 if(makeScreen != NULL && strcmp(makeScreen, "1") == 0)
366 FILE* doutput = fdopen(4, "w");
367 fprintf(doutput,"didFinishLoadForFrameCallback: ready\n");
372 LogDebug("Launch succesfull");
375 m_initializing = false;
377 ADD_PROFILING_POINT("loadFinishCallback", "stop");
378 printf("launched\n");
383 m_returnStatus = ReturnStatus::Failed;
385 DPL::Event::ControllerEventHandler<NextStepEvent>::
386 PostEvent(NextStepEvent());
391 LogDebug("Send RT signal to wrt-launcher(pid: "
392 << m_sdkLauncherPid << ", status: " << success);
394 /* send real time signal with result to wrt-launcher */
397 LogDebug("userData->portnum : " << debug->portnum);
398 sv.sival_int = debug->portnum;
404 sigqueue(m_sdkLauncherPid, SIGRTMIN, sv);
407 ApplicationDataSingleton::Instance().freeBundle();
409 LogDebug("Cleaning wrtClient launch resources...");
414 void WrtClient::progressFinishCallback()
416 m_splashScreen->stopSplashScreen();
419 void WrtClient::windowCloseCallback()
421 LogDebug("window close called, terminating app");
422 SwitchToStep(&WrtClient::shutdownStep);
423 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
427 void WrtClient::webCrashCallback()
429 LogError("webProcess crashed");
430 SwitchToStep(&WrtClient::shutdownStep);
431 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
436 void WrtClient::launchStep()
438 ADD_PROFILING_POINT("launchStep", "start");
439 LogDebug("Launching widget ...");
441 ADD_PROFILING_POINT("getRunnableWidgetObject", "start");
442 m_widget = WRT::CoreModuleSingleton::Instance()
443 .getRunnableWidgetObject(m_tizenId);
444 ADD_PROFILING_POINT("getRunnableWidgetObject", "stop");
447 LogError("RunnableWidgetObject is NULL, stop launchStep");
448 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
453 if (m_widgetState == WidgetState_Running) {
454 LogWarning("Widget already running, stop launchStep");
455 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
460 if (m_widgetState == WidgetState_Authorizing) {
461 LogWarning("Widget already authorizing, stop launchStep");
462 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
467 ADD_PROFILING_POINT("localizeWidgetModel", "start");
468 m_dao.reset(new WrtDB::WidgetDAOReadOnly(DPL::FromASCIIString(m_tizenId)));
469 Domain::localizeWidgetModel(m_dao->getDefaultlocale());
470 ADD_PROFILING_POINT("localizeWidgetModel", "stop");
471 LocalizationSetting::SetLanguageChangedCallback(
472 languageChangedCallback, this);
474 ADD_PROFILING_POINT("CreateWindow", "start");
475 m_windowData.reset(new WindowData(static_cast<unsigned long>(getpid())));
476 ADD_PROFILING_POINT("CreateWindow", "stop");
478 WRT::UserDelegatesPtr cbs(new WRT::UserDelegates);
479 ADD_PROFILING_POINT("Create splash screen", "start");
480 m_splashScreen.reset(
481 new SplashScreenSupport(m_windowData->m_win));
482 if (m_splashScreen->createSplashScreen(m_dao->getSplashImgSrc())) {
483 m_splashScreen->startSplashScreen();
484 cbs->progressFinish = DPL::MakeDelegate(this, &WrtClient::progressFinishCallback);
486 ADD_PROFILING_POINT("Create splash screen", "stop");
487 DPL::OptionalString startUrl = W3CFileLocalization::getStartFile(m_dao);
488 m_widget->PrepareView(DPL::ToUTF8String(*startUrl),
489 m_windowData->m_win);
490 //you can't show window with splash screen before PrepareView
491 //ewk_view_add_with_context() in viewLogic breaks window
493 WrtDB::WidgetLocalizedInfo localizedInfo =
494 W3CFileLocalization::getLocalizedInfo(m_dao);
495 std:: string name = "";
496 if (!(localizedInfo.name.IsNull())) {
497 name = DPL::ToUTF8String(*(localizedInfo.name));
499 elm_win_title_set(m_windowData->m_win, name.c_str());
500 evas_object_show(m_windowData->m_win);
501 initializeWindowModes();
502 connectElmCallback();
504 if (!checkWACTestCertififedWidget())
506 LogWarning("WAC Certificate failed, stop launchStep");
510 m_widgetState = WidgetState_Authorizing;
511 if (!m_widget->CheckBeforeLaunch()) {
512 LogError("CheckBeforeLaunch failed, stop launchStep");
513 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
517 LogInfo("Widget launch accepted. Entering running state");
518 m_widgetState = WidgetState_Running;
520 cbs->loadFinish = DPL::MakeDelegate(this, &WrtClient::loadFinishCallback);
521 cbs->bufferSet = DPL::MakeDelegate(this, &WrtClient::setLayout);
522 cbs->bufferUnset = DPL::MakeDelegate(this, &WrtClient::unsetLayout);
523 cbs->windowClose = DPL::MakeDelegate(this, &WrtClient::windowCloseCallback);
524 cbs->webCrash = DPL::MakeDelegate(this, &WrtClient::webCrashCallback);
525 cbs->toggleFullscreen = DPL::MakeDelegate(m_windowData.get(), &WindowData::toggleFullscreen);
527 m_widget->SetUserDelegates(cbs);
529 ADD_PROFILING_POINT("launchStep", "stop");
532 void WrtClient::initializeWindowModes()
534 Assert(m_windowData);
536 auto windowModes = m_dao->getWindowModes();
537 bool fullscreen = false;
538 FOREACH(it, windowModes)
540 std::string viewMode = DPL::ToUTF8String(*it);
541 if (viewMode == VIEWMODE_TYPE_FULLSCREEN) {
544 } else if (viewMode == VIEWMODE_TYPE_MAXIMIZED) {
548 bool indicator = true;
549 if (m_dao->getWidgetType().appType == WrtDB::APP_TYPE_TIZENWEBAPP) {
550 WidgetSettings widgetSettings;
551 m_dao->getWidgetSettings(widgetSettings);
552 WidgetSettingList settings(widgetSettings);
553 indicator = (settings.getIndicatorPresence()
554 == Indicator_Enable);
557 WrtDB::WidgetLocalizedInfo localizedInfo =
558 W3CFileLocalization::getLocalizedInfo(m_dao);
559 std::string name = "";
560 if (!(localizedInfo.name.IsNull())) {
561 name = DPL::ToUTF8String(*(localizedInfo.name));
563 LogInfo("initializeWindowModes " << m_debugMode);
566 m_windowData->m_debugMode = TRUE;
569 m_windowData->m_debugMode = FALSE;
572 WindowData::CtxMenuItems ctxMenuItems;
574 WindowData::CtxMenuItem ctxMenuBackword;
575 ctxMenuBackword.label = WRT_OPTION_LABEL_BACKWARD;
576 ctxMenuBackword.icon = WRT_OPTION_ICON_BACKWARD;
577 ctxMenuBackword.callback = backwardCallback;
578 ctxMenuBackword.data = this;
579 ctxMenuItems.push_back(ctxMenuBackword);
581 WindowData::CtxMenuItem ctxMenuReload;
582 ctxMenuReload.label = WRT_OPTION_LABEL_RELOAD;
583 ctxMenuReload.icon = WRT_OPTION_ICON_RELOAD;
584 ctxMenuReload.callback = reloadCallback;
585 ctxMenuReload.data = this;
586 ctxMenuItems.push_back(ctxMenuReload);
588 WindowData::CtxMenuItem ctxMenuForward;
589 ctxMenuForward.label = WRT_OPTION_LABEL_FORWARD;
590 ctxMenuForward.icon = WRT_OPTION_ICON_FORWARD;
591 ctxMenuForward.callback = forwardCallback;
592 ctxMenuForward.data = this;
593 ctxMenuItems.push_back(ctxMenuForward);
595 m_windowData->setViewMode(fullscreen,
599 void WrtClient::backButtonCallback(void* data,
600 Evas_Object * /*obj*/,
601 void * /*event_info*/)
603 LogInfo("BackButtonCallback");
606 WrtClient* This = static_cast<WrtClient*>(data);
608 This->m_widget->Backward();
611 void WrtClient::backwardCallback(void *data,
612 Evas_Object */*obj*/,
613 void */*event_info*/)
615 LogInfo("BackButtonCallback");
618 WrtClient* This = static_cast<WrtClient*>(data);
620 This->m_widget->Backward();
621 This->m_windowData->initFullViewMode();
624 void WrtClient::reloadCallback(void *data,
625 Evas_Object */*obj*/,
626 void */*event_info*/)
628 LogInfo("reloadCallback");
630 WrtClient* This = static_cast<WrtClient*>(data);
632 This->m_widget->Reload();
633 This->m_windowData->initFullViewMode();
636 void WrtClient::forwardCallback(void *data,
637 Evas_Object */*obj*/,
638 void */*event_info*/)
640 LogInfo("forwardCallback");
642 WrtClient* This = static_cast<WrtClient*>(data);
644 This->m_widget->Forward();
645 This->m_windowData->initFullViewMode();
649 void WrtClient::connectElmCallback()
651 Assert(m_windowData);
653 if (m_dao->getWidgetType().appType == WrtDB::APP_TYPE_TIZENWEBAPP) {
654 WidgetSettings widgetSettings;
655 m_dao->getWidgetSettings(widgetSettings);
656 WidgetSettingList settings(widgetSettings);
657 WidgetSettingScreenLock rotationValue = settings.getRotationValue();
658 if (rotationValue == Screen_Portrait) {
659 elm_win_rotation_with_resize_set(m_windowData->m_win, 0);
660 } else if (rotationValue == Screen_Landscape) {
661 elm_win_rotation_with_resize_set(m_windowData->m_win, 270);
663 elm_win_rotation_with_resize_set(m_windowData->m_win, 0);
668 void WrtClient::setLayout(Evas_Object* newBuffer) {
669 LogDebug("add new webkit buffer to window");
672 elm_object_part_content_set(m_windowData->m_user_layout, ELM_SWALLOW_CONTENT, newBuffer);
674 evas_object_show(newBuffer);
675 evas_object_focus_set(newBuffer, EINA_TRUE);
678 void WrtClient::unsetLayout(Evas_Object* currentBuffer) {
679 LogDebug("remove current webkit buffer from window");
680 Assert(currentBuffer);
681 evas_object_hide(currentBuffer);
683 elm_object_part_content_unset(m_windowData->m_user_layout, ELM_SWALLOW_CONTENT);
687 void WrtClient::shutdownStep()
689 LogDebug("Closing Wrt connection ...");
690 if (m_initialized && m_widget) {
691 m_widgetState = WidgetState_Stopped;
694 m_windowData.reset();
695 WRT::CoreModuleSingleton::Instance().Terminate();
696 m_initialized = false;
701 int WrtClient::languageChangedCallback(void *data)
703 LogDebug("Language Changed");
704 WrtClient* wrtClient = static_cast<WrtClient*>(data);
706 LanguageTags oldtags = LanguageTagsProviderSingleton::Instance().getLanguageTags();
707 // reset function fetches system locales and recreates language tags
708 LanguageTagsProviderSingleton::Instance().resetLanguageTags();
709 LanguageTags newtags = LanguageTagsProviderSingleton::Instance().getLanguageTags();
711 // check whether LanguageTags changed or not
712 if (oldtags != newtags) {
713 // update localized data
714 Domain::localizeWidgetModel(wrtClient->m_dao->getDefaultlocale());
716 if (true == wrtClient->m_launched &&
717 wrtClient->m_widgetState != WidgetState_Stopped) {
718 wrtClient->m_widget->ReloadStartPage();
727 ADD_PROFILING_POINT("main-entered", "point");
729 // Output on stdout will be flushed after every newline character,
730 // even if it is redirected to a pipe. This is useful for running
731 // from a script and parsing output.
732 // (Standard behavior of stdlib is to use full buffering when
733 // redirected to a pipe, which means even after an end of line
734 // the output may not be flushed).
737 // set evas backend type
738 if (!getenv("ELM_ENGINE")) {
739 if (setenv("ELM_ENGINE", "gl", 1)) {
740 LogDebug("Enable backend");
744 setenv("COREGL_FASTPATH", "1", 1);
746 setenv("CAIRO_GL_COMPOSITOR", "msaa", 1);
747 setenv("CAIRO_GL_LAZY_FLUSHING", "yes", 1);
748 setenv("ELM_IMAGE_CACHE", "0", 1);
751 DPL::Log::LogSystemSingleton::Instance().SetTag("WRT-CLIENT");
753 WrtClient app(argc, argv);
755 ADD_PROFILING_POINT("Before appExec", "point");
756 int ret = app.Exec();
757 LogDebug("App returned: " << ret);
758 ret = app.getReturnStatus();
759 LogDebug("WrtClient returned: " << ret);