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 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)
254 GlobalLogicSingleton::Instance().GetGlobalModel()
255 ->DeveloperMode.Get();
256 //This code will be activated
257 //after WAC test certificate is used by SDK
258 //bool isTestWidget = view->m_widgetModel->IsTestWidget.Get();
261 // LogInfo("This is not WAC Test Widget");
264 if (!developerMode) {
265 LogInfo("This is not WAC Developer Mode");
273 void WrtClient::OnEventReceived(const NextStepEvent& /*event*/)
275 LogDebug("Executing next step");
279 void WrtClient::initStep()
282 if (WRT::CoreModuleSingleton::Instance().Init()) {
283 m_initialized = true;
285 m_returnStatus = ReturnStatus::Failed;
286 SwitchToStep(&WrtClient::shutdownStep);
289 // ecore_event_jobs are processed sequentially without concession to
290 // other type events. To give a chance of execute to other events,
291 // ecore_timer_add was used.
292 DPL::Event::ControllerEventHandler<NextStepEvent>::PostTimedEvent(
293 NextStepEvent(),0.001);
296 bool WrtClient::checkWACTestCertififedWidget()
298 // WAC Waikiki Beta Release Core Specification: Widget Runtime
301 // WR-4710 The WRT MUST enable debug functions only for WAC test widgets
302 // i.e. the functions must not be usable for normal WAC widgets, even when
303 // a WAC test widget is executing.
304 ADD_PROFILING_POINT("DeveloperModeCheck", "start");
307 // A widget signed with a WAC-issued test certificate as described in
310 bool developerWidget = m_dao->isTestWidget();
312 GlobalLogicSingleton::Instance().GetGlobalModel()->DeveloperMode.Get();
314 LogDebug("Is WAC test widget: " << developerWidget);
315 LogDebug("Is developer Mode: " << developerMode);
317 if (developerWidget) {
320 LogError("WAC test certified developer widget is needed for " <<
324 //TODO: WR-4660 (show popup about developer widget
326 LogInfo("POPUP: THIS IS TEST WIDGET!");
329 ADD_PROFILING_POINT("DeveloperModeCheck", "stop");
333 void WrtClient::loadFinishCallback(Evas_Object* webview)
335 ADD_PROFILING_POINT("loadFinishCallback", "start");
336 SDKDebugData* debug = new SDKDebugData;
337 debug->debugMode = m_debugMode;
338 debug->pid = new unsigned long(getpid());
340 LogInfo("Post result of launch");
342 // Start inspector server, if current mode is debugger mode.
343 // In the WK2 case, ewk_view_inspector_server_start should
344 // be called after WebProcess is created.
345 if (checkDebugMode(debug))
348 ewk_view_inspector_server_start(m_widget->GetCurrentWebview(), 0);
349 if (debug->portnum == 0) {
350 LogWarning("Failed to get portnum");
352 LogInfo("Assigned port number for inspector : "
356 LogDebug("Debug mode is disabled");
359 //w3c packaging test debug (message on 4>)
360 const char * makeScreen = getenv(W3C_DEBUG_ENV_VARIABLE);
361 if(makeScreen != NULL && strcmp(makeScreen, "1") == 0)
363 FILE* doutput = fdopen(4, "w");
364 fprintf(doutput,"didFinishLoadForFrameCallback: ready\n");
369 LogDebug("Launch succesfull");
372 m_initializing = false;
374 ADD_PROFILING_POINT("loadFinishCallback", "stop");
375 printf("launched\n");
380 m_returnStatus = ReturnStatus::Failed;
382 DPL::Event::ControllerEventHandler<NextStepEvent>::
383 PostEvent(NextStepEvent());
388 LogDebug("Send RT signal to wrt-launcher(pid: " << m_sdkLauncherPid);
390 /* send real time signal with result to wrt-launcher */
392 LogDebug("userData->portnum : " << debug->portnum);
393 sv.sival_int = debug->portnum;
397 sigqueue(m_sdkLauncherPid, SIGRTMIN, sv);
400 ApplicationDataSingleton::Instance().freeBundle();
402 LogDebug("Cleaning wrtClient launch resources...");
407 void WrtClient::progressFinishCallback()
409 m_splashScreen->stopSplashScreen();
412 void WrtClient::webkitExitCallback()
414 LogDebug("window close called, terminating app");
415 SwitchToStep(&WrtClient::shutdownStep);
416 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
420 void WrtClient::webCrashCallback()
422 LogError("webProcess crashed");
423 SwitchToStep(&WrtClient::shutdownStep);
424 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
429 void WrtClient::launchStep()
431 ADD_PROFILING_POINT("launchStep", "start");
432 LogDebug("Launching widget ...");
434 ADD_PROFILING_POINT("getRunnableWidgetObject", "start");
435 m_widget = WRT::CoreModuleSingleton::Instance()
436 .getRunnableWidgetObject(m_tizenId);
437 ADD_PROFILING_POINT("getRunnableWidgetObject", "stop");
440 LogError("RunnableWidgetObject is NULL, stop launchStep");
441 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
446 if (m_widgetState == WidgetState_Running) {
447 LogWarning("Widget already running, stop launchStep");
448 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
453 if (m_widgetState == WidgetState_Authorizing) {
454 LogWarning("Widget already authorizing, stop launchStep");
455 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
460 m_dao.reset(new WrtDB::WidgetDAOReadOnly(DPL::FromASCIIString(m_tizenId)));
461 DPL::Optional<DPL::String> defloc = m_dao->getDefaultlocale();
464 LanguageTagsProviderSingleton::Instance().addWidgetDefaultLocales(*defloc);
467 LocalizationSetting::SetLanguageChangedCallback(
468 languageChangedCallback, this);
470 ADD_PROFILING_POINT("CreateWindow", "start");
471 m_windowData.reset(new WindowData(static_cast<unsigned long>(getpid())));
472 ADD_PROFILING_POINT("CreateWindow", "stop");
474 WRT::UserDelegatesPtr cbs(new WRT::UserDelegates);
475 ADD_PROFILING_POINT("Create splash screen", "start");
476 m_splashScreen.reset(
477 new SplashScreenSupport(m_windowData->m_win));
478 if (m_splashScreen->createSplashScreen(m_dao->getSplashImgSrc())) {
479 m_splashScreen->startSplashScreen();
480 cbs->progressFinish = DPL::MakeDelegate(this, &WrtClient::progressFinishCallback);
482 ADD_PROFILING_POINT("Create splash screen", "stop");
483 DPL::OptionalString startUrl = W3CFileLocalization::getStartFile(m_dao);
484 m_widget->PrepareView(DPL::ToUTF8String(*startUrl),
485 m_windowData->m_win);
486 //you can't show window with splash screen before PrepareView
487 //ewk_view_add_with_context() in viewLogic breaks window
489 WrtDB::WidgetLocalizedInfo localizedInfo =
490 W3CFileLocalization::getLocalizedInfo(m_dao);
491 std:: string name = "";
492 if (!(localizedInfo.name.IsNull())) {
493 name = DPL::ToUTF8String(*(localizedInfo.name));
495 elm_win_title_set(m_windowData->m_win, name.c_str());
496 evas_object_show(m_windowData->m_win);
497 initializeWindowModes();
498 connectElmCallback();
500 if (!checkWACTestCertififedWidget())
502 LogWarning("WAC Certificate failed, stop launchStep");
506 m_widgetState = WidgetState_Authorizing;
507 if (!m_widget->CheckBeforeLaunch()) {
508 LogError("CheckBeforeLaunch failed, stop launchStep");
509 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
513 LogInfo("Widget launch accepted. Entering running state");
514 m_widgetState = WidgetState_Running;
516 cbs->loadFinish = DPL::MakeDelegate(this, &WrtClient::loadFinishCallback);
517 cbs->bufferSet = DPL::MakeDelegate(this, &WrtClient::setLayout);
518 cbs->bufferUnset = DPL::MakeDelegate(this, &WrtClient::unsetLayout);
519 cbs->webkitExit = DPL::MakeDelegate(this, &WrtClient::webkitExitCallback);
520 cbs->webCrash = DPL::MakeDelegate(this, &WrtClient::webCrashCallback);
521 cbs->toggleFullscreen = DPL::MakeDelegate(m_windowData.get(), &WindowData::toggleFullscreen);
523 m_widget->SetUserDelegates(cbs);
525 m_windowData->emitSignalForUserLayout(EDJE_SHOW_BACKWARD_SIGNAL, "");
526 ADD_PROFILING_POINT("launchStep", "stop");
529 void WrtClient::initializeWindowModes()
531 Assert(m_windowData);
533 auto windowModes = m_dao->getWindowModes();
534 bool fullscreen = false;
535 bool backbutton = false;
536 if (m_dao->getWidgetType().appType == WrtDB::APP_TYPE_TIZENWEBAPP) {
537 WidgetSettings widgetSettings;
538 m_dao->getWidgetSettings(widgetSettings);
539 WidgetSettingList settings(widgetSettings);
541 (settings.getBackButtonPresence() == BackButton_Enable);
545 FOREACH(it, windowModes)
547 std::string viewMode = DPL::ToUTF8String(*it);
548 if (viewMode == VIEWMODE_TYPE_FULLSCREEN) {
551 } else if (viewMode == VIEWMODE_TYPE_MAXIMIZED) {
556 WrtDB::WidgetLocalizedInfo localizedInfo =
557 W3CFileLocalization::getLocalizedInfo(m_dao);
558 std::string name = "";
559 if (!(localizedInfo.name.IsNull())) {
560 name = DPL::ToUTF8String(*(localizedInfo.name));
562 LogInfo("initializeWindowModes " << m_debugMode);
564 m_windowData->m_debugMode = FALSE;
567 m_windowData->m_debugMode = TRUE;
570 m_windowData->m_debugMode = FALSE;
573 WindowData::CtxMenuItems ctxMenuItems;
575 WindowData::CtxMenuItem ctxMenuBackword;
576 ctxMenuBackword.label = WRT_OPTION_LABEL_BACKWARD;
577 ctxMenuBackword.icon = WRT_OPTION_ICON_BACKWARD;
578 ctxMenuBackword.callback = backwardCallback;
579 ctxMenuBackword.data = this;
580 ctxMenuItems.push_back(ctxMenuBackword);
582 WindowData::CtxMenuItem ctxMenuReload;
583 ctxMenuReload.label = WRT_OPTION_LABEL_RELOAD;
584 ctxMenuReload.icon = WRT_OPTION_ICON_RELOAD;
585 ctxMenuReload.callback = reloadCallback;
586 ctxMenuReload.data = this;
587 ctxMenuItems.push_back(ctxMenuReload);
589 WindowData::CtxMenuItem ctxMenuForward;
590 ctxMenuForward.label = WRT_OPTION_LABEL_FORWARD;
591 ctxMenuForward.icon = WRT_OPTION_ICON_FORWARD;
592 ctxMenuForward.callback = forwardCallback;
593 ctxMenuForward.data = this;
594 ctxMenuItems.push_back(ctxMenuForward);
596 m_windowData->setViewMode(name.c_str(),
602 void WrtClient::backButtonCallback(void* data,
603 Evas_Object * /*obj*/,
604 void * /*event_info*/)
606 LogInfo("BackButtonCallback");
609 WrtClient* This = static_cast<WrtClient*>(data);
611 This->m_widget->Backward();
614 void WrtClient::backwardCallback(void *data,
615 Evas_Object */*obj*/,
616 void */*event_info*/)
618 LogInfo("BackButtonCallback");
621 WrtClient* This = static_cast<WrtClient*>(data);
623 This->m_widget->Backward();
624 This->m_windowData->initFullViewMode();
627 void WrtClient::reloadCallback(void *data,
628 Evas_Object */*obj*/,
629 void */*event_info*/)
631 LogInfo("reloadCallback");
633 WrtClient* This = static_cast<WrtClient*>(data);
635 This->m_widget->Reload();
636 This->m_windowData->initFullViewMode();
639 void WrtClient::forwardCallback(void *data,
640 Evas_Object */*obj*/,
641 void */*event_info*/)
643 LogInfo("forwardCallback");
645 WrtClient* This = static_cast<WrtClient*>(data);
647 This->m_widget->Forward();
648 This->m_windowData->initFullViewMode();
652 void WrtClient::connectElmCallback()
654 Assert(m_windowData);
656 if (m_dao->getWidgetType().appType == WrtDB::APP_TYPE_TIZENWEBAPP) {
657 WidgetSettings widgetSettings;
658 m_dao->getWidgetSettings(widgetSettings);
659 WidgetSettingList settings(widgetSettings);
660 if (settings.getBackButtonPresence() == BackButton_Enable) {
661 m_windowData->addFloatBackButtonCallback(
663 &WrtClient::backButtonCallback,
667 WidgetSettingScreenLock rotationValue = settings.getRotationValue();
668 if (rotationValue == Screen_Portrait) {
669 elm_win_rotation_with_resize_set(m_windowData->m_win, 0);
670 } else if (rotationValue == Screen_Landscape) {
671 elm_win_rotation_with_resize_set(m_windowData->m_win, 270);
673 elm_win_rotation_with_resize_set(m_windowData->m_win, 0);
678 void WrtClient::setLayout(Evas_Object* newBuffer) {
679 LogDebug("add new webkit buffer to window");
681 elm_object_content_set(m_windowData->m_conformant, newBuffer);
682 evas_object_show(newBuffer);
683 evas_object_focus_set(newBuffer, EINA_TRUE);
686 void WrtClient::unsetLayout(Evas_Object* currentBuffer) {
687 LogDebug("remove current webkit buffer from window");
688 Assert(currentBuffer);
689 evas_object_hide(currentBuffer);
690 elm_object_content_unset(m_windowData->m_conformant);
693 void WrtClient::shutdownStep()
695 LogDebug("Closing Wrt connection ...");
696 if (m_initialized && m_widget) {
697 m_widgetState = WidgetState_Stopped;
700 m_windowData.reset();
701 WRT::CoreModuleSingleton::Instance().Terminate();
702 m_initialized = false;
707 int WrtClient::languageChangedCallback(void *data)
709 LogDebug("Language Changed");
712 WrtClient* wrtClient = static_cast<WrtClient*>(data);
713 if(!(wrtClient->m_dao))
716 // reset function fetches system locales and recreates language tags
717 LanguageTagsProviderSingleton::Instance().resetLanguageTags();
718 // widget default locales are added to language tags below
719 DPL::OptionalString defloc = wrtClient->m_dao->getDefaultlocale();
722 LanguageTagsProviderSingleton::Instance().addWidgetDefaultLocales(*defloc);
725 if (wrtClient->m_launched &&
726 wrtClient->m_widgetState != WidgetState_Stopped) {
727 wrtClient->m_widget->ReloadStartPage();
732 void WrtClient::Quit()
735 DPL::Application::Quit();
741 ADD_PROFILING_POINT("main-entered", "point");
743 // Output on stdout will be flushed after every newline character,
744 // even if it is redirected to a pipe. This is useful for running
745 // from a script and parsing output.
746 // (Standard behavior of stdlib is to use full buffering when
747 // redirected to a pipe, which means even after an end of line
748 // the output may not be flushed).
751 // set evas backend type
752 if (!getenv("ELM_ENGINE")) {
753 if (!setenv("ELM_ENGINE", "gl", 1)) {
754 LogDebug("Enable backend");
758 LogDebug("ELM_ENGINE : " << getenv("ELM_ENGINE"));
762 setenv("COREGL_FASTPATH", "1", 1);
764 setenv("CAIRO_GL_COMPOSITOR", "msaa", 1);
765 setenv("CAIRO_GL_LAZY_FLUSHING", "yes", 1);
766 setenv("ELM_IMAGE_CACHE", "0", 1);
769 DPL::Log::LogSystemSingleton::Instance().SetTag("WRT-CLIENT");
771 WrtClient app(argc, argv);
773 ADD_PROFILING_POINT("Before appExec", "point");
774 int ret = app.Exec();
775 LogDebug("App returned: " << ret);
776 ret = app.getReturnStatus();
777 LogDebug("WrtClient returned: " << ret);