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>
32 #include "webkit/bundles/plugin_module_support.h"
34 //W3C PACKAGING enviroment variable name
35 #define W3C_DEBUG_ENV_VARIABLE "DEBUG_LOAD_FINISH"
37 // window signal callback
38 const char *EDJE_SHOW_BACKWARD_SIGNAL = "show,backward,signal";
39 const std::string VIEWMODE_TYPE_FULLSCREEN = "fullscreen";
40 const std::string VIEWMODE_TYPE_MAXIMIZED = "maximized";
41 char const* const ELM_SWALLOW_CONTENT = "elm.swallow.content";
42 const char* const BUNDLE_PATH = "/usr/lib/wrt-wk2-bundles/libwrt-wk2-bundle.so";
44 static Ewk_Context* s_ewk_context = NULL;
46 WrtClient::WrtClient(int argc, char **argv) :
47 Application(argc, argv, "wrt-client", false),
48 DPL::TaskDecl<WrtClient>(this),
50 m_initializing(false),
55 m_returnStatus(ReturnStatus::Succeeded),
56 m_widgetState(WidgetState::WidgetState_Stopped)
59 LogDebug("App Created");
62 WrtClient::~WrtClient()
64 LogDebug("App Finished");
67 WrtClient::ReturnStatus::Type WrtClient::getReturnStatus() const
69 return m_returnStatus;
72 void WrtClient::OnStop()
74 LogInfo("Stopping Dummy Client");
77 void WrtClient::OnCreate()
80 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;
94 void WrtClient::OnPause()
98 if (m_widgetState != WidgetState_Running) {
99 LogWarning("Widget is not running to be suspended");
103 m_widgetState = WidgetState_Suspended;
106 void WrtClient::OnReset(bundle *b)
109 // bundle argument is freed after OnReset() is returned
110 // So bundle duplication is needed
111 ApplicationDataSingleton::Instance().setBundle(bundle_dup(b));
113 if (true == m_initializing) {
114 LogDebug("can not handle reset event");
117 if (true == m_launched) {
118 if (m_widgetState == WidgetState_Stopped) {
119 LogError("Widget is not running to be reset");
123 m_windowData->emitSignalForUserLayout(EDJE_SHOW_BACKWARD_SIGNAL, "");
124 m_widgetState = WidgetState_Running;
126 if (true == checkArgument()) {
133 // low memory callback set
134 appcore_set_event_callback(
135 APPCORE_EVENT_LOW_MEMORY,
136 WrtClient::appcoreLowMemoryCallback,
140 void WrtClient::OnTerminate()
142 LogDebug("Wrt Shutdown now");
146 void WrtClient::showHelpAndQuit()
148 printf("Usage: wrt-client [OPTION]... [WIDGET: ID]...\n"
150 "Mandatory arguments to long options are mandatory for short "
152 " -h, --help show this help\n"
154 "launch widget with given tizen ID\n"
156 "launch widget with given tizen ID\n"
162 bool WrtClient::checkArgument()
164 std::string tizenId = getTizenIdFromArgument(m_argc, m_argv);
166 if (tizenId.empty()) {
171 LogDebug("Tizen id: " << m_tizenId);
176 std::string WrtClient::getTizenIdFromArgument(int argc, char **argv)
178 LogInfo("checkArgument");
179 std::string arg = argv[0];
185 if (arg.find("wrt-client") != std::string::npos) {
192 if (arg == "-h" || arg == "--help") {
194 } else if (arg == "-l" || arg == "--launch" ||
195 arg == "-t" || arg == "--tizen")
205 // Launch widget based on application basename
206 size_t pos = arg.find_last_of('/');
208 if (pos != std::string::npos) {
209 arg = arg.erase(0, pos + 1);
216 void WrtClient::setStep()
220 AddStep(&WrtClient::initStep);
222 setSdkLauncherDebugData();
224 AddStep(&WrtClient::launchStep);
225 AddStep(&WrtClient::shutdownStep);
227 m_initializing = true;
229 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(NextStepEvent());
232 void WrtClient::setSdkLauncherDebugData()
234 LogDebug("setSdkLauncherDebugData");
236 /* check bundle from sdk launcher */
237 bundle *bundleFromSdkLauncher;
238 bundleFromSdkLauncher = bundle_import_from_argv(m_argc, m_argv);
239 const char *bundle_debug = bundle_get_val(bundleFromSdkLauncher, "debug");
240 const char *bundle_pid = bundle_get_val(bundleFromSdkLauncher, "pid");
241 if (bundle_debug != NULL && bundle_pid != NULL) {
242 if (strcmp(bundle_debug, "true") == 0) {
244 m_sdkLauncherPid = atoi(bundle_pid);
249 bundle_free(bundleFromSdkLauncher);
252 bool WrtClient::checkDebugMode(SDKDebugData* debugData)
254 LogError("Checking for debug mode");
257 bool debugMode = debugData->debugMode;
259 LogInfo("[DEBUG_MODE] Widget is launched in " <<
260 (debugMode ? "DEBUG" : "RETAIL") <<
263 if (debugMode == true) {
264 // In WAC widget, only test widgets can use web inspector.
266 // every launched widgets as debug mode can use it.
267 if (m_dao->getWidgetType().appType == WrtDB::APP_TYPE_WAC20) {
269 WRT::CoreModuleSingleton::Instance().developerMode();
270 //This code will be activated
271 //after WAC test certificate is used by SDK
272 //bool isTestWidget = view->m_widgetModel->IsTestWidget.Get();
275 // LogInfo("This is not WAC Test Widget");
278 if (!developerMode) {
279 LogInfo("This is not WAC Developer Mode");
287 void WrtClient::OnEventReceived(const NextStepEvent& /*event*/)
289 LogDebug("Executing next step");
293 void WrtClient::initStep()
296 if (WRT::CoreModuleSingleton::Instance().Init()) {
297 m_initialized = true;
299 m_returnStatus = ReturnStatus::Failed;
300 SwitchToStep(&WrtClient::shutdownStep);
303 // ecore_event_jobs are processed sequentially without concession to
304 // other type events. To give a chance of execute to other events,
305 // ecore_timer_add was used.
306 DPL::Event::ControllerEventHandler<NextStepEvent>::PostTimedEvent(
307 NextStepEvent(), 0.001);
310 bool WrtClient::checkWACTestCertififedWidget()
312 // WAC Waikiki Beta Release Core Specification: Widget Runtime
315 // WR-4710 The WRT MUST enable debug functions only for WAC test widgets
316 // i.e. the functions must not be usable for normal WAC widgets, even when
317 // a WAC test widget is executing.
318 ADD_PROFILING_POINT("DeveloperModeCheck", "start");
321 // A widget signed with a WAC-issued test certificate as described in
324 bool developerWidget = m_dao->isTestWidget();
325 bool developerMode = WRT::CoreModuleSingleton::Instance().developerMode();
327 LogDebug("Is WAC test widget: " << developerWidget);
328 LogDebug("Is developer Mode: " << developerMode);
330 if (developerWidget) {
331 if (!developerMode) {
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)) {
359 ewk_view_inspector_server_start(m_widget->GetCurrentWebview(), 0);
360 if (debug->portnum == 0) {
361 LogWarning("Failed to get portnum");
363 LogInfo("Assigned port number for inspector : "
367 LogDebug("Debug mode is disabled");
370 //w3c packaging test debug (message on 4>)
371 const char * makeScreen = getenv(W3C_DEBUG_ENV_VARIABLE);
372 if (makeScreen != NULL && strcmp(makeScreen, "1") == 0) {
373 FILE* doutput = fdopen(4, "w");
374 fprintf(doutput, "didFinishLoadForFrameCallback: ready\n");
379 LogDebug("Launch succesfull");
382 m_initializing = false;
384 ADD_PROFILING_POINT("loadFinishCallback", "stop");
385 printf("launched\n");
390 m_returnStatus = ReturnStatus::Failed;
392 DPL::Event::ControllerEventHandler<NextStepEvent>::
393 PostEvent(NextStepEvent());
396 if (debug->debugMode) {
397 LogDebug("Send RT signal to wrt-launcher(pid: " << m_sdkLauncherPid);
399 /* send real time signal with result to wrt-launcher */
401 LogDebug("userData->portnum : " << debug->portnum);
402 sv.sival_int = debug->portnum;
406 sigqueue(m_sdkLauncherPid, SIGRTMIN, sv);
409 ApplicationDataSingleton::Instance().freeBundle();
411 LogDebug("Cleaning wrtClient launch resources...");
416 void WrtClient::progressFinishCallback()
418 m_splashScreen->stopSplashScreen();
421 void WrtClient::webkitExitCallback()
423 LogDebug("window close called, terminating app");
424 SwitchToStep(&WrtClient::shutdownStep);
425 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
429 void WrtClient::webCrashCallback()
431 LogError("webProcess crashed");
432 SwitchToStep(&WrtClient::shutdownStep);
433 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
437 void WrtClient::launchStep()
439 ADD_PROFILING_POINT("launchStep", "start");
440 LogDebug("Launching widget ...");
442 ADD_PROFILING_POINT("getRunnableWidgetObject", "start");
443 m_widget = WRT::CoreModuleSingleton::Instance()
444 .getRunnableWidgetObject(m_tizenId);
445 ADD_PROFILING_POINT("getRunnableWidgetObject", "stop");
448 LogError("RunnableWidgetObject is NULL, stop launchStep");
449 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
454 if (m_widgetState == WidgetState_Running) {
455 LogWarning("Widget already running, stop launchStep");
456 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
461 if (m_widgetState == WidgetState_Authorizing) {
462 LogWarning("Widget already authorizing, stop launchStep");
463 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
468 m_dao.reset(new WrtDB::WidgetDAOReadOnly(DPL::FromASCIIString(m_tizenId)));
469 DPL::Optional<DPL::String> defloc = m_dao->getDefaultlocale();
470 if (!defloc.IsNull()) {
471 LanguageTagsProviderSingleton::Instance().addWidgetDefaultLocales(
475 // For localizationsetting not changed on widget running
476 //LocalizationSetting::SetLanguageChangedCallback(
477 // languageChangedCallback, this);
479 ADD_PROFILING_POINT("CreateWindow", "start");
480 m_windowData.reset(new WindowData(static_cast<unsigned long>(getpid()),
482 ADD_PROFILING_POINT("CreateWindow", "stop");
484 WRT::UserDelegatesPtr cbs(new WRT::UserDelegates);
485 ADD_PROFILING_POINT("Create splash screen", "start");
486 m_splashScreen.reset(
487 new SplashScreenSupport(m_windowData->m_win));
488 if (m_splashScreen->createSplashScreen(m_dao->getSplashImgSrc())) {
489 m_splashScreen->startSplashScreen();
490 cbs->progressFinish = DPL::MakeDelegate(
493 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, s_ewk_context))
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()) {
521 LogWarning("WAC Certificate failed, stop launchStep");
525 m_widgetState = WidgetState_Authorizing;
526 if (!m_widget->CheckBeforeLaunch()) {
527 LogError("CheckBeforeLaunch failed, stop launchStep");
528 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
532 LogInfo("Widget launch accepted. Entering running state");
533 m_widgetState = WidgetState_Running;
535 cbs->loadFinish = DPL::MakeDelegate(this, &WrtClient::loadFinishCallback);
536 cbs->bufferSet = DPL::MakeDelegate(this, &WrtClient::setLayout);
537 cbs->bufferUnset = DPL::MakeDelegate(this, &WrtClient::unsetLayout);
538 cbs->webkitExit = DPL::MakeDelegate(this, &WrtClient::webkitExitCallback);
539 cbs->webCrash = DPL::MakeDelegate(this, &WrtClient::webCrashCallback);
540 cbs->toggleFullscreen = DPL::MakeDelegate(
541 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 int WrtClient::appcoreLowMemoryCallback(void* /*data*/)
593 LogInfo("appcoreLowMemoryCallback");
594 //WrtClient* This = static_cast<WrtClient*>(data);
596 // TODO call RunnableWidgetObject API regarding low memory
597 // The API should be implemented
602 void WrtClient::connectElmCallback()
604 Assert(m_windowData);
606 if (m_dao->getWidgetType().appType == WrtDB::APP_TYPE_TIZENWEBAPP) {
607 WidgetSettings widgetSettings;
608 m_dao->getWidgetSettings(widgetSettings);
609 WidgetSettingList settings(widgetSettings);
610 if (settings.getBackButtonPresence() == BackButton_Enable) {
611 m_windowData->addFloatBackButtonCallback(
613 &WrtClient::backButtonCallback,
617 WidgetSettingScreenLock rotationValue = settings.getRotationValue();
618 if (rotationValue == Screen_Portrait) {
619 elm_win_rotation_with_resize_set(m_windowData->m_win, 0);
620 ewk_view_orientation_send(m_widget->GetCurrentWebview(), 0);
621 } else if (rotationValue == Screen_Landscape) {
622 elm_win_rotation_with_resize_set(m_windowData->m_win, 270);
623 ewk_view_orientation_send(m_widget->GetCurrentWebview(), 90);
625 elm_win_rotation_with_resize_set(m_windowData->m_win, 0);
626 ewk_view_orientation_send(m_widget->GetCurrentWebview(), 0);
631 void WrtClient::setLayout(Evas_Object* newBuffer)
633 LogDebug("add new webkit buffer to window");
636 elm_object_part_content_set(m_windowData->m_user_layout,
640 evas_object_show(newBuffer);
643 void WrtClient::unsetLayout(Evas_Object* currentBuffer)
645 LogDebug("remove current webkit buffer from window");
646 Assert(currentBuffer);
647 evas_object_hide(currentBuffer);
649 elm_object_part_content_unset(m_windowData->m_user_layout,
650 ELM_SWALLOW_CONTENT);
653 void WrtClient::shutdownStep()
655 LogDebug("Closing Wrt connection ...");
657 if (m_widget && m_widgetState) {
658 m_widgetState = WidgetState_Stopped;
661 ewk_context_delete(s_ewk_context);
662 WRT::CoreModuleSingleton::Instance().Terminate();
665 m_initialized = false;
667 m_windowData.reset();
671 int WrtClient::languageChangedCallback(void *data)
673 LogDebug("Language Changed");
677 WrtClient* wrtClient = static_cast<WrtClient*>(data);
678 if (!(wrtClient->m_dao)) {
682 // reset function fetches system locales and recreates language tags
683 LanguageTagsProviderSingleton::Instance().resetLanguageTags();
684 // widget default locales are added to language tags below
685 DPL::OptionalString defloc = wrtClient->m_dao->getDefaultlocale();
686 if (!defloc.IsNull()) {
687 LanguageTagsProviderSingleton::Instance().addWidgetDefaultLocales(
691 if (wrtClient->m_launched &&
692 wrtClient->m_widgetState != WidgetState_Stopped)
694 wrtClient->m_widget->ReloadStartPage();
699 void WrtClient::Quit()
702 DPL::Application::Quit();
708 UNHANDLED_EXCEPTION_HANDLER_BEGIN
710 ADD_PROFILING_POINT("main-entered", "point");
713 DPL::Log::LogSystemSingleton::Instance().SetTag("WRT");
715 // set evas backend type
716 if (!getenv("ELM_ENGINE")) {
717 if (!setenv("ELM_ENGINE", "gl", 1)) {
718 LogDebug("Enable backend");
721 LogDebug("ELM_ENGINE : " << getenv("ELM_ENGINE"));
725 setenv("COREGL_FASTPATH", "1", 1);
727 setenv("CAIRO_GL_COMPOSITOR", "msaa", 1);
728 setenv("CAIRO_GL_LAZY_FLUSHING", "yes", 1);
729 setenv("ELM_IMAGE_CACHE", "0", 1);
731 // This code is to fork a web process without exec.
732 std::string tizenId = WrtClient::getTizenIdFromArgument(argc, argv);
734 if (!tizenId.empty()) {
735 LogDebug("Launching by fork mode");
736 // Language env setup
737 appcore_set_i18n("wrt-client", NULL);
739 ewk_set_arguments(argc, argv);
740 setenv("WRT_LAUNCHING_PERFORMANCE", "1", 1);
741 s_ewk_context = ewk_context_new_with_injected_bundle_path(
745 PluginModuleSupport::init(s_ewk_context, tizenId);
748 // Output on stdout will be flushed after every newline character,
749 // even if it is redirected to a pipe. This is useful for running
750 // from a script and parsing output.
751 // (Standard behavior of stdlib is to use full buffering when
752 // redirected to a pipe, which means even after an end of line
753 // the output may not be flushed).
756 WrtClient app(argc, argv);
758 ADD_PROFILING_POINT("Before appExec", "point");
759 int ret = app.Exec();
760 LogDebug("App returned: " << ret);
761 ret = app.getReturnStatus();
762 LogDebug("WrtClient returned: " << ret);
765 UNHANDLED_EXCEPTION_HANDLER_END