Fix pkgmgrinfo_plugininfo_foreach_plugininfo
[platform/core/appfw/pkgmgr-info.git] / src / manager / pkginfo_manager.cc
index 1d3d120..bb9463b 100644 (file)
@@ -41,7 +41,7 @@
 #include "common/parcel/pkginfo_parcelable.hh"
 #include "common/parcel/query_parcelable.hh"
 #include "common/parcel/result_parcelable.hh"
-#include "logging.hh"
+#include "utils/logging.hh"
 
 #include "pkg_write_type.hh"
 #include "db_type.hh"
 
 namespace pcp = pkgmgr_common::parcel;
 
+namespace {
+
+int ValidateParcelable(
+    std::shared_ptr<pcp::AbstractParcelable> parcel,
+    pcp::ParcelableType parcel_type) {
+  if (parcel == nullptr) {
+    LOG(ERROR) << "Failed to get return parcelable";
+    return PMINFO_R_ERROR;
+  }
+
+  if (parcel->GetType() != parcel_type) {
+    LOG(ERROR) << "Invalid parcelable Type " << parcel->GetType() << ' ' << parcel_type;
+    return PMINFO_R_ERROR;
+  }
+
+  if (parcel->GetRequestResult() != PMINFO_R_OK) {
+    LOG(ERROR) << "Request fail";
+    return parcel->GetRequestResult();
+  }
+
+  return PMINFO_R_OK;
+}
+
+}  // namespace
+
 extern "C" EXPORT_API int _pkginfo_get_packages(uid_t uid,
     pkgmgrinfo_filter_x* filter, int flag, GHashTable* packages) {
   std::shared_ptr<pcp::AbstractParcelable> parcelable(
@@ -95,9 +120,9 @@ extern "C" EXPORT_API int _pkginfo_get_packages(uid_t uid,
     LOG(DEBUG) << "No packages meets given condition for user " << uid;
     return PMINFO_R_ENOENT;
   }
-  for (auto pkginfo : result_list)
-    g_hash_table_insert(packages, (gpointer)pkginfo->package,
-                        (gpointer)pkginfo);
+  for (auto& pkginfo : result_list)
+    g_hash_table_insert(packages, reinterpret_cast<gpointer>(pkginfo->package),
+                        reinterpret_cast<gpointer>(pkginfo.get()));
 
   return PMINFO_R_OK;
 }
@@ -113,20 +138,9 @@ extern "C" EXPORT_API int _pkginfo_get_depends_on(uid_t uid,
     return PMINFO_R_ERROR;
 
   auto ptr = client.GetResultParcel();
-  if (ptr == nullptr) {
-    LOG(ERROR) << "Fail to get return parcelable";
-    return PMINFO_R_ERROR;
-  }
-
-  if (ptr->GetRequestResult() != PMINFO_R_OK) {
-    LOG(ERROR) << "Request fail";
-    return ptr->GetRequestResult();
-  }
-
-  if (ptr->GetType() != pcp::ParcelableType::DepInfo) {
-    LOG(ERROR) << "Invalid parcelable type";
-    return PMINFO_R_ERROR;
-  }
+  int ret = ValidateParcelable(ptr, pcp::ParcelableType::DepInfo);
+  if (ret != PMINFO_R_OK)
+    return ret;
 
   std::shared_ptr<pcp::DepInfoParcelable> return_parcel(
       std::static_pointer_cast<pcp::DepInfoParcelable>(ptr));
@@ -171,10 +185,10 @@ extern "C" EXPORT_API int _appinfo_get_applications(uid_t uid,
   std::shared_ptr<pcp::AppInfoParcelable> return_parcel(
       std::static_pointer_cast<pcp::AppInfoParcelable>(ptr));
 
-  std::vector<application_x*> result_list = return_parcel->ExtractAppInfo();
-  for (application_x* app : result_list)
-    g_hash_table_insert(packages, (gpointer)app->appid,
-        (gpointer)app);
+  std::vector<std::shared_ptr<application_x>> result_list = return_parcel->ExtractAppInfo();
+  for (auto& app : result_list)
+    g_hash_table_insert(packages, reinterpret_cast<gpointer>(app->appid),
+        reinterpret_cast<gpointer>(app.get()));
 
   return PMINFO_R_OK;
 }
@@ -188,41 +202,29 @@ extern "C" EXPORT_API char* _appinfo_get_localed_label(
           pkgmgr_common::DBOperationType::OPERATION_TYPE_READ));
 
   pkgmgr_client::PkgInfoClient client(parcelable, uid,
-      pkgmgr_common::ReqType::QUERY);
+      pkgmgr_common::ReqType::READ_QUERY);
   if (!client.SendRequest())
     return nullptr;
-  auto ptr = client.GetResultParcel();
-  if (ptr == nullptr) {
-    LOG(ERROR) << "Fail to get return parcelable";
-    return nullptr;
-  }
-
-  if (ptr->GetRequestResult() != PMINFO_R_OK) {
-    LOG(ERROR) << "Request fail";
-    return nullptr;
-  }
 
-  if (ptr->GetType() != pcp::ParcelableType::Result) {
-    LOG(ERROR) << "Invalid parcelable type";
+  auto ptr = client.GetResultParcel();
+  if (ValidateParcelable(ptr, pcp::ParcelableType::Result) != PMINFO_R_OK)
     return nullptr;
-  }
 
   std::shared_ptr<pcp::ResultParcelable> return_parcel(
       std::static_pointer_cast<pcp::ResultParcelable>(ptr));
-
-  // result_list is vector of string vector
   char* label = nullptr;
   auto& result_list = return_parcel->GetResult();
+
   for (auto& result : result_list) {
-    // result is string vector
-    // it only has one string or not.
     if (!result.front() || (*result.front()).empty())
       continue;
+
     label = strdup((*result.front()).c_str());
     if (label == nullptr) {
       LOG(ERROR) << "Out of memory";
       return nullptr;
     }
+
     break;
   }
 
@@ -239,25 +241,14 @@ extern "C" EXPORT_API int _appinfo_get_datacontrol_info(
           pkgmgr_common::DBOperationType::OPERATION_TYPE_READ));
 
   pkgmgr_client::PkgInfoClient client(parcelable, uid,
-      pkgmgr_common::ReqType::QUERY);
+      pkgmgr_common::ReqType::READ_QUERY);
   if (!client.SendRequest())
     return PMINFO_R_ERROR;
 
   auto ptr = client.GetResultParcel();
-  if (ptr == nullptr) {
-    LOG(ERROR) << "Fail to get return parcelable";
-    return PMINFO_R_ERROR;
-  }
-
-  if (ptr->GetRequestResult() != PMINFO_R_OK) {
-    LOG(ERROR) << "Request fail";
-    return ptr->GetRequestResult();
-  }
-
-  if (ptr->GetType() != pcp::ParcelableType::Result) {
-    LOG(ERROR) << "Invalid parcelable type";
-    return PMINFO_R_ERROR;
-  }
+  int ret = ValidateParcelable(ptr, pcp::ParcelableType::Result);
+  if (ret != PMINFO_R_OK)
+    return ret;
 
   std::shared_ptr<pcp::ResultParcelable> return_parcel(
       std::static_pointer_cast<pcp::ResultParcelable>(ptr));
@@ -265,6 +256,7 @@ extern "C" EXPORT_API int _appinfo_get_datacontrol_info(
   auto& result_list = return_parcel->GetResult();
   if (result_list.size() == 0)
     return PMINFO_R_ENOENT;
+
   for (auto it = result_list.rbegin(); it != result_list.rend(); it++) {
     const auto& result = *it;
     if (result.size() != 2 || !result.front() || !result.back() ||
@@ -276,12 +268,14 @@ extern "C" EXPORT_API int _appinfo_get_datacontrol_info(
       LOG(ERROR) << "Out of memory";
       return PMINFO_R_ERROR;
     }
+
     char* tmp_access = strdup((*result.back()).c_str());
     if (tmp_access == nullptr) {
       LOG(ERROR) << "Out of memory";
       free(tmp_appid);
       return PMINFO_R_ERROR;
     }
+
     *appid = tmp_appid;
     *access = tmp_access;
 
@@ -300,42 +294,33 @@ extern "C" EXPORT_API int _appinfo_get_datacontrol_appid(
           pkgmgr_common::DBOperationType::OPERATION_TYPE_READ));
 
   pkgmgr_client::PkgInfoClient client(parcelable, uid,
-      pkgmgr_common::ReqType::QUERY);
+      pkgmgr_common::ReqType::READ_QUERY);
   if (!client.SendRequest())
     return PMINFO_R_ERROR;
 
   auto ptr = client.GetResultParcel();
-  if (ptr == nullptr) {
-    LOG(ERROR) << "Fail to get return parcelable";
-    return PMINFO_R_ERROR;
-  }
-
-  if (ptr->GetRequestResult() != PMINFO_R_OK) {
-    LOG(ERROR) << "Request fail";
-    return ptr->GetRequestResult();
-  }
-
-  if (ptr->GetType() != pcp::ParcelableType::Result) {
-    LOG(ERROR) << "Invalid parcelable type";
-    return PMINFO_R_ERROR;
-  }
+  int ret = ValidateParcelable(ptr, pcp::ParcelableType::Result);
+  if (ret != PMINFO_R_OK)
+    return ret;
 
   std::shared_ptr<pcp::ResultParcelable> return_parcel(
       std::static_pointer_cast<pcp::ResultParcelable>(ptr));
 
-  // result_list is vector of string vector
   auto& result_list = return_parcel->GetResult();
   if (result_list.size() == 0)
     return PMINFO_R_ENOENT;
+
   for (auto it = result_list.rbegin(); it != result_list.rend(); it++) {
     const auto& result = *it;
     if (result.size() != 1 || !result.front() || (*result.front()).empty())
       continue;
+
     *appid = strdup((*result.front()).c_str());
     if (*appid == nullptr) {
       LOG(ERROR) << "Out of memory";
       return PMINFO_R_ERROR;
     }
+
     return PMINFO_R_OK;
   }
 
@@ -353,33 +338,22 @@ extern "C" EXPORT_API int _appinfo_get_datacontrol_trusted_info(
           pkgmgr_common::DBOperationType::OPERATION_TYPE_READ));
 
   pkgmgr_client::PkgInfoClient client(parcelable, uid,
-      pkgmgr_common::ReqType::QUERY);
+      pkgmgr_common::ReqType::READ_QUERY);
   if (!client.SendRequest())
     return PMINFO_R_ERROR;
 
   auto ptr = client.GetResultParcel();
-  if (ptr == nullptr) {
-    LOG(ERROR) << "Fail to get return parcelable";
-    return PMINFO_R_ERROR;
-  }
-
-  if (ptr->GetRequestResult() != PMINFO_R_OK) {
-    LOG(ERROR) << "Request fail";
-    return ptr->GetRequestResult();
-  }
-
-  if (ptr->GetType() != pcp::ParcelableType::Result) {
-    LOG(ERROR) << "Invalid parcelable type";
-    return PMINFO_R_ERROR;
-  }
+  int ret = ValidateParcelable(ptr, pcp::ParcelableType::Result);
+  if (ret != PMINFO_R_OK)
+    return ret;
 
   std::shared_ptr<pcp::ResultParcelable> return_parcel(
       std::static_pointer_cast<pcp::ResultParcelable>(ptr));
 
-  // result_list is vector of string vector
   auto& result_list = return_parcel->GetResult();
   if (result_list.size() == 0)
     return PMINFO_R_ENOENT;
+
   for (auto it = result_list.rbegin(); it != result_list.rend(); it++) {
     const auto& result = *it;
     if (result.size() != 2 || !result.front() || !result.back() ||
@@ -391,14 +365,17 @@ extern "C" EXPORT_API int _appinfo_get_datacontrol_trusted_info(
       LOG(ERROR) << "Out of memory";
       return PMINFO_R_ERROR;
     }
+
     char* tmp_trusted = strdup((*result.back()).c_str());
     if (tmp_trusted == nullptr) {
       LOG(ERROR) << "Out of memory";
       free(tmp_appid);
       return PMINFO_R_ERROR;
     }
+
     *appid = tmp_appid;
     *trusted = tmp_trusted;
+
     return PMINFO_R_OK;
   }
 
@@ -415,30 +392,18 @@ extern "C" EXPORT_API int _appinfo_get_datacontrol_privileges(
           pkgmgr_common::DBOperationType::OPERATION_TYPE_READ));
 
   pkgmgr_client::PkgInfoClient client(parcelable, uid,
-      pkgmgr_common::ReqType::QUERY);
+      pkgmgr_common::ReqType::READ_QUERY);
   if (!client.SendRequest())
     return PMINFO_R_ERROR;
 
   auto ptr = client.GetResultParcel();
-  if (ptr == nullptr) {
-    LOG(ERROR) << "Fail to get return parcelable";
-    return PMINFO_R_ERROR;
-  }
-
-  if (ptr->GetRequestResult() != PMINFO_R_OK) {
-    LOG(ERROR) << "Request fail";
-    return ptr->GetRequestResult();
-  }
-
-  if (ptr->GetType() != pcp::ParcelableType::Result) {
-    LOG(ERROR) << "Invalid parcelable type";
-    return PMINFO_R_ERROR;
-  }
+  int ret = ValidateParcelable(ptr, pcp::ParcelableType::Result);
+  if (ret != PMINFO_R_OK)
+    return ret;
 
   std::shared_ptr<pcp::ResultParcelable> return_parcel(
       std::static_pointer_cast<pcp::ResultParcelable>(ptr));
 
-  // result_list is vector of string vector
   auto& result_list = return_parcel->GetResult();
   if (result_list.size() == 0)
     return PMINFO_R_ENOENT;
@@ -446,6 +411,7 @@ extern "C" EXPORT_API int _appinfo_get_datacontrol_privileges(
   for (auto& result : result_list) {
     if (result.size() != 1 || !result.front() || (*result.front()).empty())
       return PMINFO_R_ERROR;
+
     char* privilege = strdup((*result.front()).c_str());
     if (privilege == nullptr) {
       LOG(ERROR) << "Out of memory";
@@ -466,25 +432,14 @@ extern "C" EXPORT_API int _appinfo_get_appcontrol_privileges(
           pkgmgr_common::DBOperationType::OPERATION_TYPE_READ));
 
   pkgmgr_client::PkgInfoClient client(parcelable, uid,
-      pkgmgr_common::ReqType::QUERY);
+      pkgmgr_common::ReqType::READ_QUERY);
   if (!client.SendRequest())
     return PMINFO_R_ERROR;
 
   auto ptr = client.GetResultParcel();
-  if (ptr == nullptr) {
-    LOG(ERROR) << "Fail to get return parcelable";
-    return PMINFO_R_ERROR;
-  }
-
-  if (ptr->GetRequestResult() != PMINFO_R_OK) {
-    LOG(ERROR) << "Request fail";
-    return ptr->GetRequestResult();
-  }
-
-  if (ptr->GetType() != pcp::ParcelableType::Result) {
-    LOG(ERROR) << "Invalid parcelable type";
-    return PMINFO_R_ERROR;
-  }
+  int ret = ValidateParcelable(ptr, pcp::ParcelableType::Result);
+  if (ret != PMINFO_R_OK)
+    return ret;
 
   std::shared_ptr<pcp::ResultParcelable> return_parcel(
       std::static_pointer_cast<pcp::ResultParcelable>(ptr));
@@ -498,6 +453,7 @@ extern "C" EXPORT_API int _appinfo_get_appcontrol_privileges(
     if (result.size() != 2 || !result.front() || !result.back() ||
         (*result.front()).empty() || (*result.back()).empty())
       return PMINFO_R_ERROR;
+
     std::stringstream ss((*result.front()));
     std::string token;
     while (std::getline(ss, token, '|')) {
@@ -530,37 +486,26 @@ extern "C" EXPORT_API int _plugininfo_get_appids(
           pkgmgr_common::DBOperationType::OPERATION_TYPE_READ));
 
   pkgmgr_client::PkgInfoClient client(parcelable, _getuid(),
-      pkgmgr_common::ReqType::QUERY);
+      pkgmgr_common::ReqType::READ_QUERY);
   if (!client.SendRequest())
     return PMINFO_R_ERROR;
 
   auto ptr = client.GetResultParcel();
-  if (ptr == nullptr) {
-    LOG(ERROR) << "Fail to get return parcelable";
-    return PMINFO_R_ERROR;
-  }
-
-  if (ptr->GetRequestResult() != PMINFO_R_OK) {
-    LOG(ERROR) << "Request fail";
-    return ptr->GetRequestResult();
-  }
-
-  if (ptr->GetType() != pcp::ParcelableType::Result) {
-    LOG(ERROR) << "Invalid parcelable type";
-    return PMINFO_R_ERROR;
-  }
+  int ret = ValidateParcelable(ptr, pcp::ParcelableType::Result);
+  if (ret != PMINFO_R_OK)
+    return ret;
 
   std::shared_ptr<pcp::ResultParcelable> return_parcel(
       std::static_pointer_cast<pcp::ResultParcelable>(ptr));
 
+  auto& result_list = return_parcel->GetResult();
+  if (result_list.size() == 0)
+    return PMINFO_R_ENOENT;
+
   if (return_parcel->GetCol() != 1) {
     LOG(ERROR) << "Invalid result";
     return PMINFO_R_ERROR;
   }
-  // result_list is vector of string vector
-  auto& result_list = return_parcel->GetResult();
-  if (result_list.size() == 0)
-    return PMINFO_R_ENOENT;
 
   for (auto& result : result_list) {
     if (result.size() != 1) {
@@ -568,6 +513,7 @@ extern "C" EXPORT_API int _plugininfo_get_appids(
       g_list_free_full(*list, free);
       return PMINFO_R_ERROR;
     }
+
     *list = g_list_append(*list, strdup((*result[0]).c_str()));
   }
 
@@ -587,6 +533,7 @@ static int __convert_update_type(const char* type,
     *convert_type = PMINFO_UPDATEINFO_OPTIONAL;
   else
     return -1;
+
   return 0;
 }
 
@@ -597,8 +544,10 @@ static void __free_update_info(gpointer data) {
 
   if (update_info->pkgid)
     free(reinterpret_cast<void*>(update_info->pkgid));
+
   if (update_info->version)
     free(reinterpret_cast<void*>(update_info->version));
+
   free(reinterpret_cast<void*>(update_info));
 }
 
@@ -617,30 +566,19 @@ extern "C" EXPORT_API int _get_pkg_updateinfo(const char* pkgid,
   }
 
   std::shared_ptr<pcp::AbstractParcelable> parcelable(
-      new pcp::QueryParcelable(uid, std::move(info),
+      new pcp::QueryParcelable(uid, info,
           pkgmgr_common::DBType::DB_TYPE_FILE_PKGDB,
           pkgmgr_common::DBOperationType::OPERATION_TYPE_READ));
 
   pkgmgr_client::PkgInfoClient client(parcelable, uid,
-      pkgmgr_common::ReqType::QUERY);
+      pkgmgr_common::ReqType::READ_QUERY);
   if (!client.SendRequest())
     return PMINFO_R_ERROR;
 
   auto ptr = client.GetResultParcel();
-  if (ptr == nullptr) {
-    LOG(ERROR) << "Fail to get return parcelable";
-    return PMINFO_R_ERROR;
-  }
-
-  if (ptr->GetRequestResult() != PMINFO_R_OK) {
-    LOG(ERROR) << "Request fail";
-    return ptr->GetRequestResult();
-  }
-
-  if (ptr->GetType() != pcp::ParcelableType::Result) {
-    LOG(ERROR) << "Invalid parcelable type";
-    return PMINFO_R_ERROR;
-  }
+  ret = ValidateParcelable(ptr, pcp::ParcelableType::Result);
+  if (ret != PMINFO_R_OK)
+    return ret;
 
   std::shared_ptr<pcp::ResultParcelable> return_parcel(
       std::static_pointer_cast<pcp::ResultParcelable>(ptr));
@@ -661,6 +599,7 @@ extern "C" EXPORT_API int _get_pkg_updateinfo(const char* pkgid,
       g_slist_free_full(tmp_list, __free_update_info);
       return PMINFO_R_ERROR;
     }
+
     updateinfo_x* update_info = reinterpret_cast<updateinfo_x*>(
         calloc(1, sizeof(updateinfo_x)));
     if (update_info == nullptr) {
@@ -668,15 +607,18 @@ extern "C" EXPORT_API int _get_pkg_updateinfo(const char* pkgid,
       g_slist_free_full(tmp_list, __free_update_info);
       return PMINFO_R_ERROR;
     }
+
     update_info->pkgid = strdup((*result[0]).c_str());
     update_info->version = strdup((*result[1]).c_str());
     pkgmgrinfo_updateinfo_update_type convert_type;
+
     ret = __convert_update_type((*result[2]).c_str(), &convert_type);
     if (ret != 0) {
       __free_update_info(update_info);
       g_slist_free_full(tmp_list, __free_update_info);
       return PMINFO_R_ERROR;
     }
+
     update_info->type = static_cast<int>(convert_type);
     tmp_list = g_slist_append(tmp_list, update_info);
   }
@@ -715,27 +657,12 @@ extern "C" EXPORT_API int _pkginfo_set_usr_installed_storage(const char* pkgid,
           pkgmgr_common::DBOperationType::OPERATION_TYPE_WRITE));
 
   pkgmgr_client::PkgInfoClient client(parcelable, uid,
-      pkgmgr_common::ReqType::QUERY);
+      pkgmgr_common::ReqType::WRITE_QUERY);
   if (!client.SendRequest())
     return PMINFO_R_ERROR;
 
-  auto ptr = client.GetResultParcel();
-  if (ptr == nullptr) {
-    LOG(ERROR) << "Fail to get return parcelable";
-    return PMINFO_R_ERROR;
-  }
-
-  if (ptr->GetRequestResult() != PMINFO_R_OK) {
-    LOG(ERROR) << "Request fail";
-    return ptr->GetRequestResult();
-  }
-
-  if (ptr->GetType() != pcp::ParcelableType::Result) {
-    LOG(ERROR) << "Invalid parcelable type";
-    return PMINFO_R_ERROR;
-  }
-
-  return PMINFO_R_OK;
+  return ValidateParcelable(
+      client.GetResultParcel(), pcp::ParcelableType::Result);
 }
 
 extern "C" EXPORT_API int _certinfo_compare_pkg_certinfo(const char* l_pkgid,
@@ -746,25 +673,14 @@ extern "C" EXPORT_API int _certinfo_compare_pkg_certinfo(const char* l_pkgid,
           pkgmgr_common::DBType::DB_TYPE_FILE_CERTDB,
           pkgmgr_common::DBOperationType::OPERATION_TYPE_READ));
   pkgmgr_client::PkgInfoClient client(parcelable, 0,
-      pkgmgr_common::ReqType::QUERY);
+      pkgmgr_common::ReqType::READ_QUERY);
   if (!client.SendRequest())
     return PMINFO_R_ERROR;
 
   auto ptr = client.GetResultParcel();
-  if (ptr == nullptr) {
-    LOG(ERROR) << "Fail to get return parcelable";
-    return PMINFO_R_ERROR;
-  }
-
-  if (ptr->GetRequestResult() != PMINFO_R_OK) {
-    LOG(ERROR) << "Request fail";
-    return ptr->GetRequestResult();
-  }
-
-  if (ptr->GetType() != pcp::ParcelableType::Result) {
-    LOG(ERROR) << "Invalid parcelable type";
-    return PMINFO_R_ERROR;
-  }
+  int ret = ValidateParcelable(ptr, pcp::ParcelableType::Result);
+  if (ret != PMINFO_R_OK)
+    return ret;
 
   std::shared_ptr<pcp::ResultParcelable> return_parcel(
       std::static_pointer_cast<pcp::ResultParcelable>(
@@ -804,25 +720,14 @@ extern "C" EXPORT_API int _certinfo_compare_app_certinfo(uid_t uid,
           pkgmgr_common::DBType::DB_TYPE_FILE_PKGDB,
           pkgmgr_common::DBOperationType::OPERATION_TYPE_READ));
   pkgmgr_client::PkgInfoClient client(parcelable, uid,
-      pkgmgr_common::ReqType::QUERY);
+      pkgmgr_common::ReqType::READ_QUERY);
   if (!client.SendRequest())
     return PMINFO_R_ERROR;
 
   auto ptr = client.GetResultParcel();
-  if (ptr == nullptr) {
-    LOG(ERROR) << "Fail to get return parcelable";
-    return PMINFO_R_ERROR;
-  }
-
-  if (ptr->GetRequestResult() != PMINFO_R_OK) {
-    LOG(ERROR) << "Request fail";
-    return ptr->GetRequestResult();
-  }
-
-  if (ptr->GetType() != pcp::ParcelableType::Result) {
-    LOG(ERROR) << "Invalid parcelable type";
-    return PMINFO_R_ERROR;
-  }
+  int ret = ValidateParcelable(ptr, pcp::ParcelableType::Result);
+  if (ret != PMINFO_R_OK)
+    return ret;
 
   std::shared_ptr<pcp::ResultParcelable> return_parcel(
       std::static_pointer_cast<pcp::ResultParcelable>(ptr));
@@ -864,26 +769,12 @@ extern "C" EXPORT_API int _parser_execute_write_query(
           pkgmgr_common::DBOperationType::OPERATION_TYPE_WRITE));
 
   pkgmgr_client::PkgInfoClient client(parcelable, uid,
-                                      pkgmgr_common::ReqType::QUERY);
+                                      pkgmgr_common::ReqType::WRITE_QUERY);
   if (!client.SendRequest())
-    return -1;
-
-  auto ptr = client.GetResultParcel();
-  if (ptr == nullptr) {
-    LOG(ERROR) << "Fail to get return parcelable";
-    return -1;
-  }
-
-  if (ptr->GetRequestResult() != PMINFO_R_OK) {
-    LOG(ERROR) << "Request fail";
-    return -1;
-  }
+    return PMINFO_R_ERROR;
 
-  if (ptr->GetType() != pcp::ParcelableType::Result) {
-    LOG(ERROR) << "Invalid parcelable type";
-    return -1;
-  }
-  return 0;
+  return ValidateParcelable(
+      client.GetResultParcel(), pcp::ParcelableType::Result);
 }
 
 extern "C" EXPORT_API int _parser_execute_write_queries(
@@ -899,122 +790,68 @@ extern "C" EXPORT_API int _parser_execute_write_queries(
   }
 
   std::shared_ptr<pcp::AbstractParcelable> parcelable(
-      new pcp::QueryParcelable(uid, std::move(queries),
+      new pcp::QueryParcelable(uid, queries,
           pkgmgr_common::DBType::DB_TYPE_FILE_PKGDB,
           pkgmgr_common::DBOperationType::OPERATION_TYPE_WRITE));
 
   pkgmgr_client::PkgInfoClient client(parcelable, uid,
-      pkgmgr_common::ReqType::QUERY);
+      pkgmgr_common::ReqType::WRITE_QUERY);
   if (!client.SendRequest())
-    return -1;
-
-  auto ptr = client.GetResultParcel();
-  if (ptr == nullptr) {
-    LOG(ERROR) << "Fail to get return parcelable";
-    return -1;
-  }
-
-  if (ptr->GetRequestResult() != PMINFO_R_OK) {
-    LOG(ERROR) << "Request fail";
-    return -1;
-  }
-
-  if (ptr->GetType() != pcp::ParcelableType::Result) {
-    LOG(ERROR) << "Invalid parcelable type";
-    return -1;
-  }
+    return PMINFO_R_ERROR;
 
-  return 0;
+  return ValidateParcelable(
+      client.GetResultParcel(), pcp::ParcelableType::Result);
 }
 
 extern "C" EXPORT_API int _parser_insert_manifest_info(
     manifest_x* mfx, uid_t uid) {
   auto parcelable =
       std::make_shared<pcp::PkgInfoParcelable>(uid,
-          std::vector<package_x*>{mfx}, pkgmgr_common::PkgWriteType::Insert, false);
+          std::vector<std::shared_ptr<package_x>>(1,
+              std::shared_ptr<package_x>(mfx, [] (package_x*) -> void {})),
+              pkgmgr_common::PkgWriteType::Insert, false);
 
   pkgmgr_client::PkgInfoClient client(parcelable, uid,
       pkgmgr_common::ReqType::SET_PKG_INFO);
   if (!client.SendRequest())
-    return -1;
-
-  auto ptr = client.GetResultParcel();
-  if (ptr == nullptr) {
-    LOG(ERROR) << "Fail to get return parcelable";
-    return -1;
-  }
-
-  if (ptr->GetRequestResult() != PMINFO_R_OK) {
-    LOG(ERROR) << "Request fail";
-    return -1;
-  }
-
-  if (ptr->GetType() != pcp::ParcelableType::Result) {
-    LOG(ERROR) << "Invalid parcelable type";
-    return -1;
-  }
+    return PMINFO_R_ERROR;
 
-  return 0;
+  return ValidateParcelable(
+      client.GetResultParcel(), pcp::ParcelableType::Result);
 }
 
 extern "C" EXPORT_API int _parser_update_manifest_info(
     manifest_x* mfx, uid_t uid) {
   auto parcelable =
       std::make_shared<pcp::PkgInfoParcelable>(uid,
-          std::vector<package_x*>{mfx}, pkgmgr_common::PkgWriteType::Update, false);
+          std::vector<std::shared_ptr<package_x>>(1,
+              std::shared_ptr<package_x>(mfx, [] (package_x*) -> void {})),
+              pkgmgr_common::PkgWriteType::Update, false);
 
   pkgmgr_client::PkgInfoClient client(parcelable, uid,
       pkgmgr_common::ReqType::SET_PKG_INFO);
   if (!client.SendRequest())
-    return -1;
-
-  auto ptr = client.GetResultParcel();
-  if (ptr == nullptr) {
-    LOG(ERROR) << "Fail to get return parcelable";
-    return -1;
-  }
-
-  if (ptr->GetRequestResult() != PMINFO_R_OK) {
-    LOG(ERROR) << "Request fail";
-    return -1;
-  }
-
-  if (ptr->GetType() != pcp::ParcelableType::Result) {
-    LOG(ERROR) << "Invalid parcelable type";
-    return -1;
-  }
+    return PMINFO_R_ERROR;
 
-  return 0;
+  return ValidateParcelable(
+      client.GetResultParcel(), pcp::ParcelableType::Result);
 }
 
 extern "C" EXPORT_API int _parser_delete_manifest_info(
     manifest_x* mfx, uid_t uid) {
   auto parcelable =
       std::make_shared<pcp::PkgInfoParcelable>(uid,
-          std::vector<package_x*>{mfx}, pkgmgr_common::PkgWriteType::Delete, false);
+          std::vector<std::shared_ptr<package_x>>(1,
+              std::shared_ptr<package_x>(mfx, [] (package_x*) -> void {})),
+              pkgmgr_common::PkgWriteType::Delete, false);
 
   pkgmgr_client::PkgInfoClient client(parcelable, uid,
       pkgmgr_common::ReqType::SET_PKG_INFO);
   if (!client.SendRequest())
-    return -1;
-
-  auto ptr = client.GetResultParcel();
-  if (ptr == nullptr) {
-    LOG(ERROR) << "Fail to get return parcelable";
-    return -1;
-  }
-
-  if (ptr->GetRequestResult() != PMINFO_R_OK) {
-    LOG(ERROR) << "Request fail";
-    return -1;
-  }
-
-  if (ptr->GetType() != pcp::ParcelableType::Result) {
-    LOG(ERROR) << "Invalid parcelable type";
-    return -1;
-  }
+    return PMINFO_R_ERROR;
 
-  return 0;
+  return ValidateParcelable(
+      client.GetResultParcel(), pcp::ParcelableType::Result);
 }
 
 extern "C" EXPORT_API int _pkginfo_insert_certinfo(const char* pkgid,
@@ -1027,23 +864,8 @@ extern "C" EXPORT_API int _pkginfo_insert_certinfo(const char* pkgid,
   if (!client.SendRequest())
     return PMINFO_R_ERROR;
 
-  auto ptr = client.GetResultParcel();
-  if (ptr == nullptr) {
-    LOG(ERROR) << "Fail to get return parcelable";
-    return PMINFO_R_ERROR;
-  }
-
-  if (ptr->GetRequestResult() != PMINFO_R_OK) {
-    LOG(ERROR) << "Request fail";
-    return ptr->GetRequestResult();
-  }
-
-  if (ptr->GetType() != pcp::ParcelableType::Result) {
-    LOG(ERROR) << "Invalid parcelable type";
-    return PMINFO_R_ERROR;
-  }
-
-  return PMINFO_R_OK;
+  return ValidateParcelable(
+      client.GetResultParcel(), pcp::ParcelableType::Result);
 }
 
 extern "C" EXPORT_API int _pkginfo_get_certinfo(const char* pkgid,
@@ -1058,20 +880,9 @@ extern "C" EXPORT_API int _pkginfo_get_certinfo(const char* pkgid,
     return PMINFO_R_ERROR;
 
   auto ptr = client.GetResultParcel();
-  if (ptr == nullptr) {
-    LOG(ERROR) << "Fail to get return parcelable";
-    return PMINFO_R_ERROR;
-  }
-
-  if (ptr->GetRequestResult() != PMINFO_R_OK) {
-    LOG(ERROR) << "Request fail";
-    return ptr->GetRequestResult();
-  }
-
-  if (ptr->GetType() != pcp::ParcelableType::CertInfo) {
-    LOG(ERROR) << "Invalid parcelable type";
-    return PMINFO_R_ERROR;
-  }
+  int ret = ValidateParcelable(ptr, pcp::ParcelableType::CertInfo);
+  if (ret != PMINFO_R_OK)
+    return ret;
 
   std::shared_ptr<pcp::CertInfoParcelable> return_parcel(
       std::static_pointer_cast<pcp::CertInfoParcelable>(ptr));
@@ -1089,6 +900,7 @@ extern "C" EXPORT_API int _pkginfo_get_certinfo(const char* pkgid,
     cert->cert_info[i] = certinfo->cert_info[i];
     certinfo->cert_info[i] = nullptr;
   }
+
   for (int i = 0; i < MAX_CERT_TYPE; i++)
     cert->cert_id[i] = certinfo->cert_id[i];
 
@@ -1105,56 +917,27 @@ extern "C" EXPORT_API int _pkginfo_delete_certinfo(const char* pkgid) {
           pkgmgr_common::DBOperationType::OPERATION_TYPE_WRITE));
 
   pkgmgr_client::PkgInfoClient client(parcelable, 0,
-                                      pkgmgr_common::ReqType::QUERY);
+                                      pkgmgr_common::ReqType::WRITE_QUERY);
   if (!client.SendRequest())
     return PMINFO_R_ERROR;
 
-  auto ptr = client.GetResultParcel();
-  if (ptr == nullptr) {
-    LOG(ERROR) << "Fail to get return parcelable";
-    return PMINFO_R_ERROR;
-  }
-
-  if (ptr->GetRequestResult() != PMINFO_R_OK) {
-    LOG(ERROR) << "Request fail";
-    return PMINFO_R_ERROR;
-  }
-
-  if (ptr->GetType() != pcp::ParcelableType::Result) {
-    LOG(ERROR) << "Invalid parcelable type";
-    return PMINFO_R_ERROR;
-  }
-
-  return PMINFO_R_OK;
+  return ValidateParcelable(
+      client.GetResultParcel(), pcp::ParcelableType::Result);
 }
 
-extern "C" EXPORT_API int _parser_clear_cache_memory_db(uid_t uid) {
+extern "C" EXPORT_API int _parser_update_pending_cache(const char* pkgid) {
   std::shared_ptr<pcp::AbstractParcelable> parcelable(
-      new pcp::CommandParcelable(uid, CommandType::RemoveCache));
+      new pcp::CommandParcelable(_getuid(),
+          CommandType::UpdatePendingCache, { pkgid }));
 
-  pkgmgr_client::PkgInfoClient client(parcelable, uid,
+  pkgmgr_client::PkgInfoClient client(parcelable, _getuid(),
                                       pkgmgr_common::ReqType::COMMAND);
 
   if (!client.SendRequest())
     return PMINFO_R_ERROR;
 
-  auto ptr = client.GetResultParcel();
-  if (ptr == nullptr) {
-    LOG(ERROR) << "Fail to get return parcelable";
-    return PMINFO_R_ERROR;
-  }
-
-  if (ptr->GetRequestResult() != PMINFO_R_OK) {
-    LOG(ERROR) << "Request fail";
-    return PMINFO_R_ERROR;
-  }
-
-  if (ptr->GetType() != pcp::ParcelableType::Result) {
-    LOG(ERROR) << "Invalid parcelable type";
-    return PMINFO_R_ERROR;
-  }
-
-  return PMINFO_R_OK;
+  return  ValidateParcelable(
+      client.GetResultParcel(), pcp::ParcelableType::Result);
 }
 
 static int __create_and_initialize_db(uid_t uid) {
@@ -1165,32 +948,17 @@ static int __create_and_initialize_db(uid_t uid) {
                                       pkgmgr_common::ReqType::CREATE_DB);
 
   if (!client.SendRequest())
-    return -1;
-
-  auto ptr = client.GetResultParcel();
-  if (ptr == nullptr)   {
-    LOG(ERROR) << "Fail to get return parcelable";
-    return -1;
-  }
-
-  if (ptr->GetRequestResult() != PMINFO_R_OK) {
-    LOG(ERROR) << "Request fail";
-    return -1;
-  }
-
-  if (ptr->GetType() != pcp::ParcelableType::Result) {
-    LOG(ERROR) << "Invalid parcelable type";
-    return -1;
-  }
+    return PMINFO_R_ERROR;
 
-  return 0;
+  return ValidateParcelable(
+      client.GetResultParcel(), pcp::ParcelableType::Result);
 }
 
 extern "C" EXPORT_API int _parser_create_and_initialize_db(uid_t uid) {
   if (__create_and_initialize_db(uid) < 0) {
     LOG(ERROR) << "Fail to initialize db";
-    return -1;
+    return PMINFO_R_ERROR;
   }
 
-  return 0;
+  return PMINFO_R_OK;
 }