Add caching of App/Pkg data & use faster way to retrieve this data
[platform/core/security/askuser.git] / src / common / policy / PkgInfo.h
index e2f9e59..c838ec9 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright (c) 2017 Samsung Electronics Co.
+ *  Copyright (c) 2017-2018 Samsung Electronics Co.
  *
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
 /**
  * @file        src/common/policy/PkgInfo.h
  * @author      Zofia Abramowska <z.abramowska@samsung.com>
- * @brief       Definition of pkgmgr-info wrappers
+ * @author      Tomasz Swierczek <t.swierczek@samsung.com>
+ * @brief       Definition of package-data wrappers/cache (sub)classes
  */
 
 #pragma once
 
 #include <string>
 #include <sys/types.h>
-#include <pkgmgr-info.h>
 
-#include <log/alog.h>
+namespace AskUser {
 
-struct PkgInfo {
-    PkgInfo(const std::string &pkgId, uid_t uid) : m_handle(nullptr) {
-        int ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgId.c_str(), uid, &m_handle);
-        if (ret != PMINFO_R_OK) {
-            ALOGE("pkgmgrinfo_pkginfo_get_usr_pkginfo failed for " << pkgId  << " with " << ret);
-            m_handle = nullptr;
-        }
+class PkgInfo {
+public:
+    PkgInfo() :  m_uid(0) {};
+    virtual ~PkgInfo() {};
+
+    virtual void purgeCache() {
+        m_pkgId.clear();
+        m_mainAppId.clear();
+        m_pkgLabel.clear();
+        m_uid = 0;
     }
-    ~PkgInfo() {
-        if (m_handle)
-            pkgmgrinfo_pkginfo_destroy_pkginfo(m_handle);
+
+    std::string mainAppId(const std::string &pkgId, uid_t uid) {
+        update(pkgId, uid);
+        return m_mainAppId;
     }
-    const std::string mainAppId(){
-        if (!m_handle) {
-            return "";
-        }
-        char *mainAppId;
-        int ret = pkgmgrinfo_pkginfo_get_mainappid(m_handle, &mainAppId);
-        if (ret != PMINFO_R_OK) {
-            ALOGE("pkgmgrinfo_pkginfo_get_mainappid failed  with " << ret);
-            return "";
-        }
-        return mainAppId ? mainAppId : "";
+
+    std::string pkgLabel(const std::string &pkgId, uid_t uid) {
+        update(pkgId, uid);
+        return m_pkgLabel;
     }
-    const std::string pkgLabel() {
-        if (!m_handle) {
-            return "";
-        }
-        char *pkgLabel;
-        int ret = pkgmgrinfo_pkginfo_get_label(m_handle, &pkgLabel);
-        if (ret != PMINFO_R_OK) {
-            ALOGE("pkgmgrinfo_pkginfo_get_label failed  with " << ret);
-            return "";
+
+protected:
+    virtual void fetch(const std::string &pkgId, uid_t uid) = 0;
+
+    virtual void update(const std::string &pkgId, uid_t uid) {
+        if (pkgId != m_pkgId || uid != m_uid) {
+            purgeCache();
+            fetch(pkgId, uid);
         }
-        return pkgLabel ? pkgLabel : "";
     }
-    pkgmgrinfo_pkginfo_h m_handle;
+
+    std::string m_pkgId;
+    uid_t m_uid;
+    std::string m_mainAppId;
+    std::string m_pkgLabel;
+};
+
+class AulPkgInfo : public PkgInfo {
+protected:
+    virtual void fetch(const std::string &pkgId, uid_t uid);
 };
+
+class PkgMgrPkgInfo : public PkgInfo {
+protected:
+    virtual void fetch(const std::string &pkgId, uid_t uid);
+};
+
+} // namespace AskUser