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_widgetState = WidgetState_Running;
121 if (true == checkArgument())
132 void WrtClient::OnTerminate()
134 LogDebug("Wrt Shutdown now");
138 void WrtClient::showHelpAndQuit()
140 printf("Usage: wrt-client [OPTION]... [WIDGET: ID]...\n"
142 "Mandatory arguments to long options are mandatory for short "
144 " -h, --help show this help\n"
146 "launch widget with given tizen ID\n"
148 "launch widget with given tizen ID\n"
154 bool WrtClient::checkArgument()
156 LogInfo("checkArgument");
158 std::string arg = m_argv[0];
164 if (arg.find("wrt-client") != std::string::npos)
172 if (arg == "-h" || arg == "--help") {
175 } else if (arg == "-l" || arg == "--launch" ||
176 arg == "-t" || arg == "--tizen") {
180 m_tizenId = std::string(m_argv[2]);
185 size_t pos = arg.find_last_of('/');
187 if (pos != std::string::npos) {
188 arg = arg.erase(0, pos + 1);
191 // Launch widget based on application basename
193 LogDebug("Tizen id: " << m_tizenId);
199 void WrtClient::setStep()
203 AddStep(&WrtClient::initStep);
205 setSdkLauncherDebugData();
207 AddStep(&WrtClient::launchStep);
208 AddStep(&WrtClient::shutdownStep);
210 m_initializing = true;
212 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(NextStepEvent());
215 void WrtClient::setSdkLauncherDebugData()
217 LogDebug("setSdkLauncherDebugData");
219 /* check bundle from sdk launcher */
220 bundle *bundleFromSdkLauncher;
221 bundleFromSdkLauncher = bundle_import_from_argv(m_argc, m_argv);
222 const char *bundle_debug = bundle_get_val(bundleFromSdkLauncher, "debug");
223 const char *bundle_pid = bundle_get_val(bundleFromSdkLauncher, "pid");
224 if (bundle_debug != NULL && bundle_pid != NULL) {
225 if (strcmp(bundle_debug, "true") == 0) {
227 m_sdkLauncherPid = atoi(bundle_pid);
232 bundle_free(bundleFromSdkLauncher);
235 bool WrtClient::checkDebugMode(SDKDebugData* debugData)
237 LogError("Checking for debug mode");
240 bool debugMode = debugData->debugMode;
242 LogInfo("[DEBUG_MODE] Widget is launched in " <<
243 (debugMode ? "DEBUG" : "RETAIL") <<
246 if (debugMode == true) {
247 // In WAC widget, only test widgets can use web inspector.
249 // every launched widgets as debug mode can use it.
250 if (m_dao->getWidgetType().appType == WrtDB::APP_TYPE_WAC20)
253 GlobalLogicSingleton::Instance().GetGlobalModel()
254 ->DeveloperMode.Get();
255 //This code will be activated
256 //after WAC test certificate is used by SDK
257 //bool isTestWidget = view->m_widgetModel->IsTestWidget.Get();
260 // LogInfo("This is not WAC Test Widget");
263 if (!developerMode) {
264 LogInfo("This is not WAC Developer Mode");
272 void WrtClient::OnEventReceived(const NextStepEvent& /*event*/)
274 LogDebug("Executing next step");
278 void WrtClient::initStep()
281 if (WRT::CoreModuleSingleton::Instance().Init()) {
282 m_initialized = true;
284 m_returnStatus = ReturnStatus::Failed;
285 SwitchToStep(&WrtClient::shutdownStep);
288 // ecore_event_jobs are processed sequentially without concession to
289 // other type events. To give a chance of execute to other events,
290 // ecore_timer_add was used.
291 DPL::Event::ControllerEventHandler<NextStepEvent>::PostTimedEvent(
292 NextStepEvent(),0.001);
295 bool WrtClient::checkWACTestCertififedWidget()
297 // WAC Waikiki Beta Release Core Specification: Widget Runtime
300 // WR-4710 The WRT MUST enable debug functions only for WAC test widgets
301 // i.e. the functions must not be usable for normal WAC widgets, even when
302 // a WAC test widget is executing.
303 ADD_PROFILING_POINT("DeveloperModeCheck", "start");
306 // A widget signed with a WAC-issued test certificate as described in
309 bool developerWidget = m_dao->isTestWidget();
311 GlobalLogicSingleton::Instance().GetGlobalModel()->DeveloperMode.Get();
313 LogDebug("Is WAC test widget: " << developerWidget);
314 LogDebug("Is developer Mode: " << developerMode);
316 if (developerWidget) {
319 LogError("WAC test certified developer widget is needed for " <<
323 //TODO: WR-4660 (show popup about developer widget
325 LogInfo("POPUP: THIS IS TEST WIDGET!");
328 ADD_PROFILING_POINT("DeveloperModeCheck", "stop");
332 void WrtClient::loadFinishCallback(Evas_Object* webview)
334 ADD_PROFILING_POINT("loadFinishCallback", "start");
335 SDKDebugData* debug = new SDKDebugData;
336 debug->debugMode = m_debugMode;
337 debug->pid = new unsigned long(getpid());
339 LogInfo("Post result of launch");
341 // Start inspector server, if current mode is debugger mode.
342 // In the WK2 case, ewk_view_inspector_server_start should
343 // be called after WebProcess is created.
344 if (checkDebugMode(debug))
347 ewk_view_inspector_server_start(m_widget->GetCurrentWebview(), 0);
348 if (debug->portnum == 0) {
349 LogWarning("Failed to get portnum");
351 LogInfo("Assigned port number for inspector : "
355 LogDebug("Debug mode is disabled");
358 //w3c packaging test debug (message on 4>)
359 const char * makeScreen = getenv(W3C_DEBUG_ENV_VARIABLE);
360 if(makeScreen != NULL && strcmp(makeScreen, "1") == 0)
362 FILE* doutput = fdopen(4, "w");
363 fprintf(doutput,"didFinishLoadForFrameCallback: ready\n");
368 LogDebug("Launch succesfull");
371 m_initializing = false;
373 ADD_PROFILING_POINT("loadFinishCallback", "stop");
374 printf("launched\n");
379 m_returnStatus = ReturnStatus::Failed;
381 DPL::Event::ControllerEventHandler<NextStepEvent>::
382 PostEvent(NextStepEvent());
387 LogDebug("Send RT signal to wrt-launcher(pid: " << m_sdkLauncherPid);
389 /* send real time signal with result to wrt-launcher */
391 LogDebug("userData->portnum : " << debug->portnum);
392 sv.sival_int = debug->portnum;
396 sigqueue(m_sdkLauncherPid, SIGRTMIN, sv);
399 ApplicationDataSingleton::Instance().freeBundle();
401 LogDebug("Cleaning wrtClient launch resources...");
406 void WrtClient::progressFinishCallback()
408 m_splashScreen->stopSplashScreen();
411 void WrtClient::webkitExitCallback()
413 LogDebug("window close called, terminating app");
414 SwitchToStep(&WrtClient::shutdownStep);
415 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
419 void WrtClient::webCrashCallback()
421 LogError("webProcess crashed");
422 SwitchToStep(&WrtClient::shutdownStep);
423 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
428 void WrtClient::launchStep()
430 ADD_PROFILING_POINT("launchStep", "start");
431 LogDebug("Launching widget ...");
433 ADD_PROFILING_POINT("getRunnableWidgetObject", "start");
434 m_widget = WRT::CoreModuleSingleton::Instance()
435 .getRunnableWidgetObject(m_tizenId);
436 ADD_PROFILING_POINT("getRunnableWidgetObject", "stop");
439 LogError("RunnableWidgetObject is NULL, stop launchStep");
440 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
445 if (m_widgetState == WidgetState_Running) {
446 LogWarning("Widget already running, stop launchStep");
447 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
452 if (m_widgetState == WidgetState_Authorizing) {
453 LogWarning("Widget already authorizing, stop launchStep");
454 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
459 m_dao.reset(new WrtDB::WidgetDAOReadOnly(DPL::FromASCIIString(m_tizenId)));
460 DPL::Optional<DPL::String> defloc = m_dao->getDefaultlocale();
463 LanguageTagsProviderSingleton::Instance().addWidgetDefaultLocales(*defloc);
466 LocalizationSetting::SetLanguageChangedCallback(
467 languageChangedCallback, this);
469 ADD_PROFILING_POINT("CreateWindow", "start");
470 m_windowData.reset(new WindowData(static_cast<unsigned long>(getpid())));
471 ADD_PROFILING_POINT("CreateWindow", "stop");
473 WRT::UserDelegatesPtr cbs(new WRT::UserDelegates);
474 ADD_PROFILING_POINT("Create splash screen", "start");
475 m_splashScreen.reset(
476 new SplashScreenSupport(m_windowData->m_win));
477 if (m_splashScreen->createSplashScreen(m_dao->getSplashImgSrc())) {
478 m_splashScreen->startSplashScreen();
479 cbs->progressFinish = DPL::MakeDelegate(this, &WrtClient::progressFinishCallback);
481 ADD_PROFILING_POINT("Create splash screen", "stop");
482 DPL::OptionalString startUrl = W3CFileLocalization::getStartFile(m_dao);
483 m_widget->PrepareView(DPL::ToUTF8String(*startUrl),
484 m_windowData->m_win);
485 //you can't show window with splash screen before PrepareView
486 //ewk_view_add_with_context() in viewLogic breaks window
488 WrtDB::WidgetLocalizedInfo localizedInfo =
489 W3CFileLocalization::getLocalizedInfo(m_dao);
490 std:: string name = "";
491 if (!(localizedInfo.name.IsNull())) {
492 name = DPL::ToUTF8String(*(localizedInfo.name));
494 elm_win_title_set(m_windowData->m_win, name.c_str());
495 evas_object_show(m_windowData->m_win);
496 initializeWindowModes();
497 connectElmCallback();
499 if (!checkWACTestCertififedWidget())
501 LogWarning("WAC Certificate failed, stop launchStep");
505 m_widgetState = WidgetState_Authorizing;
506 if (!m_widget->CheckBeforeLaunch()) {
507 LogError("CheckBeforeLaunch failed, stop launchStep");
508 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
512 LogInfo("Widget launch accepted. Entering running state");
513 m_widgetState = WidgetState_Running;
515 cbs->loadFinish = DPL::MakeDelegate(this, &WrtClient::loadFinishCallback);
516 cbs->bufferSet = DPL::MakeDelegate(this, &WrtClient::setLayout);
517 cbs->bufferUnset = DPL::MakeDelegate(this, &WrtClient::unsetLayout);
518 cbs->webkitExit = DPL::MakeDelegate(this, &WrtClient::webkitExitCallback);
519 cbs->webCrash = DPL::MakeDelegate(this, &WrtClient::webCrashCallback);
520 cbs->toggleFullscreen = DPL::MakeDelegate(m_windowData.get(), &WindowData::toggleFullscreen);
522 m_widget->SetUserDelegates(cbs);
524 ADD_PROFILING_POINT("launchStep", "stop");
527 void WrtClient::initializeWindowModes()
529 Assert(m_windowData);
531 auto windowModes = m_dao->getWindowModes();
532 bool fullscreen = false;
533 FOREACH(it, windowModes)
535 std::string viewMode = DPL::ToUTF8String(*it);
536 if (viewMode == VIEWMODE_TYPE_FULLSCREEN) {
539 } else if (viewMode == VIEWMODE_TYPE_MAXIMIZED) {
544 WrtDB::WidgetLocalizedInfo localizedInfo =
545 W3CFileLocalization::getLocalizedInfo(m_dao);
546 std::string name = "";
547 if (!(localizedInfo.name.IsNull())) {
548 name = DPL::ToUTF8String(*(localizedInfo.name));
550 LogInfo("initializeWindowModes " << m_debugMode);
552 m_windowData->m_debugMode = FALSE;
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(),
589 void WrtClient::backButtonCallback(void* data,
590 Evas_Object * /*obj*/,
591 void * /*event_info*/)
593 LogInfo("BackButtonCallback");
596 WrtClient* This = static_cast<WrtClient*>(data);
598 This->m_widget->Backward();
601 void WrtClient::backwardCallback(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();
611 This->m_windowData->initFullViewMode();
614 void WrtClient::reloadCallback(void *data,
615 Evas_Object */*obj*/,
616 void */*event_info*/)
618 LogInfo("reloadCallback");
620 WrtClient* This = static_cast<WrtClient*>(data);
622 This->m_widget->Reload();
623 This->m_windowData->initFullViewMode();
626 void WrtClient::forwardCallback(void *data,
627 Evas_Object */*obj*/,
628 void */*event_info*/)
630 LogInfo("forwardCallback");
632 WrtClient* This = static_cast<WrtClient*>(data);
634 This->m_widget->Forward();
635 This->m_windowData->initFullViewMode();
639 void WrtClient::connectElmCallback()
641 Assert(m_windowData);
643 if (m_dao->getWidgetType().appType == WrtDB::APP_TYPE_TIZENWEBAPP) {
644 WidgetSettings widgetSettings;
645 m_dao->getWidgetSettings(widgetSettings);
646 WidgetSettingList settings(widgetSettings);
647 WidgetSettingScreenLock rotationValue = settings.getRotationValue();
648 if (rotationValue == Screen_Portrait) {
649 elm_win_rotation_with_resize_set(m_windowData->m_win, 0);
650 } else if (rotationValue == Screen_Landscape) {
651 elm_win_rotation_with_resize_set(m_windowData->m_win, 270);
653 elm_win_rotation_with_resize_set(m_windowData->m_win, 0);
658 void WrtClient::setLayout(Evas_Object* newBuffer) {
659 LogDebug("add new webkit buffer to window");
661 elm_object_content_set(m_windowData->m_conformant, newBuffer);
662 evas_object_show(newBuffer);
663 evas_object_focus_set(newBuffer, EINA_TRUE);
666 void WrtClient::unsetLayout(Evas_Object* currentBuffer) {
667 LogDebug("remove current webkit buffer from window");
668 Assert(currentBuffer);
669 evas_object_hide(currentBuffer);
670 elm_object_content_unset(m_windowData->m_conformant);
673 void WrtClient::shutdownStep()
675 LogDebug("Closing Wrt connection ...");
676 if (m_initialized && m_widget) {
677 m_widgetState = WidgetState_Stopped;
680 m_windowData.reset();
681 WRT::CoreModuleSingleton::Instance().Terminate();
682 m_initialized = false;
687 int WrtClient::languageChangedCallback(void *data)
689 LogDebug("Language Changed");
692 WrtClient* wrtClient = static_cast<WrtClient*>(data);
693 if(!(wrtClient->m_dao))
696 // reset function fetches system locales and recreates language tags
697 LanguageTagsProviderSingleton::Instance().resetLanguageTags();
698 // widget default locales are added to language tags below
699 DPL::OptionalString defloc = wrtClient->m_dao->getDefaultlocale();
702 LanguageTagsProviderSingleton::Instance().addWidgetDefaultLocales(*defloc);
705 if (wrtClient->m_launched &&
706 wrtClient->m_widgetState != WidgetState_Stopped) {
707 wrtClient->m_widget->ReloadStartPage();
712 void WrtClient::Quit()
715 DPL::Application::Quit();
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");
738 LogDebug("ELM_ENGINE : " << getenv("ELM_ENGINE"));
742 setenv("COREGL_FASTPATH", "1", 1);
744 setenv("CAIRO_GL_COMPOSITOR", "msaa", 1);
745 setenv("CAIRO_GL_LAZY_FLUSHING", "yes", 1);
746 setenv("ELM_IMAGE_CACHE", "0", 1);
749 DPL::Log::LogSystemSingleton::Instance().SetTag("WRT-CLIENT");
751 WrtClient app(argc, argv);
753 ADD_PROFILING_POINT("Before appExec", "point");
754 int ret = app.Exec();
755 LogDebug("App returned: " << ret);
756 ret = app.getReturnStatus();
757 LogDebug("WrtClient returned: " << ret);