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>
29 #include <dpl/localization/w3c_file_localization.h>
30 #include <dpl/localization/LanguageTagsProvider.h>
32 //W3C PACKAGING enviroment variable name
33 #define W3C_DEBUG_ENV_VARIABLE "DEBUG_LOAD_FINISH"
35 // window signal callback
36 const std::string VIEWMODE_TYPE_FULLSCREEN = "fullscreen";
37 const std::string VIEWMODE_TYPE_MAXIMIZED = "maximized";
38 char const* const ELM_SWALLOW_CONTENT = "elm.swallow.content";
40 WrtClient::WrtClient(int argc, char **argv) :
41 Application(argc, argv, "wrt-client", false),
42 DPL::TaskDecl<WrtClient>(this),
44 m_initializing(false),
49 m_returnStatus(ReturnStatus::Succeeded),
50 m_widgetState(WidgetState::WidgetState_Stopped)
53 LogDebug("App Created");
56 WrtClient::~WrtClient()
58 LogDebug("App Finished");
61 WrtClient::ReturnStatus::Type WrtClient::getReturnStatus() const
63 return m_returnStatus;
66 void WrtClient::OnStop()
68 LogInfo("Stopping Dummy Client");
72 void WrtClient::OnCreate()
75 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 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);
234 bundle_free(bundleFromSdkLauncher);
237 bool WrtClient::checkDebugMode(SDKDebugData* debugData)
239 LogError("Checking for debug mode");
242 bool debugMode = debugData->debugMode;
244 LogInfo("[DEBUG_MODE] Widget is launched in " <<
245 (debugMode ? "DEBUG" : "RETAIL") <<
248 if (debugMode == true) {
249 // In WAC widget, only test widgets can use web inspector.
251 // every launched widgets as debug mode can use it.
252 if (m_dao->getWidgetType().appType == WrtDB::APP_TYPE_WAC20)
255 GlobalLogicSingleton::Instance().GetGlobalModel()
256 ->DeveloperMode.Get();
257 //This code will be activated
258 //after WAC test certificate is used by SDK
259 //bool isTestWidget = view->m_widgetModel->IsTestWidget.Get();
262 // LogInfo("This is not WAC Test Widget");
265 if (!developerMode) {
266 LogInfo("This is not WAC Developer Mode");
274 void WrtClient::OnEventReceived(const NextStepEvent& /*event*/)
276 LogDebug("Executing next step");
280 void WrtClient::initStep()
283 if (WRT::CoreModuleSingleton::Instance().Init()) {
284 m_initialized = true;
286 m_returnStatus = ReturnStatus::Failed;
287 SwitchToStep(&WrtClient::shutdownStep);
290 // ecore_event_jobs are processed sequentially without concession to
291 // other type events. To give a chance of execute to other events,
292 // ecore_timer_add was used.
293 DPL::Event::ControllerEventHandler<NextStepEvent>::PostTimedEvent(
294 NextStepEvent(),0.001);
297 bool WrtClient::checkWACTestCertififedWidget()
299 // WAC Waikiki Beta Release Core Specification: Widget Runtime
302 // WR-4710 The WRT MUST enable debug functions only for WAC test widgets
303 // i.e. the functions must not be usable for normal WAC widgets, even when
304 // a WAC test widget is executing.
305 ADD_PROFILING_POINT("DeveloperModeCheck", "start");
308 // A widget signed with a WAC-issued test certificate as described in
311 bool developerWidget = m_dao->isTestWidget();
313 GlobalLogicSingleton::Instance().GetGlobalModel()->DeveloperMode.Get();
315 LogDebug("Is WAC test widget: " << developerWidget);
316 LogDebug("Is developer Mode: " << developerMode);
318 if (developerWidget) {
321 LogError("WAC test certified developer widget is needed for " <<
325 //TODO: WR-4660 (show popup about developer widget
327 LogInfo("POPUP: THIS IS TEST WIDGET!");
330 ADD_PROFILING_POINT("DeveloperModeCheck", "stop");
334 void WrtClient::loadFinishCallback(bool success)
336 ADD_PROFILING_POINT("loadFinishCallback", "start");
337 SDKDebugData* debug = new SDKDebugData;
338 debug->debugMode = m_debugMode;
339 debug->pid = new unsigned long(getpid());
341 LogInfo("Post result of launch");
343 // Start inspector server, if current mode is debugger mode.
344 // In the WK2 case, ewk_view_inspector_server_start should
345 // be called after WebProcess is created.
346 if (checkDebugMode(debug))
349 ewk_view_inspector_server_start(m_widget->GetCurrentWebview(), 0);
350 if (debug->portnum == 0) {
351 LogWarning("Failed to get portnum");
353 LogInfo("Assigned port number for inspector : "
357 LogDebug("Debug mode is disabled");
360 //w3c packaging test debug (message on 4>)
361 const char * makeScreen = getenv(W3C_DEBUG_ENV_VARIABLE);
362 if(makeScreen != NULL && strcmp(makeScreen, "1") == 0)
364 FILE* doutput = fdopen(4, "w");
365 fprintf(doutput,"didFinishLoadForFrameCallback: ready\n");
370 LogDebug("Launch succesfull");
373 m_initializing = false;
375 ADD_PROFILING_POINT("loadFinishCallback", "stop");
376 printf("launched\n");
381 m_returnStatus = ReturnStatus::Failed;
383 DPL::Event::ControllerEventHandler<NextStepEvent>::
384 PostEvent(NextStepEvent());
389 LogDebug("Send RT signal to wrt-launcher(pid: "
390 << m_sdkLauncherPid << ", status: " << success);
392 /* send real time signal with result to wrt-launcher */
395 LogDebug("userData->portnum : " << debug->portnum);
396 sv.sival_int = debug->portnum;
402 sigqueue(m_sdkLauncherPid, SIGRTMIN, sv);
405 ApplicationDataSingleton::Instance().freeBundle();
407 LogDebug("Cleaning wrtClient launch resources...");
412 void WrtClient::progressFinishCallback()
414 m_splashScreen->stopSplashScreen();
417 void WrtClient::windowCloseCallback()
419 LogDebug("window close called, terminating app");
420 SwitchToStep(&WrtClient::shutdownStep);
421 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
425 void WrtClient::webCrashCallback()
427 LogError("webProcess crashed");
428 SwitchToStep(&WrtClient::shutdownStep);
429 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
434 void WrtClient::launchStep()
436 ADD_PROFILING_POINT("launchStep", "start");
437 LogDebug("Launching widget ...");
439 ADD_PROFILING_POINT("getRunnableWidgetObject", "start");
440 m_widget = WRT::CoreModuleSingleton::Instance()
441 .getRunnableWidgetObject(m_tizenId);
442 ADD_PROFILING_POINT("getRunnableWidgetObject", "stop");
445 LogError("RunnableWidgetObject is NULL, stop launchStep");
446 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
451 if (m_widgetState == WidgetState_Running) {
452 LogWarning("Widget already running, stop launchStep");
453 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
458 if (m_widgetState == WidgetState_Authorizing) {
459 LogWarning("Widget already authorizing, stop launchStep");
460 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
465 ADD_PROFILING_POINT("localizeWidgetModel", "start");
466 m_dao.reset(new WrtDB::WidgetDAOReadOnly(DPL::FromASCIIString(m_tizenId)));
467 Domain::localizeWidgetModel(m_dao->getDefaultlocale());
468 ADD_PROFILING_POINT("localizeWidgetModel", "stop");
469 LocalizationSetting::SetLanguageChangedCallback(
470 languageChangedCallback, this);
472 ADD_PROFILING_POINT("CreateWindow", "start");
473 m_windowData.reset(new WindowData(static_cast<unsigned long>(getpid())));
474 ADD_PROFILING_POINT("CreateWindow", "stop");
476 WRT::UserDelegatesPtr cbs(new WRT::UserDelegates);
477 ADD_PROFILING_POINT("Create splash screen", "start");
478 m_splashScreen.reset(
479 new SplashScreenSupport(m_windowData->m_win));
480 if (m_splashScreen->createSplashScreen(m_dao->getSplashImgSrc())) {
481 m_splashScreen->startSplashScreen();
482 cbs->progressFinish = DPL::MakeDelegate(this, &WrtClient::progressFinishCallback);
484 ADD_PROFILING_POINT("Create splash screen", "stop");
485 DPL::OptionalString startUrl = W3CFileLocalization::getStartFile(m_dao);
486 m_widget->PrepareView(DPL::ToUTF8String(*startUrl),
487 m_windowData->m_win);
488 //you can't show window with splash screen before PrepareView
489 //ewk_view_add_with_context() in viewLogic breaks window
490 evas_object_show(m_windowData->m_win);
491 initializeWindowModes();
492 connectElmCallback();
494 if (!checkWACTestCertififedWidget())
496 LogWarning("WAC Certificate failed, stop launchStep");
500 m_widgetState = WidgetState_Authorizing;
501 if (!m_widget->CheckBeforeLaunch()) {
502 LogError("CheckBeforeLaunch failed, stop launchStep");
503 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
507 LogInfo("Widget launch accepted. Entering running state");
508 m_widgetState = WidgetState_Running;
510 cbs->loadFinish = DPL::MakeDelegate(this, &WrtClient::loadFinishCallback);
511 cbs->bufferSet = DPL::MakeDelegate(this, &WrtClient::setLayout);
512 cbs->bufferUnset = DPL::MakeDelegate(this, &WrtClient::unsetLayout);
513 cbs->windowClose = DPL::MakeDelegate(this, &WrtClient::windowCloseCallback);
514 cbs->webCrash = DPL::MakeDelegate(this, &WrtClient::webCrashCallback);
515 cbs->toggleFullscreen = DPL::MakeDelegate(m_windowData.get(), &WindowData::toggleFullscreen);
517 m_widget->SetUserDelegates(cbs);
519 ADD_PROFILING_POINT("launchStep", "stop");
522 void WrtClient::initializeWindowModes()
524 Assert(m_windowData);
526 auto windowModes = m_dao->getWindowModes();
527 bool fullscreen = false;
528 FOREACH(it, windowModes)
530 std::string viewMode = DPL::ToUTF8String(*it);
531 if (viewMode == VIEWMODE_TYPE_FULLSCREEN) {
534 } else if (viewMode == VIEWMODE_TYPE_MAXIMIZED) {
538 bool indicator = true;
539 if (m_dao->getWidgetType().appType == WrtDB::APP_TYPE_TIZENWEBAPP) {
540 WidgetSettings widgetSettings;
541 m_dao->getWidgetSettings(widgetSettings);
542 WidgetSettingList settings(widgetSettings);
543 indicator = (settings.getIndicatorPresence()
544 == Indicator_Enable);
547 WrtDB::WidgetLocalizedInfo localizedInfo =
548 W3CFileLocalization::getLocalizedInfo(m_dao);
549 std::string name = "";
550 if (!(localizedInfo.name.IsNull())) {
551 name = DPL::ToUTF8String(*(localizedInfo.name));
553 LogInfo("initializeWindowModes " << m_debugMode);
556 m_windowData->m_debugMode = TRUE;
559 m_windowData->m_debugMode = FALSE;
562 WindowData::CtxMenuItems ctxMenuItems;
564 WindowData::CtxMenuItem ctxMenuBackword;
565 ctxMenuBackword.label = WRT_OPTION_LABEL_BACKWARD;
566 ctxMenuBackword.icon = WRT_OPTION_ICON_BACKWARD;
567 ctxMenuBackword.callback = backwardCallback;
568 ctxMenuBackword.data = this;
569 ctxMenuItems.push_back(ctxMenuBackword);
571 WindowData::CtxMenuItem ctxMenuReload;
572 ctxMenuReload.label = WRT_OPTION_LABEL_RELOAD;
573 ctxMenuReload.icon = WRT_OPTION_ICON_RELOAD;
574 ctxMenuReload.callback = reloadCallback;
575 ctxMenuReload.data = this;
576 ctxMenuItems.push_back(ctxMenuReload);
578 WindowData::CtxMenuItem ctxMenuForward;
579 ctxMenuForward.label = WRT_OPTION_LABEL_FORWARD;
580 ctxMenuForward.icon = WRT_OPTION_ICON_FORWARD;
581 ctxMenuForward.callback = forwardCallback;
582 ctxMenuForward.data = this;
583 ctxMenuItems.push_back(ctxMenuForward);
585 m_windowData->setViewMode(fullscreen,
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");
662 elm_object_part_content_set(m_windowData->m_user_layout, ELM_SWALLOW_CONTENT, newBuffer);
664 evas_object_show(newBuffer);
665 evas_object_focus_set(newBuffer, EINA_TRUE);
668 void WrtClient::unsetLayout(Evas_Object* currentBuffer) {
669 LogDebug("remove current webkit buffer from window");
670 Assert(currentBuffer);
671 evas_object_hide(currentBuffer);
673 elm_object_part_content_unset(m_windowData->m_user_layout, ELM_SWALLOW_CONTENT);
677 void WrtClient::shutdownStep()
679 LogDebug("Closing Wrt connection ...");
680 if (m_initialized && m_widget) {
681 m_widgetState = WidgetState_Stopped;
684 m_windowData.reset();
685 WRT::CoreModuleSingleton::Instance().Terminate();
686 m_initialized = false;
691 int WrtClient::languageChangedCallback(void *data)
693 LogDebug("Language Changed");
694 WrtClient* wrtClient = static_cast<WrtClient*>(data);
696 LanguageTags oldtags = LanguageTagsProviderSingleton::Instance().getLanguageTags();
697 // reset function fetches system locales and recreates language tags
698 LanguageTagsProviderSingleton::Instance().resetLanguageTags();
699 LanguageTags newtags = LanguageTagsProviderSingleton::Instance().getLanguageTags();
701 // check whether LanguageTags changed or not
702 if (oldtags != newtags) {
703 // update localized data
704 Domain::localizeWidgetModel(wrtClient->m_dao->getDefaultlocale());
706 if (true == wrtClient->m_launched &&
707 wrtClient->m_widgetState != WidgetState_Stopped) {
708 wrtClient->m_widget->ReloadStartPage();
717 ADD_PROFILING_POINT("main-entered", "point");
719 // Output on stdout will be flushed after every newline character,
720 // even if it is redirected to a pipe. This is useful for running
721 // from a script and parsing output.
722 // (Standard behavior of stdlib is to use full buffering when
723 // redirected to a pipe, which means even after an end of line
724 // the output may not be flushed).
727 // set evas backend type
728 if (!getenv("ELM_ENGINE")) {
729 if (setenv("ELM_ENGINE", "gl", 1)) {
730 LogDebug("Enable backend");
734 setenv("COREGL_FASTPATH", "1", 1);
736 setenv("CAIRO_GL_COMPOSITOR", "msaa", 1);
737 setenv("CAIRO_GL_LAZY_FLUSHING", "yes", 1);
738 setenv("ELM_IMAGE_CACHE", "0", 1);
741 DPL::Log::LogSystemSingleton::Instance().SetTag("WRT-CLIENT");
743 WrtClient app(argc, argv);
745 ADD_PROFILING_POINT("Before appExec", "point");
746 int ret = app.Exec();
747 LogDebug("App returned: " << ret);
748 ret = app.getReturnStatus();
749 LogDebug("WrtClient returned: " << ret);