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_model.h>
26 #include <widget_localize_model.h>
27 #include <localization_setting.h>
28 #include <widget_deserialize_model.h>
29 #include <launch_user_data.h>
31 #include <dpl/localization/w3c_file_localization.h>
32 #include <dpl/localization/LanguageTagsProvider.h>
34 //W3C PACKAGING enviroment variable name
35 #define W3C_DEBUG_ENV_VARIABLE "DEBUG_LOAD_FINISH"
37 // window signal callback
38 const char *EDJE_SHOW_BACKWARD_SIGNAL = "show,backward,signal";
39 const std::string VIEWMODE_TYPE_FULLSCREEN = "fullscreen";
40 const std::string VIEWMODE_TYPE_MAXIMIZED = "maximized";
42 WrtClient::WrtClient(int argc, char **argv) :
43 Application(argc, argv, "wrt-client", false),
44 DPL::TaskDecl<WrtClient>(this),
46 m_initializing(false),
51 m_returnStatus(ReturnStatus::Succeeded),
52 m_widgetState(WidgetState::WidgetState_Stopped)
55 LogDebug("App Created");
58 WrtClient::~WrtClient()
60 LogDebug("App Finished");
63 WrtClient::ReturnStatus::Type WrtClient::getReturnStatus() const
65 return m_returnStatus;
68 void WrtClient::OnStop()
70 LogInfo("Stopping Dummy Client");
74 void WrtClient::OnCreate()
80 void WrtClient::OnResume()
82 if (m_widgetState != WidgetState_Suspended) {
83 LogWarning("Widget is not suspended, resuming was skipped");
87 evas_object_focus_set(m_widget->GetCurrentWebview(), EINA_TRUE);
88 m_widgetState = WidgetState_Running;
92 void WrtClient::OnPause()
94 if (m_widgetState != WidgetState_Running) {
95 LogWarning("Widget is not running to be suspended");
99 evas_object_focus_set(m_widget->GetCurrentWebview(), EINA_FALSE);
100 m_widgetState = WidgetState_Suspended;
103 void WrtClient::OnReset(bundle *b)
106 // bundle argument is freed after OnReset() is returned
107 // So bundle duplication is needed
108 ApplicationDataSingleton::Instance().setBundle(bundle_dup(b));
110 if (true == m_initializing) {
111 LogDebug("can not handle reset event");
114 if (true == m_launched) {
115 if (m_widgetState == WidgetState_Stopped) {
116 LogError("Widget is not running to be reset");
120 m_windowData->emitSignalForUserLayout(EDJE_SHOW_BACKWARD_SIGNAL, "");
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);
238 bool WrtClient::checkDebugMode(SDKDebugData* debugData)
240 LogError("Checking for debug mode");
243 bool debugMode = debugData->debugMode;
245 LogInfo("[DEBUG_MODE] Widget is launched in " <<
246 (debugMode ? "DEBUG" : "RETAIL") <<
249 if (debugMode == true) {
250 // In WAC widget, only test widgets can use web inspector.
252 // every launched widgets as debug mode can use it.
253 if (m_dao->getWidgetType().appType == WrtDB::APP_TYPE_WAC20)
256 GlobalLogicSingleton::Instance().GetGlobalModel()
257 ->DeveloperMode.Get();
258 //This code will be activated
259 //after WAC test certificate is used by SDK
260 //bool isTestWidget = view->m_widgetModel->IsTestWidget.Get();
263 // LogInfo("This is not WAC Test Widget");
266 if (!developerMode) {
267 LogInfo("This is not WAC Developer Mode");
275 void WrtClient::OnEventReceived(const NextStepEvent& /*event*/)
277 LogDebug("Executing next step");
281 void WrtClient::initStep()
284 if (WRT::CoreModuleSingleton::Instance().Init()) {
285 m_initialized = true;
287 m_returnStatus = ReturnStatus::Failed;
288 SwitchToStep(&WrtClient::shutdownStep);
291 // ecore_event_jobs are processed sequentially without concession to
292 // other type events. To give a chance of execute to other events,
293 // ecore_timer_add was used.
294 DPL::Event::ControllerEventHandler<NextStepEvent>::PostTimedEvent(
295 NextStepEvent(),0.001);
298 bool WrtClient::checkWACTestCertififedWidget()
300 // WAC Waikiki Beta Release Core Specification: Widget Runtime
303 // WR-4710 The WRT MUST enable debug functions only for WAC test widgets
304 // i.e. the functions must not be usable for normal WAC widgets, even when
305 // a WAC test widget is executing.
306 ADD_PROFILING_POINT("DeveloperModeCheck", "start");
309 // A widget signed with a WAC-issued test certificate as described in
312 bool developerWidget = m_dao->isTestWidget();
314 GlobalLogicSingleton::Instance().GetGlobalModel()->DeveloperMode.Get();
316 LogDebug("Is WAC test widget: " << developerWidget);
317 LogDebug("Is developer Mode: " << developerMode);
319 if (developerWidget) {
322 LogError("WAC test certified developer widget is needed for " <<
326 //TODO: WR-4660 (show popup about developer widget
328 LogInfo("POPUP: THIS IS TEST WIDGET!");
331 ADD_PROFILING_POINT("DeveloperModeCheck", "stop");
335 void WrtClient::loadFinishCallback(bool success)
337 ADD_PROFILING_POINT("loadFinishCallback", "start");
338 SDKDebugData* debug = new SDKDebugData;
339 debug->debugMode = m_debugMode;
340 debug->pid = new unsigned long(getpid());
342 LogInfo("Post result of launch");
344 // Start inspector server, if current mode is debugger mode.
345 // In the WK2 case, ewk_view_inspector_server_start should
346 // be called after WebProcess is created.
347 if (checkDebugMode(debug))
350 ewk_view_inspector_server_start(m_widget->GetCurrentWebview(), 0);
351 if (debug->portnum == 0) {
352 LogWarning("Failed to get portnum");
354 LogInfo("Assigned port number for inspector : "
358 LogDebug("Debug mode is disabled");
361 //w3c packaging test debug (message on 4>)
362 const char * makeScreen = getenv(W3C_DEBUG_ENV_VARIABLE);
363 if(makeScreen != NULL && strcmp(makeScreen, "1") == 0)
365 FILE* doutput = fdopen(4, "w");
366 fprintf(doutput,"didFinishLoadForFrameCallback: ready\n");
371 LogDebug("Launch succesfull");
374 m_initializing = false;
376 ADD_PROFILING_POINT("loadFinishCallback", "stop");
377 printf("launched\n");
382 m_returnStatus = ReturnStatus::Failed;
384 DPL::Event::ControllerEventHandler<NextStepEvent>::
385 PostEvent(NextStepEvent());
390 LogDebug("Send RT signal to wrt-launcher(pid: "
391 << m_sdkLauncherPid << ", status: " << success);
393 /* send real time signal with result to wrt-launcher */
396 LogDebug("userData->portnum : " << debug->portnum);
397 sv.sival_int = debug->portnum;
403 sigqueue(m_sdkLauncherPid, SIGRTMIN, sv);
406 ApplicationDataSingleton::Instance().freeBundle();
408 LogDebug("Cleaning wrtClient launch resources...");
413 void WrtClient::progressFinishCallback()
415 m_splashScreen->stopSplashScreen();
418 void WrtClient::windowCloseCallback()
420 LogDebug("window close called, terminating app");
421 SwitchToStep(&WrtClient::shutdownStep);
422 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
426 void WrtClient::webCrashCallback()
428 LogDebug("webProcess crashed");
429 SwitchToStep(&WrtClient::shutdownStep);
430 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
435 void WrtClient::launchStep()
437 ADD_PROFILING_POINT("launchStep", "start");
438 LogDebug("Launching widget ...");
440 m_widget = WRT::CoreModuleSingleton::Instance()
441 .getRunnableWidgetObject(m_tizenId);
443 LogError("RunnableWidgetObject is NULL, stop launchStep");
444 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
449 if (m_widgetState == WidgetState_Running) {
450 LogWarning("Widget already running, stop launchStep");
451 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
456 if (m_widgetState == WidgetState_Authorizing) {
457 LogWarning("Widget already authorizing, stop launchStep");
458 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
463 m_dao.reset(new WrtDB::WidgetDAOReadOnly(DPL::FromASCIIString(m_tizenId)));
464 Domain::localizeWidgetModel(m_dao->getDefaultlocale());
465 LocalizationSetting::SetLanguageChangedCallback(
466 languageChangedCallback, this);
468 ADD_PROFILING_POINT("CreateWindow", "start");
469 m_windowData.reset(new WindowData(static_cast<unsigned long>(getpid())));
470 ADD_PROFILING_POINT("CreateWindow", "stop");
472 WRT::UserDelegatesPtr cbs(new WRT::UserDelegates);
473 m_splashScreen.reset(
474 new SplashScreenSupport(m_windowData->m_win));
475 if (m_splashScreen->createSplashScreen(m_dao->getSplashImgSrc())) {
476 m_splashScreen->startSplashScreen();
477 cbs->progressFinish = DPL::MakeDelegate(this, &WrtClient::progressFinishCallback);
479 DPL::OptionalString startUrl = W3CFileLocalization::getStartFile(m_dao);
480 m_widget->PrepareView(DPL::ToUTF8String(*startUrl),
481 m_windowData->m_win);
482 //you can't show window with splash screen before PrepareView
483 //ewk_view_add_with_context() in viewLogic breaks window
484 evas_object_show(m_windowData->m_win);
485 initializeWindowModes();
486 connectElmCallback();
488 if (!checkWACTestCertififedWidget())
490 LogWarning("WAC Certificate failed, stop launchStep");
494 m_widgetState = WidgetState_Authorizing;
495 if (!m_widget->CheckBeforeLaunch()) {
496 LogError("CheckBeforeLaunch failed, stop launchStep");
497 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
501 LogInfo("Widget launch accepted. Entering running state");
502 m_widgetState = WidgetState_Running;
504 cbs->loadFinish = DPL::MakeDelegate(this, &WrtClient::loadFinishCallback);
505 cbs->bufferSet = DPL::MakeDelegate(this, &WrtClient::setLayout);
506 cbs->bufferUnset = DPL::MakeDelegate(this, &WrtClient::unsetLayout);
507 cbs->windowClose = DPL::MakeDelegate(this, &WrtClient::windowCloseCallback);
508 cbs->webCrash = DPL::MakeDelegate(this, &WrtClient::webCrashCallback);
509 cbs->toggleFullscreen = DPL::MakeDelegate(m_windowData.get(), &WindowData::toggleFullscreen);
511 m_widget->SetUserDelegates(cbs);
513 m_windowData->emitSignalForUserLayout(EDJE_SHOW_BACKWARD_SIGNAL, "");
514 ADD_PROFILING_POINT("launchStep", "stop");
517 void WrtClient::initializeWindowModes()
519 Assert(m_windowData);
521 auto windowModes = m_dao->getWindowModes();
522 bool fullscreen = false;
523 FOREACH(it, windowModes)
525 std::string viewMode = DPL::ToUTF8String(*it);
526 if (viewMode == VIEWMODE_TYPE_FULLSCREEN) {
529 } else if (viewMode == VIEWMODE_TYPE_MAXIMIZED) {
533 bool indicator = true;
534 bool backbutton = false;
535 if (m_dao->getWidgetType().appType == WrtDB::APP_TYPE_TIZENWEBAPP) {
536 WidgetSettings widgetSettings;
537 m_dao->getWidgetSettings(widgetSettings);
538 WidgetSettingList settings(widgetSettings);
539 indicator = (settings.getIndicatorPresence()
540 == Indicator_Enable);
541 backbutton = (settings.getBackButtonPresence()
542 == BackButton_Enable);
545 WrtDB::WidgetLocalizedInfo localizedInfo =
546 W3CFileLocalization::getLocalizedInfo(m_dao);
547 std::string name = "";
548 if (!(localizedInfo.name.IsNull())) {
549 name = DPL::ToUTF8String(*(localizedInfo.name));
551 LogInfo("initializeWindowModes " << m_debugMode);
553 SDKDebugData* debug = new SDKDebugData;
555 m_windowData->m_debugMode = TRUE;
558 m_windowData->m_debugMode = FALSE;
561 WindowData::CtxMenuItems ctxMenuItems;
563 WindowData::CtxMenuItem ctxMenuBackword;
564 ctxMenuBackword.label = WRT_OPTION_LABEL_BACKWARD;
565 ctxMenuBackword.icon = WRT_OPTION_ICON_BACKWARD;
566 ctxMenuBackword.callback = backwardCallback;
567 ctxMenuBackword.data = this;
568 ctxMenuItems.push_back(ctxMenuBackword);
570 WindowData::CtxMenuItem ctxMenuReload;
571 ctxMenuReload.label = WRT_OPTION_LABEL_RELOAD;
572 ctxMenuReload.icon = WRT_OPTION_ICON_RELOAD;
573 ctxMenuReload.callback = reloadCallback;
574 ctxMenuReload.data = this;
575 ctxMenuItems.push_back(ctxMenuReload);
577 WindowData::CtxMenuItem ctxMenuForward;
578 ctxMenuForward.label = WRT_OPTION_LABEL_FORWARD;
579 ctxMenuForward.icon = WRT_OPTION_ICON_FORWARD;
580 ctxMenuForward.callback = forwardCallback;
581 ctxMenuForward.data = this;
582 ctxMenuItems.push_back(ctxMenuForward);
584 m_windowData->setViewMode(name.c_str(), fullscreen, indicator, backbutton, ctxMenuItems);
588 void WrtClient::backButtonCallback(void* data,
589 Evas_Object * /*obj*/,
590 void * /*event_info*/)
592 LogInfo("BackButtonCallback");
595 WrtClient* This = static_cast<WrtClient*>(data);
597 This->m_widget->Backward();
600 void WrtClient::backwardCallback(void *data,
601 Evas_Object */*obj*/,
602 void */*event_info*/)
604 LogInfo("BackButtonCallback");
607 WrtClient* This = static_cast<WrtClient*>(data);
609 This->m_widget->Backward();
610 This->m_windowData->initFullViewMode();
613 void WrtClient::reloadCallback(void *data,
614 Evas_Object */*obj*/,
615 void */*event_info*/)
617 LogInfo("reloadCallback");
619 WrtClient* This = static_cast<WrtClient*>(data);
621 This->m_widget->Reload();
622 This->m_windowData->initFullViewMode();
625 void WrtClient::forwardCallback(void *data,
626 Evas_Object */*obj*/,
627 void */*event_info*/)
629 LogInfo("forwardCallback");
631 WrtClient* This = static_cast<WrtClient*>(data);
633 This->m_widget->Forward();
634 This->m_windowData->initFullViewMode();
638 void WrtClient::connectElmCallback()
640 Assert(m_windowData);
642 if (m_dao->getWidgetType().appType == WrtDB::APP_TYPE_TIZENWEBAPP) {
643 WidgetSettings widgetSettings;
644 m_dao->getWidgetSettings(widgetSettings);
645 WidgetSettingList settings(widgetSettings);
646 if (settings.getBackButtonPresence() ==
649 m_windowData->addFloatBackButtonCallback(
651 &WrtClient::backButtonCallback,
655 WidgetSettingScreenLock rotationValue = settings.getRotationValue();
656 if (rotationValue == Screen_Portrait) {
657 elm_win_rotation_with_resize_set(m_windowData->m_win, 0);
658 } else if (rotationValue == Screen_Landscape) {
659 elm_win_rotation_with_resize_set(m_windowData->m_win, 270);
661 elm_win_rotation_with_resize_set(m_windowData->m_win, 0);
666 void WrtClient::setLayout(Evas_Object* newBuffer) {
667 LogDebug("add new webkit buffer to window");
669 elm_object_content_set(m_windowData->m_conformant, newBuffer);
670 evas_object_show(newBuffer);
671 evas_object_focus_set(newBuffer, EINA_TRUE);
674 void WrtClient::unsetLayout(Evas_Object* currentBuffer) {
675 LogDebug("remove current webkit buffer from window");
676 Assert(currentBuffer);
677 evas_object_hide(currentBuffer);
678 elm_object_content_unset(m_windowData->m_conformant);
681 void WrtClient::shutdownStep()
683 LogDebug("Closing Wrt connection ...");
684 if (m_initialized && m_widget) {
685 m_widgetState = WidgetState_Stopped;
688 m_windowData.reset();
689 WRT::CoreModuleSingleton::Instance().Terminate();
690 m_initialized = false;
695 int WrtClient::languageChangedCallback(void *data)
697 LogDebug("Language Changed");
698 WrtClient* wrtClient = static_cast<WrtClient*>(data);
700 LanguageTags oldtags = LanguageTagsProviderSingleton::Instance().getLanguageTags();
701 // reset function fetches system locales and recreates language tags
702 LanguageTagsProviderSingleton::Instance().resetLanguageTags();
703 LanguageTags newtags = LanguageTagsProviderSingleton::Instance().getLanguageTags();
705 // check whether LanguageTags changed or not
706 if (oldtags != newtags) {
707 // update localized data
708 Domain::localizeWidgetModel(wrtClient->m_dao->getDefaultlocale());
710 if (true == wrtClient->m_launched &&
711 wrtClient->m_widgetState != WidgetState_Stopped) {
712 wrtClient->m_widget->ReloadStartPage();
721 ADD_PROFILING_POINT("main-entered", "point");
723 // Output on stdout will be flushed after every newline character,
724 // even if it is redirected to a pipe. This is useful for running
725 // from a script and parsing output.
726 // (Standard behavior of stdlib is to use full buffering when
727 // redirected to a pipe, which means even after an end of line
728 // the output may not be flushed).
731 // set evas backend type
732 if (!getenv("ELM_ENGINE")) {
733 if (setenv("ELM_ENGINE", "gl", 1)) {
734 LogDebug("Enable backend");
737 setenv("CAIRO_GL_COMPOSITOR", "msaa", 1);
738 setenv("CAIRO_GL_LAZY_FLUSHING", "yes", 1);
740 DPL::Log::LogSystemSingleton::Instance().SetTag("WRT-CLIENT");
742 WrtClient app(argc, argv);
743 int ret = app.Exec();
744 LogDebug("App returned: " << ret);
745 ret = app.getReturnStatus();
746 LogDebug("WrtClient returned: " << ret);