faee3ea4f9a2c1603d20267f7418a514e081f282
[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 char* toAceString(const DPL::OptionalString& os)
30 {
31     if (!os.IsNull()) {
32         return strdup(DPL::ToUTF8String(*os).c_str());
33     } else {
34         return NULL;
35     }
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 bool registerAceWidget(const WrtDB::DbWidgetHandle& widgetHandle,
50                        const WrtDB::WidgetRegisterInfo& widgetConfig,
51                        const WrtDB::WidgetCertificateDataList& certList)
52 {
53     LogDebug("Updating Ace database");
54     struct widget_info wi;
55     DPL::OptionalString os;
56
57     switch (widgetConfig.webAppType.appType) {
58     case WrtDB::APP_TYPE_WAC20:
59         wi.type = WAC20;
60         break;
61     case WrtDB::APP_TYPE_TIZENWEBAPP:
62         wi.type = Tizen;
63         break;
64     default:
65         LogError("Unknown application type");
66         return false;
67     }
68
69     wi.id = toAceString(widgetConfig.configInfo.widget_id);
70     wi.version = toAceString(widgetConfig.configInfo.version);
71     wi.author = toAceString(widgetConfig.configInfo.authorName);
72     wi.shareHerf = strdup(widgetConfig.shareHref.c_str());
73     LogDebug("Basic data converted. Certificates begin.");
74
75     //one more element for NULL termination
76     LogDebug("Found: " << certList.size() << " certificates");
77     ace_certificate_data** certData = new ace_certificate_data *
78         [certList.size() + 1];
79     certData[certList.size()] = NULL; // last element set to NULL
80
81     int i = 0;
82     FOREACH(it, certList)
83     {
84         certData[i] = new ace_certificate_data;
85         switch (it->owner) {
86         case WrtDB::WidgetCertificateData::AUTHOR:
87             certData[i]->owner = AUTHOR;
88             break;
89         case WrtDB::WidgetCertificateData::DISTRIBUTOR:
90             certData[i]->owner = DISTRIBUTOR;
91             break;
92         default:
93             LogDebug("Unknown owner type of cert");
94             certData[i]->owner = UNKNOWN;
95         }
96         switch (it->type) {
97         case WrtDB::WidgetCertificateData::ENDENTITY:
98             certData[i]->type = ENDENTITY;
99             break;
100         case WrtDB::WidgetCertificateData::ROOT:
101             certData[i]->type = ROOT;
102             break;
103         default:
104             LogError("Unknown type of cert");
105             certData[i]->type = ENDENTITY;
106         }
107         certData[i]->chain_id = it->chainId;
108
109         certData[i]->md5_fp = toAceString(it->strMD5Fingerprint);
110         certData[i]->sha1_fp = toAceString(it->strSHA1Fingerprint);
111         certData[i]->common_name =
112             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 }