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>
18 #include <appcore-common.h>
22 #include <dpl/log/log.h>
23 #include <dpl/optional_typedefs.h>
24 #include <dpl/exception.h>
25 #include <common/application_data.h>
26 #include <core_module.h>
27 #include <localization_setting.h>
28 #include <widget_deserialize_model.h>
30 #include <dpl/localization/w3c_file_localization.h>
31 #include <dpl/localization/LanguageTagsProvider.h>
33 //W3C PACKAGING enviroment variable name
34 #define W3C_DEBUG_ENV_VARIABLE "DEBUG_LOAD_FINISH"
36 // window signal callback
37 const char *EDJE_SHOW_BACKWARD_SIGNAL = "show,backward,signal";
38 const std::string VIEWMODE_TYPE_FULLSCREEN = "fullscreen";
39 const std::string VIEWMODE_TYPE_MAXIMIZED = "maximized";
40 char const* const ELM_SWALLOW_CONTENT = "elm.swallow.content";
41 const char* const BUNDLE_PATH = "/usr/lib/wrt-wk2-bundles/libwrt-wk2-bundle.so";
43 static Ewk_Context* s_ewk_context = NULL;
45 WrtClient::WrtClient(int argc, char **argv) :
46 Application(argc, argv, "wrt-client", false),
47 DPL::TaskDecl<WrtClient>(this),
49 m_initializing(false),
54 m_returnStatus(ReturnStatus::Succeeded),
55 m_widgetState(WidgetState::WidgetState_Stopped)
58 LogDebug("App Created");
61 WrtClient::~WrtClient()
63 LogDebug("App Finished");
66 WrtClient::ReturnStatus::Type WrtClient::getReturnStatus() const
68 return m_returnStatus;
71 void WrtClient::OnStop()
73 LogInfo("Stopping Dummy Client");
76 void WrtClient::OnCreate()
79 ADD_PROFILING_POINT("OnCreate callback", "point");
83 void WrtClient::OnResume()
85 if (m_widgetState != WidgetState_Suspended) {
86 LogWarning("Widget is not suspended, resuming was skipped");
90 m_widgetState = WidgetState_Running;
93 void WrtClient::OnPause()
97 if (m_widgetState != WidgetState_Running) {
98 LogWarning("Widget is not running to be suspended");
102 m_widgetState = WidgetState_Suspended;
105 void WrtClient::OnReset(bundle *b)
108 // bundle argument is freed after OnReset() is returned
109 // So bundle duplication is needed
110 ApplicationDataSingleton::Instance().setBundle(bundle_dup(b));
112 if (true == m_initializing) {
113 LogDebug("can not handle reset event");
116 if (true == m_launched) {
117 if (m_widgetState == WidgetState_Stopped) {
118 LogError("Widget is not running to be reset");
122 m_windowData->emitSignalForUserLayout(EDJE_SHOW_BACKWARD_SIGNAL, "");
123 m_widgetState = WidgetState_Running;
125 if (true == checkArgument()) {
132 // low memory callback set
133 appcore_set_event_callback(
134 APPCORE_EVENT_LOW_MEMORY,
135 WrtClient::appcoreLowMemoryCallback,
139 void WrtClient::OnTerminate()
141 LogDebug("Wrt Shutdown now");
145 void WrtClient::showHelpAndQuit()
147 printf("Usage: wrt-client [OPTION]... [WIDGET: ID]...\n"
149 "Mandatory arguments to long options are mandatory for short "
151 " -h, --help show this help\n"
153 "launch widget with given tizen ID\n"
155 "launch widget with given tizen ID\n"
161 bool WrtClient::checkArgument()
163 std::string tizenId = getTizenIdFromArgument(m_argc, m_argv);
165 if (tizenId.empty()) {
170 LogDebug("Tizen id: " << m_tizenId);
175 std::string WrtClient::getTizenIdFromArgument(int argc, char **argv)
177 LogInfo("checkArgument");
178 std::string arg = argv[0];
184 if (arg.find("wrt-client") != std::string::npos) {
191 if (arg == "-h" || arg == "--help") {
193 } else if (arg == "-l" || arg == "--launch" ||
194 arg == "-t" || arg == "--tizen")
204 // Launch widget based on application basename
205 size_t pos = arg.find_last_of('/');
207 if (pos != std::string::npos) {
208 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 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()) {
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) {
330 if (!developerMode) {
331 LogError("WAC test certified developer widget is needed for " <<
335 //TODO: WR-4660 (show popup about developer widget
337 LogInfo("POPUP: THIS IS TEST WIDGET!");
340 ADD_PROFILING_POINT("DeveloperModeCheck", "stop");
344 void WrtClient::loadFinishCallback(Evas_Object* webview)
346 ADD_PROFILING_POINT("loadFinishCallback", "start");
347 SDKDebugData* debug = new SDKDebugData;
348 debug->debugMode = m_debugMode;
349 debug->pid = new unsigned long(getpid());
351 LogInfo("Post result of launch");
353 // Start inspector server, if current mode is debugger mode.
354 // In the WK2 case, ewk_view_inspector_server_start should
355 // be called after WebProcess is created.
356 if (checkDebugMode(debug)) {
358 ewk_view_inspector_server_start(m_widget->GetCurrentWebview(), 0);
359 if (debug->portnum == 0) {
360 LogWarning("Failed to get portnum");
362 LogInfo("Assigned port number for inspector : "
366 LogDebug("Debug mode is disabled");
369 //w3c packaging test debug (message on 4>)
370 const char * makeScreen = getenv(W3C_DEBUG_ENV_VARIABLE);
371 if (makeScreen != NULL && strcmp(makeScreen, "1") == 0) {
372 FILE* doutput = fdopen(4, "w");
373 fprintf(doutput, "didFinishLoadForFrameCallback: ready\n");
378 LogDebug("Launch succesfull");
381 m_initializing = false;
383 ADD_PROFILING_POINT("loadFinishCallback", "stop");
384 printf("launched\n");
389 m_returnStatus = ReturnStatus::Failed;
391 DPL::Event::ControllerEventHandler<NextStepEvent>::
392 PostEvent(NextStepEvent());
395 if (debug->debugMode) {
396 LogDebug("Send RT signal to wrt-launcher(pid: " << m_sdkLauncherPid);
398 /* send real time signal with result to wrt-launcher */
400 LogDebug("userData->portnum : " << debug->portnum);
401 sv.sival_int = debug->portnum;
405 sigqueue(m_sdkLauncherPid, SIGRTMIN, sv);
408 ApplicationDataSingleton::Instance().freeBundle();
410 LogDebug("Cleaning wrtClient launch resources...");
415 void WrtClient::progressFinishCallback()
417 m_splashScreen->stopSplashScreen();
420 void WrtClient::webkitExitCallback()
422 LogDebug("window close called, terminating app");
423 SwitchToStep(&WrtClient::shutdownStep);
424 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
428 void WrtClient::webCrashCallback()
430 LogError("webProcess crashed");
431 SwitchToStep(&WrtClient::shutdownStep);
432 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
436 void WrtClient::launchStep()
438 ADD_PROFILING_POINT("launchStep", "start");
439 LogDebug("Launching widget ...");
441 ADD_PROFILING_POINT("getRunnableWidgetObject", "start");
442 m_widget = WRT::CoreModuleSingleton::Instance()
443 .getRunnableWidgetObject(m_tizenId);
444 ADD_PROFILING_POINT("getRunnableWidgetObject", "stop");
447 LogError("RunnableWidgetObject is NULL, stop launchStep");
448 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
453 if (m_widgetState == WidgetState_Running) {
454 LogWarning("Widget already running, stop launchStep");
455 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
460 if (m_widgetState == WidgetState_Authorizing) {
461 LogWarning("Widget already authorizing, stop launchStep");
462 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
467 m_dao.reset(new WrtDB::WidgetDAOReadOnly(DPL::FromASCIIString(m_tizenId)));
468 DPL::Optional<DPL::String> defloc = m_dao->getDefaultlocale();
469 if (!defloc.IsNull()) {
470 LanguageTagsProviderSingleton::Instance().addWidgetDefaultLocales(
474 // For localizationsetting not changed on widget running
475 //LocalizationSetting::SetLanguageChangedCallback(
476 // languageChangedCallback, this);
478 ADD_PROFILING_POINT("CreateWindow", "start");
479 m_windowData.reset(new WindowData(static_cast<unsigned long>(getpid()),
481 ADD_PROFILING_POINT("CreateWindow", "stop");
483 WRT::UserDelegatesPtr cbs(new WRT::UserDelegates);
484 ADD_PROFILING_POINT("Create splash screen", "start");
485 m_splashScreen.reset(
486 new SplashScreenSupport(m_windowData->m_win));
487 if (m_splashScreen->createSplashScreen(m_dao->getSplashImgSrc())) {
488 m_splashScreen->startSplashScreen();
489 cbs->progressFinish = DPL::MakeDelegate(
492 progressFinishCallback);
494 ADD_PROFILING_POINT("Create splash screen", "stop");
495 DPL::OptionalString startUrl = W3CFileLocalization::getStartFile(m_dao);
496 if (!m_widget->PrepareView(DPL::ToUTF8String(*startUrl),
497 m_windowData->m_win, s_ewk_context))
499 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
503 //you can't show window with splash screen before PrepareView
504 //ewk_view_add_with_context() in viewLogic breaks window
506 m_windowData->init();
508 WrtDB::WidgetLocalizedInfo localizedInfo =
509 W3CFileLocalization::getLocalizedInfo(m_dao);
510 std::string name = "";
511 if (!(localizedInfo.name.IsNull())) {
512 name = DPL::ToUTF8String(*(localizedInfo.name));
514 elm_win_title_set(m_windowData->m_win, name.c_str());
515 evas_object_show(m_windowData->m_win);
516 initializeWindowModes();
517 connectElmCallback();
519 if (!checkWACTestCertififedWidget()) {
520 LogWarning("WAC Certificate failed, stop launchStep");
524 m_widgetState = WidgetState_Authorizing;
525 if (!m_widget->CheckBeforeLaunch()) {
526 LogError("CheckBeforeLaunch failed, stop launchStep");
527 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
531 LogInfo("Widget launch accepted. Entering running state");
532 m_widgetState = WidgetState_Running;
534 cbs->loadFinish = DPL::MakeDelegate(this, &WrtClient::loadFinishCallback);
535 cbs->bufferSet = DPL::MakeDelegate(this, &WrtClient::setLayout);
536 cbs->bufferUnset = DPL::MakeDelegate(this, &WrtClient::unsetLayout);
537 cbs->webkitExit = DPL::MakeDelegate(this, &WrtClient::webkitExitCallback);
538 cbs->webCrash = DPL::MakeDelegate(this, &WrtClient::webCrashCallback);
539 cbs->toggleFullscreen = DPL::MakeDelegate(
540 m_windowData.get(), &WindowData::toggleFullscreen);
542 m_widget->SetUserDelegates(cbs);
544 m_windowData->emitSignalForUserLayout(EDJE_SHOW_BACKWARD_SIGNAL, "");
545 ADD_PROFILING_POINT("launchStep", "stop");
548 void WrtClient::initializeWindowModes()
550 Assert(m_windowData);
552 auto windowModes = m_dao->getWindowModes();
553 bool fullscreen = false;
554 bool backbutton = false;
555 if (m_dao->getWidgetType().appType == WrtDB::APP_TYPE_TIZENWEBAPP) {
556 WidgetSettings widgetSettings;
557 m_dao->getWidgetSettings(widgetSettings);
558 WidgetSettingList settings(widgetSettings);
560 (settings.getBackButtonPresence() == BackButton_Enable);
563 FOREACH(it, windowModes)
565 std::string viewMode = DPL::ToUTF8String(*it);
566 if (viewMode == VIEWMODE_TYPE_FULLSCREEN) {
569 } else if (viewMode == VIEWMODE_TYPE_MAXIMIZED) {
574 m_windowData->setViewMode(fullscreen,
578 void WrtClient::backButtonCallback(void* data,
579 Evas_Object * /*obj*/,
580 void * /*event_info*/)
582 LogInfo("BackButtonCallback");
585 WrtClient* This = static_cast<WrtClient*>(data);
587 This->m_widget->Backward();
590 int WrtClient::appcoreLowMemoryCallback(void* data)
592 LogInfo("appcoreLowMemoryCallback");
593 WrtClient* This = static_cast<WrtClient*>(data);
595 // TODO call RunnableWidgetObject API regarding low memory
596 // The API should be implemented
601 void WrtClient::connectElmCallback()
603 Assert(m_windowData);
605 if (m_dao->getWidgetType().appType == WrtDB::APP_TYPE_TIZENWEBAPP) {
606 WidgetSettings widgetSettings;
607 m_dao->getWidgetSettings(widgetSettings);
608 WidgetSettingList settings(widgetSettings);
609 if (settings.getBackButtonPresence() == BackButton_Enable) {
610 m_windowData->addFloatBackButtonCallback(
612 &WrtClient::backButtonCallback,
616 WidgetSettingScreenLock rotationValue = settings.getRotationValue();
617 if (rotationValue == Screen_Portrait) {
618 elm_win_rotation_with_resize_set(m_windowData->m_win, 0);
619 } else if (rotationValue == Screen_Landscape) {
620 elm_win_rotation_with_resize_set(m_windowData->m_win, 270);
622 elm_win_rotation_with_resize_set(m_windowData->m_win, 0);
627 void WrtClient::setLayout(Evas_Object* newBuffer)
629 LogDebug("add new webkit buffer to window");
632 elm_object_part_content_set(m_windowData->m_user_layout,
636 evas_object_show(newBuffer);
639 void WrtClient::unsetLayout(Evas_Object* currentBuffer)
641 LogDebug("remove current webkit buffer from window");
642 Assert(currentBuffer);
643 evas_object_hide(currentBuffer);
645 elm_object_part_content_unset(m_windowData->m_user_layout,
646 ELM_SWALLOW_CONTENT);
649 void WrtClient::shutdownStep()
651 LogDebug("Closing Wrt connection ...");
653 if (m_widget && m_widgetState) {
654 m_widgetState = WidgetState_Stopped;
657 ewk_context_delete(s_ewk_context);
658 WRT::CoreModuleSingleton::Instance().Terminate();
661 m_initialized = false;
663 m_windowData.reset();
667 int WrtClient::languageChangedCallback(void *data)
669 LogDebug("Language Changed");
673 WrtClient* wrtClient = static_cast<WrtClient*>(data);
674 if (!(wrtClient->m_dao)) {
678 // reset function fetches system locales and recreates language tags
679 LanguageTagsProviderSingleton::Instance().resetLanguageTags();
680 // widget default locales are added to language tags below
681 DPL::OptionalString defloc = wrtClient->m_dao->getDefaultlocale();
682 if (!defloc.IsNull()) {
683 LanguageTagsProviderSingleton::Instance().addWidgetDefaultLocales(
687 if (wrtClient->m_launched &&
688 wrtClient->m_widgetState != WidgetState_Stopped)
690 wrtClient->m_widget->ReloadStartPage();
695 void WrtClient::Quit()
698 DPL::Application::Quit();
704 UNHANDLED_EXCEPTION_HANDLER_BEGIN
706 ADD_PROFILING_POINT("main-entered", "point");
709 DPL::Log::LogSystemSingleton::Instance().SetTag("WRT");
711 // set evas backend type
712 if (!getenv("ELM_ENGINE")) {
713 if (!setenv("ELM_ENGINE", "gl", 1)) {
714 LogDebug("Enable backend");
717 LogDebug("ELM_ENGINE : " << getenv("ELM_ENGINE"));
721 setenv("COREGL_FASTPATH", "1", 1);
723 setenv("CAIRO_GL_COMPOSITOR", "msaa", 1);
724 setenv("CAIRO_GL_LAZY_FLUSHING", "yes", 1);
725 setenv("ELM_IMAGE_CACHE", "0", 1);
727 // This code is to fork a web process without exec.
728 std::string tizenId = WrtClient::getTizenIdFromArgument(argc, argv);
730 if (!tizenId.empty()) {
731 LogDebug("Launching by fork mode");
732 // Language env setup
733 appcore_set_i18n("wrt-client", NULL);
735 ewk_set_arguments(argc, argv);
736 setenv("WRT_LAUNCHING_PERFORMANCE", "1", 1);
737 s_ewk_context = ewk_context_new_with_injected_bundle_path(
741 // Output on stdout will be flushed after every newline character,
742 // even if it is redirected to a pipe. This is useful for running
743 // from a script and parsing output.
744 // (Standard behavior of stdlib is to use full buffering when
745 // redirected to a pipe, which means even after an end of line
746 // the output may not be flushed).
749 WrtClient app(argc, argv);
751 ADD_PROFILING_POINT("Before appExec", "point");
752 int ret = app.Exec();
753 LogDebug("App returned: " << ret);
754 ret = app.getReturnStatus();
755 LogDebug("WrtClient returned: " << ret);
758 UNHANDLED_EXCEPTION_HANDLER_END