Update wrt-commons_0.2.54
[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
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 #define CHECK_WIDGET_EXISTENCE(macro_transaction, macro_handle)          \
55     if (!WidgetDAO::isWidgetInstalled(macro_handle))                      \
56     {                                                                    \
57         macro_transaction.Commit();                                      \
58         LogWarning("Cannot find widget. Handle: " << macro_handle);      \
59         ThrowMsg(WidgetDAO::Exception::WidgetNotExist,                   \
60                  "Cannot find widget. Handle: " << macro_handle);        \
61     }
62
63
64 WidgetDAO::WidgetDAO(DbWidgetHandle widgetHandle) :
65     WidgetDAOReadOnly(widgetHandle)
66 {
67 }
68
69 WidgetDAO::WidgetDAO(DPL::OptionalString widgetGUID) :
70     WidgetDAOReadOnly(WidgetDAOReadOnly::getHandle(widgetGUID))
71 {
72 }
73
74 WidgetDAO::~WidgetDAO()
75 {
76 }
77
78 void WidgetDAO::removeProperty(
79         const PropertyDAOReadOnly::WidgetPropertyKey &key)
80 {
81     Try {
82         PropertyDAO::RemoveProperty(m_widgetHandle, key);
83     }
84     Catch(PropertyDAOReadOnly::Exception::ReadOnlyProperty){
85         ReThrowMsg(WidgetDAO::Exception::DatabaseError,
86                    "Failure during removing property");
87     }
88 }
89
90 void WidgetDAO::setProperty(
91         const PropertyDAOReadOnly::WidgetPropertyKey &key,
92         const PropertyDAOReadOnly::WidgetPropertyValue &value,
93         bool readOnly)
94 {
95     Try {
96         PropertyDAO::SetProperty(m_widgetHandle, key, value, readOnly);
97     }
98     Catch(PropertyDAOReadOnly::Exception::ReadOnlyProperty){
99         ReThrowMsg(WidgetDAO::Exception::DatabaseError,
100                    "Failure during setting/updating property");
101     }
102 }
103
104 void WidgetDAO::setPkgName(const DPL::OptionalString& pkgName)
105 {
106     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
107     {
108         using namespace DPL::DB::ORM;
109         wrt::ScopedTransaction transaction(&WrtDatabase::interface());
110
111         isWidgetInstalled(getHandle());
112
113         wrt::WidgetInfo::Row row;
114         row.Set_pkgname(pkgName);
115
116         WRT_DB_UPDATE(update, wrt::WidgetInfo, &WrtDatabase::interface())
117         update->Where(
118             Equals<wrt::WidgetInfo::app_id>(getHandle()));
119
120         update->Values(row);
121         update->Execute();
122         transaction.Commit();
123     }
124     SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to register widget")
125 }
126
127 void WidgetDAO::registerWidget(
128         const DbWidgetHandle& widgetHandle,
129         const WidgetRegisterInfo &widgetRegInfo,
130         const IWacSecurity &wacSecurity)
131 {
132     LogDebug("Registering widget");
133     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
134     {
135         DPL::DB::ORM::wrt::ScopedTransaction transaction(&WrtDatabase::interface());
136
137         //Register into WidgetInfo has to be first
138         //as all other tables depend upon that
139         registerWidgetInfo(widgetHandle, widgetRegInfo, wacSecurity);
140
141         registerWidgetExtendedInfo(widgetHandle, widgetRegInfo);
142
143         registerWidgetLocalizedInfo(widgetHandle, widgetRegInfo);
144
145         registerWidgetIcons(widgetHandle, widgetRegInfo);
146
147         registerWidgetStartFile(widgetHandle, widgetRegInfo);
148
149         PropertyDAO::RegisterProperties(widgetHandle, widgetRegInfo);
150
151         registerWidgetFeatures(widgetHandle, widgetRegInfo);
152
153         registerWidgetWindowModes(widgetHandle, widgetRegInfo);
154
155         registerWidgetWarpInfo(widgetHandle, widgetRegInfo);
156
157         registerWidgetCertificates(widgetHandle, wacSecurity);
158
159         CertificateChainList list;
160         wacSecurity.getCertificateChainList(list);
161         registerLaunchCertificates(widgetHandle,list);
162
163         registerWidgetSettings(widgetHandle, widgetRegInfo);
164
165         registerAppService(widgetHandle, widgetRegInfo);
166
167         registerEncryptedResouceInfo(widgetHandle, widgetRegInfo);
168
169         transaction.Commit();
170     }
171     SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to register widget")
172 }
173
174 #define DO_INSERT(row, table)                              \
175     {                                                      \
176         WRT_DB_INSERT(insert, table, &WrtDatabase::interface()) \
177         insert->Values(row);                               \
178         insert->Execute();                                 \
179     }
180
181 void WidgetDAO::registerWidgetExtendedInfo(DbWidgetHandle widgetHandle,
182         const WidgetRegisterInfo &regInfo)
183 {
184     //Try and transaction not needed
185     using namespace DPL::DB::ORM;
186     using namespace DPL::DB::ORM::wrt;
187
188
189     WidgetExtendedInfo::Row row;
190     row.Set_app_id(widgetHandle);
191     //    row.Set_share_href    (DPL::FromUTF8String(regInfo.shareHref));
192     row.Set_signature_type(regInfo.signatureType);
193     row.Set_factory_widget(regInfo.isFactoryWidget);
194     row.Set_test_widget(regInfo.isTestWidget);
195     row.Set_install_time(regInfo.installedTime);
196     row.Set_splash_img_src(regInfo.configInfo.splashImgSrc);
197
198
199     DO_INSERT(row, WidgetExtendedInfo)
200 }
201
202 void WidgetDAO::registerWidgetInfo(
203         const DbWidgetHandle& widgetHandle,
204         const WidgetRegisterInfo &regInfo,
205         const IWacSecurity &wacSecurity)
206 {
207     using namespace DPL::DB::ORM;
208     using namespace DPL::DB::ORM::wrt;
209     const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
210
211     // TODO in wrt_db all Columns in WidgetInfo have DEFAULT VALUE set.
212     // Because of that, "Optional" is not used there
213
214     WidgetInfo::Row row;
215     row.Set_app_id(widgetHandle);
216     row.Set_widget_type(regInfo.type.appType);
217     row.Set_widget_id(widgetConfigurationInfo.widget_id);
218     row.Set_defaultlocale(widgetConfigurationInfo.defaultlocale);
219     row.Set_widget_version(widgetConfigurationInfo.version);
220     row.Set_widget_width(widgetConfigurationInfo.width);
221     row.Set_widget_height(widgetConfigurationInfo.height);
222     row.Set_author_name(widgetConfigurationInfo.authorName);
223     row.Set_author_email(widgetConfigurationInfo.authorEmail);
224     row.Set_author_href(widgetConfigurationInfo.authorHref);
225     row.Set_base_folder(DPL::FromUTF8String(regInfo.baseFolder));
226     row.Set_webkit_plugins_required(widgetConfigurationInfo.flashNeeded);
227     row.Set_recognized(wacSecurity.isRecognized());
228     row.Set_wac_signed(wacSecurity.isWacSigned());
229     row.Set_distributor_signed(wacSecurity.isDistributorSigned());
230     {
231         std::stringstream tmp;
232         tmp << regInfo.minVersion;
233         row.Set_min_version(DPL::FromUTF8String(tmp.str()));
234     }
235     row.Set_back_supported(widgetConfigurationInfo.backSupported);
236     row.Set_access_network(widgetConfigurationInfo.accessNetwork);
237     row.Set_pkgname(regInfo.pkgname);
238     row.Set_pkg_type(regInfo.pType.pkgType);
239
240     Try
241     {
242         DO_INSERT(row, WidgetInfo);
243     }
244     Catch(DPL::DB::SqlConnection::Exception::Base)
245     {
246         ReThrowMsg(WidgetDAO::Exception::DatabaseError,
247                    "Failed to register widget info.");
248     }
249 }
250
251 void WidgetDAO::registerWidgetLocalizedInfo(DbWidgetHandle widgetHandle,
252         const WidgetRegisterInfo &regInfo)
253 {
254     using namespace DPL::DB::ORM;
255     using namespace DPL::DB::ORM::wrt;
256
257     const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
258
259     FOREACH(it, widgetConfigurationInfo.localizedDataSet)
260     {
261         const DPL::String& locale = it->first;
262         const ConfigParserData::LocalizedData& data = it->second;
263
264         LocalizedWidgetInfo::Row row;
265         row.Set_app_id(widgetHandle);
266         row.Set_widget_locale(locale);
267         row.Set_widget_name(data.name);
268         row.Set_widget_shortname(data.shortName);
269         row.Set_widget_description(data.description);
270         row.Set_widget_license(data.license);
271         row.Set_widget_license_file(data.licenseFile);
272         row.Set_widget_license_href(data.licenseHref);
273
274         DO_INSERT(row, LocalizedWidgetInfo)
275     }
276 }
277
278 void WidgetDAO::registerWidgetIcons(DbWidgetHandle widgetHandle,
279         const WidgetRegisterInfo &regInfo)
280 {
281     using namespace DPL::DB::ORM;
282     using namespace DPL::DB::ORM::wrt;
283
284
285     FOREACH(i, regInfo.localizationData.icons)
286     {
287         wrt::WidgetIcon::icon_id::ColumnType icon_id;
288         {
289             wrt::WidgetIcon::Row row;
290             row.Set_app_id(widgetHandle);
291             row.Set_icon_src(i->src);
292             row.Set_icon_width(i->width);
293             row.Set_icon_height(i->height);
294
295             WRT_DB_INSERT(insert, wrt::WidgetIcon, &WrtDatabase::interface())
296             insert->Values(row);
297             icon_id = insert->Execute();
298         }
299
300         FOREACH(j, i->availableLocales)
301         {
302             WidgetLocalizedIcon::Row row;
303             row.Set_app_id(widgetHandle);
304             row.Set_icon_id(icon_id);
305             row.Set_widget_locale(*j);
306             WRT_DB_SELECT(select, WidgetLocalizedIcon, &WrtDatabase::interface())
307             select->Where(And(Equals<WidgetLocalizedIcon::app_id>(widgetHandle),
308                               Equals<WidgetLocalizedIcon::widget_locale>(*j)));
309             WidgetLocalizedIcon::Select::RowList rows = select->GetRowList();
310
311             bool flag = !rows.empty();
312
313             if(flag == true)
314             {
315                 // already default icon value of same locale exists
316                 WRT_DB_UPDATE(update, WidgetLocalizedIcon, &WrtDatabase::interface())
317                 update->Where(And(Equals<WidgetLocalizedIcon::app_id>(widgetHandle),
318                                   Equals<WidgetLocalizedIcon::widget_locale>(*j)));
319                 update->Values(row);
320                 update->Execute();
321             }else{
322                 // any icon value of same locale doesn't exist
323                 DO_INSERT(row, WidgetLocalizedIcon)
324             }
325         }
326     }
327 }
328
329 void WidgetDAO::registerWidgetStartFile(DbWidgetHandle widgetHandle,
330         const WidgetRegisterInfo &regInfo)
331 {
332     using namespace DPL::DB::ORM;
333     using namespace DPL::DB::ORM::wrt;
334
335
336     FOREACH(i, regInfo.localizationData.startFiles)
337     {
338         WidgetStartFile::start_file_id::ColumnType startFileID;
339         {
340             WidgetStartFile::Row row;
341             row.Set_app_id(widgetHandle);
342             row.Set_src(i->path);
343
344             WRT_DB_INSERT(insert, WidgetStartFile, &WrtDatabase::interface())
345             insert->Values(row);
346             startFileID = insert->Execute();
347         }
348
349         FOREACH(j, i->propertiesForLocales)
350         {
351             WidgetLocalizedStartFile::Row row;
352             row.Set_app_id(widgetHandle);
353             row.Set_start_file_id(startFileID);
354             row.Set_widget_locale(j->first);
355             row.Set_type(j->second.type);
356             row.Set_encoding(j->second.encoding);
357
358             DO_INSERT(row, WidgetLocalizedStartFile)
359         }
360     }
361 }
362
363 void WidgetDAO::registerWidgetFeatures(DbWidgetHandle widgetHandle,
364         const WidgetRegisterInfo &regInfo)
365 {
366     using namespace DPL::DB::ORM;
367     const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
368
369
370     FOREACH(pWidgetFeature, widgetConfigurationInfo.featuresList)
371     {
372         wrt::WidgetFeature::Row widgetFeature;
373         widgetFeature.Set_app_id(widgetHandle);
374         widgetFeature.Set_name(pWidgetFeature->name);
375         widgetFeature.Set_required(pWidgetFeature->required);
376         widgetFeature.Set_rejected(false);
377
378         wrt::WidgetFeature::widget_feature_id::ColumnType widgetFeatureID;
379         {
380             WRT_DB_INSERT(insert, wrt::WidgetFeature, &WrtDatabase::interface())
381             insert->Values(widgetFeature);
382             widgetFeatureID = insert->Execute();
383         }
384
385         // Insert into table FeatureParam
386         wrt::FeatureParam::Row featureParam;
387         featureParam.Set_widget_feature_id(widgetFeatureID);
388
389         ConfigParserData::ParamsList::const_iterator iter;
390
391         FOREACH(iter, pWidgetFeature->paramsList)
392         {
393             featureParam.Set_name(iter->name);
394             featureParam.Set_value(iter->value);
395
396             DO_INSERT(featureParam, wrt::FeatureParam)
397         }
398     }
399 }
400
401 void WidgetDAO::updateFeatureRejectStatus(const DbWidgetFeature &widgetFeature){
402     // This function could be merged with registerWidgetFeature but it requires desing change:
403     // 1. Check "ace step" in installer must be done before "update database step"
404     // And:
405     // ConfigurationParserData shouldn't be called "ParserData" any more.
406     using namespace DPL::DB::ORM;
407
408     wrt::ScopedTransaction transaction(&WrtDatabase::interface());
409     WRT_DB_SELECT(select, wrt::WidgetFeature, &WrtDatabase::interface())
410     select->Where(And(Equals<wrt::WidgetFeature::app_id>(m_widgetHandle),
411                       Equals<wrt::WidgetFeature::name>(widgetFeature.name)));
412
413     auto row = select->GetSingleRow();
414     row.Set_rejected(widgetFeature.rejected);
415
416     WRT_DB_UPDATE(update, wrt::WidgetFeature, &WrtDatabase::interface())
417     update->Where(And(Equals<wrt::WidgetFeature::app_id>(m_widgetHandle),
418                       Equals<wrt::WidgetFeature::name>(widgetFeature.name)));
419     update->Values(row);
420     update->Execute();
421     transaction.Commit();
422 }
423
424 void WidgetDAO::registerWidgetWindowModes(DbWidgetHandle widgetHandle,
425         const WidgetRegisterInfo &regInfo)
426 {
427     using namespace DPL::DB::ORM;
428     using namespace DPL::DB::ORM::wrt;
429     const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
430
431
432     FOREACH(i, widgetConfigurationInfo.windowModes)
433     {
434         wrt::WidgetWindowModes::Row windowMode;
435         windowMode.Set_app_id(widgetHandle);
436         windowMode.Set_window_mode(*i);
437
438         DO_INSERT(windowMode, wrt::WidgetWindowModes)
439     }
440 }
441
442 void WidgetDAO::registerWidgetWarpInfo(DbWidgetHandle widgetHandle,
443         const WidgetRegisterInfo &regInfo)
444 {
445     using namespace DPL::DB::ORM;
446     using namespace DPL::DB::ORM::wrt;
447     const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
448
449
450     FOREACH(AccIt, widgetConfigurationInfo.accessInfoSet)
451     {
452         WidgetWARPInfo::Row row;
453         row.Set_app_id(widgetHandle);
454         row.Set_iri(AccIt->m_strIRI);
455         row.Set_subdomain_access(static_cast <int>(
456                                      AccIt->m_bSubDomainAccess));
457
458         DO_INSERT(row, WidgetWARPInfo)
459     }
460 }
461
462 void WidgetDAO::registerWidgetCertificates(DbWidgetHandle widgetHandle,
463         const IWacSecurity &wacSecurity)
464 {
465     using namespace DPL::DB::ORM;
466     using namespace DPL::DB::ORM::wrt;
467
468
469     FOREACH(it, wacSecurity.getCertificateList())
470     {
471         WidgetCertificateFingerprint::Row row;
472         row.Set_app_id(widgetHandle);
473         row.Set_owner(it->owner);
474         row.Set_chainid(it->chainId);
475         row.Set_type(it->type);
476         row.Set_md5_fingerprint(DPL::FromUTF8String(it->strMD5Fingerprint));
477         row.Set_sha1_fingerprint(DPL::FromUTF8String(it->strSHA1Fingerprint));
478         row.Set_common_name(it->strCommonName);
479
480         DO_INSERT(row, WidgetCertificateFingerprint)
481     }
482 }
483
484 void WidgetDAO::registerLaunchCertificates(DbWidgetHandle widgetHandle,
485         const CertificateChainList &certificateChainList)
486 {
487     using namespace DPL::DB::ORM;
488     using namespace DPL::DB::ORM::wrt;
489     FOREACH(certChain, certificateChainList)
490     {
491         WidgetCertificate::Row row;
492         row.Set_app_id(widgetHandle);
493         row.Set_encoded_chain(DPL::FromASCIIString(*certChain));
494
495         DO_INSERT(row, WidgetCertificate);
496     }
497 }
498
499 void WidgetDAO::registerWidgetSettings(DbWidgetHandle widgetHandle,
500         const WidgetRegisterInfo &regInfo)
501 {
502     using namespace DPL::DB::ORM;
503     using namespace DPL::DB::ORM::wrt;
504
505     const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
506
507     FOREACH(pWidgetSetting, widgetConfigurationInfo.settingsList)
508     {
509         SettginsList::Row row;
510         row.Set_appId(widgetHandle);
511         row.Set_settingName(pWidgetSetting->m_name);
512         row.Set_settingValue(pWidgetSetting->m_value);
513
514         DO_INSERT(row, SettginsList)
515     }
516 }
517
518 void WidgetDAO::registerAppService(DbWidgetHandle widgetHandle,
519                                    const WidgetRegisterInfo &regInfo)
520 {
521     using namespace DPL::DB::ORM;
522     using namespace DPL::DB::ORM::wrt;
523     const ConfigParserData& widgetConfigurationInfo = regInfo.configInfo;
524
525     FOREACH(ASIt, widgetConfigurationInfo.appServiceList)
526     {
527         ApplicationServiceInfo::Row row;
528         row.Set_app_id(widgetHandle);
529         row.Set_src(ASIt->m_src);
530         row.Set_operation(ASIt->m_operation);
531         row.Set_scheme(ASIt->m_scheme);
532         row.Set_mime(ASIt->m_mime);
533
534         DO_INSERT(row, ApplicationServiceInfo)
535     }
536 }
537
538 void WidgetDAO::registerEncryptedResouceInfo(DbWidgetHandle widgetHandle,
539         const WidgetRegisterInfo &regInfo)
540 {
541     using namespace DPL::DB::ORM;
542     using namespace DPL::DB::ORM::wrt;
543
544     FOREACH(it, regInfo.encryptedFiles)
545     {
546         EncryptedResourceList::Row row;
547         row.Set_app_id(widgetHandle);
548         row.Set_resource(it->fileName);
549         row.Set_size(it->fileSize);
550
551         DO_INSERT(row, EncryptedResourceList)
552     }
553 }
554
555 #undef DO_INSERT
556
557 void WidgetDAO::unregisterWidget(DbWidgetHandle widgetHandle)
558 {
559     LogDebug("Unregistering widget from DB. Handle: " << widgetHandle);
560     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
561     {
562         DPL::DB::ORM::wrt::ScopedTransaction transaction(&WrtDatabase::interface());
563         using namespace DPL::DB::ORM;
564         using namespace DPL::DB::ORM::wrt;
565
566         CHECK_WIDGET_EXISTENCE(transaction, widgetHandle)
567
568         // Delete from table Widget Info
569         {
570             WRT_DB_DELETE(del, WidgetInfo, &WrtDatabase::interface())
571             del->Where(Equals<WidgetInfo::app_id>(widgetHandle));
572             del->Execute();
573         }
574
575         // Deleting in other tables is done via "delete cascade" in SQL
576
577         transaction.Commit();
578     }
579     SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to unregister widget")
580 }
581
582 #undef SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
583 #undef SQL_CONNECTION_EXCEPTION_HANDLER_END
584 #undef CHECK_WIDGET_EXISTENCE
585
586 } // namespace WrtDB