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