[Release] wrt-commons 0.2.115
[platform/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::setFileSystemUsage(const SettingsType value)
199 {
200     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
201     {
202         using namespace DPL::DB::ORM;
203         using namespace DPL::DB::ORM::wrt;
204
205         ScopedTransaction transaction(&WrtDatabase::interface());
206         if (!isWidgetInstalled(getHandle())) {
207             ThrowMsg(WidgetDAOReadOnly::Exception::WidgetNotExist,
208                      "Cannot find widget. Handle: " << getHandle());
209         }
210
211         WidgetSecuritySettings::Row row;
212         row.Set_file_system_usage(value);
213
214         WRT_DB_UPDATE(update, WidgetSecuritySettings, &WrtDatabase::interface())
215         update->Where(Equals<WidgetSecuritySettings::app_id>(getHandle()));
216         update->Values(row);
217         update->Execute();
218
219         transaction.Commit();
220     }
221     SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to set filesystem usage")
222 }
223
224 void WidgetDAO::registerWidget(
225     const TizenAppId & tzAppId,
226     const WidgetRegisterInfo &widgetRegInfo,
227     const IWacSecurity &wacSecurity)
228 {
229     LogDebug("Registering widget");
230     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
231     {
232         DPL::DB::ORM::wrt::ScopedTransaction transaction(
233             &WrtDatabase::interface());
234         registerWidgetInternal(tzAppId, widgetRegInfo, wacSecurity);
235         transaction.Commit();
236     }
237     SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to register widget")
238 }
239
240 DbWidgetHandle WidgetDAO::registerWidget(
241     const WidgetRegisterInfo &pWidgetRegisterInfo,
242     const IWacSecurity &wacSecurity)
243 {
244     //make it more precise due to very fast tests
245     struct timeval tv;
246     gettimeofday(&tv, NULL);
247     DbWidgetHandle widgetHandle;
248     unsigned int seed = time(NULL);
249     do {
250         widgetHandle = rand_r(&seed);
251     } while (isWidgetInstalled(widgetHandle));
252
253     registerWidget(*pWidgetRegisterInfo.configInfo.tizenAppId,
254                    pWidgetRegisterInfo,
255                    wacSecurity);
256     return widgetHandle;
257 }
258
259 TizenAppId WidgetDAO::registerWidgetGeneratePkgId(
260     const WidgetRegisterInfo &pWidgetRegisterInfo,
261     const IWacSecurity &wacSecurity)
262 {
263     TizenAppId tzAppId = generatePkgId();
264     registerWidget(tzAppId, pWidgetRegisterInfo, wacSecurity);
265     return tzAppId;
266 }
267
268 void WidgetDAO::registerWidgetInternal(
269     const TizenAppId & tzAppId,
270     const WidgetRegisterInfo &widgetRegInfo,
271     const IWacSecurity &wacSecurity,
272     const DPL::Optional<DbWidgetHandle> handle)
273 {
274     //Register into WidgetInfo has to be first
275     //as all other tables depend upon that
276     DbWidgetHandle widgetHandle = registerWidgetInfo(tzAppId,
277                                                      widgetRegInfo,
278                                                      wacSecurity,
279                                                      handle);
280
281     registerWidgetExtendedInfo(widgetHandle, widgetRegInfo);
282
283     registerWidgetLocalizedInfo(widgetHandle, widgetRegInfo);
284
285     registerWidgetIcons(widgetHandle, widgetRegInfo);
286
287     registerWidgetStartFile(widgetHandle, widgetRegInfo);
288
289     PropertyDAO::RegisterProperties(tzAppId, widgetRegInfo);
290
291     registerWidgetFeatures(widgetHandle, widgetRegInfo);
292
293     registerWidgetPrivilege(widgetHandle, widgetRegInfo);
294
295     registerWidgetWindowModes(widgetHandle, widgetRegInfo);
296
297     registerWidgetWarpInfo(widgetHandle, widgetRegInfo);
298
299     registerWidgetAllowNavigationInfo(widgetHandle, widgetRegInfo);
300
301     registerWidgetCertificates(widgetHandle, wacSecurity);
302
303     CertificateChainList list;
304     wacSecurity.getCertificateChainList(list, SIGNATURE_DISTRIBUTOR);
305     registerCertificatesChains(widgetHandle, SIGNATURE_DISTRIBUTOR, list);
306
307     list.clear();
308     wacSecurity.getCertificateChainList(list, SIGNATURE_AUTHOR);
309     registerCertificatesChains(widgetHandle, SIGNATURE_AUTHOR, list);
310
311     registerWidgetSettings(widgetHandle, widgetRegInfo);
312
313     registerAppControl(widgetHandle, widgetRegInfo);
314
315     registerEncryptedResouceInfo(widgetHandle, widgetRegInfo);
316
317     registerExternalLocations(widgetHandle, widgetRegInfo.externalLocations);
318
319     registerWidgetSecuritySettings(widgetHandle);
320 }
321
322 void WidgetDAO::registerOrUpdateWidget(
323     const TizenAppId & widgetName,
324     const WidgetRegisterInfo &widgetRegInfo,
325     const IWacSecurity &wacSecurity)
326 {
327     LogDebug("Reregistering widget");
328     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
329     {
330         DPL::DB::ORM::wrt::ScopedTransaction transaction(
331             &WrtDatabase::interface());
332
333         unregisterWidgetInternal(widgetName);
334         registerWidgetInternal(widgetName, widgetRegInfo, wacSecurity);
335         transaction.Commit();
336     }
337     SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to reregister widget")
338 }
339
340 #define DO_INSERT(row, table)                              \
341     {                                                      \
342         WRT_DB_INSERT(insert, table, &WrtDatabase::interface()) \
343         insert->Values(row);                               \
344         insert->Execute();                                 \
345     }
346
347 void WidgetDAO::registerWidgetExtendedInfo(DbWidgetHandle widgetHandle,
348                                            const WidgetRegisterInfo &regInfo)
349 {
350     //Try and transaction not needed
351     using namespace DPL::DB::ORM;
352     using namespace DPL::DB::ORM::wrt;
353
354     WidgetExtendedInfo::Row row;
355     row.Set_app_id(widgetHandle);
356     //    row.Set_share_href    (DPL::FromUTF8String(regInfo.shareHref));
357     row.Set_signature_type(regInfo.signatureType);
358     row.Set_test_widget(regInfo.isTestWidget);
359     row.Set_install_time(regInfo.installedTime);
360     row.Set_splash_img_src(regInfo.configInfo.splashImgSrc);
361     row.Set_background_page(regInfo.configInfo.backgroundPage);
362     row.Set_installed_path(regInfo.widgetInstalledPath);
363
364     DO_INSERT(row, WidgetExtendedInfo)
365 }
366
367 DbWidgetHandle WidgetDAO::registerWidgetInfo(
368     const TizenAppId & tzAppId,
369     const WidgetRegisterInfo &regInfo,
370     const IWacSecurity &wacSecurity,
371     const DPL::Optional<DbWidgetHandle> handle)
372 {
373     using namespace DPL::DB::ORM;
374     using namespace DPL::DB::ORM::wrt;
375     const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
376
377     // TODO in wrt_db all Columns in WidgetInfo have DEFAULT VALUE set.
378     // Because of that, "Optional" is not used there
379
380     WidgetInfo::Row row;
381     if (!!handle) {
382         row.Set_app_id(*handle);
383     }
384
385     row.Set_widget_type(regInfo.webAppType.appType);
386
387     row.Set_widget_id(widgetConfigurationInfo.widget_id);
388     row.Set_defaultlocale(widgetConfigurationInfo.defaultlocale);
389     row.Set_widget_version(widgetConfigurationInfo.version);
390     row.Set_widget_width(widgetConfigurationInfo.width);
391     row.Set_widget_height(widgetConfigurationInfo.height);
392     row.Set_author_name(widgetConfigurationInfo.authorName);
393     row.Set_author_email(widgetConfigurationInfo.authorEmail);
394     row.Set_author_href(widgetConfigurationInfo.authorHref);
395     row.Set_csp_policy(widgetConfigurationInfo.cspPolicy);
396     row.Set_csp_policy_report_only(widgetConfigurationInfo.cspPolicyReportOnly);
397     row.Set_base_folder(DPL::FromUTF8String(regInfo.baseFolder));
398     row.Set_webkit_plugins_required(widgetConfigurationInfo.flashNeeded);
399     row.Set_recognized(wacSecurity.isRecognized());
400     row.Set_wac_signed(wacSecurity.isWacSigned());
401     row.Set_distributor_signed(wacSecurity.isDistributorSigned());
402     row.Set_tizen_appid(tzAppId);
403     row.Set_tizen_pkgid(regInfo.tzPkgid);
404     {
405         std::stringstream tmp;
406         tmp << regInfo.minVersion;
407         row.Set_min_version(DPL::FromUTF8String(tmp.str()));
408     }
409     row.Set_back_supported(widgetConfigurationInfo.backSupported);
410     row.Set_access_network(widgetConfigurationInfo.accessNetwork);
411     row.Set_pkg_type(regInfo.packagingType.pkgType);
412     row.Set_security_model_version(
413         static_cast<int>(widgetConfigurationInfo.securityModelVersion));
414
415     Try
416     {
417         DO_INSERT(row, WidgetInfo);
418     }
419     Catch(DPL::DB::SqlConnection::Exception::Base)
420     {
421         ReThrowMsg(WidgetDAO::Exception::DatabaseError,
422                    "Failed to register widget info.");
423     }
424
425     if (!handle) {
426         //get autoincremented value of widgetHandle
427         WRT_DB_SELECT(select, WidgetInfo, &WrtDatabase::interface())
428         select->Where(Equals<WidgetInfo::tizen_appid>(tzAppId));
429         return select->GetSingleValue<WidgetInfo::app_id>();
430     } else {
431         return *handle;
432     }
433 }
434
435 void WidgetDAO::registerWidgetLocalizedInfo(DbWidgetHandle widgetHandle,
436                                             const WidgetRegisterInfo &regInfo)
437 {
438     using namespace DPL::DB::ORM;
439     using namespace DPL::DB::ORM::wrt;
440
441     const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
442
443     FOREACH(it, widgetConfigurationInfo.localizedDataSet)
444     {
445         const DPL::String& locale = it->first;
446         const ConfigParserData::LocalizedData& data = it->second;
447
448         LocalizedWidgetInfo::Row row;
449         row.Set_app_id(widgetHandle);
450         row.Set_widget_locale(locale);
451         row.Set_widget_name(data.name);
452         row.Set_widget_shortname(data.shortName);
453         row.Set_widget_description(data.description);
454         row.Set_widget_license(data.license);
455         row.Set_widget_license_file(data.licenseFile);
456         row.Set_widget_license_href(data.licenseHref);
457
458         DO_INSERT(row, LocalizedWidgetInfo)
459     }
460 }
461
462 void WidgetDAO::registerWidgetIcons(DbWidgetHandle widgetHandle,
463                                     const WidgetRegisterInfo &regInfo)
464 {
465     using namespace DPL::DB::ORM;
466     using namespace DPL::DB::ORM::wrt;
467
468     FOREACH(i, regInfo.localizationData.icons)
469     {
470         wrt::WidgetIcon::icon_id::ColumnType icon_id;
471         {
472             wrt::WidgetIcon::Row row;
473             row.Set_app_id(widgetHandle);
474             row.Set_icon_src(i->src);
475             row.Set_icon_width(i->width);
476             row.Set_icon_height(i->height);
477
478             WRT_DB_INSERT(insert, wrt::WidgetIcon, &WrtDatabase::interface())
479             insert->Values(row);
480             icon_id = static_cast<int>(insert->Execute());
481         }
482
483         FOREACH(j, i->availableLocales)
484         {
485             WidgetLocalizedIcon::Row row;
486             row.Set_app_id(widgetHandle);
487             row.Set_icon_id(icon_id);
488             row.Set_widget_locale(*j);
489             DO_INSERT(row, WidgetLocalizedIcon)
490         }
491     }
492 }
493
494 void WidgetDAO::registerWidgetStartFile(DbWidgetHandle widgetHandle,
495                                         const WidgetRegisterInfo &regInfo)
496 {
497     using namespace DPL::DB::ORM;
498     using namespace DPL::DB::ORM::wrt;
499
500     FOREACH(i, regInfo.localizationData.startFiles)
501     {
502         WidgetStartFile::start_file_id::ColumnType startFileID;
503         {
504             WidgetStartFile::Row row;
505             row.Set_app_id(widgetHandle);
506             row.Set_src(i->path);
507
508             WRT_DB_INSERT(insert, WidgetStartFile, &WrtDatabase::interface())
509             insert->Values(row);
510             startFileID = static_cast<int>(insert->Execute());
511         }
512
513         FOREACH(j, i->propertiesForLocales)
514         {
515             WidgetLocalizedStartFile::Row row;
516             row.Set_app_id(widgetHandle);
517             row.Set_start_file_id(startFileID);
518             row.Set_widget_locale(j->first);
519             row.Set_type(j->second.type);
520             row.Set_encoding(j->second.encoding);
521
522             DO_INSERT(row, WidgetLocalizedStartFile)
523         }
524     }
525 }
526
527 void WidgetDAO::registerWidgetFeatures(DbWidgetHandle widgetHandle,
528                                        const WidgetRegisterInfo &regInfo)
529 {
530     using namespace DPL::DB::ORM;
531     const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
532
533     FOREACH(pWidgetFeature, widgetConfigurationInfo.featuresList)
534     {
535         wrt::WidgetFeature::Row widgetFeature;
536         widgetFeature.Set_app_id(widgetHandle);
537         widgetFeature.Set_name(pWidgetFeature->name);
538         widgetFeature.Set_rejected(false);
539
540         DO_INSERT(widgetFeature, wrt::WidgetFeature)
541     }
542 }
543
544 void WidgetDAO::registerWidgetPrivilege(DbWidgetHandle widgetHandle,
545                                         const WidgetRegisterInfo &regInfo)
546 {
547     using namespace DPL::DB::ORM;
548     const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
549
550     FOREACH(it, widgetConfigurationInfo.privilegeList)
551     {
552         wrt::WidgetPrivilege::Row widgetPrivilege;
553         widgetPrivilege.Set_app_id(widgetHandle);
554         widgetPrivilege.Set_name(it->name);
555
556         DO_INSERT(widgetPrivilege, wrt::WidgetPrivilege)
557     }
558 }
559
560 void WidgetDAO::updateFeatureRejectStatus(const DbWidgetFeature &widgetFeature)
561 {
562     // This function could be merged with registerWidgetFeature but it requires
563     // desing change:
564     // 1. Check "ace step" in installer must be done before "update database
565     // step"
566     // And:
567     // ConfigurationParserData shouldn't be called "ParserData" any more.
568     using namespace DPL::DB::ORM;
569
570     wrt::ScopedTransaction transaction(&WrtDatabase::interface());
571     WRT_DB_SELECT(select, wrt::WidgetFeature, &WrtDatabase::interface())
572     select->Where(And(Equals<wrt::WidgetFeature::app_id>(m_widgetHandle),
573                       Equals<wrt::WidgetFeature::name>(widgetFeature.name)));
574
575     auto row = select->GetSingleRow();
576     row.Set_rejected(widgetFeature.rejected);
577
578     WRT_DB_UPDATE(update, wrt::WidgetFeature, &WrtDatabase::interface())
579     update->Where(And(Equals<wrt::WidgetFeature::app_id>(m_widgetHandle),
580                       Equals<wrt::WidgetFeature::name>(widgetFeature.name)));
581     update->Values(row);
582     update->Execute();
583     transaction.Commit();
584 }
585
586 void WidgetDAO::registerWidgetWindowModes(DbWidgetHandle widgetHandle,
587                                           const WidgetRegisterInfo &regInfo)
588 {
589     using namespace DPL::DB::ORM;
590     using namespace DPL::DB::ORM::wrt;
591     const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
592
593     FOREACH(i, widgetConfigurationInfo.windowModes)
594     {
595         wrt::WidgetWindowModes::Row windowMode;
596         windowMode.Set_app_id(widgetHandle);
597         windowMode.Set_window_mode(*i);
598
599         DO_INSERT(windowMode, wrt::WidgetWindowModes)
600     }
601 }
602
603 void WidgetDAO::registerWidgetWarpInfo(DbWidgetHandle widgetHandle,
604                                        const WidgetRegisterInfo &regInfo)
605 {
606     using namespace DPL::DB::ORM;
607     using namespace DPL::DB::ORM::wrt;
608     const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
609
610     FOREACH(AccIt, widgetConfigurationInfo.accessInfoSet)
611     {
612         WidgetWARPInfo::Row row;
613         row.Set_app_id(widgetHandle);
614         row.Set_iri(AccIt->m_strIRI);
615         row.Set_subdomain_access(static_cast <int>(
616                                      AccIt->m_bSubDomainAccess));
617
618         DO_INSERT(row, WidgetWARPInfo)
619     }
620 }
621
622 void WidgetDAO::registerWidgetAllowNavigationInfo(DbWidgetHandle widgetHandle,
623                                                   const WidgetRegisterInfo &regInfo)
624 {
625     using namespace DPL::DB::ORM;
626     using namespace DPL::DB::ORM::wrt;
627     const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
628
629     FOREACH(allowNaviIt, widgetConfigurationInfo.allowNavigationInfoList)
630     {
631         WidgetAllowNavigation::Row row;
632         row.Set_app_id(widgetHandle);
633         row.Set_scheme(allowNaviIt->m_scheme);
634         row.Set_host(allowNaviIt->m_host);
635         DO_INSERT(row, WidgetAllowNavigation)
636     }
637 }
638
639 void WidgetDAO::registerWidgetCertificates(DbWidgetHandle widgetHandle,
640                                            const IWacSecurity &wacSecurity)
641 {
642     using namespace DPL::DB::ORM;
643     using namespace DPL::DB::ORM::wrt;
644
645     FOREACH(it, wacSecurity.getCertificateList())
646     {
647         WidgetCertificateFingerprint::Row row;
648         row.Set_app_id(widgetHandle);
649         row.Set_owner(it->owner);
650         row.Set_chainid(it->chainId);
651         row.Set_type(it->type);
652         row.Set_md5_fingerprint(DPL::FromUTF8String(it->strMD5Fingerprint));
653         row.Set_sha1_fingerprint(DPL::FromUTF8String(it->strSHA1Fingerprint));
654         row.Set_common_name(it->strCommonName);
655
656         DO_INSERT(row, WidgetCertificateFingerprint)
657     }
658 }
659
660 void WidgetDAO::registerCertificatesChains(
661     DbWidgetHandle widgetHandle,
662     CertificateSource certificateSource,
663     const CertificateChainList &
664     certificateChainList)
665 {
666     using namespace DPL::DB::ORM;
667     using namespace DPL::DB::ORM::wrt;
668     FOREACH(certChain, certificateChainList)
669     {
670         WidgetCertificate::Row row;
671         row.Set_app_id(widgetHandle);
672         row.Set_cert_source(certificateSource);
673         row.Set_encoded_chain(DPL::FromASCIIString(*certChain));
674
675         DO_INSERT(row, WidgetCertificate);
676     }
677 }
678
679 void WidgetDAO::registerWidgetSettings(DbWidgetHandle widgetHandle,
680                                        const WidgetRegisterInfo &regInfo)
681 {
682     using namespace DPL::DB::ORM;
683     using namespace DPL::DB::ORM::wrt;
684
685     const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
686
687     FOREACH(pWidgetSetting, widgetConfigurationInfo.settingsList)
688     {
689         SettingsList::Row row;
690         row.Set_appId(widgetHandle);
691         row.Set_settingName(pWidgetSetting->m_name);
692         row.Set_settingValue(pWidgetSetting->m_value);
693
694         DO_INSERT(row, SettingsList)
695     }
696 }
697
698 void WidgetDAO::insertAppControlInfo(DbWidgetHandle handle,
699                                              DPL::String src,
700                                              DPL::String operation,
701                                              DPL::String uri,
702                                              DPL::String mime,
703                                              unsigned index,
704                                              unsigned disposition)
705 {
706     using namespace DPL::DB::ORM;
707     using namespace DPL::DB::ORM::wrt;
708
709     AppControlInfo::Row row;
710
711     row.Set_app_id(handle);
712     row.Set_execute_index(index);
713     row.Set_src(src);
714     row.Set_operation(operation);
715     row.Set_uri(uri);
716     row.Set_mime(mime);
717     row.Set_disposition(disposition);
718
719     DO_INSERT(row, AppControlInfo);
720 }
721
722 void WidgetDAO::registerAppControl(DbWidgetHandle widgetHandle,
723                                    const WidgetRegisterInfo &regInfo)
724 {
725     using namespace DPL::DB::ORM;
726     using namespace DPL::DB::ORM::wrt;
727     const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
728
729     // appControlList
730     FOREACH(appControl_it, widgetConfigurationInfo.appControlList)
731     {
732         DPL::String src       = appControl_it->m_src;
733         DPL::String operation = appControl_it->m_operation;
734         unsigned index        = appControl_it->m_index;
735         unsigned disposition  = appControl_it->m_disposition ==
736             ConfigParserData::AppControlInfo::Disposition::INLINE ? 1 : 0;
737
738         if (!appControl_it->m_uriList.empty())
739         {
740             FOREACH(uri_it, appControl_it->m_uriList)
741             {
742                 DPL::String uri = *uri_it;
743
744                 if (!appControl_it->m_mimeList.empty())
745                 {
746                     FOREACH(mime_it, appControl_it->m_mimeList)
747                     {
748                         DPL::String mime = *mime_it;
749
750                         insertAppControlInfo(widgetHandle, src, operation, uri, mime, index, disposition);
751                     }
752                 }
753                 else
754                 {
755                     DPL::String mime = L"";
756
757                     insertAppControlInfo(widgetHandle, src, operation, uri, mime, index, disposition);
758                 }
759             }
760         }
761         else
762         {
763             DPL::String uri = L"";
764
765             if (!appControl_it->m_mimeList.empty())
766             {
767                 FOREACH(mime_it, appControl_it->m_mimeList)
768                 {
769                     DPL::String mime = *mime_it;
770
771                     insertAppControlInfo(widgetHandle, src, operation, uri, mime, index, disposition);
772                 }
773             }
774             else
775             {
776                 DPL::String mime = L"";
777
778                 insertAppControlInfo(widgetHandle, src, operation, uri, mime, index, disposition);
779             }
780         }
781     }
782 }
783
784 void WidgetDAO::registerEncryptedResouceInfo(DbWidgetHandle widgetHandle,
785                                              const WidgetRegisterInfo &regInfo)
786 {
787     using namespace DPL::DB::ORM;
788     using namespace DPL::DB::ORM::wrt;
789
790     FOREACH(it, regInfo.encryptedFiles)
791     {
792         EncryptedResourceList::Row row;
793         row.Set_app_id(widgetHandle);
794         row.Set_resource(it->fileName);
795         row.Set_size(it->fileSize);
796
797         DO_INSERT(row, EncryptedResourceList)
798     }
799 }
800
801 void WidgetDAO::registerExternalLocations(
802     DbWidgetHandle widgetHandle,
803     const ExternalLocationList &
804     externals)
805 {
806     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
807     {
808         using namespace DPL::DB::ORM;
809         using namespace DPL::DB::ORM::wrt;
810         DPL::DB::ORM::wrt::ScopedTransaction transaction(
811             &WrtDatabase::interface());
812         LogDebug("Inserting external files for widgetHandle: " << widgetHandle);
813         FOREACH(it, externals)
814         {
815             WidgetExternalLocations::Row row;
816             row.Set_app_id(widgetHandle);
817             row.Set_path(DPL::FromUTF8String(*it));
818
819             DO_INSERT(row, WidgetExternalLocations)
820         }
821         transaction.Commit();
822     }
823     SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to register external files");
824 }
825
826 void WidgetDAO::registerWidgetSecuritySettings(DbWidgetHandle widgetHandle)
827 {
828     using namespace DPL::DB::ORM;
829     using namespace DPL::DB::ORM::wrt;
830     WidgetSecuritySettings::Row row;
831     row.Set_app_id(widgetHandle);
832     row.Set_security_popup_usage(SETTINGS_TYPE_ON);
833     row.Set_geolocation_usage(SETTINGS_TYPE_ON);
834     row.Set_web_notification_usage(SETTINGS_TYPE_ON);
835     row.Set_web_database_usage(SETTINGS_TYPE_ON);
836     row.Set_file_system_usage(SETTINGS_TYPE_ON);
837
838     DO_INSERT(row, WidgetSecuritySettings)
839 }
840
841 void WidgetDAO::unregisterAllExternalLocations()
842 {
843     using namespace DPL::DB::ORM;
844     using namespace DPL::DB::ORM::wrt;
845     LogDebug("Deleting external files for widgetHandle: " << m_widgetHandle);
846     WRT_DB_DELETE(del, WidgetExternalLocations, &WrtDatabase::interface());
847     del->Where(Equals<WidgetExternalLocations::app_id>(m_widgetHandle));
848     del->Execute();
849 }
850
851 void WidgetDAO::unregisterWidget(const TizenAppId & tzAppId)
852 {
853     LogDebug("Unregistering widget from DB. tzAppId: " << tzAppId);
854     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
855     {
856         DPL::DB::ORM::wrt::ScopedTransaction transaction(
857             &WrtDatabase::interface());
858         unregisterWidgetInternal(tzAppId);
859         transaction.Commit();
860     }
861     SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to unregister widget")
862 }
863
864 void WidgetDAO::unregisterWidget(WrtDB::DbWidgetHandle handle)
865 {
866     LogDebug("Unregistering widget from DB. Handle: " << handle);
867     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
868     {
869         using namespace DPL::DB::ORM;
870         using namespace DPL::DB::ORM::wrt;
871         ScopedTransaction transaction(&WrtDatabase::interface());
872
873         // Delete from table Widget Info
874         WRT_DB_DELETE(del, WidgetInfo, &WrtDatabase::interface())
875         del->Where(Equals<WidgetInfo::app_id>(handle));
876         del->Execute();
877
878         transaction.Commit();
879     }
880     SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to unregister widget")
881 }
882
883 void WidgetDAO::unregisterWidgetInternal(
884     const TizenAppId & tzAppId)
885 {
886     using namespace DPL::DB::ORM;
887     using namespace DPL::DB::ORM::wrt;
888
889     DbWidgetHandle handle = getHandle(tzAppId);
890
891     // Delete from table Widget Info
892     WRT_DB_DELETE(del, WidgetInfo, &WrtDatabase::interface())
893     del->Where(Equals<WidgetInfo::app_id>(handle));
894     del->Execute();
895
896     // Deleting in other tables is done via "delete cascade" in SQL
897 }
898
899 #undef DO_INSERT
900
901 #undef SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
902 #undef SQL_CONNECTION_EXCEPTION_HANDLER_END
903 } // namespace WrtDB