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()), true));
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 if (!m_widget->PrepareView(DPL::ToUTF8String(*startUrl),
498 m_windowData->m_win))
500 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
504 //you can't show window with splash screen before PrepareView
505 //ewk_view_add_with_context() in viewLogic breaks window
507 m_windowData->init();
509 WrtDB::WidgetLocalizedInfo localizedInfo =
510 W3CFileLocalization::getLocalizedInfo(m_dao);
511 std:: string name = "";
512 if (!(localizedInfo.name.IsNull())) {
513 name = DPL::ToUTF8String(*(localizedInfo.name));
515 elm_win_title_set(m_windowData->m_win, name.c_str());
516 evas_object_show(m_windowData->m_win);
517 initializeWindowModes();
518 connectElmCallback();
520 if (!checkWACTestCertififedWidget())
522 LogWarning("WAC Certificate failed, stop launchStep");
526 m_widgetState = WidgetState_Authorizing;
527 if (!m_widget->CheckBeforeLaunch()) {
528 LogError("CheckBeforeLaunch failed, stop launchStep");
529 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
533 LogInfo("Widget launch accepted. Entering running state");
534 m_widgetState = WidgetState_Running;
536 cbs->loadFinish = DPL::MakeDelegate(this, &WrtClient::loadFinishCallback);
537 cbs->bufferSet = DPL::MakeDelegate(this, &WrtClient::setLayout);
538 cbs->bufferUnset = DPL::MakeDelegate(this, &WrtClient::unsetLayout);
539 cbs->webkitExit = DPL::MakeDelegate(this, &WrtClient::webkitExitCallback);
540 cbs->webCrash = DPL::MakeDelegate(this, &WrtClient::webCrashCallback);
541 cbs->toggleFullscreen = DPL::MakeDelegate(m_windowData.get(), &WindowData::toggleFullscreen);
543 m_widget->SetUserDelegates(cbs);
545 m_windowData->emitSignalForUserLayout(EDJE_SHOW_BACKWARD_SIGNAL, "");
546 ADD_PROFILING_POINT("launchStep", "stop");
549 void WrtClient::initializeWindowModes()
551 Assert(m_windowData);
553 auto windowModes = m_dao->getWindowModes();
554 bool fullscreen = false;
555 bool backbutton = false;
556 if (m_dao->getWidgetType().appType == WrtDB::APP_TYPE_TIZENWEBAPP) {
557 WidgetSettings widgetSettings;
558 m_dao->getWidgetSettings(widgetSettings);
559 WidgetSettingList settings(widgetSettings);
561 (settings.getBackButtonPresence() == BackButton_Enable);
564 FOREACH(it, windowModes)
566 std::string viewMode = DPL::ToUTF8String(*it);
567 if (viewMode == VIEWMODE_TYPE_FULLSCREEN) {
570 } else if (viewMode == VIEWMODE_TYPE_MAXIMIZED) {
575 m_windowData->setViewMode(fullscreen,
579 void WrtClient::backButtonCallback(void* data,
580 Evas_Object * /*obj*/,
581 void * /*event_info*/)
583 LogInfo("BackButtonCallback");
586 WrtClient* This = static_cast<WrtClient*>(data);
588 This->m_widget->Backward();
591 void WrtClient::connectElmCallback()
593 Assert(m_windowData);
595 if (m_dao->getWidgetType().appType == WrtDB::APP_TYPE_TIZENWEBAPP) {
596 WidgetSettings widgetSettings;
597 m_dao->getWidgetSettings(widgetSettings);
598 WidgetSettingList settings(widgetSettings);
599 if (settings.getBackButtonPresence() == BackButton_Enable) {
600 m_windowData->addFloatBackButtonCallback(
602 &WrtClient::backButtonCallback,
606 WidgetSettingScreenLock rotationValue = settings.getRotationValue();
607 if (rotationValue == Screen_Portrait) {
608 elm_win_rotation_with_resize_set(m_windowData->m_win, 0);
609 } else if (rotationValue == Screen_Landscape) {
610 elm_win_rotation_with_resize_set(m_windowData->m_win, 270);
612 elm_win_rotation_with_resize_set(m_windowData->m_win, 0);
617 void WrtClient::setLayout(Evas_Object* newBuffer) {
618 LogDebug("add new webkit buffer to window");
621 elm_object_part_content_set(m_windowData->m_user_layout, ELM_SWALLOW_CONTENT, newBuffer);
623 evas_object_show(newBuffer);
626 void WrtClient::unsetLayout(Evas_Object* currentBuffer) {
627 LogDebug("remove current webkit buffer from window");
628 Assert(currentBuffer);
629 evas_object_hide(currentBuffer);
631 elm_object_part_content_unset(m_windowData->m_user_layout, ELM_SWALLOW_CONTENT);
634 void WrtClient::shutdownStep()
636 LogDebug("Closing Wrt connection ...");
638 if (m_widget && m_widgetState)
640 m_widgetState = WidgetState_Stopped;
643 WRT::CoreModuleSingleton::Instance().Terminate();
647 m_initialized = false;
649 m_windowData.reset();
653 int WrtClient::languageChangedCallback(void *data)
655 LogDebug("Language Changed");
658 WrtClient* wrtClient = static_cast<WrtClient*>(data);
659 if(!(wrtClient->m_dao))
662 // reset function fetches system locales and recreates language tags
663 LanguageTagsProviderSingleton::Instance().resetLanguageTags();
664 // widget default locales are added to language tags below
665 DPL::OptionalString defloc = wrtClient->m_dao->getDefaultlocale();
668 LanguageTagsProviderSingleton::Instance().addWidgetDefaultLocales(*defloc);
671 if (wrtClient->m_launched &&
672 wrtClient->m_widgetState != WidgetState_Stopped) {
673 wrtClient->m_widget->ReloadStartPage();
678 void WrtClient::Quit()
681 DPL::Application::Quit();
687 UNHANDLED_EXCEPTION_HANDLER_BEGIN
689 ADD_PROFILING_POINT("main-entered", "point");
692 DPL::Log::LogSystemSingleton::Instance().SetTag("WRT");
694 // set evas backend type
695 if (!getenv("ELM_ENGINE")) {
696 if (!setenv("ELM_ENGINE", "gl", 1)) {
697 LogDebug("Enable backend");
701 LogDebug("ELM_ENGINE : " << getenv("ELM_ENGINE"));
705 setenv("COREGL_FASTPATH", "1", 1);
707 setenv("CAIRO_GL_COMPOSITOR", "msaa", 1);
708 setenv("CAIRO_GL_LAZY_FLUSHING", "yes", 1);
709 setenv("ELM_IMAGE_CACHE", "0", 1);
711 // This code is to fork a web process without exec.
712 std::string tizenId = WrtClient::getTizenIdFromArgument(argc, argv);
714 if (!tizenId.empty())
716 LogDebug("Launching by fork mode");
717 // Language env setup
718 appcore_set_i18n("wrt-client", NULL);
720 ewk_set_arguments(argc, argv);
721 setenv("WRT_LAUNCHING_PERFORMANCE", "1", 1);
722 s_ewk_context = ewk_context_new_with_injected_bundle_path(BUNDLE_PATH);
725 // Output on stdout will be flushed after every newline character,
726 // even if it is redirected to a pipe. This is useful for running
727 // from a script and parsing output.
728 // (Standard behavior of stdlib is to use full buffering when
729 // redirected to a pipe, which means even after an end of line
730 // the output may not be flushed).
734 WrtClient app(argc, argv);
736 ADD_PROFILING_POINT("Before appExec", "point");
737 int ret = app.Exec();
738 LogDebug("App returned: " << ret);
739 ret = app.getReturnStatus();
740 LogDebug("WrtClient returned: " << ret);
743 UNHANDLED_EXCEPTION_HANDLER_END