Add OOM exception handler
[platform/core/appfw/wgt-backend.git] / src / wgt / step / filesystem / step_wgt_patch_icons.cc
index c01c1c8..8bc647f 100644 (file)
@@ -17,6 +17,33 @@ namespace {
 
 const char kDefaultIconPath[] = "/usr/share/wgt-backend/default.png";
 
+bool PatchIcon(icon_x* icon, const bf::path& dst_path) {
+  bs::error_code error;
+  bf::path icon_text(icon->text);
+  bf::path icon_path = dst_path;
+  if (strcmp(icon->lang, DEFAULT_LOCALE)) {
+    icon_path += ".";
+    icon_path += icon->lang;
+  }
+  if (icon_text.has_extension())
+    icon_path += icon_text.extension();
+  else
+    icon_path += ".png";
+
+  bf::copy_file(icon->text, icon_path,
+      bf::copy_option::overwrite_if_exists, error);
+  if (error) {
+    LOG(ERROR) << "Failed to move icon from " << icon->text << " to "
+               << icon_path;
+    return false;
+  }
+  if (icon->text)
+    free(const_cast<char*>(icon->text));
+  icon->text = strdup(icon_path.c_str());
+
+  return true;
+}
+
 }  // namespace
 
 namespace wgt {
@@ -26,6 +53,13 @@ common_installer::Step::Status StepWgtPatchIcons::process() {
   bf::path common_icon_location = context_->pkg_path.get() / "shared" / "res";
   bs::error_code error;
   bf::create_directories(common_icon_location, error);
+  for (icon_x* icon :
+      GListRange<icon_x*>(context_->manifest_data.get()->icon)) {
+    bf::path icon_path = common_icon_location /
+        context_->manifest_data.get()->mainapp_id;
+    if (!PatchIcon(icon, icon_path))
+      return Status::ICON_ERROR;
+  }
   for (application_x* app :
       GListRange<application_x*>(context_->manifest_data.get()->application)) {
     if (strcmp(app->type, "webapp") != 0)
@@ -33,39 +67,24 @@ common_installer::Step::Status StepWgtPatchIcons::process() {
     if (app->icon) {
       // edit icon->text and copy icons to common location
       for (auto& icon : GListRange<icon_x*>(app->icon)) {
-        bf::path icon_text(icon->text);
         bf::path icon_path = common_icon_location / app->appid;
-        if (strcmp(icon->lang, DEFAULT_LOCALE)) {
-          icon_path += ".";
-          icon_path += icon->lang;
-        }
-        if (icon_text.has_extension())
-          icon_path += icon_text.extension();
-        else
-          icon_path += ".png";
-
-        bf::copy_file(icon->text, icon_path,
-                      bf::copy_option::overwrite_if_exists, error);
-        if (error) {
-          LOG(ERROR) << "Failed to move icon from " << icon->text << " to "
-                     << icon_path;
+        if (!PatchIcon(icon, icon_path))
           return Status::ICON_ERROR;
-        }
-        if (icon->text)
-          free(const_cast<char*>(icon->text));
-        icon->text = strdup(icon_path.c_str());
       }
     } else {
       LOG(INFO) << "Application provides no icon. Using Tizen default icon.";
       // create default icon if there is no icon at all
       bf::path icon_path = common_icon_location / app->appid;
       icon_path += ".png";
-      bf::copy_file(kDefaultIconPath, icon_path, bf::copy_option::overwrite_if_exists, error);
+      bf::copy_file(kDefaultIconPath, icon_path,
+              bf::copy_option::overwrite_if_exists, error);
       if (error) {
         LOG(ERROR) << "Failed to create default icon for web application";
         return Status::ICON_ERROR;
       }
       icon_x* icon = reinterpret_cast<icon_x*>(calloc(1, sizeof(icon_x)));
+      if (!icon)
+        return Status::ICON_ERROR;
       icon->text = strdup(icon_path.c_str());
       icon->lang = strdup(DEFAULT_LOCALE);
       app->icon = g_list_append(app->icon, icon);