[Release] wrt-installer_0.0.89
[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/log/log.h>
25 #include <dpl/foreach.h>
26 #include <ace_api_install.h>
27
28 namespace {
29
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 char* toAceString(const std::string& str)
39 {
40     if (!str.empty())
41         return strdup(str.c_str());
42     else
43         return NULL;
44 }
45
46 } //anonymous namespace
47
48 namespace AceApi {
49
50 bool registerAceWidget(const WrtDB::DbWidgetHandle& widgetHandle,
51                                const WrtDB::WidgetRegisterInfo& widgetConfig,
52                                const WrtDB::WidgetCertificateDataList& certList)
53 {
54     LogDebug("Updating Ace database");
55     struct widget_info wi;
56     DPL::OptionalString os;
57
58     switch(widgetConfig.webAppType.appType)
59     {
60         case WrtDB::APP_TYPE_WAC20:
61             wi.type = WAC20;
62             break;
63         case WrtDB::APP_TYPE_TIZENWEBAPP:
64             wi.type = Tizen;
65             break;
66         default:
67             LogError("Unknown application type");
68             return false;
69     }
70
71     wi.id = toAceString(widgetConfig.configInfo.widget_id);
72     wi.version = toAceString(widgetConfig.configInfo.version);
73     wi.author = toAceString(widgetConfig.configInfo.authorName);
74     wi.shareHerf = strdup(widgetConfig.shareHref.c_str());
75     LogDebug("Basic data converted. Certificates begin.");
76
77     //one more element for NULL termination
78     LogDebug("Found: " << certList.size() << " certificates");
79     ace_certificate_data** certData = new ace_certificate_data*[certList.size() + 1];
80     certData[certList.size()] = NULL; // last element set to NULL
81
82     int i = 0;
83     FOREACH(it, certList)
84     {
85         certData[i] = new ace_certificate_data;
86         switch (it->owner) {
87         case WrtDB::WidgetCertificateData::AUTHOR :
88             certData[i]->owner = AUTHOR;
89             break;
90         case WrtDB::WidgetCertificateData::DISTRIBUTOR :
91             certData[i]->owner = DISTRIBUTOR;
92             break;
93         default :
94             LogDebug("Unknown owner type of cert");
95             certData[i]->owner = UNKNOWN;
96         }
97         switch (it->type) {
98             case WrtDB::WidgetCertificateData::ENDENTITY :
99                 certData[i]->type = ENDENTITY;
100                 break;
101             case WrtDB::WidgetCertificateData::ROOT :
102                 certData[i]->type = ROOT;
103                 break;
104             default :
105                 LogError("Unknown type of cert");
106                 certData[i]->type = ENDENTITY;
107         }
108         certData[i]->chain_id = it->chainId;
109
110         certData[i]->md5_fp = toAceString(it->strMD5Fingerprint);
111         certData[i]->sha1_fp = toAceString(it->strSHA1Fingerprint);
112         certData[i]->common_name = toAceString(DPL::ToUTF8String(it->strCommonName));
113         ++i;
114     }
115
116     LogDebug("Registerign widget in ace");
117     ace_return_t retval = ACE_ACE_UNKNOWN_ERROR;
118     retval = ace_register_widget(
119         static_cast<ace_widget_handle_t>(widgetHandle), &wi, certData);
120
121     //clean up - WidgetInfo
122     free(wi.author);
123     free(wi.id);
124     free(wi.shareHerf);
125     free(wi.version);
126
127     //free cert list
128     i = 0;
129     while (certData[i] != NULL) {
130         free(certData[i]->common_name);
131         free(certData[i]->md5_fp);
132         free(certData[i]->sha1_fp);
133         delete certData[i];
134         ++i;
135     }
136     delete[] certData;
137     return retval == ACE_OK;
138 }
139 }