2 * Copyright (c) 2013 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.
17 * @file task_configuration.cpp
19 * @author Tomasz Iwanek
20 * @brief implementation file for configuration task
22 #include "task_configuration.h"
34 #include <dpl/utils/wrt_utility.h>
35 #include <dpl/utils/path.h>
36 #include <dpl/wrt-dao-ro/common_dao_types.h>
37 #include <dpl/wrt-dao-ro/widget_dao_read_only.h>
38 #include <dpl/wrt-dao-ro/global_config.h>
39 #include <dpl/wrt-dao-ro/config_parser_data.h>
40 #include <dpl/localization/w3c_file_localization.h>
42 #include <libiriwrapper.h>
43 #include <app_manager.h>
45 #include "root_parser.h"
46 #include "widget_parser.h"
47 #include "parser_runner.h"
49 #include <widget_install/widget_install_errors.h>
50 #include <widget_install/widget_install_context.h>
51 #include <widget_install_to_external.h>
52 #include <widget_install/widget_unzip.h>
53 #include <widget_install/job_widget_install.h>
54 #include <widget_install/task_commons.h>
56 #include <installer_log.h>
58 using namespace WrtDB;
61 const char* const CONFIG_XML = "config.xml";
62 const char* const WITH_OSP_XML = "res/wgt/config.xml";
63 const char* const OSP_MANIFEST_XML = "info/manifest.xml";
65 //allowed: a-z, A-Z, 0-9
66 const char* REG_TIZENID_PATTERN = "^[a-zA-Z0-9]{10}.{1,}$";
67 const char* REG_NAME_PATTERN = "^[a-zA-Z0-9._-]{1,}$";
68 const size_t PACKAGE_ID_LENGTH = 10;
70 static const DPL::String SETTING_VALUE_ENCRYPTION = L"encryption";
71 static const DPL::String SETTING_VALUE_ENCRYPTION_ENABLE = L"enable";
72 static const DPL::String SETTING_VALUE_ENCRYPTION_DISABLE = L"disable";
73 const DPL::String SETTING_VALUE_INSTALLTOEXT_NAME = L"install-location";
74 const DPL::String SETTING_VALUE_INSTALLTOEXT_PREPER_EXT = L"prefer-external";
75 const DPL::String SETTING_VALUE_INSTALLTOEXT_AUTO = L"auto";
76 const std::string XML_EXTENSION = ".xml";
78 bool hasExtension(const std::string& filename, const std::string& extension)
80 _D("Looking for extension %s in %s", extension.c_str(), filename.c_str());
81 size_t fileLen = filename.length();
82 size_t extLen = extension.length();
83 if (fileLen < extLen) {
84 _E("Filename %s is shorter than extension %s", filename.c_str(), extension.c_str());
87 return (0 == filename.compare(fileLen - extLen, extLen, extension));
89 } // namespace anonymous
92 namespace WidgetInstall {
94 TaskConfiguration::TaskConfiguration(InstallerContext& context) :
95 DPL::TaskDecl<TaskConfiguration>(this),
97 m_widgetConfig(m_context.widgetConfig.configInfo)
99 AddStep(&TaskConfiguration::StartStep);
101 AddStep(&TaskConfiguration::SetupTempDirStep);
102 AddStep(&TaskConfiguration::CheckPackageTypeStep);
104 AddStep(&TaskConfiguration::ParseXMLConfigStep);
106 AddStep(&TaskConfiguration::TizenIdStep);
107 AddStep(&TaskConfiguration::CheckAppRunningStateStep);
108 AddStep(&TaskConfiguration::ApplicationTypeStep);
109 AddStep(&TaskConfiguration::ResourceEncryptionStep);
110 AddStep(&TaskConfiguration::InstallationFSLocationStep);
112 AddStep(&TaskConfiguration::DetectUpdateInstallationStep);
113 AddStep(&TaskConfiguration::CheckRDSSupportStep);
114 AddStep(&TaskConfiguration::ConfigureWidgetLocationStep);
115 AddStep(&TaskConfiguration::PkgmgrStartStep);
117 AddStep(&TaskConfiguration::AppendTasklistStep);
119 AddStep(&TaskConfiguration::EndStep);
122 void TaskConfiguration::StartStep()
124 _D("--------- <TaskConfiguration> : START ----------");
127 void TaskConfiguration::EndStep()
129 _D("--------- <TaskConfiguration> : END ----------");
132 void TaskConfiguration::PkgmgrStartStep()
134 pkgMgrInterface()->sendProgress(0);
137 void TaskConfiguration::AppendTasklistStep()
139 if (!m_context.isUpdateMode) {
140 _D("TaskConfiguration -> new installation task list");
141 m_context.job->appendNewInstallationTaskList();
143 _D("TaskConfiguration -> update installation task list");
144 m_context.job->appendUpdateInstallationTaskList();
148 std::shared_ptr<PackageManager::IPkgmgrSignal> TaskConfiguration::pkgMgrInterface()
150 return m_context.job->GetInstallerStruct().pkgmgrInterface;
153 void TaskConfiguration::SetupTempDirStep()
155 _D("widgetPath: %s", m_context.requestedPath.c_str());
156 _D("tempPath: %s", m_tempDir.c_str());
157 if (m_context.mode.extension == InstallMode::ExtensionType::DIR) {
158 if (m_context.mode.command ==
159 InstallMode::Command::REINSTALL) {
160 std::ostringstream tempPathBuilder;
161 tempPathBuilder << WrtDB::GlobalConfig::GetUserInstalledWidgetPath();
162 tempPathBuilder << WrtDB::GlobalConfig::GetTmpDirPath();
163 tempPathBuilder << "/";
164 tempPathBuilder << m_context.requestedPath;
165 m_tempDir = tempPathBuilder.str();
167 m_tempDir = m_context.requestedPath;
171 Jobs::WidgetInstall::createTempPath(
172 m_context.mode.rootPath ==
173 InstallMode::RootPath::RO);
174 if(!hasExtension(m_context.requestedPath, XML_EXTENSION)) //unzip everything except xml files
176 WidgetUnzip wgtUnzip;
177 wgtUnzip.unzipWgtFile(m_context.requestedPath, m_tempDir);
181 _D("From browser installation - unzip is not done");
186 void TaskConfiguration::ParseXMLConfigStep()
188 parseWidgetXMLConfig(
189 m_context.requestedPath, m_tempDir,
190 m_context.widgetConfig.packagingType,
191 m_context.mode.command == InstallMode::Command::REINSTALL);
192 _D("widget packaging type : %d", static_cast<WrtDB::PkgType>(m_context.widgetConfig.packagingType.pkgType));
195 void TaskConfiguration::TizenIdStep()
197 bool shouldMakeAppid = false;
198 using namespace PackageManager;
200 if (!!m_widgetConfig.tizenAppId) {
201 _D("Setting tizenAppId provided in config.xml: %s", DPL::ToUTF8String(*m_widgetConfig.tizenAppId).c_str());
203 m_context.widgetConfig.tzAppid = *m_widgetConfig.tizenAppId;
205 if (!!m_widgetConfig.tizenPkgId) {
206 _D("Setting tizenPkgId provided in config.xml: %s", DPL::ToUTF8String(*m_widgetConfig.tizenPkgId).c_str());
208 m_context.widgetConfig.tzPkgid = *m_widgetConfig.tizenPkgId;
210 DPL::String appid = *m_widgetConfig.tizenAppId;
211 if (appid.length() > PACKAGE_ID_LENGTH) {
212 m_context.widgetConfig.tzPkgid =
213 appid.substr(0, PACKAGE_ID_LENGTH);
215 //old version appid only has 10byte random character is able to install for a while.
216 //this case appid equal pkgid.
217 m_context.widgetConfig.tzPkgid =
218 *m_widgetConfig.tizenAppId;
219 shouldMakeAppid = true;
223 shouldMakeAppid = true;
224 TizenPkgId pkgId = WidgetDAOReadOnly::generatePkgId();
225 _D("Checking if pkg id is unique");
227 if (!validateTizenPackageID(pkgId)) {
228 //path exist, chose another one
229 pkgId = WidgetDAOReadOnly::generatePkgId();
234 m_context.widgetConfig.tzPkgid = pkgId;
235 _D("tizen_id name was generated by WRT: %ls", m_context.widgetConfig.tzPkgid.c_str());
238 if (shouldMakeAppid == true) {
239 DPL::OptionalString name;
240 DPL::OptionalString defaultLocale = m_widgetConfig.defaultlocale;
242 FOREACH(localizedData, m_widgetConfig.localizedDataSet)
244 Locale i = localizedData->first;
245 if (!!defaultLocale) {
246 if (defaultLocale == i) {
247 name = localizedData->second.name;
251 name = localizedData->second.name;
256 if (regcomp(®x, REG_NAME_PATTERN, REG_NOSUB | REG_EXTENDED) != 0) {
257 _D("Regcomp failed");
260 _D("Name : %ls", (*name).c_str());
261 if (!name || (regexec(®x, DPL::ToUTF8String(*name).c_str(),
262 static_cast<size_t>(0), NULL, 0) != REG_NOERROR))
264 const std::string allowedString("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
265 std::ostringstream genName;
267 gettimeofday(&tv, NULL);
268 unsigned int seed = time(NULL) + tv.tv_usec;
270 genName << "_" << allowedString[rand_r(&seed) % allowedString.length()];
271 name = DPL::FromUTF8String(genName.str());
272 _D("name was generated by WRT");
275 _D("Name : %ls", (*name).c_str());
276 std::ostringstream genid;
277 genid << m_context.widgetConfig.tzPkgid << "." << name;
278 _D("tizen appid was generated by WRT : %s", genid.str().c_str());
280 DPL::OptionalString appid = DPL::FromUTF8String(genid.str());
281 NormalizeAndTrimSpaceString(appid);
282 m_context.widgetConfig.tzAppid = *appid;
285 // send start signal of pkgmgr
286 pkgMgrInterface()->setPkgname(DPL::ToUTF8String(m_context.widgetConfig.tzPkgid));
288 _D("Tizen App Id : %ls", (m_context.widgetConfig.tzAppid).c_str());
289 _D("Tizen Pkg Id : %ls", (m_context.widgetConfig.tzPkgid).c_str());
292 void TaskConfiguration::CheckAppRunningStateStep()
294 bool isRunning = false;
296 app_manager_is_running(DPL::ToUTF8String(m_context.widgetConfig.tzAppid).c_str(),
298 if (APP_MANAGER_ERROR_NONE != ret) {
299 _E("Fail to get running state");
300 ThrowMsg(Jobs::WidgetInstall::Exceptions::WidgetRunningError,
301 "widget is running");
304 if (true == isRunning) {
305 // get app_context for running application
306 // app_context must be released with app_context_destroy
307 app_context_h appCtx = NULL;
309 app_manager_get_app_context(
310 DPL::ToUTF8String(m_context.widgetConfig.tzAppid).c_str(),
312 if (APP_MANAGER_ERROR_NONE != ret) {
313 _E("Fail to get app_context");
314 ThrowMsg(Jobs::WidgetInstall::Exceptions::WidgetRunningError,
315 "widget is running");
318 // terminate app_context_h
319 ret = app_manager_terminate_app(appCtx);
320 if (APP_MANAGER_ERROR_NONE != ret) {
321 _E("Fail to terminate running application");
322 app_context_destroy(appCtx);
323 ThrowMsg(Jobs::WidgetInstall::Exceptions::WidgetRunningError,
324 "widget is running");
326 app_context_destroy(appCtx);
327 // app_manager_terminate_app isn't sync API
328 // wait until application isn't running (50ms * 100)
329 bool isStillRunning = true;
330 int checkingloop = 100;
331 struct timespec duration = { 0, 50 * 1000 * 1000 };
332 while (--checkingloop >= 0) {
333 nanosleep(&duration, NULL);
334 int ret = app_manager_is_running(
335 DPL::ToUTF8String(m_context.widgetConfig.tzAppid).c_str(),
337 if (APP_MANAGER_ERROR_NONE != ret) {
338 _E("Fail to get running state");
339 ThrowMsg(Jobs::WidgetInstall::Exceptions::WidgetRunningError,
340 "widget is running");
342 if (!isStillRunning) {
346 if (isStillRunning) {
347 _E("Fail to terminate running application");
348 ThrowMsg(Jobs::WidgetInstall::Exceptions::WidgetRunningError,
349 "widget is running");
351 _D("terminate application");
356 void TaskConfiguration::ConfigureWidgetLocationStep()
358 m_context.locations =
359 WidgetLocation(DPL::ToUTF8String(m_context.widgetConfig.tzPkgid),
360 m_context.requestedPath, m_tempDir,
361 m_context.widgetConfig.packagingType,
362 m_context.mode.rootPath ==
363 InstallMode::RootPath::RO,
364 m_context.mode.extension);
365 m_context.locations->registerAppid(
366 DPL::ToUTF8String(m_context.widgetConfig.tzAppid));
368 _D("widgetSource %s", m_context.requestedPath.c_str());
371 void TaskConfiguration::DetectUpdateInstallationStep()
373 WidgetUpdateInfo update;
374 // checking installed web application
376 // no excpetion means, it isn't update mode
377 update = detectWidgetUpdate(m_widgetConfig,
378 m_context.widgetConfig.tzAppid);
379 checkWidgetUpdate(update);
381 m_context.isUpdateMode = true;
383 //if update, notify pkgmgr that this is update
384 pkgMgrInterface()->startJob(InstallationType::UpdateInstallation);
386 Catch(WidgetDAOReadOnly::Exception::WidgetNotExist) {
387 pkgMgrInterface()->startJob(InstallationType::NewInstallation);
389 m_context.isUpdateMode = false;
391 if (!validateTizenApplicationID(
392 m_context.widgetConfig.tzAppid))
394 _E("tizen application ID is already used");
395 ThrowMsg(Jobs::WidgetInstall::Exceptions::WidgetConfigFileInvalid,
398 if (!validateTizenPackageID(m_context.widgetConfig.tzPkgid)) {
399 _E("tizen package ID is already used");
400 ThrowMsg(Jobs::WidgetInstall::Exceptions::PackageAlreadyInstalled,
401 "package is already installed");
406 void TaskConfiguration::CheckRDSSupportStep()
408 //update needs RDS support to go ahead if REINSTALL command is given
409 if(m_context.isUpdateMode)
411 if (!checkSupportRDSUpdateIfReinstall(m_widgetConfig)) {
412 ThrowMsg(Jobs::WidgetInstall::Exceptions::NotSupportRDSUpdate,
413 "RDS update failed");
418 bool TaskConfiguration::validateTizenApplicationID(
419 const WrtDB::TizenAppId &tizenAppId)
421 _D("tizen application ID = [%ls]", tizenAppId.c_str());
424 if (regcomp(®, REG_TIZENID_PATTERN, REG_NOSUB | REG_EXTENDED) != 0) {
425 _D("Regcomp failed");
428 if (regexec(®, DPL::ToUTF8String(tizenAppId).c_str(), 0, NULL, 0)
438 bool TaskConfiguration::validateTizenPackageID(
439 const WrtDB::TizenPkgId &tizenPkgId)
441 std::string pkgId = DPL::ToUTF8String(tizenPkgId);
443 std::string installPath =
444 std::string(GlobalConfig::GetUserInstalledWidgetPath()) +
448 if ((stat(installPath.c_str(), &dirStat) == 0))
455 bool TaskConfiguration::checkWidgetUpdate(
456 const WidgetUpdateInfo &update)
458 if (update.existingVersion.IsNull() || update.incomingVersion.IsNull()) {
462 _D("existing version = '%ls", update.existingVersion->Raw().c_str());
463 _D("incoming version = '%ls", update.incomingVersion->Raw().c_str());
464 _D("Tizen AppID = %ls", update.tzAppId.c_str());
466 m_context.widgetConfig.tzAppid = update.tzAppId;
468 if (!!update.existingVersion ||
469 m_context.mode.extension ==
470 InstallMode::ExtensionType::DIR) {
477 void TaskConfiguration::parseWidgetXMLConfig(
478 const std::string &widgetSource,
479 const std::string &tempPath,
480 WrtDB::PackagingType pkgType,
487 if (pkgType == PKG_TYPE_HOSTED_WEB_APP) {
488 parser.Parse(widgetSource,
490 new RootParser<WidgetParser>(m_widgetConfig,
491 DPL::FromUTF32String(
494 std::string configFile;
495 configFile = tempPath + "/" + CONFIG_XML;
496 if (!WrtUtilFileExists(configFile)) {
497 configFile = tempPath + "/" + WITH_OSP_XML;
501 // checking RDS data directory
502 if (access(configFile.c_str(), F_OK) != 0) {
503 std::string tzAppId =
504 widgetSource.substr(widgetSource.find_last_of("/")+1);
505 WidgetDAOReadOnly dao(WidgetDAOReadOnly::getTzAppId(DPL::FromUTF8String(tzAppId)));
506 configFile = DPL::ToUTF8String(*dao.getWidgetInstalledPath());
508 configFile += WITH_OSP_XML;
512 if(!DPL::Utils::Path(configFile).Exists())
514 ThrowMsg(Exceptions::MissingConfig, "Config file not exists");
517 #ifdef SCHEMA_VALIDATION_ENABLED
518 if(!parser.Validate(configFilePath, WRT_WIDGETS_XML_SCHEMA))
520 _E("Invalid configuration file - schema validation failed");
521 ThrowMsg(Exceptions::WidgetConfigFileInvalid, "Failed to parse config.xml file");
524 parser.Parse(configFile,
526 new RootParser<WidgetParser>(m_widgetConfig,
532 Catch(ElementParser::Exception::ParseError)
534 _E("Failed to parse config.xml file");
535 ThrowMsg(Exceptions::WidgetConfigFileInvalid, "Parser exeption");
537 Catch(WidgetDAOReadOnly::Exception::WidgetNotExist)
539 _E("Failed to find installed widget - give proper tizenId");
540 ThrowMsg(Exceptions::RDSDeltaFailure, "WidgetNotExist");
542 Catch(Exceptions::WidgetConfigFileNotFound){
543 _E("Failed to find config.xml");
544 ThrowMsg(Exceptions::MissingConfig, "Parser exeption");
548 WidgetUpdateInfo TaskConfiguration::detectWidgetUpdate(
549 const ConfigParserData &configInfo,
550 const WrtDB::TizenAppId &tizenId)
552 _D("Checking up widget package for config.xml...");
553 OptionalWidgetVersion incomingVersion;
555 if (!configInfo.version.IsNull()) {
557 DPL::Optional<WidgetVersion>(
558 WidgetVersion(*configInfo.version));
561 WidgetDAOReadOnly dao(tizenId);
563 OptionalWidgetVersion optVersion;
564 DPL::OptionalString version = dao.getVersion();
565 if (!version.IsNull()) {
566 optVersion = OptionalWidgetVersion(WidgetVersion(*version));
569 return WidgetUpdateInfo(
575 void TaskConfiguration::CheckPackageTypeStep()
577 if (hasExtension(m_context.requestedPath, XML_EXTENSION)) {
578 _D("Hosted app installation");
579 m_context.widgetConfig.packagingType = PKG_TYPE_HOSTED_WEB_APP;
583 std::string configFile = m_tempDir + "/" + OSP_MANIFEST_XML;
584 if (WrtUtilFileExists(configFile)) {
585 m_context.widgetConfig.packagingType = PKG_TYPE_HYBRID_WEB_APP;
589 m_context.widgetConfig.packagingType = PKG_TYPE_NOMAL_WEB_APP;
592 void TaskConfiguration::ApplicationTypeStep() //TODO: is this really needed as WAC is not supported?
594 AppType widgetAppType = APP_TYPE_UNKNOWN;
595 FOREACH(iterator, m_widgetConfig.nameSpaces) {
596 _D("namespace = [%ls]", (*iterator).c_str());
598 if (*iterator == ConfigurationNamespace::TizenWebAppNamespaceName) {
599 widgetAppType = APP_TYPE_TIZENWEBAPP;
604 m_context.widgetConfig.webAppType = widgetAppType;
606 _D("type = [%s]", m_context.widgetConfig.webAppType.getApptypeToString().c_str());
609 void TaskConfiguration::ResourceEncryptionStep()
611 m_context.needEncryption = false;
612 FOREACH(it, m_widgetConfig.settingsList)
614 if (it->m_name == SETTING_VALUE_ENCRYPTION &&
615 it->m_value == SETTING_VALUE_ENCRYPTION_ENABLE)
617 _D("resource need encryption");
618 m_context.needEncryption = true;
623 bool TaskConfiguration::getMMCStatus()
626 if (vconf_get_int(VCONFKEY_SYSMAN_MMC_STATUS, &mmcStatus)) {
627 _E("vconf_get_int(VCONFKEY_SYSMAN_MMC_STATUS) failed.");
633 case VCONFKEY_SYSMAN_MMC_MOUNTED:
634 _D("mmcStatus is MMC_MOUNTED.");
636 case VCONFKEY_SYSMAN_MMC_REMOVED:
637 case VCONFKEY_SYSMAN_MMC_INSERTED_NOT_MOUNTED:
638 _D("mmcStatus is MMC_REMOVED or NOT_MOUNTED.");
641 _E("Platform is not supported, use the default settings");
646 bool TaskConfiguration::getDefaultExternalStorage()
648 // XXX NOT IMPLEMENTED.
652 void TaskConfiguration::InstallationFSLocationStep()
654 m_context.locationType = INSTALL_LOCATION_TYPE_NOMAL;
655 DPL::String locationValue;
657 if (m_context.mode.installTime != InstallMode::InstallTime::PRELOAD) {
658 FOREACH(it, m_widgetConfig.settingsList) {
659 if (it->m_name == SETTING_VALUE_INSTALLTOEXT_NAME) {
660 locationValue = it->m_value;
665 if ((SETTING_VALUE_INSTALLTOEXT_PREPER_EXT == locationValue
666 && getMMCStatus()) ||
667 (SETTING_VALUE_INSTALLTOEXT_AUTO == locationValue
668 && getDefaultExternalStorage())) {
669 _D("This webapp will be installed to sd card");
670 m_context.locationType = INSTALL_LOCATION_TYPE_EXTERNAL;
675 bool TaskConfiguration::checkSupportRDSUpdateIfReinstall(const WrtDB::ConfigParserData
678 if (m_context.mode.command ==
679 InstallMode::Command::REINSTALL)
681 DPL::String configValue = SETTING_VALUE_ENCRYPTION_DISABLE;
682 DPL::String dbValue = SETTING_VALUE_ENCRYPTION_DISABLE;
684 WidgetDAOReadOnly dao(m_context.widgetConfig.tzAppid);
685 WrtDB::WidgetSettings widgetSettings;
686 dao.getWidgetSettings(widgetSettings);
688 FOREACH(it, widgetSettings) {
689 if (it->settingName == SETTING_VALUE_ENCRYPTION) {
690 dbValue = it->settingValue;
694 FOREACH(data, configInfo.settingsList)
696 if (data->m_name == SETTING_VALUE_ENCRYPTION)
698 configValue = data->m_value;
701 if (configValue != dbValue) {
702 _E("Not Support RDS mode because of encryption setting");