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 <localization_setting.h>
26 #include <widget_deserialize_model.h>
28 #include <dpl/localization/w3c_file_localization.h>
29 #include <dpl/localization/LanguageTagsProvider.h>
31 //W3C PACKAGING enviroment variable name
32 #define W3C_DEBUG_ENV_VARIABLE "DEBUG_LOAD_FINISH"
34 // window signal callback
35 const std::string VIEWMODE_TYPE_FULLSCREEN = "fullscreen";
36 const std::string VIEWMODE_TYPE_MAXIMIZED = "maximized";
37 char const* const ELM_SWALLOW_CONTENT = "elm.swallow.content";
39 WrtClient::WrtClient(int argc, char **argv) :
40 Application(argc, argv, "wrt-client", false),
41 DPL::TaskDecl<WrtClient>(this),
43 m_initializing(false),
48 m_returnStatus(ReturnStatus::Succeeded),
49 m_widgetState(WidgetState::WidgetState_Stopped)
52 LogDebug("App Created");
55 WrtClient::~WrtClient()
57 LogDebug("App Finished");
60 WrtClient::ReturnStatus::Type WrtClient::getReturnStatus() const
62 return m_returnStatus;
65 void WrtClient::OnStop()
67 LogInfo("Stopping Dummy Client");
71 void WrtClient::OnCreate()
74 ADD_PROFILING_POINT("OnCreate callback", "point");
78 void WrtClient::OnResume()
80 if (m_widgetState != WidgetState_Suspended) {
81 LogWarning("Widget is not suspended, resuming was skipped");
85 evas_object_focus_set(m_widget->GetCurrentWebview(), EINA_TRUE);
86 m_widgetState = WidgetState_Running;
90 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 elm_win_raise(m_windowData->m_win);
121 evas_object_focus_set(m_widget->GetCurrentWebview(), EINA_TRUE);
122 m_widgetState = WidgetState_Running;
124 if (true == checkArgument())
135 void WrtClient::OnTerminate()
137 LogDebug("Wrt Shutdown now");
141 void WrtClient::showHelpAndQuit()
143 printf("Usage: wrt-client [OPTION]... [WIDGET: ID]...\n"
145 "Mandatory arguments to long options are mandatory for short "
147 " -h, --help show this help\n"
149 "launch widget with given tizen ID\n"
151 "launch widget with given tizen ID\n"
157 bool WrtClient::checkArgument()
159 LogInfo("checkArgument");
161 std::string arg = m_argv[0];
167 if (arg.find("wrt-client") != std::string::npos)
175 if (arg == "-h" || arg == "--help") {
178 } else if (arg == "-l" || arg == "--launch" ||
179 arg == "-t" || arg == "--tizen") {
183 m_tizenId = std::string(m_argv[2]);
188 size_t pos = arg.find_last_of('/');
190 if (pos != std::string::npos) {
191 arg = arg.erase(0, pos + 1);
194 // Launch widget based on application basename
196 LogDebug("Tizen id: " << m_tizenId);
202 void WrtClient::setStep()
206 AddStep(&WrtClient::initStep);
208 setSdkLauncherDebugData();
210 AddStep(&WrtClient::launchStep);
211 AddStep(&WrtClient::shutdownStep);
213 m_initializing = true;
215 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(NextStepEvent());
218 void WrtClient::setSdkLauncherDebugData()
220 LogDebug("setSdkLauncherDebugData");
222 /* check bundle from sdk launcher */
223 bundle *bundleFromSdkLauncher;
224 bundleFromSdkLauncher = bundle_import_from_argv(m_argc, m_argv);
225 const char *bundle_debug = bundle_get_val(bundleFromSdkLauncher, "debug");
226 const char *bundle_pid = bundle_get_val(bundleFromSdkLauncher, "pid");
227 if (bundle_debug != NULL && bundle_pid != NULL) {
228 if (strcmp(bundle_debug, "true") == 0) {
230 m_sdkLauncherPid = atoi(bundle_pid);
235 bundle_free(bundleFromSdkLauncher);
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 LogError("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 ADD_PROFILING_POINT("getRunnableWidgetObject", "start");
441 m_widget = WRT::CoreModuleSingleton::Instance()
442 .getRunnableWidgetObject(m_tizenId);
443 ADD_PROFILING_POINT("getRunnableWidgetObject", "stop");
446 LogError("RunnableWidgetObject is NULL, stop launchStep");
447 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
452 if (m_widgetState == WidgetState_Running) {
453 LogWarning("Widget already running, stop launchStep");
454 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
459 if (m_widgetState == WidgetState_Authorizing) {
460 LogWarning("Widget already authorizing, stop launchStep");
461 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
466 m_dao.reset(new WrtDB::WidgetDAOReadOnly(DPL::FromASCIIString(m_tizenId)));
467 DPL::Optional<DPL::String> defloc = m_dao->getDefaultlocale();
470 LanguageTagsProviderSingleton::Instance().addWidgetDefaultLocales(*defloc);
473 LocalizationSetting::SetLanguageChangedCallback(
474 languageChangedCallback, this);
476 ADD_PROFILING_POINT("CreateWindow", "start");
477 m_windowData.reset(new WindowData(static_cast<unsigned long>(getpid())));
478 ADD_PROFILING_POINT("CreateWindow", "stop");
480 WRT::UserDelegatesPtr cbs(new WRT::UserDelegates);
481 ADD_PROFILING_POINT("Create splash screen", "start");
482 m_splashScreen.reset(
483 new SplashScreenSupport(m_windowData->m_win));
484 if (m_splashScreen->createSplashScreen(m_dao->getSplashImgSrc())) {
485 m_splashScreen->startSplashScreen();
486 cbs->progressFinish = DPL::MakeDelegate(this, &WrtClient::progressFinishCallback);
488 ADD_PROFILING_POINT("Create splash screen", "stop");
489 DPL::OptionalString startUrl = W3CFileLocalization::getStartFile(m_dao);
490 m_widget->PrepareView(DPL::ToUTF8String(*startUrl),
491 m_windowData->m_win);
492 //you can't show window with splash screen before PrepareView
493 //ewk_view_add_with_context() in viewLogic breaks window
495 WrtDB::WidgetLocalizedInfo localizedInfo =
496 W3CFileLocalization::getLocalizedInfo(m_dao);
497 std:: string name = "";
498 if (!(localizedInfo.name.IsNull())) {
499 name = DPL::ToUTF8String(*(localizedInfo.name));
501 elm_win_title_set(m_windowData->m_win, name.c_str());
502 evas_object_show(m_windowData->m_win);
503 initializeWindowModes();
504 connectElmCallback();
506 if (!checkWACTestCertififedWidget())
508 LogWarning("WAC Certificate failed, stop launchStep");
512 m_widgetState = WidgetState_Authorizing;
513 if (!m_widget->CheckBeforeLaunch()) {
514 LogError("CheckBeforeLaunch failed, stop launchStep");
515 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
519 LogInfo("Widget launch accepted. Entering running state");
520 m_widgetState = WidgetState_Running;
522 cbs->loadFinish = DPL::MakeDelegate(this, &WrtClient::loadFinishCallback);
523 cbs->bufferSet = DPL::MakeDelegate(this, &WrtClient::setLayout);
524 cbs->bufferUnset = DPL::MakeDelegate(this, &WrtClient::unsetLayout);
525 cbs->windowClose = DPL::MakeDelegate(this, &WrtClient::windowCloseCallback);
526 cbs->webCrash = DPL::MakeDelegate(this, &WrtClient::webCrashCallback);
527 cbs->toggleFullscreen = DPL::MakeDelegate(m_windowData.get(), &WindowData::toggleFullscreen);
529 m_widget->SetUserDelegates(cbs);
531 ADD_PROFILING_POINT("launchStep", "stop");
534 void WrtClient::initializeWindowModes()
536 Assert(m_windowData);
538 auto windowModes = m_dao->getWindowModes();
539 bool fullscreen = false;
540 FOREACH(it, windowModes)
542 std::string viewMode = DPL::ToUTF8String(*it);
543 if (viewMode == VIEWMODE_TYPE_FULLSCREEN) {
546 } else if (viewMode == VIEWMODE_TYPE_MAXIMIZED) {
550 bool indicator = true;
551 if (m_dao->getWidgetType().appType == WrtDB::APP_TYPE_TIZENWEBAPP) {
552 WidgetSettings widgetSettings;
553 m_dao->getWidgetSettings(widgetSettings);
554 WidgetSettingList settings(widgetSettings);
555 indicator = (settings.getIndicatorPresence()
556 == Indicator_Enable);
559 WrtDB::WidgetLocalizedInfo localizedInfo =
560 W3CFileLocalization::getLocalizedInfo(m_dao);
561 std::string name = "";
562 if (!(localizedInfo.name.IsNull())) {
563 name = DPL::ToUTF8String(*(localizedInfo.name));
565 LogInfo("initializeWindowModes " << m_debugMode);
568 m_windowData->m_debugMode = TRUE;
571 m_windowData->m_debugMode = FALSE;
574 WindowData::CtxMenuItems ctxMenuItems;
576 WindowData::CtxMenuItem ctxMenuBackword;
577 ctxMenuBackword.label = WRT_OPTION_LABEL_BACKWARD;
578 ctxMenuBackword.icon = WRT_OPTION_ICON_BACKWARD;
579 ctxMenuBackword.callback = backwardCallback;
580 ctxMenuBackword.data = this;
581 ctxMenuItems.push_back(ctxMenuBackword);
583 WindowData::CtxMenuItem ctxMenuReload;
584 ctxMenuReload.label = WRT_OPTION_LABEL_RELOAD;
585 ctxMenuReload.icon = WRT_OPTION_ICON_RELOAD;
586 ctxMenuReload.callback = reloadCallback;
587 ctxMenuReload.data = this;
588 ctxMenuItems.push_back(ctxMenuReload);
590 WindowData::CtxMenuItem ctxMenuForward;
591 ctxMenuForward.label = WRT_OPTION_LABEL_FORWARD;
592 ctxMenuForward.icon = WRT_OPTION_ICON_FORWARD;
593 ctxMenuForward.callback = forwardCallback;
594 ctxMenuForward.data = this;
595 ctxMenuItems.push_back(ctxMenuForward);
597 m_windowData->setViewMode(fullscreen,
601 void WrtClient::backButtonCallback(void* data,
602 Evas_Object * /*obj*/,
603 void * /*event_info*/)
605 LogInfo("BackButtonCallback");
608 WrtClient* This = static_cast<WrtClient*>(data);
610 This->m_widget->Backward();
613 void WrtClient::backwardCallback(void *data,
614 Evas_Object */*obj*/,
615 void */*event_info*/)
617 LogInfo("BackButtonCallback");
620 WrtClient* This = static_cast<WrtClient*>(data);
622 This->m_widget->Backward();
623 This->m_windowData->initFullViewMode();
626 void WrtClient::reloadCallback(void *data,
627 Evas_Object */*obj*/,
628 void */*event_info*/)
630 LogInfo("reloadCallback");
632 WrtClient* This = static_cast<WrtClient*>(data);
634 This->m_widget->Reload();
635 This->m_windowData->initFullViewMode();
638 void WrtClient::forwardCallback(void *data,
639 Evas_Object */*obj*/,
640 void */*event_info*/)
642 LogInfo("forwardCallback");
644 WrtClient* This = static_cast<WrtClient*>(data);
646 This->m_widget->Forward();
647 This->m_windowData->initFullViewMode();
651 void WrtClient::connectElmCallback()
653 Assert(m_windowData);
655 if (m_dao->getWidgetType().appType == WrtDB::APP_TYPE_TIZENWEBAPP) {
656 WidgetSettings widgetSettings;
657 m_dao->getWidgetSettings(widgetSettings);
658 WidgetSettingList settings(widgetSettings);
659 WidgetSettingScreenLock rotationValue = settings.getRotationValue();
660 if (rotationValue == Screen_Portrait) {
661 elm_win_rotation_with_resize_set(m_windowData->m_win, 0);
662 } else if (rotationValue == Screen_Landscape) {
663 elm_win_rotation_with_resize_set(m_windowData->m_win, 270);
665 elm_win_rotation_with_resize_set(m_windowData->m_win, 0);
670 void WrtClient::setLayout(Evas_Object* newBuffer) {
671 LogDebug("add new webkit buffer to window");
674 elm_object_part_content_set(m_windowData->m_user_layout, ELM_SWALLOW_CONTENT, newBuffer);
676 evas_object_show(newBuffer);
677 evas_object_focus_set(newBuffer, EINA_TRUE);
680 void WrtClient::unsetLayout(Evas_Object* currentBuffer) {
681 LogDebug("remove current webkit buffer from window");
682 Assert(currentBuffer);
683 evas_object_hide(currentBuffer);
685 elm_object_part_content_unset(m_windowData->m_user_layout, ELM_SWALLOW_CONTENT);
689 void WrtClient::shutdownStep()
691 LogDebug("Closing Wrt connection ...");
692 if (m_initialized && m_widget) {
693 m_widgetState = WidgetState_Stopped;
696 m_windowData.reset();
697 WRT::CoreModuleSingleton::Instance().Terminate();
698 m_initialized = false;
703 int WrtClient::languageChangedCallback(void *data)
705 LogDebug("Language Changed");
708 WrtClient* wrtClient = static_cast<WrtClient*>(data);
709 if(!(wrtClient->m_dao))
712 // reset function fetches system locales and recreates language tags
713 LanguageTagsProviderSingleton::Instance().resetLanguageTags();
714 // widget default locales are added to language tags below
715 DPL::OptionalString defloc = wrtClient->m_dao->getDefaultlocale();
718 LanguageTagsProviderSingleton::Instance().addWidgetDefaultLocales(*defloc);
721 if (wrtClient->m_launched &&
722 wrtClient->m_widgetState != WidgetState_Stopped) {
723 wrtClient->m_widget->ReloadStartPage();
731 ADD_PROFILING_POINT("main-entered", "point");
733 // Output on stdout will be flushed after every newline character,
734 // even if it is redirected to a pipe. This is useful for running
735 // from a script and parsing output.
736 // (Standard behavior of stdlib is to use full buffering when
737 // redirected to a pipe, which means even after an end of line
738 // the output may not be flushed).
741 // set evas backend type
742 if (!getenv("ELM_ENGINE")) {
743 if (setenv("ELM_ENGINE", "gl", 1)) {
744 LogDebug("Enable backend");
748 setenv("COREGL_FASTPATH", "1", 1);
750 setenv("CAIRO_GL_COMPOSITOR", "msaa", 1);
751 setenv("CAIRO_GL_LAZY_FLUSHING", "yes", 1);
752 setenv("ELM_IMAGE_CACHE", "0", 1);
755 DPL::Log::LogSystemSingleton::Instance().SetTag("WRT-CLIENT");
757 WrtClient app(argc, argv);
759 ADD_PROFILING_POINT("Before appExec", "point");
760 int ret = app.Exec();
761 LogDebug("App returned: " << ret);
762 ret = app.getReturnStatus();
763 LogDebug("WrtClient returned: " << ret);