Support Multi-Package Install Scenario
[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 std::string prevInstallPkgId = std::string("");
34 static std::string prevFinishPkgId = std::string("");
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         if (pkgId == NULL) {
44                 return 0;
45         }
46
47         // Can be multiple apps in one package
48         if (strcmp(pkgId, prevInstallPkgId.c_str()) == 0) {
49                 _INFO("AOT Plugin already run for same pkgId (%s)", pkgId);
50                 return 0;
51         }
52         prevInstallPkgId = pkgId;
53
54         int skipOpt = false;
55         if (!pkgmgr_installer_info_get_skip_optimization(&skipOpt)) {
56                 if (skipOpt) {
57                         _DBG("Skip dotnet AOT");
58                         return 0;
59                 }
60         }
61
62         bool doAOT = false;
63         GList* iter = list;
64         while (iter) {
65                 metadata_t* md = static_cast<metadata_t*>(iter->data);
66                 if (strcmp(AOT_METADATA_KEY, md->key) == 0) {
67                         if (strcmp(METADATA_VALUE_TRUE, md->value) == 0) {
68                                 doAOT = true;
69                         }
70                         break;
71                 }
72                 iter = g_list_next(iter);
73         }
74
75         if (doAOT) {
76                 _DBG("Prefer dotnet application AOT set TRUE");
77
78                 if (initNICommon() != NI_ERROR_NONE) {
79                         _ERR("Fail to initialize NI Common");
80                         return -1;
81                 }
82
83                 NIOption* opt = getNIOption();
84                 if (opt == nullptr) {
85                         _ERR("Fail to create option structure.");
86                         return -1;
87                 }
88
89                 if (createNIUnderPkgRoot(pkgId, opt) != NI_ERROR_NONE) {
90                         _ERR("Failed to generate application to native image [%s]", pkgId);
91                         return -1;
92                 }
93
94                 _INFO("Complete make application to native image");
95         }
96         return 0;
97 }
98
99 extern "C" int PKGMGR_MDPARSER_PLUGIN_UPGRADE(const char *pkgId, const char *appId, GList *list)
100 {
101         return PKGMGR_MDPARSER_PLUGIN_INSTALL(pkgId, appId, list);
102 }
103
104 extern "C" int PKGMGR_MDPARSER_PLUGIN_UNINSTALL(const char *pkgId, const char *appId, GList *list)
105 {
106         return 0;
107 }
108
109 extern "C" int PKGMGR_MDPARSER_PLUGIN_REMOVED(const char *pkgId, const char *appId, GList *list)
110 {
111         return PKGMGR_MDPARSER_PLUGIN_UPGRADE(pkgId, appId, list);
112 }
113
114 extern "C" int PKGMGR_MDPARSER_PLUGIN_CLEAN(const char *pkgId, const char *appId, GList *list)
115 {
116         if (pkgId == NULL) {
117                 return 0;
118         }
119
120         // Can be multiple apps in one package
121         if (strcmp(pkgId, prevFinishPkgId.c_str()) == 0) {
122                 _INFO("AOT Plugin(CLEAN) already run for same pkgId (%s)", pkgId);
123                 return 0;
124         }
125         prevFinishPkgId = pkgId;
126
127         finalizeNICommon();
128         return 0;
129 }
130
131 extern "C" int PKGMGR_MDPARSER_PLUGIN_UNDO(const char *pkgId, const char *appId, GList *list)
132 {
133         if (pkgId == NULL) {
134                 return 0;
135         }
136
137         // Can be multiple apps in one package
138         if (strcmp(pkgId, prevFinishPkgId.c_str()) == 0) {
139                 _INFO("AOT Plugin(UNDO) already run for same pkgId (%s)", pkgId);
140                 return 0;
141         }
142         prevFinishPkgId = pkgId;
143
144         finalizeNICommon();
145         return 0;
146 }