Refactoring 99/93299/2
authorPiotr Ganicz <p.ganicz@samsung.com>
Fri, 21 Oct 2016 11:11:53 +0000 (13:11 +0200)
committerjongmyeong ko <jongmyeong.ko@samsung.com>
Thu, 3 Nov 2016 14:14:36 +0000 (07:14 -0700)
Change-Id: Ia18a60b030852dfba346c7bae092553e13ea0e87

src/common/pkgmgr_registration.cc

index e2e45eb89e81b79544940b4453de9331f815a333..245038a372ffdfd664accfa3eb4099371cb3106b 100644 (file)
 #include <unistd.h>
 
 #include <vector>
+#include <utility>
 
 namespace bf = boost::filesystem;
 
+
 namespace {
 
+using CertInfo = common_installer::CertificateInfo;
+
+bool RegisterCertificate(pkgmgr_instcertinfo_h handle,
+const ValidationCore::CertificatePtr& certPtr, pkgmgr_instcert_type type) {
+    if (certPtr) {
+      if (pkgmgr_installer_set_cert_value(handle, type,
+          const_cast<char*>(certPtr->getBase64().c_str())) < 0) {
+        pkgmgr_installer_destroy_certinfo_set_handle(handle);
+        LOG(ERROR) << "pkgmgrInstallerSetCertValue fail";
+        return false;
+      }
+    }
+    return true;
+}
+
+typedef
+Property<ValidationCore::CertificatePtr> CertInfo::*propertyOfCertInf;
+
+const
+std::vector<std::pair<propertyOfCertInf, pkgmgr_instcert_type>>
+certPtrToInstCertType = {
+  {&CertInfo::author_certificate, PM_SET_AUTHOR_INTERMEDIATE_CERT},
+  {&CertInfo::author_root_certificate, PM_SET_AUTHOR_ROOT_CERT},
+  {&CertInfo::distributor_certificate, PM_SET_DISTRIBUTOR_SIGNER_CERT},
+  {&CertInfo::distributor_intermediate_certificate,
+      PM_SET_DISTRIBUTOR_INTERMEDIATE_CERT},
+  {&CertInfo::distributor_root_certificate, PM_SET_DISTRIBUTOR_ROOT_CERT},
+  {&CertInfo::distributor2_certificate, PM_SET_DISTRIBUTOR2_SIGNER_CERT},
+  {&CertInfo::distributor2_intermediate_certificate,
+      PM_SET_DISTRIBUTOR2_INTERMEDIATE_CERT},
+  {&CertInfo::distributor2_root_certificate, PM_SET_DISTRIBUTOR2_ROOT_CERT}
+};
+
 bool RegisterCertificates(
-    const common_installer::CertificateInfo& cert_info,
+    const CertInfo& cert_info,
     const std::string& pkgid, uid_t uid) {
   pkgmgr_instcertinfo_h handle;
   if (pkgmgr_installer_create_certinfo_set_handle(&handle) < 0) {
     LOG(ERROR) << "Cannot create pkgmgr_instcertinfo_h";
     return false;
   }
-
-  const auto& author_cert = cert_info.author_certificate.get();
-  if (author_cert) {
-    if (pkgmgr_installer_set_cert_value(handle, PM_SET_AUTHOR_SIGNER_CERT,
-        const_cast<char*>(author_cert->getBase64().c_str())) < 0) {
-      pkgmgr_installer_destroy_certinfo_set_handle(handle);
-      LOG(ERROR) << "pkgmgrInstallerSetCertValue fail";
-      return false;
-    }
-  }
-
-  const auto& author_im_cert = cert_info.author_intermediate_certificate.get();
-  if (author_im_cert) {
-    if (pkgmgr_installer_set_cert_value(handle, PM_SET_AUTHOR_INTERMEDIATE_CERT,
-        const_cast<char*>(author_im_cert->getBase64().c_str())) < 0) {
-      pkgmgr_installer_destroy_certinfo_set_handle(handle);
-      LOG(ERROR) << "pkgmgrInstallerSetCertValue fail";
-      return false;
-    }
-  }
-
-  const auto& author_root_cert = cert_info.author_root_certificate.get();
-  if (author_root_cert) {
-    if (pkgmgr_installer_set_cert_value(handle, PM_SET_AUTHOR_ROOT_CERT,
-        const_cast<char*>(author_root_cert->getBase64().c_str())) < 0) {
-      pkgmgr_installer_destroy_certinfo_set_handle(handle);
-      LOG(ERROR) << "pkgmgrInstallerSetCertValue fail";
-      return false;
-    }
-  }
-
-  const auto& dist_cert = cert_info.distributor_certificate.get();
-  if (dist_cert) {
-    if (pkgmgr_installer_set_cert_value(handle, PM_SET_DISTRIBUTOR_SIGNER_CERT,
-        const_cast<char*>(dist_cert->getBase64().c_str())) < 0) {
-      pkgmgr_installer_destroy_certinfo_set_handle(handle);
-      LOG(ERROR) << "pkgmgrInstallerSetCertValue fail";
-      return false;
-    }
-  }
-
-  const auto& dist_im_cert =
-      cert_info.distributor_intermediate_certificate.get();
-  if (dist_im_cert) {
-    if (pkgmgr_installer_set_cert_value(handle,
-        PM_SET_DISTRIBUTOR_INTERMEDIATE_CERT,
-        const_cast<char*>(dist_im_cert->getBase64().c_str())) < 0) {
-      pkgmgr_installer_destroy_certinfo_set_handle(handle);
-      LOG(ERROR) << "pkgmgrInstallerSetCertValue fail";
-      return false;
-    }
-  }
-
-  const auto& dist_root_cert = cert_info.distributor_root_certificate.get();
-  if (dist_root_cert) {
-    if (pkgmgr_installer_set_cert_value(handle, PM_SET_DISTRIBUTOR_ROOT_CERT,
-        const_cast<char*>(dist_root_cert->getBase64().c_str())) < 0) {
-      pkgmgr_installer_destroy_certinfo_set_handle(handle);
-      LOG(ERROR) << "pkgmgrInstallerSetCertValue fail";
-      return false;
-    }
-  }
-
-  const auto& dist2_cert = cert_info.distributor2_certificate.get();
-  if (dist2_cert) {
-    if (pkgmgr_installer_set_cert_value(handle, PM_SET_DISTRIBUTOR2_SIGNER_CERT,
-        const_cast<char*>(dist2_cert->getBase64().c_str())) < 0) {
-      pkgmgr_installer_destroy_certinfo_set_handle(handle);
-      LOG(ERROR) << "pkgmgrInstallerSetCertValue fail";
-      return false;
-    }
-  }
-
-  const auto& dist2_im_cert =
-      cert_info.distributor2_intermediate_certificate.get();
-  if (dist2_im_cert) {
-    if (pkgmgr_installer_set_cert_value(handle,
-        PM_SET_DISTRIBUTOR2_INTERMEDIATE_CERT,
-        const_cast<char*>(dist2_im_cert->getBase64().c_str())) < 0) {
-      pkgmgr_installer_destroy_certinfo_set_handle(handle);
-      LOG(ERROR) << "pkgmgrInstallerSetCertValue fail";
-      return false;
-    }
-  }
-
-  const auto& dist2_root_cert = cert_info.distributor2_root_certificate.get();
-  if (dist2_root_cert) {
-    if (pkgmgr_installer_set_cert_value(handle, PM_SET_DISTRIBUTOR2_ROOT_CERT,
-        const_cast<char*>(dist2_root_cert->getBase64().c_str())) < 0) {
-      pkgmgr_installer_destroy_certinfo_set_handle(handle);
-      LOG(ERROR) << "pkgmgrInstallerSetCertValue fail";
-      return false;
-    }
+  for (auto pair : certPtrToInstCertType) {
+     auto cert = (cert_info.*(pair.first)).get();
+     auto type = pair.second;
+     if (!RegisterCertificate(handle, cert, type))
+       return false;
   }
-
   if (pkgmgr_installer_save_certinfo(pkgid.c_str(), handle, uid) < 0) {
     pkgmgr_installer_destroy_certinfo_set_handle(handle);
     LOG(ERROR) << "Failed to save certificate information";