[Release] wrt-commons_0.2.139
[framework/web/wrt-commons.git] / modules / widget_dao / dao / widget_dao.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 /**
17  * This file contains the definition of widget dao class.
18  *
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)
25  * @version 1.0
26  * @brief   This file contains the definition of Configuration.
27  */
28 #include <stddef.h>
29 #include <dpl/wrt-dao-rw/widget_dao.h>
30
31 #include <sstream>
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>
38
39 namespace WrtDB {
40 //TODO in current solution in each getter there exists a check
41 //"IsWidgetInstalled". Maybe it should be verified, if it could be done
42 //differently  (check in WidgetDAO constructor)
43
44 #define SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN          Try
45
46 #define SQL_CONNECTION_EXCEPTION_HANDLER_END(message)   \
47     Catch(DPL::DB::SqlConnection::Exception::Base) {       \
48         LogError(message);                              \
49         ReThrowMsg(WidgetDAO::Exception::DatabaseError, \
50                    message);                            \
51     }
52
53 WidgetDAO::WidgetDAO(DPL::OptionalString widgetGUID) :
54     WidgetDAOReadOnly(WidgetDAOReadOnly::getHandle(widgetGUID))
55 {}
56
57 WidgetDAO::WidgetDAO(DPL::String tzAppId) :
58     WidgetDAOReadOnly(WidgetDAOReadOnly::getHandle(tzAppId))
59 {}
60
61 WidgetDAO::WidgetDAO(DbWidgetHandle handle) :
62     WidgetDAOReadOnly(handle)
63 {}
64
65 WidgetDAO::~WidgetDAO()
66 {}
67
68 void WidgetDAO::setTizenAppId(const DPL::OptionalString& tzAppId)
69 {
70     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
71     {
72         using namespace DPL::DB::ORM;
73         wrt::ScopedTransaction transaction(&WrtDatabase::interface());
74
75         if (!isWidgetInstalled(getHandle())) {
76             ThrowMsg(WidgetDAOReadOnly::Exception::WidgetNotExist,
77                      "Cannot find widget. Handle: " << getHandle());
78         }
79
80         wrt::WidgetInfo::Row row;
81         row.Set_tizen_appid(*tzAppId);
82
83         WRT_DB_UPDATE(update, wrt::WidgetInfo, &WrtDatabase::interface())
84         update->Where(
85             Equals<wrt::WidgetInfo::app_id>(getHandle()));
86
87         update->Values(row);
88         update->Execute();
89         transaction.Commit();
90     }
91     SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to register widget")
92 }
93
94 void WidgetDAO::setSecurityPopupUsage(const SettingsType value)
95 {
96     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
97     {
98         using namespace DPL::DB::ORM;
99         using namespace DPL::DB::ORM::wrt;
100
101         ScopedTransaction transaction(&WrtDatabase::interface());
102         if (!isWidgetInstalled(getHandle())) {
103             ThrowMsg(WidgetDAOReadOnly::Exception::WidgetNotExist,
104                      "Cannot find widget. Handle: " << getHandle());
105         }
106
107         WidgetSecuritySettings::Row row;
108         row.Set_security_popup_usage(value);
109
110         WRT_DB_UPDATE(update, WidgetSecuritySettings, &WrtDatabase::interface())
111         update->Where(Equals<WidgetSecuritySettings::app_id>(getHandle()));
112         update->Values(row);
113         update->Execute();
114
115         transaction.Commit();
116     }
117     SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to set security popup usage")
118 }
119
120 void WidgetDAO::setGeolocationUsage(const SettingsType value)
121 {
122     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
123     {
124         using namespace DPL::DB::ORM;
125         using namespace DPL::DB::ORM::wrt;
126
127         ScopedTransaction transaction(&WrtDatabase::interface());
128         if (!isWidgetInstalled(getHandle())) {
129             ThrowMsg(WidgetDAOReadOnly::Exception::WidgetNotExist,
130                      "Cannot find widget. Handle: " << getHandle());
131         }
132
133         WidgetSecuritySettings::Row row;
134         row.Set_geolocation_usage(value);
135
136         WRT_DB_UPDATE(update, WidgetSecuritySettings, &WrtDatabase::interface())
137         update->Where(Equals<WidgetSecuritySettings::app_id>(getHandle()));
138         update->Values(row);
139         update->Execute();
140
141         transaction.Commit();
142     }
143     SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to set geolocation usage")
144 }
145
146 void WidgetDAO::setWebNotificationUsage(const SettingsType value)
147 {
148     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
149     {
150         using namespace DPL::DB::ORM;
151         using namespace DPL::DB::ORM::wrt;
152
153         ScopedTransaction transaction(&WrtDatabase::interface());
154         if (!isWidgetInstalled(getHandle())) {
155             ThrowMsg(WidgetDAOReadOnly::Exception::WidgetNotExist,
156                      "Cannot find widget. Handle: " << getHandle());
157         }
158
159         WidgetSecuritySettings::Row row;
160         row.Set_web_notification_usage(value);
161
162         WRT_DB_UPDATE(update, WidgetSecuritySettings, &WrtDatabase::interface())
163         update->Where(Equals<WidgetSecuritySettings::app_id>(getHandle()));
164         update->Values(row);
165         update->Execute();
166
167         transaction.Commit();
168     }
169     SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to set web notification usage")
170 }
171
172 void WidgetDAO::setWebDatabaseUsage(const SettingsType value)
173 {
174     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
175     {
176         using namespace DPL::DB::ORM;
177         using namespace DPL::DB::ORM::wrt;
178
179         ScopedTransaction transaction(&WrtDatabase::interface());
180         if (!isWidgetInstalled(getHandle())) {
181             ThrowMsg(WidgetDAOReadOnly::Exception::WidgetNotExist,
182                      "Cannot find widget. Handle: " << getHandle());
183         }
184
185         WidgetSecuritySettings::Row row;
186         row.Set_web_database_usage(value);
187
188         WRT_DB_UPDATE(update, WidgetSecuritySettings, &WrtDatabase::interface())
189         update->Where(Equals<WidgetSecuritySettings::app_id>(getHandle()));
190         update->Values(row);
191         update->Execute();
192
193         transaction.Commit();
194     }
195     SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to set web database usage")
196 }
197
198 void WidgetDAO::registerWidget(
199     const TizenAppId & tzAppId,
200     const WidgetRegisterInfo &widgetRegInfo,
201     const IWidgetSecurity &widgetSecurity)
202 {
203     LogDebug("Registering widget");
204     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
205     {
206         DPL::DB::ORM::wrt::ScopedTransaction transaction(
207             &WrtDatabase::interface());
208         registerWidgetInternal(tzAppId, widgetRegInfo, widgetSecurity);
209         transaction.Commit();
210     }
211     SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to register widget")
212 }
213
214 DbWidgetHandle WidgetDAO::registerWidget(
215     const WidgetRegisterInfo &pWidgetRegisterInfo,
216     const IWidgetSecurity &widgetSecurity)
217 {
218     //make it more precise due to very fast tests
219     struct timeval tv;
220     gettimeofday(&tv, NULL);
221     DbWidgetHandle widgetHandle;
222     unsigned int seed = time(NULL);
223     do {
224         widgetHandle = rand_r(&seed);
225     } while (isWidgetInstalled(widgetHandle));
226
227     registerWidget(*pWidgetRegisterInfo.configInfo.tizenAppId,
228                    pWidgetRegisterInfo,
229                    widgetSecurity);
230     return widgetHandle;
231 }
232
233 TizenAppId WidgetDAO::registerWidgetGeneratePkgId(
234     const WidgetRegisterInfo &pWidgetRegisterInfo,
235     const IWidgetSecurity &widgetSecurity)
236 {
237     TizenAppId tzAppId = generatePkgId();
238     registerWidget(tzAppId, pWidgetRegisterInfo, widgetSecurity);
239     return tzAppId;
240 }
241
242 void WidgetDAO::registerWidgetInternal(
243     const TizenAppId & tzAppId,
244     const WidgetRegisterInfo &widgetRegInfo,
245     const IWidgetSecurity &widgetSecurity,
246     const DPL::Optional<DbWidgetHandle> handle)
247 {
248     //Register into WidgetInfo has to be first
249     //as all other tables depend upon that
250     DbWidgetHandle widgetHandle = registerWidgetInfo(tzAppId,
251                                                      widgetRegInfo,
252                                                      widgetSecurity,
253                                                      handle);
254
255     registerWidgetExtendedInfo(widgetHandle, widgetRegInfo);
256
257     registerWidgetLocalizedInfo(widgetHandle, widgetRegInfo);
258
259     registerWidgetIcons(widgetHandle, widgetRegInfo);
260
261     registerWidgetStartFile(widgetHandle, widgetRegInfo);
262
263     PropertyDAO::RegisterProperties(widgetHandle, tzAppId, widgetRegInfo);
264
265     registerWidgetFeatures(widgetHandle, widgetRegInfo);
266
267     registerWidgetPrivilege(widgetHandle, widgetRegInfo);
268
269     registerWidgetWindowModes(widgetHandle, widgetRegInfo);
270
271     registerWidgetWarpInfo(widgetHandle, widgetRegInfo);
272
273     registerWidgetAllowNavigationInfo(widgetHandle, widgetRegInfo);
274
275     registerWidgetCertificates(widgetHandle, widgetSecurity);
276
277     CertificateChainList list;
278     widgetSecurity.getCertificateChainList(list, SIGNATURE_DISTRIBUTOR);
279     registerCertificatesChains(widgetHandle, SIGNATURE_DISTRIBUTOR, list);
280
281     list.clear();
282     widgetSecurity.getCertificateChainList(list, SIGNATURE_AUTHOR);
283     registerCertificatesChains(widgetHandle, SIGNATURE_AUTHOR, list);
284
285     registerWidgetSettings(widgetHandle, widgetRegInfo);
286
287     registerAppControl(widgetHandle, widgetRegInfo);
288
289     registerEncryptedResouceInfo(widgetHandle, widgetRegInfo);
290
291     registerExternalLocations(widgetHandle, widgetRegInfo.externalLocations);
292
293     registerWidgetSecuritySettings(widgetHandle);
294 }
295
296 void WidgetDAO::registerOrUpdateWidget(
297     const TizenAppId & widgetName,
298     const WidgetRegisterInfo &widgetRegInfo,
299     const IWidgetSecurity &widgetSecurity)
300 {
301     LogDebug("Reregistering widget");
302     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
303     {
304         DPL::DB::ORM::wrt::ScopedTransaction transaction(
305             &WrtDatabase::interface());
306
307         unregisterWidgetInternal(widgetName);
308         registerWidgetInternal(widgetName, widgetRegInfo, widgetSecurity);
309         transaction.Commit();
310     }
311     SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to reregister widget")
312 }
313
314 void WidgetDAO::backupAndUpdateWidget(
315     const TizenAppId & oldAppId,
316     const TizenAppId & newAppId,
317     const WidgetRegisterInfo &widgetRegInfo,
318     const IWidgetSecurity &widgetSecurity)
319 {
320     LogDebug("Backup and Register widget");
321     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
322     {
323         DPL::DB::ORM::wrt::ScopedTransaction transaction(
324             &WrtDatabase::interface());
325
326         updateWidgetAppIdInternal(newAppId, oldAppId);
327         registerWidgetInternal(newAppId, widgetRegInfo, widgetSecurity);
328         transaction.Commit();
329     }
330     SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to reregister widget")
331 }
332
333 void WidgetDAO::restoreUpdateWidget(
334     const TizenAppId & oldAppId,
335     const TizenAppId & newAppId)
336 {
337     LogDebug("restore widget");
338     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
339     {
340         DPL::DB::ORM::wrt::ScopedTransaction transaction(
341             &WrtDatabase::interface());
342
343         unregisterWidgetInternal(newAppId);
344         updateWidgetAppIdInternal(oldAppId, newAppId);
345         transaction.Commit();
346     }
347     SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to reregister widget")
348 }
349
350 #define DO_INSERT(row, table)                              \
351     {                                                      \
352         WRT_DB_INSERT(insert, table, &WrtDatabase::interface()) \
353         insert->Values(row);                               \
354         insert->Execute();                                 \
355     }
356
357 void WidgetDAO::registerWidgetExtendedInfo(DbWidgetHandle widgetHandle,
358                                            const WidgetRegisterInfo &regInfo)
359 {
360     //Try and transaction not needed
361     using namespace DPL::DB::ORM;
362     using namespace DPL::DB::ORM::wrt;
363
364     WidgetExtendedInfo::Row row;
365     row.Set_app_id(widgetHandle);
366     //    row.Set_share_href    (DPL::FromUTF8String(regInfo.shareHref));
367     row.Set_signature_type(regInfo.signatureType);
368     row.Set_install_time(regInfo.installedTime);
369     row.Set_splash_img_src(regInfo.configInfo.splashImgSrc);
370     row.Set_background_page(regInfo.configInfo.backgroundPage);
371     row.Set_installed_path(regInfo.widgetInstalledPath);
372
373     DO_INSERT(row, WidgetExtendedInfo)
374 }
375
376 DbWidgetHandle WidgetDAO::registerWidgetInfo(
377     const TizenAppId & tzAppId,
378     const WidgetRegisterInfo &regInfo,
379     const IWidgetSecurity &widgetSecurity,
380     const DPL::Optional<DbWidgetHandle> handle)
381 {
382     using namespace DPL::DB::ORM;
383     using namespace DPL::DB::ORM::wrt;
384     const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
385
386     // TODO in wrt_db all Columns in WidgetInfo have DEFAULT VALUE set.
387     // Because of that, "Optional" is not used there
388
389     WidgetInfo::Row row;
390     if (!!handle) {
391         row.Set_app_id(*handle);
392     }
393
394     row.Set_widget_type(regInfo.webAppType.appType);
395
396     row.Set_widget_id(widgetConfigurationInfo.widget_id);
397     row.Set_defaultlocale(widgetConfigurationInfo.defaultlocale);
398     row.Set_widget_version(widgetConfigurationInfo.version);
399     row.Set_widget_width(widgetConfigurationInfo.width);
400     row.Set_widget_height(widgetConfigurationInfo.height);
401     row.Set_author_name(widgetConfigurationInfo.authorName);
402     row.Set_author_email(widgetConfigurationInfo.authorEmail);
403     row.Set_author_href(widgetConfigurationInfo.authorHref);
404     row.Set_csp_policy(widgetConfigurationInfo.cspPolicy);
405     row.Set_csp_policy_report_only(widgetConfigurationInfo.cspPolicyReportOnly);
406     row.Set_base_folder(DPL::FromUTF8String(regInfo.baseFolder));
407     row.Set_webkit_plugins_required(widgetConfigurationInfo.flashNeeded);
408     row.Set_recognized(widgetSecurity.isRecognized());
409     row.Set_distributor_signed(widgetSecurity.isDistributorSigned());
410     row.Set_tizen_appid(tzAppId);
411     row.Set_tizen_pkgid(regInfo.tzPkgid);
412     {
413         std::stringstream tmp;
414         tmp << regInfo.minVersion;
415         row.Set_min_version(DPL::FromUTF8String(tmp.str()));
416     }
417     row.Set_back_supported(widgetConfigurationInfo.backSupported);
418     row.Set_access_network(widgetConfigurationInfo.accessNetwork);
419     row.Set_pkg_type(regInfo.packagingType.pkgType);
420     row.Set_security_model_version(
421         static_cast<int>(widgetConfigurationInfo.securityModelVersion));
422
423     Try
424     {
425         DO_INSERT(row, WidgetInfo);
426     }
427     Catch(DPL::DB::SqlConnection::Exception::Base)
428     {
429         ReThrowMsg(WidgetDAO::Exception::DatabaseError,
430                    "Failed to register widget info.");
431     }
432
433     if (!handle) {
434         //get autoincremented value of widgetHandle
435         WRT_DB_SELECT(select, WidgetInfo, &WrtDatabase::interface())
436         select->Where(Equals<WidgetInfo::tizen_appid>(tzAppId));
437         return select->GetSingleValue<WidgetInfo::app_id>();
438     } else {
439         return *handle;
440     }
441 }
442
443 void WidgetDAO::registerWidgetLocalizedInfo(DbWidgetHandle widgetHandle,
444                                             const WidgetRegisterInfo &regInfo)
445 {
446     using namespace DPL::DB::ORM;
447     using namespace DPL::DB::ORM::wrt;
448
449     const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
450
451     FOREACH(it, widgetConfigurationInfo.localizedDataSet)
452     {
453         const DPL::String& locale = it->first;
454         const ConfigParserData::LocalizedData& data = it->second;
455
456         LocalizedWidgetInfo::Row row;
457         row.Set_app_id(widgetHandle);
458         row.Set_widget_locale(locale);
459         row.Set_widget_name(data.name);
460         row.Set_widget_shortname(data.shortName);
461         row.Set_widget_description(data.description);
462         row.Set_widget_license(data.license);
463         row.Set_widget_license_file(data.licenseFile);
464         row.Set_widget_license_href(data.licenseHref);
465
466         DO_INSERT(row, LocalizedWidgetInfo)
467     }
468 }
469
470 void WidgetDAO::registerWidgetIcons(DbWidgetHandle widgetHandle,
471                                     const WidgetRegisterInfo &regInfo)
472 {
473     using namespace DPL::DB::ORM;
474     using namespace DPL::DB::ORM::wrt;
475
476     FOREACH(i, regInfo.localizationData.icons)
477     {
478         wrt::WidgetIcon::icon_id::ColumnType icon_id;
479         {
480             wrt::WidgetIcon::Row row;
481             row.Set_app_id(widgetHandle);
482             row.Set_icon_src(i->src);
483             row.Set_icon_width(i->width);
484             row.Set_icon_height(i->height);
485
486             WRT_DB_INSERT(insert, wrt::WidgetIcon, &WrtDatabase::interface())
487             insert->Values(row);
488             icon_id = static_cast<int>(insert->Execute());
489         }
490
491         FOREACH(j, i->availableLocales)
492         {
493             WidgetLocalizedIcon::Row row;
494             row.Set_app_id(widgetHandle);
495             row.Set_icon_id(icon_id);
496             row.Set_widget_locale(*j);
497             DO_INSERT(row, WidgetLocalizedIcon)
498         }
499     }
500 }
501
502 void WidgetDAO::registerWidgetStartFile(DbWidgetHandle widgetHandle,
503                                         const WidgetRegisterInfo &regInfo)
504 {
505     using namespace DPL::DB::ORM;
506     using namespace DPL::DB::ORM::wrt;
507
508     FOREACH(i, regInfo.localizationData.startFiles)
509     {
510         WidgetStartFile::start_file_id::ColumnType startFileID;
511         {
512             WidgetStartFile::Row row;
513             row.Set_app_id(widgetHandle);
514             row.Set_src(i->path);
515
516             WRT_DB_INSERT(insert, WidgetStartFile, &WrtDatabase::interface())
517             insert->Values(row);
518             startFileID = static_cast<int>(insert->Execute());
519         }
520
521         FOREACH(j, i->propertiesForLocales)
522         {
523             WidgetLocalizedStartFile::Row row;
524             row.Set_app_id(widgetHandle);
525             row.Set_start_file_id(startFileID);
526             row.Set_widget_locale(j->first);
527             row.Set_type(j->second.type);
528             row.Set_encoding(j->second.encoding);
529
530             DO_INSERT(row, WidgetLocalizedStartFile)
531         }
532     }
533 }
534
535 void WidgetDAO::registerWidgetFeatures(DbWidgetHandle widgetHandle,
536                                        const WidgetRegisterInfo &regInfo)
537 {
538     using namespace DPL::DB::ORM;
539     const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
540
541     FOREACH(pWidgetFeature, widgetConfigurationInfo.featuresList)
542     {
543         wrt::WidgetFeature::Row widgetFeature;
544         widgetFeature.Set_app_id(widgetHandle);
545         widgetFeature.Set_name(pWidgetFeature->name);
546         widgetFeature.Set_rejected(false);
547
548         DO_INSERT(widgetFeature, wrt::WidgetFeature)
549     }
550 }
551
552 void WidgetDAO::registerWidgetPrivilege(DbWidgetHandle widgetHandle,
553                                         const WidgetRegisterInfo &regInfo)
554 {
555     using namespace DPL::DB::ORM;
556     using namespace DPL::DB::ORM::wrt;
557     const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
558
559     FOREACH(it, widgetConfigurationInfo.privilegeList)
560     {
561         WidgetPrivilege::Row widgetPrivilege;
562         widgetPrivilege.Set_app_id(widgetHandle);
563         widgetPrivilege.Set_name(it->name);
564
565         DO_INSERT(widgetPrivilege, WidgetPrivilege)
566     }
567 }
568
569 void WidgetDAO::updateFeatureRejectStatus(const DbWidgetFeature &widgetFeature)
570 {
571     // This function could be merged with registerWidgetFeature but it requires
572     // desing change:
573     // 1. Check "ace step" in installer must be done before "update database
574     // step"
575     // And:
576     // ConfigurationParserData shouldn't be called "ParserData" any more.
577     using namespace DPL::DB::ORM;
578
579     wrt::ScopedTransaction transaction(&WrtDatabase::interface());
580     WRT_DB_SELECT(select, wrt::WidgetFeature, &WrtDatabase::interface())
581     select->Where(And(Equals<wrt::WidgetFeature::app_id>(m_widgetHandle),
582                       Equals<wrt::WidgetFeature::name>(widgetFeature.name)));
583
584     auto row = select->GetSingleRow();
585     row.Set_rejected(widgetFeature.rejected);
586
587     WRT_DB_UPDATE(update, wrt::WidgetFeature, &WrtDatabase::interface())
588     update->Where(And(Equals<wrt::WidgetFeature::app_id>(m_widgetHandle),
589                       Equals<wrt::WidgetFeature::name>(widgetFeature.name)));
590     update->Values(row);
591     update->Execute();
592     transaction.Commit();
593 }
594
595 void WidgetDAO::registerWidgetWindowModes(DbWidgetHandle widgetHandle,
596                                           const WidgetRegisterInfo &regInfo)
597 {
598     using namespace DPL::DB::ORM;
599     using namespace DPL::DB::ORM::wrt;
600     const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
601
602     FOREACH(i, widgetConfigurationInfo.windowModes)
603     {
604         wrt::WidgetWindowModes::Row windowMode;
605         windowMode.Set_app_id(widgetHandle);
606         windowMode.Set_window_mode(*i);
607
608         DO_INSERT(windowMode, wrt::WidgetWindowModes)
609     }
610 }
611
612 void WidgetDAO::registerWidgetWarpInfo(DbWidgetHandle widgetHandle,
613                                        const WidgetRegisterInfo &regInfo)
614 {
615     using namespace DPL::DB::ORM;
616     using namespace DPL::DB::ORM::wrt;
617     const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
618
619     FOREACH(AccIt, widgetConfigurationInfo.accessInfoSet)
620     {
621         WidgetWARPInfo::Row row;
622         row.Set_app_id(widgetHandle);
623         row.Set_iri(AccIt->m_strIRI);
624         row.Set_subdomain_access(static_cast <int>(
625                                      AccIt->m_bSubDomainAccess));
626
627         DO_INSERT(row, WidgetWARPInfo)
628     }
629 }
630
631 void WidgetDAO::registerWidgetAllowNavigationInfo(DbWidgetHandle widgetHandle,
632                                                   const WidgetRegisterInfo &regInfo)
633 {
634     using namespace DPL::DB::ORM;
635     using namespace DPL::DB::ORM::wrt;
636     const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
637
638     FOREACH(allowNaviIt, widgetConfigurationInfo.allowNavigationInfoList)
639     {
640         WidgetAllowNavigation::Row row;
641         row.Set_app_id(widgetHandle);
642         row.Set_scheme(allowNaviIt->m_scheme);
643         row.Set_host(allowNaviIt->m_host);
644         DO_INSERT(row, WidgetAllowNavigation)
645     }
646 }
647
648 void WidgetDAO::registerWidgetCertificates(DbWidgetHandle widgetHandle,
649                                            const IWidgetSecurity &widgetSecurity)
650 {
651     using namespace DPL::DB::ORM;
652     using namespace DPL::DB::ORM::wrt;
653
654     FOREACH(it, widgetSecurity.getCertificateList())
655     {
656         WidgetCertificateFingerprint::Row row;
657         row.Set_app_id(widgetHandle);
658         row.Set_owner(it->owner);
659         row.Set_chainid(it->chainId);
660         row.Set_type(it->type);
661         row.Set_md5_fingerprint(DPL::FromUTF8String(it->strMD5Fingerprint));
662         row.Set_sha1_fingerprint(DPL::FromUTF8String(it->strSHA1Fingerprint));
663         row.Set_common_name(it->strCommonName);
664
665         DO_INSERT(row, WidgetCertificateFingerprint)
666     }
667 }
668
669 void WidgetDAO::registerCertificatesChains(
670     DbWidgetHandle widgetHandle,
671     CertificateSource certificateSource,
672     const CertificateChainList &
673     certificateChainList)
674 {
675     using namespace DPL::DB::ORM;
676     using namespace DPL::DB::ORM::wrt;
677     FOREACH(certChain, certificateChainList)
678     {
679         WidgetCertificate::Row row;
680         row.Set_app_id(widgetHandle);
681         row.Set_cert_source(certificateSource);
682         row.Set_encoded_chain(DPL::FromASCIIString(*certChain));
683
684         DO_INSERT(row, WidgetCertificate);
685     }
686 }
687
688 void WidgetDAO::registerWidgetSettings(DbWidgetHandle widgetHandle,
689                                        const WidgetRegisterInfo &regInfo)
690 {
691     using namespace DPL::DB::ORM;
692     using namespace DPL::DB::ORM::wrt;
693
694     const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
695
696     FOREACH(pWidgetSetting, widgetConfigurationInfo.settingsList)
697     {
698         SettingsList::Row row;
699         row.Set_appId(widgetHandle);
700         row.Set_settingName(pWidgetSetting->m_name);
701         row.Set_settingValue(pWidgetSetting->m_value);
702
703         DO_INSERT(row, SettingsList)
704     }
705 }
706
707 void WidgetDAO::insertAppControlInfo(DbWidgetHandle handle,
708                                              DPL::String src,
709                                              DPL::String operation,
710                                              DPL::String uri,
711                                              DPL::String mime,
712                                              unsigned index,
713                                              unsigned disposition)
714 {
715     using namespace DPL::DB::ORM;
716     using namespace DPL::DB::ORM::wrt;
717
718     AppControlInfo::Row row;
719
720     row.Set_app_id(handle);
721     row.Set_execute_index(index);
722     row.Set_src(src);
723     row.Set_operation(operation);
724     row.Set_uri(uri);
725     row.Set_mime(mime);
726     row.Set_disposition(disposition);
727
728     DO_INSERT(row, AppControlInfo);
729 }
730
731 void WidgetDAO::registerAppControl(DbWidgetHandle widgetHandle,
732                                    const WidgetRegisterInfo &regInfo)
733 {
734     using namespace DPL::DB::ORM;
735     using namespace DPL::DB::ORM::wrt;
736     const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
737
738     // appControlList
739     FOREACH(appControl_it, widgetConfigurationInfo.appControlList)
740     {
741         DPL::String src       = appControl_it->m_src;
742         DPL::String operation = appControl_it->m_operation;
743         unsigned index        = appControl_it->m_index;
744         unsigned disposition  = appControl_it->m_disposition ==
745             ConfigParserData::AppControlInfo::Disposition::INLINE ? 1 : 0;
746
747         if (!appControl_it->m_uriList.empty())
748         {
749             FOREACH(uri_it, appControl_it->m_uriList)
750             {
751                 DPL::String uri = *uri_it;
752
753                 if (!appControl_it->m_mimeList.empty())
754                 {
755                     FOREACH(mime_it, appControl_it->m_mimeList)
756                     {
757                         DPL::String mime = *mime_it;
758
759                         insertAppControlInfo(widgetHandle, src, operation, uri, mime, index, disposition);
760                     }
761                 }
762                 else
763                 {
764                     DPL::String mime = L"";
765
766                     insertAppControlInfo(widgetHandle, src, operation, uri, mime, index, disposition);
767                 }
768             }
769         }
770         else
771         {
772             DPL::String uri = L"";
773
774             if (!appControl_it->m_mimeList.empty())
775             {
776                 FOREACH(mime_it, appControl_it->m_mimeList)
777                 {
778                     DPL::String mime = *mime_it;
779
780                     insertAppControlInfo(widgetHandle, src, operation, uri, mime, index, disposition);
781                 }
782             }
783             else
784             {
785                 DPL::String mime = L"";
786
787                 insertAppControlInfo(widgetHandle, src, operation, uri, mime, index, disposition);
788             }
789         }
790     }
791 }
792
793 void WidgetDAO::registerEncryptedResouceInfo(DbWidgetHandle widgetHandle,
794                                              const WidgetRegisterInfo &regInfo)
795 {
796     using namespace DPL::DB::ORM;
797     using namespace DPL::DB::ORM::wrt;
798
799     FOREACH(it, regInfo.encryptedFiles)
800     {
801         EncryptedResourceList::Row row;
802         row.Set_app_id(widgetHandle);
803         row.Set_resource(it->fileName);
804         row.Set_size(it->fileSize);
805
806         DO_INSERT(row, EncryptedResourceList)
807     }
808 }
809
810 void WidgetDAO::registerExternalLocations(
811     DbWidgetHandle widgetHandle,
812     const ExternalLocationList &
813     externals)
814 {
815     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
816     {
817         using namespace DPL::DB::ORM;
818         using namespace DPL::DB::ORM::wrt;
819         DPL::DB::ORM::wrt::ScopedTransaction transaction(
820             &WrtDatabase::interface());
821         LogDebug("Inserting external files for widgetHandle: " << widgetHandle);
822         FOREACH(it, externals)
823         {
824             WidgetExternalLocations::Row row;
825             row.Set_app_id(widgetHandle);
826             row.Set_path(DPL::FromUTF8String(*it));
827
828             DO_INSERT(row, WidgetExternalLocations)
829         }
830         transaction.Commit();
831     }
832     SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to register external files");
833 }
834
835 void WidgetDAO::registerWidgetSecuritySettings(DbWidgetHandle widgetHandle)
836 {
837     using namespace DPL::DB::ORM;
838     using namespace DPL::DB::ORM::wrt;
839     WidgetSecuritySettings::Row row;
840     row.Set_app_id(widgetHandle);
841     row.Set_security_popup_usage(SETTINGS_TYPE_ON);
842     row.Set_geolocation_usage(SETTINGS_TYPE_ON);
843     row.Set_web_notification_usage(SETTINGS_TYPE_ON);
844     row.Set_web_database_usage(SETTINGS_TYPE_ON);
845
846     DO_INSERT(row, WidgetSecuritySettings)
847 }
848
849 void WidgetDAO::unregisterAllExternalLocations()
850 {
851     using namespace DPL::DB::ORM;
852     using namespace DPL::DB::ORM::wrt;
853     LogDebug("Deleting external files for widgetHandle: " << m_widgetHandle);
854     WRT_DB_DELETE(del, WidgetExternalLocations, &WrtDatabase::interface());
855     del->Where(Equals<WidgetExternalLocations::app_id>(m_widgetHandle));
856     del->Execute();
857 }
858
859 void WidgetDAO::unregisterWidget(const TizenAppId & tzAppId)
860 {
861     LogDebug("Unregistering widget from DB. tzAppId: " << tzAppId);
862     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
863     {
864         DPL::DB::ORM::wrt::ScopedTransaction transaction(
865             &WrtDatabase::interface());
866         unregisterWidgetInternal(tzAppId);
867         transaction.Commit();
868     }
869     SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to unregister widget")
870 }
871
872 void WidgetDAO::unregisterWidget(WrtDB::DbWidgetHandle handle)
873 {
874     LogDebug("Unregistering widget from DB. Handle: " << handle);
875     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
876     {
877         using namespace DPL::DB::ORM;
878         using namespace DPL::DB::ORM::wrt;
879         ScopedTransaction transaction(&WrtDatabase::interface());
880
881         // Delete from table Widget Info
882         WRT_DB_DELETE(del, WidgetInfo, &WrtDatabase::interface())
883         del->Where(Equals<WidgetInfo::app_id>(handle));
884         del->Execute();
885
886         transaction.Commit();
887     }
888     SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to unregister widget")
889 }
890
891 void WidgetDAO::unregisterWidgetInternal(
892     const TizenAppId & tzAppId)
893 {
894     using namespace DPL::DB::ORM;
895     using namespace DPL::DB::ORM::wrt;
896
897     DbWidgetHandle handle = getHandle(tzAppId);
898
899     // Delete from table Widget Info
900     WRT_DB_DELETE(del, WidgetInfo, &WrtDatabase::interface())
901     del->Where(Equals<WidgetInfo::app_id>(handle));
902     del->Execute();
903
904     // Deleting in other tables is done via "delete cascade" in SQL
905 }
906
907 void WidgetDAO::updateWidgetAppIdInternal(
908     const TizenAppId & fromAppId,
909     const TizenAppId & toAppId)
910 {
911     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
912     {
913         using namespace DPL::DB::ORM;
914         using namespace DPL::DB::ORM::wrt;
915
916         ScopedTransaction transaction(&WrtDatabase::interface());
917         if (!isWidgetInstalled(fromAppId)) {
918             ThrowMsg(WidgetDAOReadOnly::Exception::WidgetNotExist,
919                      "Cannot find widget. tzAppId: " << fromAppId);
920         }
921
922         WRT_DB_SELECT(select, WidgetInfo, &WrtDatabase::interface())
923         select->Where(Equals<WidgetInfo::tizen_appid>(fromAppId));
924
925         WidgetInfo::Row row = select->GetSingleRow();
926
927         //WidgetInfo::Row row;
928         row.Set_tizen_appid(toAppId);
929
930         WRT_DB_UPDATE(update, WidgetInfo, &WrtDatabase::interface())
931         update->Where(Equals<WidgetInfo::tizen_appid>(fromAppId));
932         update->Values(row);
933         update->Execute();
934
935         transaction.Commit();
936     }
937     SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to update appid")
938 }
939
940 #undef DO_INSERT
941
942 #undef SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
943 #undef SQL_CONNECTION_EXCEPTION_HANDLER_END
944 } // namespace WrtDB