Change pkgname to appid for docomo request. PART1
[framework/web/wrt-installer.git] / src / jobs / widget_install / task_ace_check.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  * @file    task_ace_check.cpp
18  * @author  Pawel Sikorski (p.sikorski@samsung.com)
19  * @version 1.0
20  * @brief   Implementation file for installer task ace check
21  */
22
23 #include <utility>
24 #include <vector>
25 #include <string>
26
27 #include <widget_install/task_ace_check.h>
28 #include <dpl/assert.h>
29 #include <dpl/log/log.h>
30 #include <dpl/foreach.h>
31
32 #include <widget_install/widget_install_context.h>
33 #include <widget_install/widget_install_errors.h>
34 #include <widget_install/job_widget_install.h>
35
36 #include <dpl/wrt-dao-rw/widget_dao.h>
37 #include <ace_api_install.h>
38
39 namespace Jobs {
40 namespace WidgetInstall {
41
42 TaskAceCheck::TaskAceCheck(InstallerContext& context) :
43     DPL::TaskDecl<TaskAceCheck>(this),
44     m_context(context)
45 {
46     AddStep(&TaskAceCheck::StepPrepareForAce);
47     AddStep(&TaskAceCheck::StepAceCheck);
48     AddStep(&TaskAceCheck::StepProcessAceResponse);
49     AddStep(&TaskAceCheck::StepCheckAceResponse);
50 }
51
52 void TaskAceCheck::StepPrepareForAce()
53 {
54     m_context.featureLogic =
55         FeatureLogicPtr(new FeatureLogic(m_context.widgetConfig.tzAppid));
56     m_context.job->UpdateProgress(
57         InstallerContext::INSTALL_ACE_PREPARE,
58         "Widget Access Control Check Prepared");
59 }
60
61 void TaskAceCheck::StepAceCheck()
62 {
63     WrtDB::WidgetDAO dao(m_context.widgetConfig.tzAppid);
64     LogInfo("StepAceCheck!");
65     // This widget does not use any device cap
66     if (m_context.featureLogic->isDone()) {
67         return;
68     }
69
70     LogInfo("StepAceCheck!");
71     DPL::String deviceCap = m_context.featureLogic->getDevice();
72
73     LogInfo("StepAceCheck!");
74     LogInfo("DevCap is : " << deviceCap);
75
76     std::string devCapStr = DPL::ToUTF8String(deviceCap);
77     ace_policy_result_t policyResult = ACE_DENY;
78     ace_return_t ret = ace_get_policy_result(
79             const_cast<const ace_resource_t>(devCapStr.c_str()),
80             dao.getHandle(),                         //TODO: remove dao.getHandle()
81             &policyResult);
82     if (ACE_OK != ret) {
83         ThrowMsg(Exceptions::NotAllowed, "Instalation failure. "
84                  "ACE check failure");
85     }
86
87     LogInfo("PolicyResult is : " << static_cast<int>(policyResult));
88     m_context.staticPermittedDevCaps.insert(std::make_pair(deviceCap,
89             policyResult == ACE_PERMIT));
90
91     m_context.featureLogic->setAceResponse(policyResult != ACE_DENY);
92 }
93
94 void TaskAceCheck::StepProcessAceResponse()
95 {
96     WrtDB::WidgetDAO dao(m_context.widgetConfig.tzAppid);
97     if (m_context.widgetConfig.packagingType ==
98             WrtDB::PKG_TYPE_HOSTED_WEB_APP) {
99         return;
100     }
101
102     LogInfo("StepProcessAceResponse");
103     m_context.featureLogic->next();
104
105     // No device caps left to process
106     if (m_context.featureLogic->isDone()) {
107         LogInfo("All responses has been received from ACE.");
108         // Data to convert to C API
109         std::vector<std::string> devCaps;
110         std::vector<bool> devCapsSmack;
111         // Saving static dev cap permissions
112         FOREACH (cap, m_context.staticPermittedDevCaps) {
113             LogInfo("staticPermittedDevCaps : " << cap->first
114                     << " smack: " << cap->second);
115             std::string devCapStr = DPL::ToUTF8String(cap->first);
116             devCaps.push_back(devCapStr);
117             devCapsSmack.push_back(cap->second);
118         }
119         ace_requested_dev_cap_list_t list;
120         list.count = devCaps.size();
121         list.items = new ace_requested_dev_cap_t[list.count];
122
123         for (unsigned int i = 0; i < devCaps.size(); ++i) {
124             list.items[i].device_capability =
125                     const_cast<const ace_resource_t>(devCaps[i].c_str());
126             list.items[i].smack_granted =
127                     devCapsSmack[i] ? ACE_TRUE : ACE_FALSE;
128         }
129         ace_return_t ret = ace_set_requested_dev_caps(dao.getHandle(),   //TODO: remove dao.getHandle()
130                                                       &list);
131         if (ACE_OK != ret) {
132             ThrowMsg(Exceptions::NotAllowed, "Instalation failure. "
133                      "ACE failure");
134         }
135         delete [] list.items;
136
137         std::set<std::string> acceptedFeature;
138         auto it = m_context.featureLogic->resultBegin();
139         for (;it != m_context.featureLogic->resultEnd(); ++it) {
140             if (!(it->rejected)) {
141                 acceptedFeature.insert(DPL::ToUTF8String(it->name));
142             }
143         }
144         ace_feature_list_t featureList;
145         featureList.count = acceptedFeature.size();
146         featureList.items = new ace_string_t[featureList.count];
147
148         size_t i=0;
149         for (std::set<std::string>::const_iterator iter = acceptedFeature.begin();
150                 iter != acceptedFeature.end(); ++iter) {
151             LogDebug("Accepted feature item: " << iter->c_str());
152             featureList.items[i] = const_cast<char *>(iter->c_str());
153             i++;
154         }
155
156         ret = ace_set_accepted_feature(dao.getHandle(), &featureList); //TODO: remove dao.getHandle()
157
158         delete [] featureList.items;
159
160         if (ACE_OK != ret) {
161             LogError("Error in ace_set_feature");
162             ThrowMsg(Exceptions::NotAllowed, "Instalation failure. "
163               "ace_set_feature failure.");
164         }
165         return;
166     }
167
168     LogInfo("Next device cap.");
169     // Process next device cap
170     SwitchToStep(&TaskAceCheck::StepAceCheck);
171 }
172
173 void TaskAceCheck::StepCheckAceResponse()
174 {
175     LogInfo("Checking ACE response");
176     if (m_context.featureLogic->isRejected()) {
177         LogError("Installation failure. Some devCap was not accepted by ACE.");
178         ThrowMsg(Exceptions::NotAllowed, "Instalation failure. "
179             "Some deviceCap was not accepted by ACE.");
180     }
181     LogInfo("Updating \"feature reject status\" in database!");
182     auto it = m_context.featureLogic->resultBegin();
183     auto end = m_context.featureLogic->resultEnd();
184     for(;it != end; ++it){
185         LogInfo("  |-  Feature: " << it->name << " has reject status: " << it->rejected);
186         if (it->rejected) {
187             WrtDB::WidgetDAO dao(m_context.widgetConfig.tzAppid);
188             dao.updateFeatureRejectStatus(*it);
189         }
190     }
191     LogInfo("Installation continues...");
192
193     m_context.job->UpdateProgress(
194         InstallerContext::INSTALL_ACE_CHECK,
195         "Widget Access Control Check Finished");
196 }
197
198 } //namespace WidgetInstall
199 } //namespace Jobs