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"
17 #include <appcore-efl.h>
21 #include <dpl/log/log.h>
22 #include <dpl/optional_typedefs.h>
23 #include <dpl/exception.h>
24 #include <common/application_data.h>
25 #include <core_module.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 char *EDJE_SHOW_BACKWARD_SIGNAL = "show,backward,signal";
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";
40 const char* const BUNDLE_PATH = "/usr/lib/wrt-wk2-bundles/libwrt-wk2-bundle.so";
42 static Ewk_Context* s_ewk_context = NULL;
44 WrtClient::WrtClient(int argc, char **argv) :
45 Application(argc, argv, "wrt-client", false),
46 DPL::TaskDecl<WrtClient>(this),
48 m_initializing(false),
53 m_returnStatus(ReturnStatus::Succeeded),
54 m_widgetState(WidgetState::WidgetState_Stopped)
57 LogDebug("App Created");
60 WrtClient::~WrtClient()
62 LogDebug("App Finished");
65 WrtClient::ReturnStatus::Type WrtClient::getReturnStatus() const
67 return m_returnStatus;
70 void WrtClient::OnStop()
72 LogInfo("Stopping Dummy Client");
76 void WrtClient::OnCreate()
79 ADD_PROFILING_POINT("OnCreate callback", "point");
84 void WrtClient::OnResume()
86 if (m_widgetState != WidgetState_Suspended) {
87 LogWarning("Widget is not suspended, resuming was skipped");
91 m_widgetState = WidgetState_Running;
95 void WrtClient::OnPause()
99 if (m_widgetState != WidgetState_Running) {
100 LogWarning("Widget is not running to be suspended");
104 m_widgetState = WidgetState_Suspended;
107 void WrtClient::OnReset(bundle *b)
110 // bundle argument is freed after OnReset() is returned
111 // So bundle duplication is needed
112 ApplicationDataSingleton::Instance().setBundle(bundle_dup(b));
114 if (true == m_initializing) {
115 LogDebug("can not handle reset event");
118 if (true == m_launched) {
119 if (m_widgetState == WidgetState_Stopped) {
120 LogError("Widget is not running to be reset");
124 m_windowData->emitSignalForUserLayout(EDJE_SHOW_BACKWARD_SIGNAL, "");
125 m_widgetState = WidgetState_Running;
127 if (true == checkArgument())
138 void WrtClient::OnTerminate()
140 LogDebug("Wrt Shutdown now");
144 void WrtClient::showHelpAndQuit()
146 printf("Usage: wrt-client [OPTION]... [WIDGET: ID]...\n"
148 "Mandatory arguments to long options are mandatory for short "
150 " -h, --help show this help\n"
152 "launch widget with given tizen ID\n"
154 "launch widget with given tizen ID\n"
160 bool WrtClient::checkArgument()
162 std::string tizenId = getTizenIdFromArgument(m_argc, m_argv);
169 LogDebug("Tizen id: " << m_tizenId);
174 std::string WrtClient::getTizenIdFromArgument(int argc, char **argv)
176 LogInfo("checkArgument");
177 std::string arg = argv[0];
183 if (arg.find("wrt-client") != std::string::npos) {
190 if (arg == "-h" || arg == "--help") {
192 } else if (arg == "-l" || arg == "--launch" ||
193 arg == "-t" || arg == "--tizen") {
202 // Launch widget based on application basename
203 size_t pos = arg.find_last_of('/');
205 if (pos != std::string::npos) {
206 arg = arg.erase(0, pos + 1);
215 void WrtClient::setStep()
219 AddStep(&WrtClient::initStep);
221 setSdkLauncherDebugData();
223 AddStep(&WrtClient::launchStep);
224 AddStep(&WrtClient::shutdownStep);
226 m_initializing = true;
228 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(NextStepEvent());
231 void WrtClient::setSdkLauncherDebugData()
233 LogDebug("setSdkLauncherDebugData");
235 /* check bundle from sdk launcher */
236 bundle *bundleFromSdkLauncher;
237 bundleFromSdkLauncher = bundle_import_from_argv(m_argc, m_argv);
238 const char *bundle_debug = bundle_get_val(bundleFromSdkLauncher, "debug");
239 const char *bundle_pid = bundle_get_val(bundleFromSdkLauncher, "pid");
240 if (bundle_debug != NULL && bundle_pid != NULL) {
241 if (strcmp(bundle_debug, "true") == 0) {
243 m_sdkLauncherPid = atoi(bundle_pid);
248 bundle_free(bundleFromSdkLauncher);
251 bool WrtClient::checkDebugMode(SDKDebugData* debugData)
253 LogError("Checking for debug mode");
256 bool debugMode = debugData->debugMode;
258 LogInfo("[DEBUG_MODE] Widget is launched in " <<
259 (debugMode ? "DEBUG" : "RETAIL") <<
262 if (debugMode == true) {
263 // In WAC widget, only test widgets can use web inspector.
265 // every launched widgets as debug mode can use it.
266 if (m_dao->getWidgetType().appType == WrtDB::APP_TYPE_WAC20)
268 bool developerMode = WRT::CoreModuleSingleton::Instance().developerMode();
269 //This code will be activated
270 //after WAC test certificate is used by SDK
271 //bool isTestWidget = view->m_widgetModel->IsTestWidget.Get();
274 // LogInfo("This is not WAC Test Widget");
277 if (!developerMode) {
278 LogInfo("This is not WAC Developer Mode");
286 void WrtClient::OnEventReceived(const NextStepEvent& /*event*/)
288 LogDebug("Executing next step");
292 void WrtClient::initStep()
295 if (WRT::CoreModuleSingleton::Instance().Init(s_ewk_context)) {
296 m_initialized = true;
298 m_returnStatus = ReturnStatus::Failed;
299 SwitchToStep(&WrtClient::shutdownStep);
302 // ecore_event_jobs are processed sequentially without concession to
303 // other type events. To give a chance of execute to other events,
304 // ecore_timer_add was used.
305 DPL::Event::ControllerEventHandler<NextStepEvent>::PostTimedEvent(
306 NextStepEvent(),0.001);
309 bool WrtClient::checkWACTestCertififedWidget()
311 // WAC Waikiki Beta Release Core Specification: Widget Runtime
314 // WR-4710 The WRT MUST enable debug functions only for WAC test widgets
315 // i.e. the functions must not be usable for normal WAC widgets, even when
316 // a WAC test widget is executing.
317 ADD_PROFILING_POINT("DeveloperModeCheck", "start");
320 // A widget signed with a WAC-issued test certificate as described in
323 bool developerWidget = m_dao->isTestWidget();
324 bool developerMode = WRT::CoreModuleSingleton::Instance().developerMode();
326 LogDebug("Is WAC test widget: " << developerWidget);
327 LogDebug("Is developer Mode: " << developerMode);
329 if (developerWidget) {
332 LogError("WAC test certified developer widget is needed for " <<
336 //TODO: WR-4660 (show popup about developer widget
338 LogInfo("POPUP: THIS IS TEST WIDGET!");
341 ADD_PROFILING_POINT("DeveloperModeCheck", "stop");
345 void WrtClient::loadFinishCallback(Evas_Object* webview)
347 ADD_PROFILING_POINT("loadFinishCallback", "start");
348 SDKDebugData* debug = new SDKDebugData;
349 debug->debugMode = m_debugMode;
350 debug->pid = new unsigned long(getpid());
352 LogInfo("Post result of launch");
354 // Start inspector server, if current mode is debugger mode.
355 // In the WK2 case, ewk_view_inspector_server_start should
356 // be called after WebProcess is created.
357 if (checkDebugMode(debug))
360 ewk_view_inspector_server_start(m_widget->GetCurrentWebview(), 0);
361 if (debug->portnum == 0) {
362 LogWarning("Failed to get portnum");
364 LogInfo("Assigned port number for inspector : "
368 LogDebug("Debug mode is disabled");
371 //w3c packaging test debug (message on 4>)
372 const char * makeScreen = getenv(W3C_DEBUG_ENV_VARIABLE);
373 if(makeScreen != NULL && strcmp(makeScreen, "1") == 0)
375 FILE* doutput = fdopen(4, "w");
376 fprintf(doutput,"didFinishLoadForFrameCallback: ready\n");
381 LogDebug("Launch succesfull");
384 m_initializing = false;
386 ADD_PROFILING_POINT("loadFinishCallback", "stop");
387 printf("launched\n");
392 m_returnStatus = ReturnStatus::Failed;
394 DPL::Event::ControllerEventHandler<NextStepEvent>::
395 PostEvent(NextStepEvent());
400 LogDebug("Send RT signal to wrt-launcher(pid: " << m_sdkLauncherPid);
402 /* send real time signal with result to wrt-launcher */
404 LogDebug("userData->portnum : " << debug->portnum);
405 sv.sival_int = debug->portnum;
409 sigqueue(m_sdkLauncherPid, SIGRTMIN, sv);
412 ApplicationDataSingleton::Instance().freeBundle();
414 LogDebug("Cleaning wrtClient launch resources...");
419 void WrtClient::progressFinishCallback()
421 m_splashScreen->stopSplashScreen();
424 void WrtClient::webkitExitCallback()
426 LogDebug("window close called, terminating app");
427 SwitchToStep(&WrtClient::shutdownStep);
428 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
432 void WrtClient::webCrashCallback()
434 LogError("webProcess crashed");
435 SwitchToStep(&WrtClient::shutdownStep);
436 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
441 void WrtClient::launchStep()
443 ADD_PROFILING_POINT("launchStep", "start");
444 LogDebug("Launching widget ...");
446 ADD_PROFILING_POINT("getRunnableWidgetObject", "start");
447 m_widget = WRT::CoreModuleSingleton::Instance()
448 .getRunnableWidgetObject(m_tizenId);
449 ADD_PROFILING_POINT("getRunnableWidgetObject", "stop");
452 LogError("RunnableWidgetObject is NULL, stop launchStep");
453 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
458 if (m_widgetState == WidgetState_Running) {
459 LogWarning("Widget already running, stop launchStep");
460 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
465 if (m_widgetState == WidgetState_Authorizing) {
466 LogWarning("Widget already authorizing, stop launchStep");
467 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
472 m_dao.reset(new WrtDB::WidgetDAOReadOnly(DPL::FromASCIIString(m_tizenId)));
473 DPL::Optional<DPL::String> defloc = m_dao->getDefaultlocale();
476 LanguageTagsProviderSingleton::Instance().addWidgetDefaultLocales(*defloc);
479 // For localizationsetting not changed on widget running
480 //LocalizationSetting::SetLanguageChangedCallback(
481 // languageChangedCallback, this);
483 ADD_PROFILING_POINT("CreateWindow", "start");
484 m_windowData.reset(new WindowData(static_cast<unsigned long>(getpid())));
485 ADD_PROFILING_POINT("CreateWindow", "stop");
487 WRT::UserDelegatesPtr cbs(new WRT::UserDelegates);
488 ADD_PROFILING_POINT("Create splash screen", "start");
489 m_splashScreen.reset(
490 new SplashScreenSupport(m_windowData->m_win));
491 if (m_splashScreen->createSplashScreen(m_dao->getSplashImgSrc())) {
492 m_splashScreen->startSplashScreen();
493 cbs->progressFinish = DPL::MakeDelegate(this, &WrtClient::progressFinishCallback);
495 ADD_PROFILING_POINT("Create splash screen", "stop");
496 DPL::OptionalString startUrl = W3CFileLocalization::getStartFile(m_dao);
497 m_widget->PrepareView(DPL::ToUTF8String(*startUrl),
498 m_windowData->m_win);
499 //you can't show window with splash screen before PrepareView
500 //ewk_view_add_with_context() in viewLogic breaks window
502 WrtDB::WidgetLocalizedInfo localizedInfo =
503 W3CFileLocalization::getLocalizedInfo(m_dao);
504 std:: string name = "";
505 if (!(localizedInfo.name.IsNull())) {
506 name = DPL::ToUTF8String(*(localizedInfo.name));
508 elm_win_title_set(m_windowData->m_win, name.c_str());
509 evas_object_show(m_windowData->m_win);
510 initializeWindowModes();
511 connectElmCallback();
513 if (!checkWACTestCertififedWidget())
515 LogWarning("WAC Certificate failed, stop launchStep");
519 m_widgetState = WidgetState_Authorizing;
520 if (!m_widget->CheckBeforeLaunch()) {
521 LogError("CheckBeforeLaunch failed, stop launchStep");
522 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
526 LogInfo("Widget launch accepted. Entering running state");
527 m_widgetState = WidgetState_Running;
529 cbs->loadFinish = DPL::MakeDelegate(this, &WrtClient::loadFinishCallback);
530 cbs->bufferSet = DPL::MakeDelegate(this, &WrtClient::setLayout);
531 cbs->bufferUnset = DPL::MakeDelegate(this, &WrtClient::unsetLayout);
532 cbs->webkitExit = DPL::MakeDelegate(this, &WrtClient::webkitExitCallback);
533 cbs->webCrash = DPL::MakeDelegate(this, &WrtClient::webCrashCallback);
534 cbs->toggleFullscreen = DPL::MakeDelegate(m_windowData.get(), &WindowData::toggleFullscreen);
536 m_widget->SetUserDelegates(cbs);
538 m_windowData->emitSignalForUserLayout(EDJE_SHOW_BACKWARD_SIGNAL, "");
539 ADD_PROFILING_POINT("launchStep", "stop");
542 void WrtClient::initializeWindowModes()
544 Assert(m_windowData);
546 auto windowModes = m_dao->getWindowModes();
547 bool fullscreen = false;
548 bool backbutton = false;
549 if (m_dao->getWidgetType().appType == WrtDB::APP_TYPE_TIZENWEBAPP) {
550 WidgetSettings widgetSettings;
551 m_dao->getWidgetSettings(widgetSettings);
552 WidgetSettingList settings(widgetSettings);
554 (settings.getBackButtonPresence() == BackButton_Enable);
557 FOREACH(it, windowModes)
559 std::string viewMode = DPL::ToUTF8String(*it);
560 if (viewMode == VIEWMODE_TYPE_FULLSCREEN) {
563 } else if (viewMode == VIEWMODE_TYPE_MAXIMIZED) {
568 m_windowData->setViewMode(fullscreen,
572 void WrtClient::backButtonCallback(void* data,
573 Evas_Object * /*obj*/,
574 void * /*event_info*/)
576 LogInfo("BackButtonCallback");
579 WrtClient* This = static_cast<WrtClient*>(data);
581 This->m_widget->Backward();
584 void WrtClient::connectElmCallback()
586 Assert(m_windowData);
588 if (m_dao->getWidgetType().appType == WrtDB::APP_TYPE_TIZENWEBAPP) {
589 WidgetSettings widgetSettings;
590 m_dao->getWidgetSettings(widgetSettings);
591 WidgetSettingList settings(widgetSettings);
592 if (settings.getBackButtonPresence() == BackButton_Enable) {
593 m_windowData->addFloatBackButtonCallback(
595 &WrtClient::backButtonCallback,
599 WidgetSettingScreenLock rotationValue = settings.getRotationValue();
600 if (rotationValue == Screen_Portrait) {
601 elm_win_rotation_with_resize_set(m_windowData->m_win, 0);
602 } else if (rotationValue == Screen_Landscape) {
603 elm_win_rotation_with_resize_set(m_windowData->m_win, 270);
605 elm_win_rotation_with_resize_set(m_windowData->m_win, 0);
610 void WrtClient::setLayout(Evas_Object* newBuffer) {
611 LogDebug("add new webkit buffer to window");
614 elm_object_part_content_set(m_windowData->m_user_layout, ELM_SWALLOW_CONTENT, newBuffer);
616 evas_object_show(newBuffer);
619 void WrtClient::unsetLayout(Evas_Object* currentBuffer) {
620 LogDebug("remove current webkit buffer from window");
621 Assert(currentBuffer);
622 evas_object_hide(currentBuffer);
624 elm_object_part_content_unset(m_windowData->m_user_layout, ELM_SWALLOW_CONTENT);
627 void WrtClient::shutdownStep()
629 LogDebug("Closing Wrt connection ...");
630 if (m_initialized && m_widget) {
631 m_widgetState = WidgetState_Stopped;
634 m_windowData.reset();
635 WRT::CoreModuleSingleton::Instance().Terminate();
636 m_initialized = false;
641 int WrtClient::languageChangedCallback(void *data)
643 LogDebug("Language Changed");
646 WrtClient* wrtClient = static_cast<WrtClient*>(data);
647 if(!(wrtClient->m_dao))
650 // reset function fetches system locales and recreates language tags
651 LanguageTagsProviderSingleton::Instance().resetLanguageTags();
652 // widget default locales are added to language tags below
653 DPL::OptionalString defloc = wrtClient->m_dao->getDefaultlocale();
656 LanguageTagsProviderSingleton::Instance().addWidgetDefaultLocales(*defloc);
659 if (wrtClient->m_launched &&
660 wrtClient->m_widgetState != WidgetState_Stopped) {
661 wrtClient->m_widget->ReloadStartPage();
666 void WrtClient::Quit()
669 DPL::Application::Quit();
675 UNHANDLED_EXCEPTION_HANDLER_BEGIN
677 ADD_PROFILING_POINT("main-entered", "point");
680 DPL::Log::LogSystemSingleton::Instance().SetTag("WRT");
682 // This code is to fork a web process without exec.
683 std::string tizenId = WrtClient::getTizenIdFromArgument(argc, argv);
685 if (!tizenId.empty())
687 LogDebug("Launching by fork mode");
688 // Language env setup
689 appcore_set_i18n("wrt-client", NULL);
691 ewk_set_arguments(argc, argv);
692 setenv("WRT_LAUNCHING_PERFORMANCE", "1", 1);
693 s_ewk_context = ewk_context_new_with_injected_bundle_path(BUNDLE_PATH);
696 // Output on stdout will be flushed after every newline character,
697 // even if it is redirected to a pipe. This is useful for running
698 // from a script and parsing output.
699 // (Standard behavior of stdlib is to use full buffering when
700 // redirected to a pipe, which means even after an end of line
701 // the output may not be flushed).
704 // set evas backend type
705 if (!getenv("ELM_ENGINE")) {
706 if (!setenv("ELM_ENGINE", "gl", 1)) {
707 LogDebug("Enable backend");
711 LogDebug("ELM_ENGINE : " << getenv("ELM_ENGINE"));
715 setenv("COREGL_FASTPATH", "1", 1);
717 setenv("CAIRO_GL_COMPOSITOR", "msaa", 1);
718 setenv("CAIRO_GL_LAZY_FLUSHING", "yes", 1);
719 setenv("ELM_IMAGE_CACHE", "0", 1);
721 WrtClient app(argc, argv);
723 ADD_PROFILING_POINT("Before appExec", "point");
724 int ret = app.Exec();
725 LogDebug("App returned: " << ret);
726 ret = app.getReturnStatus();
727 LogDebug("WrtClient returned: " << ret);
730 UNHANDLED_EXCEPTION_HANDLER_END