Add options to support crossgen2 (#351)
[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 #include <vconf.h>
21
22 #include <cstring>
23 #include <vector>
24 #include <glib.h>
25 #include <pkgmgr_installer_info.h>
26
27 #ifdef  LOG_TAG
28 #undef  LOG_TAG
29 #endif
30 #define LOG_TAG "DOTNET_INSTALLER_PLUGIN"
31
32 bool aotPluginInstalled = false;
33 bool aotPluginFinished = false;
34
35 extern "C" int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char *pkgId, const char *appId, GList *list)
36 {
37         // Can be multiple apps in one package
38         if (aotPluginInstalled) {
39                 _INFO("AOT plugin already installed");
40                 return 0;
41         }
42         aotPluginInstalled = true;
43
44         int skipOpt = false;
45         if (!pkgmgr_installer_info_get_skip_optimization(&skipOpt)) {
46                 if (skipOpt) {
47                         _DBG("Skip dotnet AOT");
48                         return 0;
49                 }
50         }
51
52         std::string metaValue = getMetadataValue(std::string(pkgId), AOT_METADATA_KEY);
53         if (metaValue.empty()) {
54                 _ERR("Failed to get metadata from [%s]", pkgId);
55                 return -1;
56         }
57
58         if (metaValue == METADATA_VALUE) {
59                 _DBG("Prefer dotnet application AOT set TRUE");
60
61                 if (initNICommon() != NI_ERROR_NONE) {
62                         _ERR("Fail to initialize NI Common");
63                         return -1;
64                 }
65
66                 NIOption* opt = getNIOption();
67                 if (opt == nullptr) {
68                         _ERR("Fail to create option structure.");
69                         return -1;
70                 }
71
72                 if (createNIUnderPkgRoot(pkgId, opt) != NI_ERROR_NONE) {
73                         _ERR("Failed to generate application to native image [%s]", pkgId);
74                         return -1;
75                 }
76
77                 _INFO("Complete make application to native image");
78         }
79         return 0;
80 }
81
82 extern "C" int PKGMGR_MDPARSER_PLUGIN_UPGRADE(const char *pkgId, const char *appId, GList *list)
83 {
84         return PKGMGR_MDPARSER_PLUGIN_INSTALL(pkgId, appId, list);
85 }
86
87 extern "C" int PKGMGR_MDPARSER_PLUGIN_UNINSTALL(const char *pkgId, const char *appId, GList *list)
88 {
89         return 0;
90 }
91
92 extern "C" int PKGMGR_MDPARSER_PLUGIN_REMOVED(const char *pkgId, const char *appId, GList *list)
93 {
94         return PKGMGR_MDPARSER_PLUGIN_UPGRADE(pkgId, appId, list);
95 }
96
97 extern "C" int PKGMGR_MDPARSER_PLUGIN_CLEAN(const char *pkgId, const char *appId, GList *list)
98 {
99         // Can be multiple apps in one package
100         if (aotPluginFinished) {
101                 _INFO("AOT plugin already finished(CLEAN)");
102                 return 0;
103         }
104         aotPluginFinished = true;
105
106         finalizeNICommon();
107         return 0;
108 }
109
110 extern "C" int PKGMGR_MDPARSER_PLUGIN_UNDO(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(UNDO)");
115                 return 0;
116         }
117         aotPluginFinished = true;
118
119         finalizeNICommon();
120         return 0;
121 }