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