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>
28 #include <launch_user_data.h>
30 #include <dpl/localization/w3c_file_localization.h>
31 #include <dpl/localization/LanguageTagsProvider.h>
33 //W3C PACKAGING enviroment variable name
34 #define W3C_DEBUG_ENV_VARIABLE "DEBUG_LOAD_FINISH"
36 // window signal callback
37 const std::string VIEWMODE_TYPE_FULLSCREEN = "fullscreen";
38 const std::string VIEWMODE_TYPE_MAXIMIZED = "maximized";
39 char const* const ELM_SWALLOW_CONTENT = "elm.swallow.content";
41 WrtClient::WrtClient(int argc, char **argv) :
42 Application(argc, argv, "wrt-client", false),
43 DPL::TaskDecl<WrtClient>(this),
45 m_initializing(false),
50 m_returnStatus(ReturnStatus::Succeeded),
51 m_widgetState(WidgetState::WidgetState_Stopped)
54 LogDebug("App Created");
57 WrtClient::~WrtClient()
59 LogDebug("App Finished");
62 WrtClient::ReturnStatus::Type WrtClient::getReturnStatus() const
64 return m_returnStatus;
67 void WrtClient::OnStop()
69 LogInfo("Stopping Dummy Client");
73 void WrtClient::OnCreate()
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()
93 if (m_widgetState != WidgetState_Running) {
94 LogWarning("Widget is not running to be suspended");
98 evas_object_focus_set(m_widget->GetCurrentWebview(), EINA_FALSE);
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 elm_win_raise(m_windowData->m_win);
120 evas_object_focus_set(m_widget->GetCurrentWebview(), EINA_TRUE);
121 m_widgetState = WidgetState_Running;
123 if (true == checkArgument())
134 void WrtClient::OnTerminate()
136 LogDebug("Wrt Shutdown now");
140 void WrtClient::showHelpAndQuit()
142 printf("Usage: wrt-client [OPTION]... [WIDGET: ID]...\n"
144 "Mandatory arguments to long options are mandatory for short "
146 " -h, --help show this help\n"
148 "launch widget with given tizen ID\n"
150 "launch widget with given tizen ID\n"
156 bool WrtClient::checkArgument()
158 LogInfo("checkArgument");
160 std::string arg = m_argv[0];
166 if (arg.find("wrt-client") != std::string::npos)
174 if (arg == "-h" || arg == "--help") {
177 } else if (arg == "-l" || arg == "--launch" ||
178 arg == "-t" || arg == "--tizen") {
182 m_tizenId = std::string(m_argv[2]);
187 size_t pos = arg.find_last_of('/');
189 if (pos != std::string::npos) {
190 arg = arg.erase(0, pos + 1);
193 // Launch widget based on application basename
195 LogDebug("Tizen id: " << m_tizenId);
201 void WrtClient::setStep()
205 AddStep(&WrtClient::initStep);
207 setSdkLauncherDebugData();
209 AddStep(&WrtClient::launchStep);
210 AddStep(&WrtClient::shutdownStep);
212 m_initializing = true;
214 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(NextStepEvent());
217 void WrtClient::setSdkLauncherDebugData()
219 LogDebug("setSdkLauncherDebugData");
221 /* check bundle from sdk launcher */
222 bundle *bundleFromSdkLauncher;
223 bundleFromSdkLauncher = bundle_import_from_argv(m_argc, m_argv);
224 const char *bundle_debug = bundle_get_val(bundleFromSdkLauncher, "debug");
225 const char *bundle_pid = bundle_get_val(bundleFromSdkLauncher, "pid");
226 if (bundle_debug != NULL && bundle_pid != NULL) {
227 if (strcmp(bundle_debug, "true") == 0) {
229 m_sdkLauncherPid = atoi(bundle_pid);
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(bool success)
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: "
389 << m_sdkLauncherPid << ", status: " << success);
391 /* send real time signal with result to wrt-launcher */
394 LogDebug("userData->portnum : " << debug->portnum);
395 sv.sival_int = debug->portnum;
401 sigqueue(m_sdkLauncherPid, SIGRTMIN, sv);
404 ApplicationDataSingleton::Instance().freeBundle();
406 LogDebug("Cleaning wrtClient launch resources...");
411 void WrtClient::progressFinishCallback()
413 m_splashScreen->stopSplashScreen();
416 void WrtClient::windowCloseCallback()
418 LogDebug("window close called, terminating app");
419 SwitchToStep(&WrtClient::shutdownStep);
420 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
424 void WrtClient::webCrashCallback()
426 LogDebug("webProcess crashed");
427 SwitchToStep(&WrtClient::shutdownStep);
428 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
433 void WrtClient::launchStep()
435 ADD_PROFILING_POINT("launchStep", "start");
436 LogDebug("Launching widget ...");
438 m_widget = WRT::CoreModuleSingleton::Instance()
439 .getRunnableWidgetObject(m_tizenId);
441 LogError("RunnableWidgetObject is NULL, stop launchStep");
442 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
447 if (m_widgetState == WidgetState_Running) {
448 LogWarning("Widget already running, stop launchStep");
449 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
454 if (m_widgetState == WidgetState_Authorizing) {
455 LogWarning("Widget already authorizing, stop launchStep");
456 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
461 m_dao.reset(new WrtDB::WidgetDAOReadOnly(DPL::FromASCIIString(m_tizenId)));
462 Domain::localizeWidgetModel(m_dao->getDefaultlocale());
463 LocalizationSetting::SetLanguageChangedCallback(
464 languageChangedCallback, this);
466 ADD_PROFILING_POINT("CreateWindow", "start");
467 m_windowData.reset(new WindowData(static_cast<unsigned long>(getpid())));
468 ADD_PROFILING_POINT("CreateWindow", "stop");
470 WRT::UserDelegatesPtr cbs(new WRT::UserDelegates);
471 m_splashScreen.reset(
472 new SplashScreenSupport(m_windowData->m_win));
473 if (m_splashScreen->createSplashScreen(m_dao->getSplashImgSrc())) {
474 m_splashScreen->startSplashScreen();
475 cbs->progressFinish = DPL::MakeDelegate(this, &WrtClient::progressFinishCallback);
477 DPL::OptionalString startUrl = W3CFileLocalization::getStartFile(m_dao);
478 m_widget->PrepareView(DPL::ToUTF8String(*startUrl),
479 m_windowData->m_win);
480 //you can't show window with splash screen before PrepareView
481 //ewk_view_add_with_context() in viewLogic breaks window
482 evas_object_show(m_windowData->m_win);
483 initializeWindowModes();
484 connectElmCallback();
486 if (!checkWACTestCertififedWidget())
488 LogWarning("WAC Certificate failed, stop launchStep");
492 m_widgetState = WidgetState_Authorizing;
493 if (!m_widget->CheckBeforeLaunch()) {
494 LogError("CheckBeforeLaunch failed, stop launchStep");
495 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
499 LogInfo("Widget launch accepted. Entering running state");
500 m_widgetState = WidgetState_Running;
502 cbs->loadFinish = DPL::MakeDelegate(this, &WrtClient::loadFinishCallback);
503 cbs->bufferSet = DPL::MakeDelegate(this, &WrtClient::setLayout);
504 cbs->bufferUnset = DPL::MakeDelegate(this, &WrtClient::unsetLayout);
505 cbs->windowClose = DPL::MakeDelegate(this, &WrtClient::windowCloseCallback);
506 cbs->webCrash = DPL::MakeDelegate(this, &WrtClient::webCrashCallback);
507 cbs->toggleFullscreen = DPL::MakeDelegate(m_windowData.get(), &WindowData::toggleFullscreen);
509 m_widget->SetUserDelegates(cbs);
511 ADD_PROFILING_POINT("launchStep", "stop");
514 void WrtClient::initializeWindowModes()
516 Assert(m_windowData);
518 auto windowModes = m_dao->getWindowModes();
519 bool fullscreen = false;
520 FOREACH(it, windowModes)
522 std::string viewMode = DPL::ToUTF8String(*it);
523 if (viewMode == VIEWMODE_TYPE_FULLSCREEN) {
526 } else if (viewMode == VIEWMODE_TYPE_MAXIMIZED) {
530 bool indicator = true;
531 if (m_dao->getWidgetType().appType == WrtDB::APP_TYPE_TIZENWEBAPP) {
532 WidgetSettings widgetSettings;
533 m_dao->getWidgetSettings(widgetSettings);
534 WidgetSettingList settings(widgetSettings);
535 indicator = (settings.getIndicatorPresence()
536 == Indicator_Enable);
539 WrtDB::WidgetLocalizedInfo localizedInfo =
540 W3CFileLocalization::getLocalizedInfo(m_dao);
541 std::string name = "";
542 if (!(localizedInfo.name.IsNull())) {
543 name = DPL::ToUTF8String(*(localizedInfo.name));
545 LogInfo("initializeWindowModes " << m_debugMode);
547 SDKDebugData* debug = new SDKDebugData;
549 m_windowData->m_debugMode = TRUE;
552 m_windowData->m_debugMode = FALSE;
555 WindowData::CtxMenuItems ctxMenuItems;
557 WindowData::CtxMenuItem ctxMenuBackword;
558 ctxMenuBackword.label = WRT_OPTION_LABEL_BACKWARD;
559 ctxMenuBackword.icon = WRT_OPTION_ICON_BACKWARD;
560 ctxMenuBackword.callback = backwardCallback;
561 ctxMenuBackword.data = this;
562 ctxMenuItems.push_back(ctxMenuBackword);
564 WindowData::CtxMenuItem ctxMenuReload;
565 ctxMenuReload.label = WRT_OPTION_LABEL_RELOAD;
566 ctxMenuReload.icon = WRT_OPTION_ICON_RELOAD;
567 ctxMenuReload.callback = reloadCallback;
568 ctxMenuReload.data = this;
569 ctxMenuItems.push_back(ctxMenuReload);
571 WindowData::CtxMenuItem ctxMenuForward;
572 ctxMenuForward.label = WRT_OPTION_LABEL_FORWARD;
573 ctxMenuForward.icon = WRT_OPTION_ICON_FORWARD;
574 ctxMenuForward.callback = forwardCallback;
575 ctxMenuForward.data = this;
576 ctxMenuItems.push_back(ctxMenuForward);
578 m_windowData->setViewMode(fullscreen,
582 void WrtClient::backButtonCallback(void* data,
583 Evas_Object * /*obj*/,
584 void * /*event_info*/)
586 LogInfo("BackButtonCallback");
589 WrtClient* This = static_cast<WrtClient*>(data);
591 This->m_widget->Backward();
594 void WrtClient::backwardCallback(void *data,
595 Evas_Object */*obj*/,
596 void */*event_info*/)
598 LogInfo("BackButtonCallback");
601 WrtClient* This = static_cast<WrtClient*>(data);
603 This->m_widget->Backward();
604 This->m_windowData->initFullViewMode();
607 void WrtClient::reloadCallback(void *data,
608 Evas_Object */*obj*/,
609 void */*event_info*/)
611 LogInfo("reloadCallback");
613 WrtClient* This = static_cast<WrtClient*>(data);
615 This->m_widget->Reload();
616 This->m_windowData->initFullViewMode();
619 void WrtClient::forwardCallback(void *data,
620 Evas_Object */*obj*/,
621 void */*event_info*/)
623 LogInfo("forwardCallback");
625 WrtClient* This = static_cast<WrtClient*>(data);
627 This->m_widget->Forward();
628 This->m_windowData->initFullViewMode();
632 void WrtClient::connectElmCallback()
634 Assert(m_windowData);
636 if (m_dao->getWidgetType().appType == WrtDB::APP_TYPE_TIZENWEBAPP) {
637 WidgetSettings widgetSettings;
638 m_dao->getWidgetSettings(widgetSettings);
639 WidgetSettingList settings(widgetSettings);
640 WidgetSettingScreenLock rotationValue = settings.getRotationValue();
641 if (rotationValue == Screen_Portrait) {
642 elm_win_rotation_with_resize_set(m_windowData->m_win, 0);
643 } else if (rotationValue == Screen_Landscape) {
644 elm_win_rotation_with_resize_set(m_windowData->m_win, 270);
646 elm_win_rotation_with_resize_set(m_windowData->m_win, 0);
651 void WrtClient::setLayout(Evas_Object* newBuffer) {
652 LogDebug("add new webkit buffer to window");
655 elm_object_part_content_set(m_windowData->m_user_layout, ELM_SWALLOW_CONTENT, newBuffer);
657 evas_object_show(newBuffer);
658 evas_object_focus_set(newBuffer, EINA_TRUE);
661 void WrtClient::unsetLayout(Evas_Object* currentBuffer) {
662 LogDebug("remove current webkit buffer from window");
663 Assert(currentBuffer);
664 evas_object_hide(currentBuffer);
666 elm_object_part_content_unset(m_windowData->m_user_layout, ELM_SWALLOW_CONTENT);
670 void WrtClient::shutdownStep()
672 LogDebug("Closing Wrt connection ...");
673 if (m_initialized && m_widget) {
674 m_widgetState = WidgetState_Stopped;
677 m_windowData.reset();
678 WRT::CoreModuleSingleton::Instance().Terminate();
679 m_initialized = false;
684 int WrtClient::languageChangedCallback(void *data)
686 LogDebug("Language Changed");
687 WrtClient* wrtClient = static_cast<WrtClient*>(data);
689 LanguageTags oldtags = LanguageTagsProviderSingleton::Instance().getLanguageTags();
690 // reset function fetches system locales and recreates language tags
691 LanguageTagsProviderSingleton::Instance().resetLanguageTags();
692 LanguageTags newtags = LanguageTagsProviderSingleton::Instance().getLanguageTags();
694 // check whether LanguageTags changed or not
695 if (oldtags != newtags) {
696 // update localized data
697 Domain::localizeWidgetModel(wrtClient->m_dao->getDefaultlocale());
699 if (true == wrtClient->m_launched &&
700 wrtClient->m_widgetState != WidgetState_Stopped) {
701 wrtClient->m_widget->ReloadStartPage();
710 ADD_PROFILING_POINT("main-entered", "point");
712 // Output on stdout will be flushed after every newline character,
713 // even if it is redirected to a pipe. This is useful for running
714 // from a script and parsing output.
715 // (Standard behavior of stdlib is to use full buffering when
716 // redirected to a pipe, which means even after an end of line
717 // the output may not be flushed).
720 // set evas backend type
721 if (!getenv("ELM_ENGINE")) {
722 if (setenv("ELM_ENGINE", "gl", 1)) {
723 LogDebug("Enable backend");
727 setenv("COREGL_FASTPATH", "1", 1);
729 setenv("CAIRO_GL_COMPOSITOR", "msaa", 1);
730 setenv("CAIRO_GL_LAZY_FLUSHING", "yes", 1);
731 setenv("ELM_IMAGE_CACHE", "0", 1);
734 DPL::Log::LogSystemSingleton::Instance().SetTag("WRT-CLIENT");
736 WrtClient app(argc, argv);
737 int ret = app.Exec();
738 LogDebug("App returned: " << ret);
739 ret = app.getReturnStatus();
740 LogDebug("WrtClient returned: " << ret);