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 <dpl/exception.h>
23 #include <common/application_data.h>
24 #include <core_module.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 char *EDJE_SHOW_BACKWARD_SIGNAL = "show,backward,signal";
36 const std::string VIEWMODE_TYPE_FULLSCREEN = "fullscreen";
37 const std::string VIEWMODE_TYPE_MAXIMIZED = "maximized";
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");
79 void WrtClient::OnResume()
81 if (m_widgetState != WidgetState_Suspended) {
82 LogWarning("Widget is not suspended, resuming was skipped");
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 m_widgetState = WidgetState_Suspended;
102 void WrtClient::OnReset(bundle *b)
105 // bundle argument is freed after OnReset() is returned
106 // So bundle duplication is needed
107 ApplicationDataSingleton::Instance().setBundle(bundle_dup(b));
109 if (true == m_initializing) {
110 LogDebug("can not handle reset event");
113 if (true == m_launched) {
114 if (m_widgetState == WidgetState_Stopped) {
115 LogError("Widget is not running to be reset");
119 m_windowData->emitSignalForUserLayout(EDJE_SHOW_BACKWARD_SIGNAL, "");
120 m_widgetState = WidgetState_Running;
122 if (true == checkArgument())
133 void WrtClient::OnTerminate()
135 LogDebug("Wrt Shutdown now");
139 void WrtClient::showHelpAndQuit()
141 printf("Usage: wrt-client [OPTION]... [WIDGET: ID]...\n"
143 "Mandatory arguments to long options are mandatory for short "
145 " -h, --help show this help\n"
147 "launch widget with given tizen ID\n"
149 "launch widget with given tizen ID\n"
155 bool WrtClient::checkArgument()
157 LogInfo("checkArgument");
159 std::string arg = m_argv[0];
165 if (arg.find("wrt-client") != std::string::npos)
173 if (arg == "-h" || arg == "--help") {
176 } else if (arg == "-l" || arg == "--launch" ||
177 arg == "-t" || arg == "--tizen") {
181 m_tizenId = std::string(m_argv[2]);
186 size_t pos = arg.find_last_of('/');
188 if (pos != std::string::npos) {
189 arg = arg.erase(0, pos + 1);
192 // Launch widget based on application basename
194 LogDebug("Tizen id: " << m_tizenId);
200 void WrtClient::setStep()
204 AddStep(&WrtClient::initStep);
206 setSdkLauncherDebugData();
208 AddStep(&WrtClient::launchStep);
209 AddStep(&WrtClient::shutdownStep);
211 m_initializing = true;
213 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(NextStepEvent());
216 void WrtClient::setSdkLauncherDebugData()
218 LogDebug("setSdkLauncherDebugData");
220 /* check bundle from sdk launcher */
221 bundle *bundleFromSdkLauncher;
222 bundleFromSdkLauncher = bundle_import_from_argv(m_argc, m_argv);
223 const char *bundle_debug = bundle_get_val(bundleFromSdkLauncher, "debug");
224 const char *bundle_pid = bundle_get_val(bundleFromSdkLauncher, "pid");
225 if (bundle_debug != NULL && bundle_pid != NULL) {
226 if (strcmp(bundle_debug, "true") == 0) {
228 m_sdkLauncherPid = atoi(bundle_pid);
233 bundle_free(bundleFromSdkLauncher);
236 bool WrtClient::checkDebugMode(SDKDebugData* debugData)
238 LogError("Checking for debug mode");
241 bool debugMode = debugData->debugMode;
243 LogInfo("[DEBUG_MODE] Widget is launched in " <<
244 (debugMode ? "DEBUG" : "RETAIL") <<
247 if (debugMode == true) {
248 // In WAC widget, only test widgets can use web inspector.
250 // every launched widgets as debug mode can use it.
251 if (m_dao->getWidgetType().appType == WrtDB::APP_TYPE_WAC20)
253 bool developerMode = WRT::CoreModuleSingleton::Instance().developerMode();
254 //This code will be activated
255 //after WAC test certificate is used by SDK
256 //bool isTestWidget = view->m_widgetModel->IsTestWidget.Get();
259 // LogInfo("This is not WAC Test Widget");
262 if (!developerMode) {
263 LogInfo("This is not WAC Developer Mode");
271 void WrtClient::OnEventReceived(const NextStepEvent& /*event*/)
273 LogDebug("Executing next step");
277 void WrtClient::initStep()
280 if (WRT::CoreModuleSingleton::Instance().Init()) {
281 m_initialized = true;
283 m_returnStatus = ReturnStatus::Failed;
284 SwitchToStep(&WrtClient::shutdownStep);
287 // ecore_event_jobs are processed sequentially without concession to
288 // other type events. To give a chance of execute to other events,
289 // ecore_timer_add was used.
290 DPL::Event::ControllerEventHandler<NextStepEvent>::PostTimedEvent(
291 NextStepEvent(),0.001);
294 bool WrtClient::checkWACTestCertififedWidget()
296 // WAC Waikiki Beta Release Core Specification: Widget Runtime
299 // WR-4710 The WRT MUST enable debug functions only for WAC test widgets
300 // i.e. the functions must not be usable for normal WAC widgets, even when
301 // a WAC test widget is executing.
302 ADD_PROFILING_POINT("DeveloperModeCheck", "start");
305 // A widget signed with a WAC-issued test certificate as described in
308 bool developerWidget = m_dao->isTestWidget();
309 bool developerMode = WRT::CoreModuleSingleton::Instance().developerMode();
311 LogDebug("Is WAC test widget: " << developerWidget);
312 LogDebug("Is developer Mode: " << developerMode);
314 if (developerWidget) {
317 LogError("WAC test certified developer widget is needed for " <<
321 //TODO: WR-4660 (show popup about developer widget
323 LogInfo("POPUP: THIS IS TEST WIDGET!");
326 ADD_PROFILING_POINT("DeveloperModeCheck", "stop");
330 void WrtClient::loadFinishCallback(Evas_Object* webview)
332 ADD_PROFILING_POINT("loadFinishCallback", "start");
333 SDKDebugData* debug = new SDKDebugData;
334 debug->debugMode = m_debugMode;
335 debug->pid = new unsigned long(getpid());
337 LogInfo("Post result of launch");
339 // Start inspector server, if current mode is debugger mode.
340 // In the WK2 case, ewk_view_inspector_server_start should
341 // be called after WebProcess is created.
342 if (checkDebugMode(debug))
345 ewk_view_inspector_server_start(m_widget->GetCurrentWebview(), 0);
346 if (debug->portnum == 0) {
347 LogWarning("Failed to get portnum");
349 LogInfo("Assigned port number for inspector : "
353 LogDebug("Debug mode is disabled");
356 //w3c packaging test debug (message on 4>)
357 const char * makeScreen = getenv(W3C_DEBUG_ENV_VARIABLE);
358 if(makeScreen != NULL && strcmp(makeScreen, "1") == 0)
360 FILE* doutput = fdopen(4, "w");
361 fprintf(doutput,"didFinishLoadForFrameCallback: ready\n");
366 LogDebug("Launch succesfull");
369 m_initializing = false;
371 ADD_PROFILING_POINT("loadFinishCallback", "stop");
372 printf("launched\n");
377 m_returnStatus = ReturnStatus::Failed;
379 DPL::Event::ControllerEventHandler<NextStepEvent>::
380 PostEvent(NextStepEvent());
385 LogDebug("Send RT signal to wrt-launcher(pid: " << m_sdkLauncherPid);
387 /* send real time signal with result to wrt-launcher */
389 LogDebug("userData->portnum : " << debug->portnum);
390 sv.sival_int = debug->portnum;
394 sigqueue(m_sdkLauncherPid, SIGRTMIN, sv);
397 ApplicationDataSingleton::Instance().freeBundle();
399 LogDebug("Cleaning wrtClient launch resources...");
404 void WrtClient::progressFinishCallback()
406 m_splashScreen->stopSplashScreen();
409 void WrtClient::webkitExitCallback()
411 LogDebug("window close called, terminating app");
412 SwitchToStep(&WrtClient::shutdownStep);
413 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
417 void WrtClient::webCrashCallback()
419 LogError("webProcess crashed");
420 SwitchToStep(&WrtClient::shutdownStep);
421 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
426 void WrtClient::launchStep()
428 ADD_PROFILING_POINT("launchStep", "start");
429 LogDebug("Launching widget ...");
431 ADD_PROFILING_POINT("getRunnableWidgetObject", "start");
432 m_widget = WRT::CoreModuleSingleton::Instance()
433 .getRunnableWidgetObject(m_tizenId);
434 ADD_PROFILING_POINT("getRunnableWidgetObject", "stop");
437 LogError("RunnableWidgetObject is NULL, stop launchStep");
438 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
443 if (m_widgetState == WidgetState_Running) {
444 LogWarning("Widget already running, stop launchStep");
445 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
450 if (m_widgetState == WidgetState_Authorizing) {
451 LogWarning("Widget already authorizing, stop launchStep");
452 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
457 m_dao.reset(new WrtDB::WidgetDAOReadOnly(DPL::FromASCIIString(m_tizenId)));
458 DPL::Optional<DPL::String> defloc = m_dao->getDefaultlocale();
461 LanguageTagsProviderSingleton::Instance().addWidgetDefaultLocales(*defloc);
464 LocalizationSetting::SetLanguageChangedCallback(
465 languageChangedCallback, this);
467 ADD_PROFILING_POINT("CreateWindow", "start");
468 m_windowData.reset(new WindowData(static_cast<unsigned long>(getpid())));
469 ADD_PROFILING_POINT("CreateWindow", "stop");
471 WRT::UserDelegatesPtr cbs(new WRT::UserDelegates);
472 ADD_PROFILING_POINT("Create splash screen", "start");
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 ADD_PROFILING_POINT("Create splash screen", "stop");
480 DPL::OptionalString startUrl = W3CFileLocalization::getStartFile(m_dao);
481 m_widget->PrepareView(DPL::ToUTF8String(*startUrl),
482 m_windowData->m_win);
483 //you can't show window with splash screen before PrepareView
484 //ewk_view_add_with_context() in viewLogic breaks window
486 WrtDB::WidgetLocalizedInfo localizedInfo =
487 W3CFileLocalization::getLocalizedInfo(m_dao);
488 std:: string name = "";
489 if (!(localizedInfo.name.IsNull())) {
490 name = DPL::ToUTF8String(*(localizedInfo.name));
492 elm_win_title_set(m_windowData->m_win, name.c_str());
493 evas_object_show(m_windowData->m_win);
494 initializeWindowModes();
495 connectElmCallback();
497 if (!checkWACTestCertififedWidget())
499 LogWarning("WAC Certificate failed, stop launchStep");
503 m_widgetState = WidgetState_Authorizing;
504 if (!m_widget->CheckBeforeLaunch()) {
505 LogError("CheckBeforeLaunch failed, stop launchStep");
506 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
510 LogInfo("Widget launch accepted. Entering running state");
511 m_widgetState = WidgetState_Running;
513 cbs->loadFinish = DPL::MakeDelegate(this, &WrtClient::loadFinishCallback);
514 cbs->bufferSet = DPL::MakeDelegate(this, &WrtClient::setLayout);
515 cbs->bufferUnset = DPL::MakeDelegate(this, &WrtClient::unsetLayout);
516 cbs->webkitExit = DPL::MakeDelegate(this, &WrtClient::webkitExitCallback);
517 cbs->webCrash = DPL::MakeDelegate(this, &WrtClient::webCrashCallback);
518 cbs->toggleFullscreen = DPL::MakeDelegate(m_windowData.get(), &WindowData::toggleFullscreen);
520 m_widget->SetUserDelegates(cbs);
522 m_windowData->emitSignalForUserLayout(EDJE_SHOW_BACKWARD_SIGNAL, "");
523 ADD_PROFILING_POINT("launchStep", "stop");
526 void WrtClient::initializeWindowModes()
528 Assert(m_windowData);
530 auto windowModes = m_dao->getWindowModes();
531 bool fullscreen = false;
532 bool backbutton = false;
533 if (m_dao->getWidgetType().appType == WrtDB::APP_TYPE_TIZENWEBAPP) {
534 WidgetSettings widgetSettings;
535 m_dao->getWidgetSettings(widgetSettings);
536 WidgetSettingList settings(widgetSettings);
538 (settings.getBackButtonPresence() == BackButton_Enable);
542 FOREACH(it, windowModes)
544 std::string viewMode = DPL::ToUTF8String(*it);
545 if (viewMode == VIEWMODE_TYPE_FULLSCREEN) {
548 } else if (viewMode == VIEWMODE_TYPE_MAXIMIZED) {
553 WrtDB::WidgetLocalizedInfo localizedInfo =
554 W3CFileLocalization::getLocalizedInfo(m_dao);
555 std::string name = "";
556 if (!(localizedInfo.name.IsNull())) {
557 name = DPL::ToUTF8String(*(localizedInfo.name));
559 LogInfo("initializeWindowModes " << m_debugMode);
561 m_windowData->m_debugMode = FALSE;
564 m_windowData->m_debugMode = TRUE;
567 m_windowData->m_debugMode = FALSE;
570 WindowData::CtxMenuItems ctxMenuItems;
572 WindowData::CtxMenuItem ctxMenuBackword;
573 ctxMenuBackword.label = WRT_OPTION_LABEL_BACKWARD;
574 ctxMenuBackword.icon = WRT_OPTION_ICON_BACKWARD;
575 ctxMenuBackword.callback = backwardCallback;
576 ctxMenuBackword.data = this;
577 ctxMenuItems.push_back(ctxMenuBackword);
579 WindowData::CtxMenuItem ctxMenuReload;
580 ctxMenuReload.label = WRT_OPTION_LABEL_RELOAD;
581 ctxMenuReload.icon = WRT_OPTION_ICON_RELOAD;
582 ctxMenuReload.callback = reloadCallback;
583 ctxMenuReload.data = this;
584 ctxMenuItems.push_back(ctxMenuReload);
586 WindowData::CtxMenuItem ctxMenuForward;
587 ctxMenuForward.label = WRT_OPTION_LABEL_FORWARD;
588 ctxMenuForward.icon = WRT_OPTION_ICON_FORWARD;
589 ctxMenuForward.callback = forwardCallback;
590 ctxMenuForward.data = this;
591 ctxMenuItems.push_back(ctxMenuForward);
593 m_windowData->setViewMode(fullscreen,
598 void WrtClient::backButtonCallback(void* data,
599 Evas_Object * /*obj*/,
600 void * /*event_info*/)
602 LogInfo("BackButtonCallback");
605 WrtClient* This = static_cast<WrtClient*>(data);
607 This->m_widget->Backward();
610 void WrtClient::backwardCallback(void *data,
611 Evas_Object */*obj*/,
612 void */*event_info*/)
614 LogInfo("BackButtonCallback");
617 WrtClient* This = static_cast<WrtClient*>(data);
619 This->m_widget->Backward();
620 This->m_windowData->initFullViewMode();
623 void WrtClient::reloadCallback(void *data,
624 Evas_Object */*obj*/,
625 void */*event_info*/)
627 LogInfo("reloadCallback");
629 WrtClient* This = static_cast<WrtClient*>(data);
631 This->m_widget->Reload();
632 This->m_windowData->initFullViewMode();
635 void WrtClient::forwardCallback(void *data,
636 Evas_Object */*obj*/,
637 void */*event_info*/)
639 LogInfo("forwardCallback");
641 WrtClient* This = static_cast<WrtClient*>(data);
643 This->m_widget->Forward();
644 This->m_windowData->initFullViewMode();
648 void WrtClient::connectElmCallback()
650 Assert(m_windowData);
652 if (m_dao->getWidgetType().appType == WrtDB::APP_TYPE_TIZENWEBAPP) {
653 WidgetSettings widgetSettings;
654 m_dao->getWidgetSettings(widgetSettings);
655 WidgetSettingList settings(widgetSettings);
656 if (settings.getBackButtonPresence() == BackButton_Enable) {
657 m_windowData->addFloatBackButtonCallback(
659 &WrtClient::backButtonCallback,
663 WidgetSettingScreenLock rotationValue = settings.getRotationValue();
664 if (rotationValue == Screen_Portrait) {
665 elm_win_rotation_with_resize_set(m_windowData->m_win, 0);
666 } else if (rotationValue == Screen_Landscape) {
667 elm_win_rotation_with_resize_set(m_windowData->m_win, 270);
669 elm_win_rotation_with_resize_set(m_windowData->m_win, 0);
674 void WrtClient::setLayout(Evas_Object* newBuffer) {
675 LogDebug("add new webkit buffer to window");
677 elm_object_content_set(m_windowData->m_conformant, newBuffer);
678 evas_object_show(newBuffer);
679 evas_object_focus_set(newBuffer, EINA_TRUE);
682 void WrtClient::unsetLayout(Evas_Object* currentBuffer) {
683 LogDebug("remove current webkit buffer from window");
684 Assert(currentBuffer);
685 evas_object_hide(currentBuffer);
686 elm_object_content_unset(m_windowData->m_conformant);
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();
728 void WrtClient::Quit()
731 DPL::Application::Quit();
737 UNHANDLED_EXCEPTION_HANDLER_BEGIN
739 ADD_PROFILING_POINT("main-entered", "point");
741 // Output on stdout will be flushed after every newline character,
742 // even if it is redirected to a pipe. This is useful for running
743 // from a script and parsing output.
744 // (Standard behavior of stdlib is to use full buffering when
745 // redirected to a pipe, which means even after an end of line
746 // the output may not be flushed).
749 // set evas backend type
750 if (!getenv("ELM_ENGINE")) {
751 if (!setenv("ELM_ENGINE", "gl", 1)) {
752 LogDebug("Enable backend");
756 LogDebug("ELM_ENGINE : " << getenv("ELM_ENGINE"));
760 setenv("COREGL_FASTPATH", "1", 1);
762 setenv("CAIRO_GL_COMPOSITOR", "msaa", 1);
763 setenv("CAIRO_GL_LAZY_FLUSHING", "yes", 1);
764 setenv("ELM_IMAGE_CACHE", "0", 1);
767 DPL::Log::LogSystemSingleton::Instance().SetTag("WRT-CLIENT");
769 WrtClient app(argc, argv);
771 ADD_PROFILING_POINT("Before appExec", "point");
772 int ret = app.Exec();
773 LogDebug("App returned: " << ret);
774 ret = app.getReturnStatus();
775 LogDebug("WrtClient returned: " << ret);
778 UNHANDLED_EXCEPTION_HANDLER_END