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.
17 * @file task_ace_check.cpp
18 * @author Pawel Sikorski (p.sikorski@samsung.com)
20 * @brief Implementation file for installer task ace check
27 #include <widget_install/task_ace_check.h>
28 #include <dpl/assert.h>
29 #include <dpl/log/log.h>
30 #include <dpl/foreach.h>
32 #include <widget_install/widget_install_context.h>
33 #include <widget_install/widget_install_errors.h>
34 #include <widget_install/job_widget_install.h>
36 #include <dpl/wrt-dao-rw/widget_dao.h>
37 #include <ace_api_install.h>
40 namespace WidgetInstall {
42 TaskAceCheck::TaskAceCheck(InstallerContext& context) :
43 DPL::TaskDecl<TaskAceCheck>(this),
46 AddStep(&TaskAceCheck::StepPrepareForAce);
47 AddStep(&TaskAceCheck::StepAceCheck);
48 AddStep(&TaskAceCheck::StepProcessAceResponse);
49 AddStep(&TaskAceCheck::StepCheckAceResponse);
52 void TaskAceCheck::StepPrepareForAce()
54 WrtDB::WidgetDAO dao(m_context.locations->getPkgname());
55 m_context.featureLogic =
56 FeatureLogicPtr(new FeatureLogic(dao.getHandle()));
57 m_context.job->UpdateProgress(
58 InstallerContext::INSTALL_ACE_PREPARE,
59 "Widget Access Control Check Prepared");
62 void TaskAceCheck::StepAceCheck()
64 WrtDB::WidgetDAO dao(m_context.locations->getPkgname());
65 LogInfo("StepAceCheck!");
66 // This widget does not use any device cap
67 if (m_context.featureLogic->isDone()) {
71 LogInfo("StepAceCheck!");
72 DPL::String deviceCap = m_context.featureLogic->getDevice();
74 LogInfo("StepAceCheck!");
75 LogInfo("DevCap is : " << deviceCap);
77 std::string devCapStr = DPL::ToUTF8String(deviceCap);
78 ace_policy_result_t policyResult = ACE_DENY;
79 ace_return_t ret = ace_get_policy_result(
80 const_cast<const ace_resource_t>(devCapStr.c_str()),
84 ThrowMsg(Exceptions::NotAllowed, "Instalation failure. "
88 LogInfo("PolicyResult is : " << static_cast<int>(policyResult));
89 m_context.staticPermittedDevCaps.insert(std::make_pair(deviceCap,
90 policyResult == ACE_PERMIT));
92 m_context.featureLogic->setAceResponse(policyResult != ACE_DENY);
95 void TaskAceCheck::StepProcessAceResponse()
97 WrtDB::WidgetDAO dao(m_context.locations->getPkgname());
98 if (m_context.widgetConfig.packagingType ==
99 WrtDB::PKG_TYPE_HOSTED_WEB_APP) {
103 LogInfo("StepProcessAceResponse");
104 m_context.featureLogic->next();
106 // No device caps left to process
107 if (m_context.featureLogic->isDone()) {
108 LogInfo("All responses has been received from ACE.");
109 // Data to convert to C API
110 std::vector<std::string> devCaps;
111 std::vector<bool> devCapsSmack;
112 // Saving static dev cap permissions
113 FOREACH (cap, m_context.staticPermittedDevCaps) {
114 LogInfo("staticPermittedDevCaps : " << cap->first
115 << " smack: " << cap->second);
116 std::string devCapStr = DPL::ToUTF8String(cap->first);
117 devCaps.push_back(devCapStr);
118 devCapsSmack.push_back(cap->second);
120 ace_requested_dev_cap_list_t list;
121 list.count = devCaps.size();
122 list.items = new ace_requested_dev_cap_t[list.count];
124 for (unsigned int i = 0; i < devCaps.size(); ++i) {
125 list.items[i].device_capability =
126 const_cast<const ace_resource_t>(devCaps[i].c_str());
127 list.items[i].smack_granted =
128 devCapsSmack[i] ? ACE_TRUE : ACE_FALSE;
130 ace_return_t ret = ace_set_requested_dev_caps(dao.getHandle(), //TODO: (ace_widget_handle_t not int needed)
133 ThrowMsg(Exceptions::NotAllowed, "Instalation failure. "
136 delete [] list.items;
138 std::set<std::string> acceptedFeature;
139 auto it = m_context.featureLogic->resultBegin();
140 for (;it != m_context.featureLogic->resultEnd(); ++it) {
141 if (!(it->rejected)) {
142 acceptedFeature.insert(DPL::ToUTF8String(it->name));
145 ace_feature_list_t featureList;
146 featureList.count = acceptedFeature.size();
147 featureList.items = new ace_string_t[featureList.count];
150 for (std::set<std::string>::const_iterator iter = acceptedFeature.begin();
151 iter != acceptedFeature.end(); ++iter) {
152 LogDebug("Accepted feature item: " << iter->c_str());
153 featureList.items[i] = const_cast<char *>(iter->c_str());
157 ret = ace_set_accepted_feature(dao.getHandle(), &featureList);
159 delete [] featureList.items;
162 LogError("Error in ace_set_feature");
163 ThrowMsg(Exceptions::NotAllowed, "Instalation failure. "
164 "ace_set_feature failure.");
169 LogInfo("Next device cap.");
170 // Process next device cap
171 SwitchToStep(&TaskAceCheck::StepAceCheck);
174 void TaskAceCheck::StepCheckAceResponse()
176 LogInfo("Checking ACE response");
177 if (m_context.featureLogic->isRejected()) {
178 LogError("Installation failure. Some devCap was not accepted by ACE.");
179 ThrowMsg(Exceptions::NotAllowed, "Instalation failure. "
180 "Some deviceCap was not accepted by ACE.");
182 LogInfo("Updating \"feature reject status\" in database!");
183 auto it = m_context.featureLogic->resultBegin();
184 auto end = m_context.featureLogic->resultEnd();
185 for(;it != end; ++it){
186 LogInfo(" |- Feature: " << it->name << " has reject status: " << it->rejected);
188 WrtDB::WidgetDAO dao(m_context.locations->getPkgname());
189 dao.updateFeatureRejectStatus(*it);
192 LogInfo("Installation continues...");
194 m_context.job->UpdateProgress(
195 InstallerContext::INSTALL_ACE_CHECK,
196 "Widget Access Control Check Finished");
199 } //namespace WidgetInstall