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