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");
78 void WrtClient::OnResume()
80 if (m_widgetState != WidgetState_Suspended) {
81 LogWarning("Widget is not suspended, resuming was skipped");
85 m_widgetState = WidgetState_Running;
89 void WrtClient::OnPause()
93 if (m_widgetState != WidgetState_Running) {
94 LogWarning("Widget is not running to be suspended");
98 m_widgetState = WidgetState_Suspended;
101 void WrtClient::OnReset(bundle *b)
104 // bundle argument is freed after OnReset() is returned
105 // So bundle duplication is needed
106 ApplicationDataSingleton::Instance().setBundle(bundle_dup(b));
108 if (true == m_initializing) {
109 LogDebug("can not handle reset event");
112 if (true == m_launched) {
113 if (m_widgetState == WidgetState_Stopped) {
114 LogError("Widget is not running to be reset");
118 m_widgetState = WidgetState_Running;
120 if (true == checkArgument())
131 void WrtClient::OnTerminate()
133 LogDebug("Wrt Shutdown now");
137 void WrtClient::showHelpAndQuit()
139 printf("Usage: wrt-client [OPTION]... [WIDGET: ID]...\n"
141 "Mandatory arguments to long options are mandatory for short "
143 " -h, --help show this help\n"
145 "launch widget with given tizen ID\n"
147 "launch widget with given tizen ID\n"
153 bool WrtClient::checkArgument()
155 LogInfo("checkArgument");
157 std::string arg = m_argv[0];
163 if (arg.find("wrt-client") != std::string::npos)
171 if (arg == "-h" || arg == "--help") {
174 } else if (arg == "-l" || arg == "--launch" ||
175 arg == "-t" || arg == "--tizen") {
179 m_tizenId = std::string(m_argv[2]);
184 size_t pos = arg.find_last_of('/');
186 if (pos != std::string::npos) {
187 arg = arg.erase(0, pos + 1);
190 // Launch widget based on application basename
192 LogDebug("Tizen id: " << m_tizenId);
198 void WrtClient::setStep()
202 AddStep(&WrtClient::initStep);
204 setSdkLauncherDebugData();
206 AddStep(&WrtClient::launchStep);
207 AddStep(&WrtClient::shutdownStep);
209 m_initializing = true;
211 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(NextStepEvent());
214 void WrtClient::setSdkLauncherDebugData()
216 LogDebug("setSdkLauncherDebugData");
218 /* check bundle from sdk launcher */
219 bundle *bundleFromSdkLauncher;
220 bundleFromSdkLauncher = bundle_import_from_argv(m_argc, m_argv);
221 const char *bundle_debug = bundle_get_val(bundleFromSdkLauncher, "debug");
222 const char *bundle_pid = bundle_get_val(bundleFromSdkLauncher, "pid");
223 if (bundle_debug != NULL && bundle_pid != NULL) {
224 if (strcmp(bundle_debug, "true") == 0) {
226 m_sdkLauncherPid = atoi(bundle_pid);
231 bundle_free(bundleFromSdkLauncher);
234 bool WrtClient::checkDebugMode(SDKDebugData* debugData)
236 LogError("Checking for debug mode");
239 bool debugMode = debugData->debugMode;
241 LogInfo("[DEBUG_MODE] Widget is launched in " <<
242 (debugMode ? "DEBUG" : "RETAIL") <<
245 if (debugMode == true) {
246 // In WAC widget, only test widgets can use web inspector.
248 // every launched widgets as debug mode can use it.
249 if (m_dao->getWidgetType().appType == WrtDB::APP_TYPE_WAC20)
252 GlobalLogicSingleton::Instance().GetGlobalModel()
253 ->DeveloperMode.Get();
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();
310 GlobalLogicSingleton::Instance().GetGlobalModel()->DeveloperMode.Get();
312 LogDebug("Is WAC test widget: " << developerWidget);
313 LogDebug("Is developer Mode: " << developerMode);
315 if (developerWidget) {
318 LogError("WAC test certified developer widget is needed for " <<
322 //TODO: WR-4660 (show popup about developer widget
324 LogInfo("POPUP: THIS IS TEST WIDGET!");
327 ADD_PROFILING_POINT("DeveloperModeCheck", "stop");
331 void WrtClient::loadFinishCallback(Evas_Object* webview)
333 ADD_PROFILING_POINT("loadFinishCallback", "start");
334 SDKDebugData* debug = new SDKDebugData;
335 debug->debugMode = m_debugMode;
336 debug->pid = new unsigned long(getpid());
338 LogInfo("Post result of launch");
340 // Start inspector server, if current mode is debugger mode.
341 // In the WK2 case, ewk_view_inspector_server_start should
342 // be called after WebProcess is created.
343 if (checkDebugMode(debug))
346 ewk_view_inspector_server_start(m_widget->GetCurrentWebview(), 0);
347 if (debug->portnum == 0) {
348 LogWarning("Failed to get portnum");
350 LogInfo("Assigned port number for inspector : "
354 LogDebug("Debug mode is disabled");
357 //w3c packaging test debug (message on 4>)
358 const char * makeScreen = getenv(W3C_DEBUG_ENV_VARIABLE);
359 if(makeScreen != NULL && strcmp(makeScreen, "1") == 0)
361 FILE* doutput = fdopen(4, "w");
362 fprintf(doutput,"didFinishLoadForFrameCallback: ready\n");
367 LogDebug("Launch succesfull");
370 m_initializing = false;
372 ADD_PROFILING_POINT("loadFinishCallback", "stop");
373 printf("launched\n");
378 m_returnStatus = ReturnStatus::Failed;
380 DPL::Event::ControllerEventHandler<NextStepEvent>::
381 PostEvent(NextStepEvent());
386 LogDebug("Send RT signal to wrt-launcher(pid: " << m_sdkLauncherPid);
388 /* send real time signal with result to wrt-launcher */
390 LogDebug("userData->portnum : " << debug->portnum);
391 sv.sival_int = debug->portnum;
395 sigqueue(m_sdkLauncherPid, SIGRTMIN, sv);
398 ApplicationDataSingleton::Instance().freeBundle();
400 LogDebug("Cleaning wrtClient launch resources...");
405 void WrtClient::progressFinishCallback()
407 m_splashScreen->stopSplashScreen();
410 void WrtClient::webkitExitCallback()
412 LogDebug("window close called, terminating app");
413 SwitchToStep(&WrtClient::shutdownStep);
414 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
418 void WrtClient::webCrashCallback()
420 LogError("webProcess crashed");
421 SwitchToStep(&WrtClient::shutdownStep);
422 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
427 void WrtClient::launchStep()
429 ADD_PROFILING_POINT("launchStep", "start");
430 LogDebug("Launching widget ...");
432 ADD_PROFILING_POINT("getRunnableWidgetObject", "start");
433 m_widget = WRT::CoreModuleSingleton::Instance()
434 .getRunnableWidgetObject(m_tizenId);
435 ADD_PROFILING_POINT("getRunnableWidgetObject", "stop");
438 LogError("RunnableWidgetObject is NULL, stop launchStep");
439 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
444 if (m_widgetState == WidgetState_Running) {
445 LogWarning("Widget already running, stop launchStep");
446 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
451 if (m_widgetState == WidgetState_Authorizing) {
452 LogWarning("Widget already authorizing, stop launchStep");
453 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
458 m_dao.reset(new WrtDB::WidgetDAOReadOnly(DPL::FromASCIIString(m_tizenId)));
459 DPL::Optional<DPL::String> defloc = m_dao->getDefaultlocale();
462 LanguageTagsProviderSingleton::Instance().addWidgetDefaultLocales(*defloc);
465 LocalizationSetting::SetLanguageChangedCallback(
466 languageChangedCallback, this);
468 ADD_PROFILING_POINT("CreateWindow", "start");
469 m_windowData.reset(new WindowData(static_cast<unsigned long>(getpid())));
470 ADD_PROFILING_POINT("CreateWindow", "stop");
472 WRT::UserDelegatesPtr cbs(new WRT::UserDelegates);
473 ADD_PROFILING_POINT("Create splash screen", "start");
474 m_splashScreen.reset(
475 new SplashScreenSupport(m_windowData->m_win));
476 if (m_splashScreen->createSplashScreen(m_dao->getSplashImgSrc())) {
477 m_splashScreen->startSplashScreen();
478 cbs->progressFinish = DPL::MakeDelegate(this, &WrtClient::progressFinishCallback);
480 ADD_PROFILING_POINT("Create splash screen", "stop");
481 DPL::OptionalString startUrl = W3CFileLocalization::getStartFile(m_dao);
482 m_widget->PrepareView(DPL::ToUTF8String(*startUrl),
483 m_windowData->m_win);
484 //you can't show window with splash screen before PrepareView
485 //ewk_view_add_with_context() in viewLogic breaks window
487 WrtDB::WidgetLocalizedInfo localizedInfo =
488 W3CFileLocalization::getLocalizedInfo(m_dao);
489 std:: string name = "";
490 if (!(localizedInfo.name.IsNull())) {
491 name = DPL::ToUTF8String(*(localizedInfo.name));
493 elm_win_title_set(m_windowData->m_win, name.c_str());
494 evas_object_show(m_windowData->m_win);
495 initializeWindowModes();
496 connectElmCallback();
498 if (!checkWACTestCertififedWidget())
500 LogWarning("WAC Certificate failed, stop launchStep");
504 m_widgetState = WidgetState_Authorizing;
505 if (!m_widget->CheckBeforeLaunch()) {
506 LogError("CheckBeforeLaunch failed, stop launchStep");
507 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
511 LogInfo("Widget launch accepted. Entering running state");
512 m_widgetState = WidgetState_Running;
514 cbs->loadFinish = DPL::MakeDelegate(this, &WrtClient::loadFinishCallback);
515 cbs->bufferSet = DPL::MakeDelegate(this, &WrtClient::setLayout);
516 cbs->bufferUnset = DPL::MakeDelegate(this, &WrtClient::unsetLayout);
517 cbs->webkitExit = DPL::MakeDelegate(this, &WrtClient::webkitExitCallback);
518 cbs->webCrash = DPL::MakeDelegate(this, &WrtClient::webCrashCallback);
519 cbs->toggleFullscreen = DPL::MakeDelegate(m_windowData.get(), &WindowData::toggleFullscreen);
521 m_widget->SetUserDelegates(cbs);
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 FOREACH(it, windowModes)
534 std::string viewMode = DPL::ToUTF8String(*it);
535 if (viewMode == VIEWMODE_TYPE_FULLSCREEN) {
538 } else if (viewMode == VIEWMODE_TYPE_MAXIMIZED) {
543 WrtDB::WidgetLocalizedInfo localizedInfo =
544 W3CFileLocalization::getLocalizedInfo(m_dao);
545 std::string name = "";
546 if (!(localizedInfo.name.IsNull())) {
547 name = DPL::ToUTF8String(*(localizedInfo.name));
549 LogInfo("initializeWindowModes " << m_debugMode);
551 m_windowData->m_debugMode = FALSE;
554 m_windowData->m_debugMode = TRUE;
557 m_windowData->m_debugMode = FALSE;
560 WindowData::CtxMenuItems ctxMenuItems;
562 WindowData::CtxMenuItem ctxMenuBackword;
563 ctxMenuBackword.label = WRT_OPTION_LABEL_BACKWARD;
564 ctxMenuBackword.icon = WRT_OPTION_ICON_BACKWARD;
565 ctxMenuBackword.callback = backwardCallback;
566 ctxMenuBackword.data = this;
567 ctxMenuItems.push_back(ctxMenuBackword);
569 WindowData::CtxMenuItem ctxMenuReload;
570 ctxMenuReload.label = WRT_OPTION_LABEL_RELOAD;
571 ctxMenuReload.icon = WRT_OPTION_ICON_RELOAD;
572 ctxMenuReload.callback = reloadCallback;
573 ctxMenuReload.data = this;
574 ctxMenuItems.push_back(ctxMenuReload);
576 WindowData::CtxMenuItem ctxMenuForward;
577 ctxMenuForward.label = WRT_OPTION_LABEL_FORWARD;
578 ctxMenuForward.icon = WRT_OPTION_ICON_FORWARD;
579 ctxMenuForward.callback = forwardCallback;
580 ctxMenuForward.data = this;
581 ctxMenuItems.push_back(ctxMenuForward);
583 m_windowData->setViewMode(name.c_str(),
588 void WrtClient::backButtonCallback(void* data,
589 Evas_Object * /*obj*/,
590 void * /*event_info*/)
592 LogInfo("BackButtonCallback");
595 WrtClient* This = static_cast<WrtClient*>(data);
597 This->m_widget->Backward();
600 void WrtClient::backwardCallback(void *data,
601 Evas_Object */*obj*/,
602 void */*event_info*/)
604 LogInfo("BackButtonCallback");
607 WrtClient* This = static_cast<WrtClient*>(data);
609 This->m_widget->Backward();
610 This->m_windowData->initFullViewMode();
613 void WrtClient::reloadCallback(void *data,
614 Evas_Object */*obj*/,
615 void */*event_info*/)
617 LogInfo("reloadCallback");
619 WrtClient* This = static_cast<WrtClient*>(data);
621 This->m_widget->Reload();
622 This->m_windowData->initFullViewMode();
625 void WrtClient::forwardCallback(void *data,
626 Evas_Object */*obj*/,
627 void */*event_info*/)
629 LogInfo("forwardCallback");
631 WrtClient* This = static_cast<WrtClient*>(data);
633 This->m_widget->Forward();
634 This->m_windowData->initFullViewMode();
638 void WrtClient::connectElmCallback()
640 Assert(m_windowData);
642 if (m_dao->getWidgetType().appType == WrtDB::APP_TYPE_TIZENWEBAPP) {
643 WidgetSettings widgetSettings;
644 m_dao->getWidgetSettings(widgetSettings);
645 WidgetSettingList settings(widgetSettings);
646 WidgetSettingScreenLock rotationValue = settings.getRotationValue();
647 if (rotationValue == Screen_Portrait) {
648 elm_win_rotation_with_resize_set(m_windowData->m_win, 0);
649 } else if (rotationValue == Screen_Landscape) {
650 elm_win_rotation_with_resize_set(m_windowData->m_win, 270);
652 elm_win_rotation_with_resize_set(m_windowData->m_win, 0);
657 void WrtClient::setLayout(Evas_Object* newBuffer) {
658 LogDebug("add new webkit buffer to window");
660 elm_object_content_set(m_windowData->m_conformant, newBuffer);
661 evas_object_show(newBuffer);
662 evas_object_focus_set(newBuffer, EINA_TRUE);
665 void WrtClient::unsetLayout(Evas_Object* currentBuffer) {
666 LogDebug("remove current webkit buffer from window");
667 Assert(currentBuffer);
668 evas_object_hide(currentBuffer);
669 elm_object_content_unset(m_windowData->m_conformant);
672 void WrtClient::shutdownStep()
674 LogDebug("Closing Wrt connection ...");
675 if (m_initialized && m_widget) {
676 m_widgetState = WidgetState_Stopped;
679 m_windowData.reset();
680 WRT::CoreModuleSingleton::Instance().Terminate();
681 m_initialized = false;
686 int WrtClient::languageChangedCallback(void *data)
688 LogDebug("Language Changed");
691 WrtClient* wrtClient = static_cast<WrtClient*>(data);
692 if(!(wrtClient->m_dao))
695 // reset function fetches system locales and recreates language tags
696 LanguageTagsProviderSingleton::Instance().resetLanguageTags();
697 // widget default locales are added to language tags below
698 DPL::OptionalString defloc = wrtClient->m_dao->getDefaultlocale();
701 LanguageTagsProviderSingleton::Instance().addWidgetDefaultLocales(*defloc);
704 if (wrtClient->m_launched &&
705 wrtClient->m_widgetState != WidgetState_Stopped) {
706 wrtClient->m_widget->ReloadStartPage();
714 ADD_PROFILING_POINT("main-entered", "point");
716 // Output on stdout will be flushed after every newline character,
717 // even if it is redirected to a pipe. This is useful for running
718 // from a script and parsing output.
719 // (Standard behavior of stdlib is to use full buffering when
720 // redirected to a pipe, which means even after an end of line
721 // the output may not be flushed).
724 // set evas backend type
725 if (!getenv("ELM_ENGINE")) {
726 if (setenv("ELM_ENGINE", "gl", 1)) {
727 LogDebug("Enable backend");
731 setenv("COREGL_FASTPATH", "1", 1);
733 setenv("CAIRO_GL_COMPOSITOR", "msaa", 1);
734 setenv("CAIRO_GL_LAZY_FLUSHING", "yes", 1);
735 setenv("ELM_IMAGE_CACHE", "0", 1);
738 DPL::Log::LogSystemSingleton::Instance().SetTag("WRT-CLIENT");
740 WrtClient app(argc, argv);
742 ADD_PROFILING_POINT("Before appExec", "point");
743 int ret = app.Exec();
744 LogDebug("App returned: " << ret);
745 ret = app.getReturnStatus();
746 LogDebug("WrtClient returned: " << ret);