[Release] wrt-installer_0.1.9
[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             break;
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             break;
108         }
109         certData[i]->chain_id = it->chainId;
110
111         certData[i]->md5_fp = toAceString(it->strMD5Fingerprint);
112         certData[i]->sha1_fp = toAceString(it->strSHA1Fingerprint);
113         certData[i]->common_name =
114             toAceString(DPL::ToUTF8String(it->strCommonName));
115         ++i;
116     }
117
118     LogDebug("Registerign widget in ace");
119     ace_return_t retval = ace_register_widget(
120             static_cast<ace_widget_handle_t>(widgetHandle), &wi, certData);
121
122     //clean up - WidgetInfo
123     free(wi.author);
124     free(wi.id);
125     free(wi.shareHerf);
126     free(wi.version);
127
128     //free cert list
129     i = 0;
130     while (certData[i] != NULL) {
131         free(certData[i]->common_name);
132         free(certData[i]->md5_fp);
133         free(certData[i]->sha1_fp);
134         delete certData[i];
135         ++i;
136     }
137     delete[] certData;
138     return retval == ACE_OK;
139 }
140 }