2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @file AceDAOReadOnlyReadOnly.cpp
20 * @author Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
21 * @author Grzegorz Krawczyk (g.krawczyk@samsung.com)
29 #include <ace-dao-ro/AceDAOReadOnly.h>
30 #include <ace-dao-ro/AceDAOUtilities.h>
31 #include <ace-dao-ro/AceDAOConversions.h>
32 #include <ace-dao-ro/AceDatabase.h>
33 #include <dpl/foreach.h>
35 using namespace DPL::DB::ORM;
36 using namespace DPL::DB::ORM::ace;
37 using namespace AceDB::AceDaoUtilities;
38 using namespace AceDB::AceDaoConversions;
42 static const int DB_ALLOW_ALWAYS = 0;
43 static const int DB_ALLOW_FOR_SESSION = 1;
44 static const int DB_ALLOW_THIS_TIME = 2;
45 static const int DB_DENY_ALWAYS = 3;
46 static const int DB_DENY_FOR_SESSION = 4;
47 static const int DB_DENY_THIS_TIME = 5;
49 static const int DB_APP_UNKNOWN = 0;
50 static const int DB_APP_WAC20 = 1;
51 static const int DB_APP_TIZEN = 2;
53 int AceDAOReadOnly::promptDecisionToInt(PromptDecision decision)
55 if (PromptDecision::ALLOW_ALWAYS == decision) {
56 return DB_ALLOW_ALWAYS;
57 } else if (PromptDecision::DENY_ALWAYS == decision) {
58 return DB_DENY_ALWAYS;
59 } else if (PromptDecision::ALLOW_THIS_TIME == decision) {
60 return DB_ALLOW_THIS_TIME;
61 } else if (PromptDecision::DENY_THIS_TIME == decision) {
62 return DB_DENY_THIS_TIME;
63 } else if (PromptDecision::ALLOW_FOR_SESSION == decision) {
64 return DB_ALLOW_FOR_SESSION;
67 return DB_DENY_FOR_SESSION;
70 PromptDecision AceDAOReadOnly::intToPromptDecision(int dec) {
71 if (dec == DB_ALLOW_ALWAYS) {
72 return PromptDecision::ALLOW_ALWAYS;
73 } else if (dec == DB_DENY_ALWAYS) {
74 return PromptDecision::DENY_ALWAYS;
75 } else if (dec == DB_ALLOW_THIS_TIME) {
76 return PromptDecision::ALLOW_THIS_TIME;
77 } else if (dec == DB_DENY_THIS_TIME) {
78 return PromptDecision::DENY_THIS_TIME;
79 } else if (dec == DB_ALLOW_FOR_SESSION) {
80 return PromptDecision::ALLOW_FOR_SESSION;
82 // DB_DENY_FOR_SESSION
83 return PromptDecision::DENY_FOR_SESSION;
86 int AceDAOReadOnly::appTypeToInt(AppTypes app_type)
89 case AppTypes::Unknown:
90 return DB_APP_UNKNOWN;
96 return DB_APP_UNKNOWN;
101 AppTypes AceDAOReadOnly::intToAppType(int app_type)
105 return AppTypes::Unknown;
107 return AppTypes::WAC20;
109 return AppTypes::Tizen;
111 return AppTypes::Unknown;
115 void AceDAOReadOnly::attachToThreadRO()
117 AceDaoUtilities::m_databaseInterface.AttachToThread(
118 DPL::DB::SqlConnection::Flag::RO);
121 void AceDAOReadOnly::attachToThreadRW()
123 AceDaoUtilities::m_databaseInterface.AttachToThread(
124 DPL::DB::SqlConnection::Flag::RW);
127 void AceDAOReadOnly::detachFromThread()
129 AceDaoUtilities::m_databaseInterface.DetachFromThread();
132 OptionalCachedPromptDecision AceDAOReadOnly::getPromptDecision(
133 WidgetHandle widgetHandle,
137 // get matching subject verdict
138 ACE_DB_SELECT(select, AcePromptDecision, &AceDaoUtilities::m_databaseInterface);
142 Equals<AcePromptDecision::rule_id>(ruleId),
143 Equals<AcePromptDecision::app_id>(widgetHandle)));
145 std::list<AcePromptDecision::Row> rows = select->GetRowList();
147 return OptionalCachedPromptDecision();
150 AcePromptDecision::Row row = rows.front();
151 CachedPromptDecision decision;
152 decision.decision = intToPromptDecision(row.Get_decision());
153 decision.session = row.Get_session();
155 return OptionalCachedPromptDecision(decision);
157 Catch(DPL::DB::SqlConnection::Exception::Base) {
158 ReThrowMsg(Exception::DatabaseError, "Failed to getPromptDecision");
162 void AceDAOReadOnly::getAttributes(BaseAttributeSet *attributes)
164 if (NULL == attributes) {
165 LogError("NULL pointer");
172 ACE_DB_SELECT(select, AceAttribute, &AceDaoUtilities::m_databaseInterface);
173 typedef std::list<AceAttribute::Row> RowList;
174 RowList list = select->GetRowList();
177 BaseAttributePtr attribute(new BaseAttribute());
178 DPL::String name = i->Get_name();
179 aname = DPL::ToUTF8String(name);
180 type = i->Get_type();
182 attribute->setName(&aname);
183 attribute->setType(intToAttributeType(type));
184 attributes->insert(attribute);
187 Catch(DPL::DB::SqlConnection::Exception::Base) {
188 ReThrowMsg(Exception::DatabaseError, "Failed to getAttributes");
192 OptionalExtendedPolicyResult AceDAOReadOnly::getPolicyResult(
193 const BaseAttributeSet &attributes)
196 auto attrHash = convertToHash(attributes);
197 return getPolicyResult(attrHash);
200 OptionalExtendedPolicyResult AceDAOReadOnly::getPolicyResult(
201 const DPL::String &attrHash)
204 // get matching subject verdict
205 ACE_DB_SELECT(select, AcePolicyResult, &AceDaoUtilities::m_databaseInterface);
206 Equals<AcePolicyResult::hash> e1(attrHash);
209 std::list<AcePolicyResult::Row> rows = select->GetRowList();
211 return OptionalExtendedPolicyResult();
214 AcePolicyResult::Row row = rows.front();
215 int decision = row.Get_decision();
216 ExtendedPolicyResult res;
217 res.policyResult = PolicyResult::deserialize(decision);
218 res.ruleId = row.Get_rule_id();
219 return OptionalExtendedPolicyResult(res);
221 Catch(DPL::DB::SqlConnection::Exception::Base) {
222 ReThrowMsg(Exception::DatabaseError, "Failed to getVerdict");
226 PreferenceTypes AceDAOReadOnly::getDevCapSetting(const std::string &resource)
230 if (!getResourceByUri(resource, row)) {
231 return PreferenceTypes::PREFERENCE_DEFAULT;
233 return intToPreference(row.Get_general_setting());
235 Catch(DPL::DB::SqlConnection::Exception::Base) {
236 ReThrowMsg(Exception::DatabaseError, "Failed to getResourceSetting");
240 void AceDAOReadOnly::getDevCapSettings(PreferenceTypesMap *globalSettingsMap)
242 if (NULL == globalSettingsMap) {
243 LogError("Null pointer");
246 globalSettingsMap->clear();
248 ACE_DB_SELECT(select, AceDevCap, &AceDaoUtilities::m_databaseInterface);
249 typedef std::list<AceDevCap::Row> RowList;
250 RowList list = select->GetRowList();
253 PreferenceTypes p = intToPreference(i->Get_general_setting());
254 globalSettingsMap->insert(make_pair(DPL::ToUTF8String(
255 i->Get_id_uri()), p));
258 Catch(DPL::DB::SqlConnection::Exception::Base) {
259 ReThrowMsg(Exception::DatabaseError, "Failed to getResourceSettings");
263 void AceDAOReadOnly::getWidgetDevCapSettings(BasePermissionList *outputList)
265 if (NULL == outputList) {
266 LogError("NULL pointer");
271 std::string resourceName;
272 PreferenceTypes allowAccess;
274 ACE_DB_SELECT(select,
275 AceWidgetDevCapSetting,
276 &AceDaoUtilities::m_databaseInterface);
278 typedef std::list<AceWidgetDevCapSetting::Row> RowList;
279 RowList list = select->GetRowList();
283 int app_id = i->Get_app_id();
284 int res_id = i->Get_resource_id();
286 ACE_DB_SELECT(resourceSelect, AceDevCap, &AceDaoUtilities::m_databaseInterface);
287 resourceSelect->Where(Equals<AceDevCap::resource_id>(res_id));
288 AceDevCap::Row rrow = resourceSelect->GetSingleRow();
290 resourceName = DPL::ToUTF8String(rrow.Get_id_uri());
292 if (!resourceName.empty()) {
293 allowAccess = intToPreference(i->Get_access_value());
294 outputList->push_back(
295 BasePermission(app_id,
301 Catch(DPL::DB::SqlConnection::Exception::Base) {
302 ReThrowMsg(Exception::DatabaseError, "Failed to findUserSettings");
306 PreferenceTypes AceDAOReadOnly::getWidgetDevCapSetting(
307 const std::string &resource,
308 WidgetHandle handler)
312 if (!getResourceByUri(resource, rrow)) {
313 return PreferenceTypes::PREFERENCE_DEFAULT;
315 int resourceId = rrow.Get_resource_id();
317 // get matching user setting
318 ACE_DB_SELECT(select, AceWidgetDevCapSetting, &AceDaoUtilities::m_databaseInterface);
320 select->Where(And(Equals<AceWidgetDevCapSetting::resource_id>(resourceId),
321 Equals<AceWidgetDevCapSetting::app_id>(handler)));
323 std::list<int> values =
324 select->GetValueList<AceWidgetDevCapSetting::access_value>();
325 if (values.empty()) {
326 return PreferenceTypes::PREFERENCE_DEFAULT;
328 return intToPreference(values.front());
330 Catch(DPL::DB::SqlConnection::Exception::Base) {
331 ReThrowMsg(Exception::DatabaseError, "Failed in getUserSetting");
335 void AceDAOReadOnly::getRequestedDevCaps(
336 WidgetHandle widgetHandle,
337 RequestedDevCapsMap *permissions)
339 if (NULL == permissions) {
340 LogError("NULL pointer");
343 permissions->clear();
345 ACE_DB_SELECT(select, AceRequestedDevCaps,
346 &AceDaoUtilities::m_databaseInterface);
348 Equals<AceRequestedDevCaps::app_id>(widgetHandle));
349 std::list<AceRequestedDevCaps::Row> list = select->GetRowList();
352 permissions->insert(std::make_pair(i->Get_dev_cap(),
353 i->Get_grant_smack() == 1));
355 } Catch(DPL::DB::SqlConnection::Exception::Base) {
356 ReThrowMsg(Exception::DatabaseError, "Failed to getRequestedDevCaps");
360 void AceDAOReadOnly::getAcceptedFeature(
361 WidgetHandle widgetHandle,
362 FeatureNameVector *fvector)
364 if (NULL == fvector) {
365 LogError("NULL pointer");
371 ACE_DB_SELECT(select, AceAcceptedFeature,
372 &AceDaoUtilities::m_databaseInterface);
374 Equals<AceAcceptedFeature::app_id>(widgetHandle));
375 std::list<AceAcceptedFeature::Row> list = select->GetRowList();
378 fvector->push_back(i->Get_feature());
380 } Catch(DPL::DB::SqlConnection::Exception::Base) {
381 ReThrowMsg(Exception::DatabaseError, "Failed to getRequestedDevCaps");
385 AppTypes AceDAOReadOnly::getWidgetType(WidgetHandle handle)
388 ACE_DB_SELECT(select, WidgetInfo, &AceDaoUtilities::m_databaseInterface);
389 select->Where(Equals<WidgetInfo::app_id>(handle));
390 WidgetInfo::Select::RowList rows = select->GetRowList();
391 DPL::OptionalInt res;
393 res = rows.front().Get_widget_type();
394 AppTypes retType = (res.IsNull() ? AppTypes::Unknown : static_cast<AppTypes>(*res));
397 LogDebug("Can not find widget type");
398 return AppTypes::Unknown;
401 Catch(DPL::DB::SqlConnection::Exception::Base) {
402 ReThrowMsg(Exception::DatabaseError, "Failed to getWidgetType");
406 std::string AceDAOReadOnly::getVersion(WidgetHandle widgetHandle)
410 ACE_DB_SELECT(select, WidgetInfo, &AceDaoUtilities::m_databaseInterface);
411 select->Where(Equals<WidgetInfo::app_id>(widgetHandle));
412 WidgetInfo::Select::RowList rows = select->GetRowList();
413 DPL::OptionalString res;
415 res = rows.front().Get_widget_version();
416 return (res.IsNull() ? "" : DPL::ToUTF8String(*res));
418 LogDebug("Widget not installed");
421 } Catch(DPL::DB::SqlConnection::Exception::Base) {
422 ReThrowMsg(Exception::DatabaseError, "Failed to getVersion");
426 std::string AceDAOReadOnly::getAuthorName(WidgetHandle widgetHandle)
430 ACE_DB_SELECT(select, WidgetInfo, &AceDaoUtilities::m_databaseInterface);
431 select->Where(Equals<WidgetInfo::app_id>(widgetHandle));
432 WidgetInfo::Select::RowList rows = select->GetRowList();
433 DPL::OptionalString res;
435 res = rows.front().Get_author_name();
436 return (res.IsNull() ? "" : DPL::ToUTF8String(*res));
438 LogDebug("Widget not installed");
441 } Catch(DPL::DB::SqlConnection::Exception::Base) {
442 ReThrowMsg(Exception::DatabaseError, "Failed to getAuthorName");
446 std::string AceDAOReadOnly::getGUID(WidgetHandle widgetHandle)
450 ACE_DB_SELECT(select, WidgetInfo, &AceDaoUtilities::m_databaseInterface);
451 select->Where(Equals<WidgetInfo::app_id>(widgetHandle));
452 WidgetInfo::Select::RowList rows = select->GetRowList();
453 DPL::OptionalString res;
455 res = rows.front().Get_widget_id();
456 return (res.IsNull() ? "" : DPL::ToUTF8String(*res));
458 LogDebug("Widget not installed");
461 } Catch(DPL::DB::SqlConnection::Exception::Base) {
462 ReThrowMsg(Exception::DatabaseError, "Failed to getGUID");
466 WidgetCertificateCNList AceDAOReadOnly::getKeyCommonNameList(
467 WidgetHandle widgetHandle,
468 WidgetCertificateData::Owner owner,
469 WidgetCertificateData::Type type)
472 ACE_DB_SELECT(select, WidgetCertificateFingerprint, &AceDaoUtilities::m_databaseInterface);
473 select->Where(And(And(
474 Equals<WidgetCertificateFingerprint::app_id>(widgetHandle),
475 Equals<WidgetCertificateFingerprint::owner>(owner)),
476 Equals<WidgetCertificateFingerprint::type>(type)));
477 WidgetCertificateFingerprint::Select::RowList rows = select->GetRowList();
479 WidgetCertificateCNList out;
482 DPL::Optional<DPL::String> cn = it->Get_common_name();
483 out.push_back(cn.IsNull() ? "" : DPL::ToUTF8String(*cn));
487 Catch(DPL::DB::SqlConnection::Exception::Base) {
488 ReThrowMsg(Exception::DatabaseError, "Failed to getKeyCommonNameList");
492 FingerPrintList AceDAOReadOnly::getKeyFingerprints(
493 WidgetHandle widgetHandle,
494 WidgetCertificateData::Owner owner,
495 WidgetCertificateData::Type type)
499 ACE_DB_SELECT(select, WidgetCertificateFingerprint, &AceDaoUtilities::m_databaseInterface);
500 select->Where(And(And(
501 Equals<WidgetCertificateFingerprint::app_id>(widgetHandle),
502 Equals<WidgetCertificateFingerprint::owner>(owner)),
503 Equals<WidgetCertificateFingerprint::type>(type)));
504 WidgetCertificateFingerprint::Select::RowList rows = select->GetRowList();
506 FingerPrintList keys;
509 DPL::Optional<DPL::String> sha1 = it->Get_sha1_fingerprint();
511 keys.push_back(DPL::ToUTF8String(*sha1));
512 DPL::Optional<DPL::String> md5 = it->Get_md5_fingerprint();
514 keys.push_back(DPL::ToUTF8String(*md5));
518 Catch(DPL::DB::SqlConnection::Exception::Base) {
519 ReThrowMsg(Exception::DatabaseError, "Failed to getKeyFingerprints");
523 std::string AceDAOReadOnly::getShareHref(WidgetHandle widgetHandle)
527 ACE_DB_SELECT(select, WidgetInfo, &AceDaoUtilities::m_databaseInterface);
528 select->Where(Equals<WidgetInfo::app_id>(widgetHandle));
529 WidgetInfo::Select::RowList rows = select->GetRowList();
532 ThrowMsg(Exception::DatabaseError, "Cannot find widget. Handle: " << widgetHandle);
534 DPL::Optional<DPL::String> value = rows.front().Get_share_href();
535 std::string ret = "";
537 ret = DPL::ToUTF8String(*value);
540 Catch(DPL::DB::SqlConnection::Exception::Base) {
541 ReThrowMsg(Exception::DatabaseError, "Failed to getShareHref");
545 WidgetHandleList AceDAOReadOnly::getHandleList()
547 LogDebug("Getting DbWidgetHandle List");
550 ACE_DB_SELECT(select, WidgetInfo, &AceDaoUtilities::m_databaseInterface);
551 return select->GetValueList<WidgetInfo::app_id>();
553 Catch(DPL::DB::SqlConnection::Exception::Base) {
554 ReThrowMsg(Exception::DatabaseError, "Failed to list of widget handles");
558 bool AceDAOReadOnly::isWidgetInstalled(WidgetHandle handle)
561 ACE_DB_SELECT(select, WidgetInfo, &AceDaoUtilities::m_databaseInterface);
562 select->Where(Equals<WidgetInfo::app_id>(handle));
563 WidgetInfo::Select::RowList rows = select->GetRowList();
564 return !rows.empty() ? true : false;
565 } Catch(DPL::DB::SqlConnection::Exception::Base) {
566 ReThrowMsg(Exception::DatabaseError, "Failed in isWidgetInstalled");