739d2b476a30da6ca85d87f1af898866dcad41b4
[platform/core/dotnet/launcher.git] / NativeLauncher / installer-plugin / prefer_dotnet_aot_plugin.cc
1 /*
2  * Copyright (c) 2016 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 #include "ni_common.h"
18 #include "log.h"
19 #include "utils.h"
20
21 #include <cstring>
22 #include <vector>
23
24 #include <vconf.h>
25 #include <glib.h>
26 #include <pkgmgr_installer_info.h>
27
28 #ifdef  LOG_TAG
29 #undef  LOG_TAG
30 #endif
31 #define LOG_TAG "DOTNET_INSTALLER_PLUGIN"
32
33 static bool aotPluginInstalled = false;
34 static bool aotPluginFinished = false;
35
36 typedef struct metadata_s {
37         const char* key;
38         const char* value;
39 } metadata_t;
40
41 extern "C" int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char *pkgId, const char *appId, GList *list)
42 {
43         // Can be multiple apps in one package
44         if (aotPluginInstalled) {
45                 _INFO("AOT plugin already installed");
46                 return 0;
47         }
48         aotPluginInstalled = true;
49
50         int skipOpt = false;
51         if (!pkgmgr_installer_info_get_skip_optimization(&skipOpt)) {
52                 if (skipOpt) {
53                         _DBG("Skip dotnet AOT");
54                         return 0;
55                 }
56         }
57
58         bool doAOT = false;
59         GList* iter = list;
60         while (iter) {
61                 metadata_t* md = static_cast<metadata_t*>(iter->data);
62                 if (strcmp(AOT_METADATA_KEY, md->key) == 0) {
63                         if (strcmp(METADATA_VALUE_TRUE, md->value) == 0) {
64                                 doAOT = true;
65                         }
66                         break;
67                 }
68                 iter = g_list_next(iter);
69         }
70
71         if (doAOT) {
72                 _DBG("Prefer dotnet application AOT set TRUE");
73
74                 if (initNICommon() != NI_ERROR_NONE) {
75                         _ERR("Fail to initialize NI Common");
76                         return -1;
77                 }
78
79                 NIOption* opt = getNIOption();
80                 if (opt == nullptr) {
81                         _ERR("Fail to create option structure.");
82                         return -1;
83                 }
84
85                 if (createNIUnderPkgRoot(pkgId, opt) != NI_ERROR_NONE) {
86                         _ERR("Failed to generate application to native image [%s]", pkgId);
87                         return -1;
88                 }
89
90                 _INFO("Complete make application to native image");
91         }
92         return 0;
93 }
94
95 extern "C" int PKGMGR_MDPARSER_PLUGIN_UPGRADE(const char *pkgId, const char *appId, GList *list)
96 {
97         return PKGMGR_MDPARSER_PLUGIN_INSTALL(pkgId, appId, list);
98 }
99
100 extern "C" int PKGMGR_MDPARSER_PLUGIN_UNINSTALL(const char *pkgId, const char *appId, GList *list)
101 {
102         return 0;
103 }
104
105 extern "C" int PKGMGR_MDPARSER_PLUGIN_REMOVED(const char *pkgId, const char *appId, GList *list)
106 {
107         return PKGMGR_MDPARSER_PLUGIN_UPGRADE(pkgId, appId, list);
108 }
109
110 extern "C" int PKGMGR_MDPARSER_PLUGIN_CLEAN(const char *pkgId, const char *appId, GList *list)
111 {
112         // Can be multiple apps in one package
113         if (aotPluginFinished) {
114                 _INFO("AOT plugin already finished(CLEAN)");
115                 return 0;
116         }
117         aotPluginFinished = true;
118
119         finalizeNICommon();
120         return 0;
121 }
122
123 extern "C" int PKGMGR_MDPARSER_PLUGIN_UNDO(const char *pkgId, const char *appId, GList *list)
124 {
125         // Can be multiple apps in one package
126         if (aotPluginFinished) {
127                 _INFO("AOT plugin already finished(UNDO)");
128                 return 0;
129         }
130         aotPluginFinished = true;
131
132         finalizeNICommon();
133         return 0;
134 }