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