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"
35 #include <dpl/utils/wrt_utility.h>
36 #include <dpl/utils/path.h>
37 #include <dpl/wrt-dao-ro/common_dao_types.h>
38 #include <dpl/wrt-dao-ro/widget_dao_read_only.h>
39 #include <dpl/wrt-dao-ro/global_config.h>
40 #include <dpl/wrt-dao-ro/config_parser_data.h>
41 #include <dpl/localization/w3c_file_localization.h>
43 #include <libiriwrapper.h>
44 #include <app_manager.h>
46 #include "root_parser.h"
47 #include "widget_parser.h"
48 #include "parser_runner.h"
50 #include <widget_install/widget_install_errors.h>
51 #include <widget_install/widget_install_context.h>
52 #include <widget_install_to_external.h>
53 #include <widget_install/widget_unzip.h>
54 #include <widget_install/job_widget_install.h>
55 #include <widget_install/task_commons.h>
57 #include <installer_log.h>
59 using namespace WrtDB;
62 const char* const CONFIG_XML = "config.xml";
63 const char* const WITH_OSP_XML = "res/wgt/config.xml";
64 const char* const OSP_MANIFEST_XML = "info/manifest.xml";
66 //allowed: a-z, A-Z, 0-9
67 const char* REG_TIZENID_PATTERN = "^[a-zA-Z0-9]{10}.{1,}$";
68 const char* REG_NAME_PATTERN = "^[a-zA-Z0-9._-]{1,}$";
69 const size_t PACKAGE_ID_LENGTH = 10;
71 static const DPL::String SETTING_VALUE_ENCRYPTION = L"encryption";
72 static const DPL::String SETTING_VALUE_ENCRYPTION_ENABLE = L"enable";
73 static const DPL::String SETTING_VALUE_ENCRYPTION_DISABLE = L"disable";
74 const DPL::String SETTING_VALUE_INSTALLTOEXT_NAME = L"install-location";
75 const DPL::String SETTING_VALUE_INSTALLTOEXT_PREPER_EXT = L"prefer-external";
76 const DPL::String SETTING_VALUE_INSTALLTOEXT_AUTO = L"auto";
77 const std::string XML_EXTENSION = ".xml";
79 bool hasExtension(const std::string& filename, const std::string& extension)
81 _D("Looking for extension %s in %s", extension.c_str(), filename.c_str());
82 size_t fileLen = filename.length();
83 size_t extLen = extension.length();
84 if (fileLen < extLen) {
85 _E("Filename %s is shorter than extension %s", filename.c_str(), extension.c_str());
88 return (0 == filename.compare(fileLen - extLen, extLen, extension));
90 } // namespace anonymous
93 namespace WidgetInstall {
95 TaskConfiguration::TaskConfiguration(InstallerContext& context) :
96 DPL::TaskDecl<TaskConfiguration>(this),
98 m_widgetConfig(m_context.widgetConfig.configInfo)
100 AddStep(&TaskConfiguration::StartStep);
102 AddStep(&TaskConfiguration::SetupTempDirStep);
103 AddStep(&TaskConfiguration::CheckPackageTypeStep);
105 AddStep(&TaskConfiguration::ParseXMLConfigStep);
107 AddStep(&TaskConfiguration::TizenIdStep);
108 AddStep(&TaskConfiguration::CheckAppRunningStateStep);
109 AddStep(&TaskConfiguration::ApplicationTypeStep);
110 AddStep(&TaskConfiguration::ResourceEncryptionStep);
111 AddStep(&TaskConfiguration::InstallationFSLocationStep);
113 AddStep(&TaskConfiguration::DetectUpdateInstallationStep);
114 AddStep(&TaskConfiguration::CheckRDSSupportStep);
115 AddStep(&TaskConfiguration::ConfigureWidgetLocationStep);
116 AddStep(&TaskConfiguration::PkgmgrStartStep);
118 AddStep(&TaskConfiguration::AppendTasklistStep);
120 AddStep(&TaskConfiguration::EndStep);
123 void TaskConfiguration::StartStep()
125 _D("--------- <TaskConfiguration> : START ----------");
128 void TaskConfiguration::EndStep()
130 _D("--------- <TaskConfiguration> : END ----------");
133 void TaskConfiguration::PkgmgrStartStep()
135 pkgMgrInterface()->sendProgress(0);
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::CheckAppRunningStateStep()
295 bool isRunning = false;
297 app_manager_is_running(DPL::ToUTF8String(m_context.widgetConfig.tzAppid).c_str(),
299 if (APP_MANAGER_ERROR_NONE != ret) {
300 _E("Fail to get running state");
301 ThrowMsg(Jobs::WidgetInstall::Exceptions::WidgetRunningError,
302 "widget is running");
305 if (true == isRunning) {
306 // get app_context for running application
307 // app_context must be released with app_context_destroy
308 app_context_h appCtx = NULL;
310 app_manager_get_app_context(
311 DPL::ToUTF8String(m_context.widgetConfig.tzAppid).c_str(),
313 if (APP_MANAGER_ERROR_NONE != ret) {
314 _E("Fail to get app_context");
315 ThrowMsg(Jobs::WidgetInstall::Exceptions::WidgetRunningError,
316 "widget is running");
319 // terminate app_context_h
320 ret = app_manager_terminate_app(appCtx);
321 if (APP_MANAGER_ERROR_NONE != ret) {
322 _E("Fail to terminate running application");
323 app_context_destroy(appCtx);
324 ThrowMsg(Jobs::WidgetInstall::Exceptions::WidgetRunningError,
325 "widget is running");
327 app_context_destroy(appCtx);
328 // app_manager_terminate_app isn't sync API
329 // wait until application isn't running (50ms * 100)
330 bool isStillRunning = true;
331 int checkingloop = 100;
332 struct timespec duration = { 0, 50 * 1000 * 1000 };
333 while (--checkingloop >= 0) {
334 nanosleep(&duration, NULL);
335 int ret = app_manager_is_running(
336 DPL::ToUTF8String(m_context.widgetConfig.tzAppid).c_str(),
338 if (APP_MANAGER_ERROR_NONE != ret) {
339 _E("Fail to get running state");
340 ThrowMsg(Jobs::WidgetInstall::Exceptions::WidgetRunningError,
341 "widget is running");
343 if (!isStillRunning) {
347 if (isStillRunning) {
348 _E("Fail to terminate running application");
349 ThrowMsg(Jobs::WidgetInstall::Exceptions::WidgetRunningError,
350 "widget is running");
352 _D("terminate application");
357 void TaskConfiguration::ConfigureWidgetLocationStep()
359 m_context.locations =
360 WidgetLocation(DPL::ToUTF8String(m_context.widgetConfig.tzPkgid),
361 m_context.requestedPath, m_tempDir,
362 m_context.widgetConfig.packagingType,
363 m_context.mode.rootPath ==
364 InstallMode::RootPath::RO,
365 m_context.mode.extension);
366 m_context.locations->registerAppid(
367 DPL::ToUTF8String(m_context.widgetConfig.tzAppid));
369 _D("widgetSource %s", m_context.requestedPath.c_str());
372 void TaskConfiguration::DetectUpdateInstallationStep()
374 WidgetUpdateInfo update;
375 // checking installed web application
377 // no excpetion means, it isn't update mode
378 update = detectWidgetUpdate(m_widgetConfig,
379 m_context.widgetConfig.tzAppid);
380 checkWidgetUpdate(update);
382 m_context.isUpdateMode = true;
384 //if update, notify pkgmgr that this is update
385 pkgMgrInterface()->startJob(InstallationType::UpdateInstallation);
387 Catch(WidgetDAOReadOnly::Exception::WidgetNotExist) {
388 pkgMgrInterface()->startJob(InstallationType::NewInstallation);
390 m_context.isUpdateMode = false;
392 if (!validateTizenApplicationID(
393 m_context.widgetConfig.tzAppid))
395 _E("tizen application ID is already used");
396 ThrowMsg(Jobs::WidgetInstall::Exceptions::WidgetConfigFileInvalid,
399 if (!validateTizenPackageID(m_context.widgetConfig.tzPkgid)) {
400 _E("tizen package ID is already used");
401 ThrowMsg(Jobs::WidgetInstall::Exceptions::PackageAlreadyInstalled,
402 "package is already installed");
407 void TaskConfiguration::CheckRDSSupportStep()
409 //update needs RDS support to go ahead if REINSTALL command is given
410 if(m_context.isUpdateMode)
412 if (!checkSupportRDSUpdateIfReinstall(m_widgetConfig)) {
413 ThrowMsg(Jobs::WidgetInstall::Exceptions::NotSupportRDSUpdate,
414 "RDS update failed");
419 bool TaskConfiguration::validateTizenApplicationID(
420 const WrtDB::TizenAppId &tizenAppId)
422 _D("tizen application ID = [%ls]", tizenAppId.c_str());
425 if (regcomp(®, REG_TIZENID_PATTERN, REG_NOSUB | REG_EXTENDED) != 0) {
426 _D("Regcomp failed");
429 if (regexec(®, DPL::ToUTF8String(tizenAppId).c_str(), 0, NULL, 0)
439 bool TaskConfiguration::validateTizenPackageID(
440 const WrtDB::TizenPkgId &tizenPkgId)
442 std::string pkgId = DPL::ToUTF8String(tizenPkgId);
444 std::string installPath =
445 std::string(GlobalConfig::GetUserInstalledWidgetPath()) +
449 if ((stat(installPath.c_str(), &dirStat) == 0))
456 bool TaskConfiguration::checkWidgetUpdate(
457 const WidgetUpdateInfo &update)
459 if (update.existingVersion.IsNull() || update.incomingVersion.IsNull()) {
463 _D("existing version = '%ls", update.existingVersion->Raw().c_str());
464 _D("incoming version = '%ls", update.incomingVersion->Raw().c_str());
465 _D("Tizen AppID = %ls", update.tzAppId.c_str());
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::checkSupportRDSUpdateIfReinstall(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");