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.
17 * This file contains the definition of widget dao class.
19 * @file widget_dao.cpp
20 * @author Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
21 * @author Bartosz Janiak (b.janiak@samsung.com)
22 * @author Yang Jie (jie2.yang@samsung.com)
23 * @author Koeun Choi(koeun.choi@samsung.com)
24 * @author Pawel Sikorski(p.sikorski@samsung.com)
26 * @brief This file contains the definition of Configuration.
29 #include <dpl/wrt-dao-rw/widget_dao.h>
32 #include <dpl/log/log.h>
33 #include <dpl/foreach.h>
34 #include <dpl/wrt-dao-ro/webruntime_database.h>
35 #include <dpl/wrt-dao-rw/property_dao.h>
36 #include <orm_generator_wrt.h>
37 #include <dpl/wrt-dao-ro/WrtDatabase.h>
41 //TODO in current solution in each getter there exists a check
42 //"IsWidgetInstalled". Maybe it should be verified, if it could be done
43 //differently (check in WidgetDAO constructor)
45 #define SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN Try
47 #define SQL_CONNECTION_EXCEPTION_HANDLER_END(message) \
48 Catch(DPL::DB::SqlConnection::Exception::Base) { \
50 ReThrowMsg(WidgetDAO::Exception::DatabaseError, \
54 #define CHECK_WIDGET_EXISTENCE(macro_transaction, macro_handle) \
55 if (!WidgetDAO::isWidgetInstalled(macro_handle)) \
57 macro_transaction.Commit(); \
58 LogWarning("Cannot find widget. Handle: " << macro_handle); \
59 ThrowMsg(WidgetDAO::Exception::WidgetNotExist, \
60 "Cannot find widget. Handle: " << macro_handle); \
64 WidgetDAO::WidgetDAO(DbWidgetHandle widgetHandle) :
65 WidgetDAOReadOnly(widgetHandle)
69 WidgetDAO::WidgetDAO(DPL::OptionalString widgetGUID) :
70 WidgetDAOReadOnly(WidgetDAOReadOnly::getHandle(widgetGUID))
74 WidgetDAO::WidgetDAO(DPL::String pkgName) :
75 WidgetDAOReadOnly(WidgetDAOReadOnly::getHandle(pkgName))
79 WidgetDAO::~WidgetDAO()
83 void WidgetDAO::removeProperty(
84 const PropertyDAOReadOnly::WidgetPropertyKey &key)
87 PropertyDAO::RemoveProperty(m_widgetHandle, key);
89 Catch(PropertyDAOReadOnly::Exception::ReadOnlyProperty){
90 ReThrowMsg(WidgetDAO::Exception::DatabaseError,
91 "Failure during removing property");
95 void WidgetDAO::setProperty(
96 const PropertyDAOReadOnly::WidgetPropertyKey &key,
97 const PropertyDAOReadOnly::WidgetPropertyValue &value,
101 PropertyDAO::SetProperty(m_widgetHandle, key, value, readOnly);
103 Catch(PropertyDAOReadOnly::Exception::ReadOnlyProperty){
104 ReThrowMsg(WidgetDAO::Exception::DatabaseError,
105 "Failure during setting/updating property");
109 void WidgetDAO::setPkgName(const DPL::OptionalString& pkgName)
111 SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
113 using namespace DPL::DB::ORM;
114 wrt::ScopedTransaction transaction(&WrtDatabase::interface());
116 isWidgetInstalled(getHandle());
118 wrt::WidgetInfo::Row row;
119 row.Set_pkgname(pkgName);
121 WRT_DB_UPDATE(update, wrt::WidgetInfo, &WrtDatabase::interface())
123 Equals<wrt::WidgetInfo::app_id>(getHandle()));
127 transaction.Commit();
129 SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to register widget")
132 void WidgetDAO::registerWidget(
133 const DbWidgetHandle& widgetHandle,
134 const WidgetRegisterInfo &widgetRegInfo,
135 const IWacSecurity &wacSecurity)
137 LogDebug("Registering widget");
138 SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
140 DPL::DB::ORM::wrt::ScopedTransaction transaction(&WrtDatabase::interface());
142 //Register into WidgetInfo has to be first
143 //as all other tables depend upon that
144 registerWidgetInfo(widgetHandle, widgetRegInfo, wacSecurity);
146 registerWidgetExtendedInfo(widgetHandle, widgetRegInfo);
148 registerWidgetLocalizedInfo(widgetHandle, widgetRegInfo);
150 registerWidgetIcons(widgetHandle, widgetRegInfo);
152 registerWidgetStartFile(widgetHandle, widgetRegInfo);
154 PropertyDAO::RegisterProperties(widgetHandle, widgetRegInfo);
156 registerWidgetFeatures(widgetHandle, widgetRegInfo);
158 registerWidgetWindowModes(widgetHandle, widgetRegInfo);
160 registerWidgetWarpInfo(widgetHandle, widgetRegInfo);
162 registerWidgetCertificates(widgetHandle, wacSecurity);
164 CertificateChainList list;
165 wacSecurity.getCertificateChainList(list, SIGNATURE_DISTRIBUTOR);
166 registerCertificatesChains(widgetHandle, SIGNATURE_DISTRIBUTOR, list);
169 wacSecurity.getCertificateChainList(list, SIGNATURE_AUTHOR);
170 registerCertificatesChains(widgetHandle, SIGNATURE_AUTHOR, list);
172 registerWidgetSettings(widgetHandle, widgetRegInfo);
174 registerAppService(widgetHandle, widgetRegInfo);
176 registerEncryptedResouceInfo(widgetHandle, widgetRegInfo);
178 transaction.Commit();
180 SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to register widget")
183 #define DO_INSERT(row, table) \
185 WRT_DB_INSERT(insert, table, &WrtDatabase::interface()) \
186 insert->Values(row); \
190 void WidgetDAO::registerWidgetExtendedInfo(DbWidgetHandle widgetHandle,
191 const WidgetRegisterInfo ®Info)
193 //Try and transaction not needed
194 using namespace DPL::DB::ORM;
195 using namespace DPL::DB::ORM::wrt;
198 WidgetExtendedInfo::Row row;
199 row.Set_app_id(widgetHandle);
200 // row.Set_share_href (DPL::FromUTF8String(regInfo.shareHref));
201 row.Set_signature_type(regInfo.signatureType);
202 row.Set_test_widget(regInfo.isTestWidget);
203 row.Set_install_time(regInfo.installedTime);
204 row.Set_splash_img_src(regInfo.configInfo.splashImgSrc);
205 row.Set_background_page(regInfo.configInfo.backgroundPage);
208 DO_INSERT(row, WidgetExtendedInfo)
211 void WidgetDAO::registerWidgetInfo(
212 const DbWidgetHandle& widgetHandle,
213 const WidgetRegisterInfo ®Info,
214 const IWacSecurity &wacSecurity)
216 using namespace DPL::DB::ORM;
217 using namespace DPL::DB::ORM::wrt;
218 const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
220 // TODO in wrt_db all Columns in WidgetInfo have DEFAULT VALUE set.
221 // Because of that, "Optional" is not used there
224 row.Set_app_id(widgetHandle);
225 row.Set_widget_type(regInfo.type.appType);
226 row.Set_widget_id(widgetConfigurationInfo.widget_id);
227 row.Set_defaultlocale(widgetConfigurationInfo.defaultlocale);
228 row.Set_widget_version(widgetConfigurationInfo.version);
229 row.Set_widget_width(widgetConfigurationInfo.width);
230 row.Set_widget_height(widgetConfigurationInfo.height);
231 row.Set_author_name(widgetConfigurationInfo.authorName);
232 row.Set_author_email(widgetConfigurationInfo.authorEmail);
233 row.Set_author_href(widgetConfigurationInfo.authorHref);
234 row.Set_base_folder(DPL::FromUTF8String(regInfo.baseFolder));
235 row.Set_webkit_plugins_required(widgetConfigurationInfo.flashNeeded);
236 row.Set_recognized(wacSecurity.isRecognized());
237 row.Set_wac_signed(wacSecurity.isWacSigned());
238 row.Set_distributor_signed(wacSecurity.isDistributorSigned());
240 std::stringstream tmp;
241 tmp << regInfo.minVersion;
242 row.Set_min_version(DPL::FromUTF8String(tmp.str()));
244 row.Set_back_supported(widgetConfigurationInfo.backSupported);
245 row.Set_access_network(widgetConfigurationInfo.accessNetwork);
246 row.Set_pkgname(regInfo.pkgname);
247 row.Set_pkg_type(regInfo.pType.pkgType);
251 DO_INSERT(row, WidgetInfo);
253 Catch(DPL::DB::SqlConnection::Exception::Base)
255 ReThrowMsg(WidgetDAO::Exception::DatabaseError,
256 "Failed to register widget info.");
260 void WidgetDAO::registerWidgetLocalizedInfo(DbWidgetHandle widgetHandle,
261 const WidgetRegisterInfo ®Info)
263 using namespace DPL::DB::ORM;
264 using namespace DPL::DB::ORM::wrt;
266 const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
268 FOREACH(it, widgetConfigurationInfo.localizedDataSet)
270 const DPL::String& locale = it->first;
271 const ConfigParserData::LocalizedData& data = it->second;
273 LocalizedWidgetInfo::Row row;
274 row.Set_app_id(widgetHandle);
275 row.Set_widget_locale(locale);
276 row.Set_widget_name(data.name);
277 row.Set_widget_shortname(data.shortName);
278 row.Set_widget_description(data.description);
279 row.Set_widget_license(data.license);
280 row.Set_widget_license_file(data.licenseFile);
281 row.Set_widget_license_href(data.licenseHref);
283 DO_INSERT(row, LocalizedWidgetInfo)
287 void WidgetDAO::registerWidgetIcons(DbWidgetHandle widgetHandle,
288 const WidgetRegisterInfo ®Info)
290 using namespace DPL::DB::ORM;
291 using namespace DPL::DB::ORM::wrt;
294 FOREACH(i, regInfo.localizationData.icons)
296 wrt::WidgetIcon::icon_id::ColumnType icon_id;
298 wrt::WidgetIcon::Row row;
299 row.Set_app_id(widgetHandle);
300 row.Set_icon_src(i->src);
301 row.Set_icon_width(i->width);
302 row.Set_icon_height(i->height);
304 WRT_DB_INSERT(insert, wrt::WidgetIcon, &WrtDatabase::interface())
306 icon_id = insert->Execute();
309 FOREACH(j, i->availableLocales)
311 WidgetLocalizedIcon::Row row;
312 row.Set_app_id(widgetHandle);
313 row.Set_icon_id(icon_id);
314 row.Set_widget_locale(*j);
315 WRT_DB_SELECT(select, WidgetLocalizedIcon, &WrtDatabase::interface())
316 select->Where(And(Equals<WidgetLocalizedIcon::app_id>(widgetHandle),
317 Equals<WidgetLocalizedIcon::widget_locale>(*j)));
318 WidgetLocalizedIcon::Select::RowList rows = select->GetRowList();
320 bool flag = !rows.empty();
324 // already default icon value of same locale exists
325 WRT_DB_UPDATE(update, WidgetLocalizedIcon, &WrtDatabase::interface())
326 update->Where(And(Equals<WidgetLocalizedIcon::app_id>(widgetHandle),
327 Equals<WidgetLocalizedIcon::widget_locale>(*j)));
331 // any icon value of same locale doesn't exist
332 DO_INSERT(row, WidgetLocalizedIcon)
338 void WidgetDAO::registerWidgetStartFile(DbWidgetHandle widgetHandle,
339 const WidgetRegisterInfo ®Info)
341 using namespace DPL::DB::ORM;
342 using namespace DPL::DB::ORM::wrt;
345 FOREACH(i, regInfo.localizationData.startFiles)
347 WidgetStartFile::start_file_id::ColumnType startFileID;
349 WidgetStartFile::Row row;
350 row.Set_app_id(widgetHandle);
351 row.Set_src(i->path);
353 WRT_DB_INSERT(insert, WidgetStartFile, &WrtDatabase::interface())
355 startFileID = insert->Execute();
358 FOREACH(j, i->propertiesForLocales)
360 WidgetLocalizedStartFile::Row row;
361 row.Set_app_id(widgetHandle);
362 row.Set_start_file_id(startFileID);
363 row.Set_widget_locale(j->first);
364 row.Set_type(j->second.type);
365 row.Set_encoding(j->second.encoding);
367 DO_INSERT(row, WidgetLocalizedStartFile)
372 void WidgetDAO::registerWidgetFeatures(DbWidgetHandle widgetHandle,
373 const WidgetRegisterInfo ®Info)
375 using namespace DPL::DB::ORM;
376 const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
379 FOREACH(pWidgetFeature, widgetConfigurationInfo.featuresList)
381 wrt::WidgetFeature::Row widgetFeature;
382 widgetFeature.Set_app_id(widgetHandle);
383 widgetFeature.Set_name(pWidgetFeature->name);
384 widgetFeature.Set_required(pWidgetFeature->required);
385 widgetFeature.Set_rejected(false);
387 wrt::WidgetFeature::widget_feature_id::ColumnType widgetFeatureID;
389 WRT_DB_INSERT(insert, wrt::WidgetFeature, &WrtDatabase::interface())
390 insert->Values(widgetFeature);
391 widgetFeatureID = insert->Execute();
394 // Insert into table FeatureParam
395 wrt::FeatureParam::Row featureParam;
396 featureParam.Set_widget_feature_id(widgetFeatureID);
398 ConfigParserData::ParamsList::const_iterator iter;
400 FOREACH(iter, pWidgetFeature->paramsList)
402 featureParam.Set_name(iter->name);
403 featureParam.Set_value(iter->value);
405 DO_INSERT(featureParam, wrt::FeatureParam)
410 void WidgetDAO::updateFeatureRejectStatus(const DbWidgetFeature &widgetFeature){
411 // This function could be merged with registerWidgetFeature but it requires desing change:
412 // 1. Check "ace step" in installer must be done before "update database step"
414 // ConfigurationParserData shouldn't be called "ParserData" any more.
415 using namespace DPL::DB::ORM;
417 wrt::ScopedTransaction transaction(&WrtDatabase::interface());
418 WRT_DB_SELECT(select, wrt::WidgetFeature, &WrtDatabase::interface())
419 select->Where(And(Equals<wrt::WidgetFeature::app_id>(m_widgetHandle),
420 Equals<wrt::WidgetFeature::name>(widgetFeature.name)));
422 auto row = select->GetSingleRow();
423 row.Set_rejected(widgetFeature.rejected);
425 WRT_DB_UPDATE(update, wrt::WidgetFeature, &WrtDatabase::interface())
426 update->Where(And(Equals<wrt::WidgetFeature::app_id>(m_widgetHandle),
427 Equals<wrt::WidgetFeature::name>(widgetFeature.name)));
430 transaction.Commit();
433 void WidgetDAO::registerWidgetWindowModes(DbWidgetHandle widgetHandle,
434 const WidgetRegisterInfo ®Info)
436 using namespace DPL::DB::ORM;
437 using namespace DPL::DB::ORM::wrt;
438 const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
441 FOREACH(i, widgetConfigurationInfo.windowModes)
443 wrt::WidgetWindowModes::Row windowMode;
444 windowMode.Set_app_id(widgetHandle);
445 windowMode.Set_window_mode(*i);
447 DO_INSERT(windowMode, wrt::WidgetWindowModes)
451 void WidgetDAO::registerWidgetWarpInfo(DbWidgetHandle widgetHandle,
452 const WidgetRegisterInfo ®Info)
454 using namespace DPL::DB::ORM;
455 using namespace DPL::DB::ORM::wrt;
456 const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
459 FOREACH(AccIt, widgetConfigurationInfo.accessInfoSet)
461 WidgetWARPInfo::Row row;
462 row.Set_app_id(widgetHandle);
463 row.Set_iri(AccIt->m_strIRI);
464 row.Set_subdomain_access(static_cast <int>(
465 AccIt->m_bSubDomainAccess));
467 DO_INSERT(row, WidgetWARPInfo)
471 void WidgetDAO::registerWidgetCertificates(DbWidgetHandle widgetHandle,
472 const IWacSecurity &wacSecurity)
474 using namespace DPL::DB::ORM;
475 using namespace DPL::DB::ORM::wrt;
478 FOREACH(it, wacSecurity.getCertificateList())
480 WidgetCertificateFingerprint::Row row;
481 row.Set_app_id(widgetHandle);
482 row.Set_owner(it->owner);
483 row.Set_chainid(it->chainId);
484 row.Set_type(it->type);
485 row.Set_md5_fingerprint(DPL::FromUTF8String(it->strMD5Fingerprint));
486 row.Set_sha1_fingerprint(DPL::FromUTF8String(it->strSHA1Fingerprint));
487 row.Set_common_name(it->strCommonName);
489 DO_INSERT(row, WidgetCertificateFingerprint)
493 void WidgetDAO::registerCertificatesChains(DbWidgetHandle widgetHandle,
494 CertificateSource certificateSource,
495 const CertificateChainList &certificateChainList)
497 using namespace DPL::DB::ORM;
498 using namespace DPL::DB::ORM::wrt;
499 FOREACH(certChain, certificateChainList)
501 WidgetCertificate::Row row;
502 row.Set_app_id(widgetHandle);
503 row.Set_cert_source(certificateSource);
504 row.Set_encoded_chain(DPL::FromASCIIString(*certChain));
506 DO_INSERT(row, WidgetCertificate);
510 void WidgetDAO::registerWidgetSettings(DbWidgetHandle widgetHandle,
511 const WidgetRegisterInfo ®Info)
513 using namespace DPL::DB::ORM;
514 using namespace DPL::DB::ORM::wrt;
516 const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
518 FOREACH(pWidgetSetting, widgetConfigurationInfo.settingsList)
520 SettingsList::Row row;
521 row.Set_appId(widgetHandle);
522 row.Set_settingName(pWidgetSetting->m_name);
523 row.Set_settingValue(pWidgetSetting->m_value);
525 DO_INSERT(row, SettingsList)
529 void WidgetDAO::registerAppService(DbWidgetHandle widgetHandle,
530 const WidgetRegisterInfo ®Info)
532 using namespace DPL::DB::ORM;
533 using namespace DPL::DB::ORM::wrt;
534 const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
536 FOREACH(ASIt, widgetConfigurationInfo.appServiceList)
538 ApplicationServiceInfo::Row row;
539 row.Set_app_id(widgetHandle);
540 row.Set_src(ASIt->m_src);
541 row.Set_operation(ASIt->m_operation);
542 row.Set_scheme(ASIt->m_scheme);
543 row.Set_mime(ASIt->m_mime);
545 DO_INSERT(row, ApplicationServiceInfo)
549 void WidgetDAO::registerEncryptedResouceInfo(DbWidgetHandle widgetHandle,
550 const WidgetRegisterInfo ®Info)
552 using namespace DPL::DB::ORM;
553 using namespace DPL::DB::ORM::wrt;
555 FOREACH(it, regInfo.encryptedFiles)
557 EncryptedResourceList::Row row;
558 row.Set_app_id(widgetHandle);
559 row.Set_resource(it->fileName);
560 row.Set_size(it->fileSize);
562 DO_INSERT(row, EncryptedResourceList)
566 void WidgetDAO::registerExternalLocations(const ExternalLocationList & externals)
568 SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
570 using namespace DPL::DB::ORM;
571 using namespace DPL::DB::ORM::wrt;
572 DPL::DB::ORM::wrt::ScopedTransaction transaction(&WrtDatabase::interface());
573 LogDebug("Inserting external files for widgetHandle: " << m_widgetHandle);
574 FOREACH(it, externals)
576 WidgetExternalLocations::Row row;
577 row.Set_app_id(m_widgetHandle);
578 row.Set_path(DPL::FromUTF8String(*it));
580 DO_INSERT(row, WidgetExternalLocations)
582 transaction.Commit();
584 SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to register external files");
587 void WidgetDAO::unregisterAllExternalLocations()
589 using namespace DPL::DB::ORM;
590 using namespace DPL::DB::ORM::wrt;
591 LogDebug("Deleting external files for widgetHandle: " << m_widgetHandle);
592 WRT_DB_DELETE(del, WidgetExternalLocations, &WrtDatabase::interface());
593 del->Where(Equals<WidgetExternalLocations::app_id>(m_widgetHandle));
599 void WidgetDAO::unregisterWidget(DbWidgetHandle widgetHandle)
601 LogDebug("Unregistering widget from DB. Handle: " << widgetHandle);
602 SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
604 DPL::DB::ORM::wrt::ScopedTransaction transaction(&WrtDatabase::interface());
605 using namespace DPL::DB::ORM;
606 using namespace DPL::DB::ORM::wrt;
608 CHECK_WIDGET_EXISTENCE(transaction, widgetHandle)
610 // Delete from table Widget Info
612 WRT_DB_DELETE(del, WidgetInfo, &WrtDatabase::interface())
613 del->Where(Equals<WidgetInfo::app_id>(widgetHandle));
617 // Deleting in other tables is done via "delete cascade" in SQL
619 transaction.Commit();
621 SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to unregister widget")
624 #undef SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
625 #undef SQL_CONNECTION_EXCEPTION_HANDLER_END
626 #undef CHECK_WIDGET_EXISTENCE