Skip AOT in plugin when pkg contains ni files
[platform/core/dotnet/launcher.git] / NativeLauncher / installer-plugin / prefer_dotnet_aot_plugin.cc
old mode 100755 (executable)
new mode 100644 (file)
index f180739..68b3d91
  * limitations under the License.
  */
 
-#include "common.h"
+#include "ni_common.h"
 #include "log.h"
 #include "utils.h"
 
+#include <cstring>
+#include <vector>
+
+#include <vconf.h>
+#include <glib.h>
+#include <pkgmgr_installer_info.h>
+
 #ifdef  LOG_TAG
 #undef  LOG_TAG
 #endif
-#define LOG_TAG "NETCORE_INSTALLER_PLUGIN"
+#define LOG_TAG "DOTNET_INSTALLER_PLUGIN"
 
-#include <cstring>
-#include <vector>
-#include <sstream>
-#include <glib.h>
+bool aotPluginInstalled = false;
+bool aotPluginFinished = false;
 
-typedef struct Metadata {
-    const char *key;
-    const char *value;
-} Metadata;
+typedef struct metadata_s {
+       const char* key;
+       const char* value;
+} metadata_t;
 
-const std::string VALUE_TRUE = "true";
-const std::string mdKey = "http://tizen.org/metadata/prefer_dotnet_aot";
-extern "C" int PKGMGR_MDPARSER_PLUGIN_INSTALL (const char *pkgid, const char *appid, GList *list)
+extern "C" int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char *pkgId, const char *appId, GList *list)
 {
+       // Can be multiple apps in one package
+       if (aotPluginInstalled) {
+               _INFO("AOT plugin already installed");
+               return 0;
+       }
+       aotPluginInstalled = true;
+
+       int skipOpt = false;
+       if (!pkgmgr_installer_info_get_skip_optimization(&skipOpt)) {
+               if (skipOpt) {
+                       _DBG("Skip dotnet AOT");
+                       return 0;
+               }
+       }
+
+       bool doAOT = false;
+       GList* iter = list;
+       while (iter) {
+               metadata_t* md = static_cast<metadata_t*>(iter->data);
+               if (strcmp(AOT_METADATA_KEY, md->key) == 0) {
+                       if (strcmp(METADATA_VALUE_TRUE, md->value) == 0) {
+                               doAOT = true;
+                       }
+                       break;
+               }
+               iter = g_list_next(iter);
+       }
+
+       // if package contains native image, skip AOT.
+       std::string rootPath = getRootPath(pkgId);
+       if (!rootPath.empty()) {
+               if (exist(concatPath(rootPath, concatPath("bin", APP_NI_SUB_DIR)))) {
+                       _INFO("Package already contains native images. Skip native image generation");
+                       doAOT = false;
+               }
+       }
+
+       if (doAOT) {
+               _DBG("Prefer dotnet application AOT set TRUE");
+
+               if (initNICommon() != NI_ERROR_NONE) {
+                       _ERR("Fail to initialize NI Common");
+                       return -1;
+               }
+
+               NIOption* opt = getNIOption();
+               if (opt == nullptr) {
+                       _ERR("Fail to create option structure.");
+                       return -1;
+               }
+
+               if (createNIUnderPkgRoot(pkgId, opt) != NI_ERROR_NONE) {
+                       _ERR("Failed to generate application to native image [%s]", pkgId);
+                       return -1;
+               }
 
-    GList *tag = NULL;
-    bool mdValue = false;
-    Metadata *mdInfo = NULL;
-    tag = g_list_first(list);
-    while (tag) {
-        mdInfo = (Metadata*)tag->data;
-        if(mdInfo->key == mdKey && mdInfo->value == VALUE_TRUE) {
-            _DBG("Prefer dotnet application AOT set TRUE");
-            mdValue = true;
-        }
-        tag = g_list_next(tag);
-    }
-
-    if (mdValue) {
-        if (create_ni_under_pkg_root(pkgid) != 0)
-        {
-            _ERR("Failed to get root path from [%s]", pkgid);
-            return -1;
-        } else {
-            _DBG("Complete make application to native image");
-        }
-    }
-    return 0;
+               _INFO("Complete make application to native image");
+       }
+       return 0;
 }
 
-extern "C" int PKGMGR_MDPARSER_PLUGIN_UPGRADE (const char *pkgid, const char *appid, GList *list)
+extern "C" int PKGMGR_MDPARSER_PLUGIN_UPGRADE(const char *pkgId, const char *appId, GList *list)
 {
-    return PKGMGR_MDPARSER_PLUGIN_INSTALL(pkgid, appid, list);
+       return PKGMGR_MDPARSER_PLUGIN_INSTALL(pkgId, appId, list);
+}
+
+extern "C" int PKGMGR_MDPARSER_PLUGIN_UNINSTALL(const char *pkgId, const char *appId, GList *list)
+{
+       return 0;
+}
+
+extern "C" int PKGMGR_MDPARSER_PLUGIN_REMOVED(const char *pkgId, const char *appId, GList *list)
+{
+       return PKGMGR_MDPARSER_PLUGIN_UPGRADE(pkgId, appId, list);
+}
+
+extern "C" int PKGMGR_MDPARSER_PLUGIN_CLEAN(const char *pkgId, const char *appId, GList *list)
+{
+       // Can be multiple apps in one package
+       if (aotPluginFinished) {
+               _INFO("AOT plugin already finished(CLEAN)");
+               return 0;
+       }
+       aotPluginFinished = true;
+
+       finalizeNICommon();
+       return 0;
+}
+
+extern "C" int PKGMGR_MDPARSER_PLUGIN_UNDO(const char *pkgId, const char *appId, GList *list)
+{
+       // Can be multiple apps in one package
+       if (aotPluginFinished) {
+               _INFO("AOT plugin already finished(UNDO)");
+               return 0;
+       }
+       aotPluginFinished = true;
+
+       finalizeNICommon();
+       return 0;
 }