Rename invalid WRT_SMACK_LABEL macro to WRT_SMACK_ENABLED
[platform/framework/web/wrt-installer.git] / src / jobs / widget_install / ace_registration.cpp
1 /*
2  * Copyright (c) 2012 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    ace_registration.cpp
18  * @author  Andrzej Surdej (a.surdej@gmail.com)
19  * @version 1.0
20  * @brief   Translate structures to ace api - implementation file
21  */
22
23 #include <ace_registration.h>
24 #include <dpl/foreach.h>
25 #include <ace_api_install.h>
26
27 #include <installer_log.h>
28
29 namespace {
30 char* toAceString(const DPL::OptionalString& os)
31 {
32     if (!os.IsNull()) {
33         return strdup(DPL::ToUTF8String(*os).c_str());
34     } else {
35         return NULL;
36     }
37 }
38
39 char* toAceString(const std::string& str)
40 {
41     if (!str.empty()) {
42         return strdup(str.c_str());
43     } else {
44         return NULL;
45     }
46 }
47 } //anonymous namespace
48
49 namespace AceApi {
50 bool registerAceWidget(const WrtDB::DbWidgetHandle& widgetHandle,
51                        const WrtDB::WidgetRegisterInfo& widgetConfig,
52                        const WrtDB::WidgetCertificateDataList& certList)
53 {
54     _D("Updating Ace database");
55     struct widget_info wi;
56
57     switch (widgetConfig.webAppType.appType) {
58     case WrtDB::APP_TYPE_TIZENWEBAPP:
59         wi.type = Tizen;
60         break;
61     default:
62         _E("Unknown application type");
63         return false;
64     }
65
66     wi.id = toAceString(widgetConfig.configInfo.widget_id);
67     wi.version = toAceString(widgetConfig.configInfo.version);
68     wi.author = toAceString(widgetConfig.configInfo.authorName);
69     wi.shareHerf = strdup(widgetConfig.shareHref.c_str());
70     _D("Basic data converted. Certificates begin.");
71
72     //one more element for NULL termination
73     _D("Found: %d certificates", certList.size());
74     ace_certificate_data** certData = new ace_certificate_data *
75         [certList.size() + 1];
76     certData[certList.size()] = NULL; // last element set to NULL
77
78     int i = 0;
79     FOREACH(it, certList)
80     {
81         certData[i] = new ace_certificate_data;
82         switch (it->owner) {
83         case WrtDB::WidgetCertificateData::AUTHOR:
84             certData[i]->owner = AUTHOR;
85             break;
86         case WrtDB::WidgetCertificateData::DISTRIBUTOR:
87             certData[i]->owner = DISTRIBUTOR;
88             break;
89         default:
90             _D("Unknown owner type of cert");
91             certData[i]->owner = UNKNOWN;
92             break;
93         }
94         switch (it->type) {
95         case WrtDB::WidgetCertificateData::ENDENTITY:
96             certData[i]->type = ENDENTITY;
97             break;
98         case WrtDB::WidgetCertificateData::ROOT:
99             certData[i]->type = ROOT;
100             break;
101         default:
102             _E("Unknown type of cert");
103             certData[i]->type = ENDENTITY;
104             break;
105         }
106         certData[i]->chain_id = it->chainId;
107
108         certData[i]->md5_fp = toAceString(it->strMD5Fingerprint);
109         certData[i]->sha1_fp = toAceString(it->strSHA1Fingerprint);
110         certData[i]->common_name =
111             toAceString(DPL::ToUTF8String(it->strCommonName));
112         ++i;
113     }
114
115     _D("Registerign widget in ace");
116     ace_return_t retval = ace_register_widget(
117             static_cast<ace_widget_handle_t>(widgetHandle), &wi, certData);
118
119     //clean up - WidgetInfo
120     free(wi.author);
121     free(wi.id);
122     free(wi.shareHerf);
123     free(wi.version);
124
125     //free cert list
126     i = 0;
127     while (certData[i] != NULL) {
128         free(certData[i]->common_name);
129         free(certData[i]->md5_fp);
130         free(certData[i]->sha1_fp);
131         delete certData[i];
132         ++i;
133     }
134     delete[] certData;
135     return retval == ACE_OK;
136 }
137 bool registerAceWidgetFromDB(const WrtDB::DbWidgetHandle& widgetHandle)
138 {
139     using namespace WrtDB;
140     _D("Updating Ace database from Widget DB");
141     struct widget_info wi;
142     DPL::OptionalString os;
143     WrtDB::WidgetCertificateDataList certList;
144
145     Try {
146         WidgetDAOReadOnly dao(widgetHandle);
147
148         WidgetType type = dao.getWidgetType();
149         if (type == WrtDB::APP_TYPE_TIZENWEBAPP) {
150             wi.type = Tizen;
151         } else {
152             _E("Unknown application type");
153             return false;
154         }
155
156         wi.id = toAceString(dao.getGUID());
157         wi.version = toAceString(dao.getVersion());
158         wi.author = toAceString(dao.getAuthorName());
159         wi.shareHerf = strdup(dao.getShareHref().c_str());
160         _D("Basic data converted. Certificates begin.");
161         certList = dao.getCertificateDataList();
162     }
163     Catch(WidgetDAOReadOnly::Exception::WidgetNotExist) {
164         _E("Widget does not exist");
165         return false;
166     }
167
168     //one more element for NULL termination
169     _D("Found: %d certificates", certList.size());
170     ace_certificate_data** certData = new ace_certificate_data *
171         [certList.size() + 1];
172     certData[certList.size()] = NULL; // last element set to NULL
173
174     int i = 0;
175     FOREACH(it, certList)
176     {
177         certData[i] = new ace_certificate_data;
178         switch (it->owner) {
179         case WrtDB::WidgetCertificateData::AUTHOR:
180             certData[i]->owner = AUTHOR;
181             break;
182         case WrtDB::WidgetCertificateData::DISTRIBUTOR:
183             certData[i]->owner = DISTRIBUTOR;
184             break;
185         default:
186             _D("Unknown owner type of cert");
187             certData[i]->owner = UNKNOWN;
188             break;
189         }
190         switch (it->type) {
191         case WrtDB::WidgetCertificateData::ENDENTITY:
192             certData[i]->type = ENDENTITY;
193             break;
194         case WrtDB::WidgetCertificateData::ROOT:
195             certData[i]->type = ROOT;
196             break;
197         default:
198             _E("Unknown type of cert");
199             certData[i]->type = ENDENTITY;
200             break;
201         }
202         certData[i]->chain_id = it->chainId;
203
204         certData[i]->md5_fp = toAceString(it->strMD5Fingerprint);
205         certData[i]->sha1_fp = toAceString(it->strSHA1Fingerprint);
206         certData[i]->common_name =
207             toAceString(DPL::ToUTF8String(it->strCommonName));
208         ++i;
209     }
210
211     _D("Registerign widget in ace");
212     ace_return_t retval = ace_register_widget(
213             static_cast<ace_widget_handle_t>(widgetHandle), &wi, certData);
214
215     //clean up - WidgetInfo
216     free(wi.author);
217     free(wi.id);
218     free(wi.shareHerf);
219     free(wi.version);
220
221     //free cert list
222     i = 0;
223     while (certData[i] != NULL) {
224         free(certData[i]->common_name);
225         free(certData[i]->md5_fp);
226         free(certData[i]->sha1_fp);
227         delete certData[i];
228         ++i;
229     }
230     delete[] certData;
231     return retval == ACE_OK;
232 }
233 }