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::ApplicationTypeStep);
108 AddStep(&TaskConfiguration::ResourceEncryptionStep);
109 AddStep(&TaskConfiguration::InstallationFSLocationStep);
111 AddStep(&TaskConfiguration::DetectUpdateInstallationStep);
112 AddStep(&TaskConfiguration::CheckRDSSupportStep);
113 AddStep(&TaskConfiguration::ConfigureWidgetLocationStep);
114 AddStep(&TaskConfiguration::PkgmgrStartStep);
116 AddStep(&TaskConfiguration::AppendTasklistStep);
118 AddStep(&TaskConfiguration::EndStep);
121 void TaskConfiguration::StartStep()
123 _D("--------- <TaskConfiguration> : START ----------");
126 void TaskConfiguration::EndStep()
128 _D("--------- <TaskConfiguration> : END ----------");
131 void TaskConfiguration::PkgmgrStartStep()
133 pkgMgrInterface()->sendSignal(
138 void TaskConfiguration::AppendTasklistStep()
140 if (!m_context.isUpdateMode) {
141 _D("TaskConfiguration -> new installation task list");
142 m_context.job->appendNewInstallationTaskList();
144 _D("TaskConfiguration -> update installation task list");
145 m_context.job->appendUpdateInstallationTaskList();
149 std::shared_ptr<PackageManager::IPkgmgrSignal> TaskConfiguration::pkgMgrInterface()
151 return m_context.job->GetInstallerStruct().pkgmgrInterface;
154 void TaskConfiguration::SetupTempDirStep()
156 _D("widgetPath: %s", m_context.requestedPath.c_str());
157 _D("tempPath: %s", m_tempDir.c_str());
158 if (m_context.mode.extension == InstallMode::ExtensionType::DIR) {
159 if (m_context.mode.command ==
160 InstallMode::Command::REINSTALL) {
161 std::ostringstream tempPathBuilder;
162 tempPathBuilder << WrtDB::GlobalConfig::GetUserInstalledWidgetPath();
163 tempPathBuilder << WrtDB::GlobalConfig::GetTmpDirPath();
164 tempPathBuilder << "/";
165 tempPathBuilder << m_context.requestedPath;
166 m_tempDir = tempPathBuilder.str();
168 m_tempDir = m_context.requestedPath;
172 Jobs::WidgetInstall::createTempPath(
173 m_context.mode.rootPath ==
174 InstallMode::RootPath::RO);
175 if(!hasExtension(m_context.requestedPath, XML_EXTENSION)) //unzip everything except xml files
177 WidgetUnzip wgtUnzip;
178 wgtUnzip.unzipWgtFile(m_context.requestedPath, m_tempDir);
182 _D("From browser installation - unzip is not done");
187 void TaskConfiguration::ParseXMLConfigStep()
189 parseWidgetXMLConfig(
190 m_context.requestedPath, m_tempDir,
191 m_context.widgetConfig.packagingType,
192 m_context.mode.command == InstallMode::Command::REINSTALL);
193 _D("widget packaging type : %d", static_cast<WrtDB::PkgType>(m_context.widgetConfig.packagingType.pkgType));
196 void TaskConfiguration::TizenIdStep()
198 bool shouldMakeAppid = false;
199 using namespace PackageManager;
201 if (!!m_widgetConfig.tizenAppId) {
202 _D("Setting tizenAppId provided in config.xml: %s", DPL::ToUTF8String(*m_widgetConfig.tizenAppId).c_str());
204 m_context.widgetConfig.tzAppid = *m_widgetConfig.tizenAppId;
206 if (!!m_widgetConfig.tizenPkgId) {
207 _D("Setting tizenPkgId provided in config.xml: %s", DPL::ToUTF8String(*m_widgetConfig.tizenPkgId).c_str());
209 m_context.widgetConfig.tzPkgid = *m_widgetConfig.tizenPkgId;
211 DPL::String appid = *m_widgetConfig.tizenAppId;
212 if (appid.length() > PACKAGE_ID_LENGTH) {
213 m_context.widgetConfig.tzPkgid =
214 appid.substr(0, PACKAGE_ID_LENGTH);
216 //old version appid only has 10byte random character is able to install for a while.
217 //this case appid equal pkgid.
218 m_context.widgetConfig.tzPkgid =
219 *m_widgetConfig.tizenAppId;
220 shouldMakeAppid = true;
224 shouldMakeAppid = true;
225 TizenPkgId pkgId = WidgetDAOReadOnly::generatePkgId();
226 _D("Checking if pkg id is unique");
228 if (!validateTizenPackageID(pkgId)) {
229 //path exist, chose another one
230 pkgId = WidgetDAOReadOnly::generatePkgId();
235 m_context.widgetConfig.tzPkgid = pkgId;
236 _D("tizen_id name was generated by WRT: %ls", m_context.widgetConfig.tzPkgid.c_str());
239 if (shouldMakeAppid == true) {
240 DPL::OptionalString name;
241 DPL::OptionalString defaultLocale = m_widgetConfig.defaultlocale;
243 FOREACH(localizedData, m_widgetConfig.localizedDataSet)
245 Locale i = localizedData->first;
246 if (!!defaultLocale) {
247 if (defaultLocale == i) {
248 name = localizedData->second.name;
252 name = localizedData->second.name;
257 if (regcomp(®x, REG_NAME_PATTERN, REG_NOSUB | REG_EXTENDED) != 0) {
258 _D("Regcomp failed");
261 _D("Name : %ls", (*name).c_str());
262 if (!name || (regexec(®x, DPL::ToUTF8String(*name).c_str(),
263 static_cast<size_t>(0), NULL, 0) != REG_NOERROR))
265 const std::string allowedString("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
266 std::ostringstream genName;
268 gettimeofday(&tv, NULL);
269 unsigned int seed = time(NULL) + tv.tv_usec;
271 genName << "_" << allowedString[rand_r(&seed) % allowedString.length()];
272 name = DPL::FromUTF8String(genName.str());
273 _D("name was generated by WRT");
276 _D("Name : %ls", (*name).c_str());
277 std::ostringstream genid;
278 genid << m_context.widgetConfig.tzPkgid << "." << name;
279 _D("tizen appid was generated by WRT : %s", genid.str().c_str());
281 DPL::OptionalString appid = DPL::FromUTF8String(genid.str());
282 NormalizeAndTrimSpaceString(appid);
283 m_context.widgetConfig.tzAppid = *appid;
286 // send start signal of pkgmgr
287 pkgMgrInterface()->setPkgname(DPL::ToUTF8String(m_context.widgetConfig.tzPkgid));
289 _D("Tizen App Id : %ls", (m_context.widgetConfig.tzAppid).c_str());
290 _D("Tizen Pkg Id : %ls", (m_context.widgetConfig.tzPkgid).c_str());
293 void TaskConfiguration::ConfigureWidgetLocationStep()
295 m_context.locations =
296 WidgetLocation(DPL::ToUTF8String(m_context.widgetConfig.tzPkgid),
297 m_context.requestedPath, m_tempDir,
298 m_context.widgetConfig.packagingType,
299 m_context.mode.rootPath ==
300 InstallMode::RootPath::RO,
301 m_context.mode.extension);
302 m_context.locations->registerAppid(
303 DPL::ToUTF8String(m_context.widgetConfig.tzAppid));
305 _D("widgetSource %s", m_context.requestedPath.c_str());
308 void TaskConfiguration::DetectUpdateInstallationStep()
310 WidgetUpdateInfo update;
311 // checking installed web application
313 // no excpetion means, it isn't update mode
314 update = detectWidgetUpdate(m_widgetConfig,
315 m_context.widgetConfig.tzAppid);
316 checkWidgetUpdate(update);
318 m_context.isUpdateMode = true;
320 //if update, notify pkgmgr that this is update
321 pkgMgrInterface()->sendSignal( PKGMGR_START_KEY, PKGMGR_START_UPDATE);
323 Catch(WidgetDAOReadOnly::Exception::WidgetNotExist) {
324 pkgMgrInterface()->sendSignal(PKGMGR_START_KEY, PKGMGR_START_INSTALL);
326 m_context.isUpdateMode = false;
328 if (!validateTizenApplicationID(
329 m_context.widgetConfig.tzAppid))
331 _E("tizen application ID is already used");
332 ThrowMsg(Jobs::WidgetInstall::Exceptions::WidgetConfigFileInvalid,
335 if (!validateTizenPackageID(m_context.widgetConfig.tzPkgid)) {
336 _E("tizen package ID is already used");
337 ThrowMsg(Jobs::WidgetInstall::Exceptions::PackageAlreadyInstalled,
338 "package is already installed");
343 void TaskConfiguration::CheckRDSSupportStep()
345 //update needs RDS support to go ahead
346 if(m_context.isUpdateMode)
348 if (!checkSupportRDSUpdate(m_widgetConfig)) {
349 ThrowMsg(Jobs::WidgetInstall::Exceptions::NotSupportRDSUpdate,
350 "RDS update failed");
355 bool TaskConfiguration::validateTizenApplicationID(
356 const WrtDB::TizenAppId &tizenAppId)
358 _D("tizen application ID = [%ls]", tizenAppId.c_str());
361 if (regcomp(®, REG_TIZENID_PATTERN, REG_NOSUB | REG_EXTENDED) != 0) {
362 _D("Regcomp failed");
365 if (regexec(®, DPL::ToUTF8String(tizenAppId).c_str(), 0, NULL, 0)
375 bool TaskConfiguration::validateTizenPackageID(
376 const WrtDB::TizenPkgId &tizenPkgId)
378 std::string pkgId = DPL::ToUTF8String(tizenPkgId);
380 std::string installPath =
381 std::string(GlobalConfig::GetUserInstalledWidgetPath()) +
385 if ((stat(installPath.c_str(), &dirStat) == 0))
392 bool TaskConfiguration::checkWidgetUpdate(
393 const WidgetUpdateInfo &update)
395 if (update.existingVersion.IsNull() || update.incomingVersion.IsNull()) {
399 _D("existing version = '%ls", update.existingVersion->Raw().c_str());
400 _D("incoming version = '%ls", update.incomingVersion->Raw().c_str());
401 _D("Tizen AppID = %ls", update.tzAppId.c_str());
403 // TODO: step Check running state
404 bool isRunning = false;
406 app_manager_is_running(DPL::ToUTF8String(update.tzAppId).c_str(),
408 if (APP_MANAGER_ERROR_NONE != ret) {
409 _E("Fail to get running state");
410 ThrowMsg(Jobs::WidgetInstall::Exceptions::WidgetRunningError,
411 "widget is running");
414 if (true == isRunning) {
415 // get app_context for running application
416 // app_context must be released with app_context_destroy
417 app_context_h appCtx = NULL;
419 app_manager_get_app_context(
420 DPL::ToUTF8String(update.tzAppId).c_str(),
422 if (APP_MANAGER_ERROR_NONE != ret) {
423 _E("Fail to get app_context");
424 ThrowMsg(Jobs::WidgetInstall::Exceptions::WidgetRunningError,
425 "widget is running");
428 // terminate app_context_h
429 ret = app_manager_terminate_app(appCtx);
430 if (APP_MANAGER_ERROR_NONE != ret) {
431 _E("Fail to terminate running application");
432 app_context_destroy(appCtx);
433 ThrowMsg(Jobs::WidgetInstall::Exceptions::WidgetRunningError,
434 "widget is running");
436 app_context_destroy(appCtx);
437 // app_manager_terminate_app isn't sync API
438 // wait until application isn't running (50ms * 100)
439 bool isStillRunning = true;
440 int checkingloop = 100;
441 struct timespec duration = { 0, 50 * 1000 * 1000 };
442 while (--checkingloop >= 0) {
443 nanosleep(&duration, NULL);
445 app_manager_is_running(
446 DPL::ToUTF8String(update.tzAppId).c_str(),
448 if (APP_MANAGER_ERROR_NONE != ret) {
449 _E("Fail to get running state");
450 ThrowMsg(Jobs::WidgetInstall::Exceptions::WidgetRunningError,
451 "widget is running");
453 if (!isStillRunning) {
457 if (isStillRunning) {
458 _E("Fail to terminate running application");
459 ThrowMsg(Jobs::WidgetInstall::Exceptions::WidgetRunningError,
460 "widget is running");
462 _D("terminate application");
467 m_context.widgetConfig.tzAppid = update.tzAppId;
469 if (!!update.existingVersion ||
470 m_context.mode.extension ==
471 InstallMode::ExtensionType::DIR) {
478 void TaskConfiguration::parseWidgetXMLConfig(
479 const std::string &widgetSource,
480 const std::string &tempPath,
481 WrtDB::PackagingType pkgType,
488 if (pkgType == PKG_TYPE_HOSTED_WEB_APP) {
489 parser.Parse(widgetSource,
491 new RootParser<WidgetParser>(m_widgetConfig,
492 DPL::FromUTF32String(
495 std::string configFile;
496 configFile = tempPath + "/" + CONFIG_XML;
497 if (!WrtUtilFileExists(configFile)) {
498 configFile = tempPath + "/" + WITH_OSP_XML;
502 // checking RDS data directory
503 if (access(configFile.c_str(), F_OK) != 0) {
504 std::string tzAppId =
505 widgetSource.substr(widgetSource.find_last_of("/")+1);
506 WidgetDAOReadOnly dao(WidgetDAOReadOnly::getTzAppId(DPL::FromUTF8String(tzAppId)));
507 configFile = DPL::ToUTF8String(*dao.getWidgetInstalledPath());
509 configFile += WITH_OSP_XML;
513 if(!DPL::Utils::Path(configFile).Exists())
515 ThrowMsg(Exceptions::MissingConfig, "Config file not exists");
518 #ifdef SCHEMA_VALIDATION_ENABLED
519 if(!parser.Validate(configFilePath, WRT_WIDGETS_XML_SCHEMA))
521 _E("Invalid configuration file - schema validation failed");
522 ThrowMsg(Exceptions::WidgetConfigFileInvalid, "Failed to parse config.xml file");
525 parser.Parse(configFile,
527 new RootParser<WidgetParser>(m_widgetConfig,
533 Catch(ElementParser::Exception::ParseError)
535 _E("Failed to parse config.xml file");
536 ThrowMsg(Exceptions::WidgetConfigFileInvalid, "Parser exeption");
538 Catch(WidgetDAOReadOnly::Exception::WidgetNotExist)
540 _E("Failed to find installed widget - give proper tizenId");
541 ThrowMsg(Exceptions::RDSDeltaFailure, "WidgetNotExist");
543 Catch(Exceptions::WidgetConfigFileNotFound){
544 _E("Failed to find config.xml");
545 ThrowMsg(Exceptions::MissingConfig, "Parser exeption");
549 WidgetUpdateInfo TaskConfiguration::detectWidgetUpdate(
550 const ConfigParserData &configInfo,
551 const WrtDB::TizenAppId &tizenId)
553 _D("Checking up widget package for config.xml...");
554 OptionalWidgetVersion incomingVersion;
556 if (!configInfo.version.IsNull()) {
558 DPL::Optional<WidgetVersion>(
559 WidgetVersion(*configInfo.version));
562 WidgetDAOReadOnly dao(tizenId);
564 OptionalWidgetVersion optVersion;
565 DPL::OptionalString version = dao.getVersion();
566 if (!version.IsNull()) {
567 optVersion = OptionalWidgetVersion(WidgetVersion(*version));
570 return WidgetUpdateInfo(
576 void TaskConfiguration::CheckPackageTypeStep()
578 if (hasExtension(m_context.requestedPath, XML_EXTENSION)) {
579 _D("Hosted app installation");
580 m_context.widgetConfig.packagingType = PKG_TYPE_HOSTED_WEB_APP;
584 std::string configFile = m_tempDir + "/" + OSP_MANIFEST_XML;
585 if (WrtUtilFileExists(configFile)) {
586 m_context.widgetConfig.packagingType = PKG_TYPE_HYBRID_WEB_APP;
590 m_context.widgetConfig.packagingType = PKG_TYPE_NOMAL_WEB_APP;
593 void TaskConfiguration::ApplicationTypeStep() //TODO: is this really needed as WAC is not supported?
595 AppType widgetAppType = APP_TYPE_UNKNOWN;
596 FOREACH(iterator, m_widgetConfig.nameSpaces) {
597 _D("namespace = [%ls]", (*iterator).c_str());
599 if (*iterator == ConfigurationNamespace::TizenWebAppNamespaceName) {
600 widgetAppType = APP_TYPE_TIZENWEBAPP;
605 m_context.widgetConfig.webAppType = widgetAppType;
607 _D("type = [%s]", m_context.widgetConfig.webAppType.getApptypeToString().c_str());
610 void TaskConfiguration::ResourceEncryptionStep()
612 m_context.needEncryption = false;
613 FOREACH(it, m_widgetConfig.settingsList)
615 if (it->m_name == SETTING_VALUE_ENCRYPTION &&
616 it->m_value == SETTING_VALUE_ENCRYPTION_ENABLE)
618 _D("resource need encryption");
619 m_context.needEncryption = true;
624 bool TaskConfiguration::getMMCStatus()
627 if (vconf_get_int(VCONFKEY_SYSMAN_MMC_STATUS, &mmcStatus)) {
628 _E("vconf_get_int(VCONFKEY_SYSMAN_MMC_STATUS) failed.");
634 case VCONFKEY_SYSMAN_MMC_MOUNTED:
635 _D("mmcStatus is MMC_MOUNTED.");
637 case VCONFKEY_SYSMAN_MMC_REMOVED:
638 case VCONFKEY_SYSMAN_MMC_INSERTED_NOT_MOUNTED:
639 _D("mmcStatus is MMC_REMOVED or NOT_MOUNTED.");
642 _E("Platform is not supported, use the default settings");
647 bool TaskConfiguration::getDefaultExternalStorage()
649 // XXX NOT IMPLEMENTED.
653 void TaskConfiguration::InstallationFSLocationStep()
655 m_context.locationType = INSTALL_LOCATION_TYPE_NOMAL;
656 DPL::String locationValue;
658 if (m_context.mode.installTime != InstallMode::InstallTime::PRELOAD) {
659 FOREACH(it, m_widgetConfig.settingsList) {
660 if (it->m_name == SETTING_VALUE_INSTALLTOEXT_NAME) {
661 locationValue = it->m_value;
666 if ((SETTING_VALUE_INSTALLTOEXT_PREPER_EXT == locationValue
667 && getMMCStatus()) ||
668 (SETTING_VALUE_INSTALLTOEXT_AUTO == locationValue
669 && getDefaultExternalStorage())) {
670 _D("This webapp will be installed to sd card");
671 m_context.locationType = INSTALL_LOCATION_TYPE_EXTERNAL;
676 bool TaskConfiguration::checkSupportRDSUpdate(const WrtDB::ConfigParserData
679 if (m_context.mode.command ==
680 InstallMode::Command::REINSTALL)
682 DPL::String configValue = SETTING_VALUE_ENCRYPTION_DISABLE;
683 DPL::String dbValue = SETTING_VALUE_ENCRYPTION_DISABLE;
685 WidgetDAOReadOnly dao(m_context.widgetConfig.tzAppid);
686 WrtDB::WidgetSettings widgetSettings;
687 dao.getWidgetSettings(widgetSettings);
689 FOREACH(it, widgetSettings) {
690 if (it->settingName == SETTING_VALUE_ENCRYPTION) {
691 dbValue = it->settingValue;
695 FOREACH(data, configInfo.settingsList)
697 if (data->m_name == SETTING_VALUE_ENCRYPTION)
699 configValue = data->m_value;
702 if (configValue != dbValue) {
703 _E("Not Support RDS mode because of encryption setting");