Add caching of App/Pkg data & use faster way to retrieve this data
[platform/core/security/askuser.git] / src / common / policy / PkgInfo.cpp
1 /*
2  *  Copyright (c) 2018 Samsung Electronics Co.
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License");
5  *  you may not use this file except in compliance with the License.
6  *  You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License
15  */
16 /**
17  * @file        src/common/policy/PkgInfo.cpp
18  * @author      Tomasz Swierczek <t.swierczek@samsung.com>
19  * @brief       Implementation of PkgInfo (sub)classes
20  */
21
22 #include "PkgInfo.h"
23
24 #include <aul.h>
25 #include <pkgmgr-info.h>
26
27 #include <log/alog.h>
28
29 namespace AskUser {
30
31 void AulPkgInfo::fetch(const std::string &pkgId, uid_t uid) {
32     char buffer[1024];
33     memset(buffer, 0, sizeof(buffer));
34     int ret = aul_app_get_appid_bypid(getpid(), buffer, sizeof(buffer) - 1);
35     if (AUL_R_OK == ret)
36         m_mainAppId = std::string(buffer);
37     else
38         ALOGE("aul_app_get_appid_bypid failed");
39
40     ret = aul_app_get_pkgname_bypid(getpid(), buffer, sizeof(buffer));
41     if (AUL_R_OK == ret)
42         m_pkgLabel = std::string(buffer);
43     else
44         ALOGE("aul_app_get_pkgname_bypid failed");
45     m_pkgId = pkgId;
46     m_uid = uid;
47 }
48
49 void PkgMgrPkgInfo::fetch(const std::string &pkgId, uid_t uid) {
50     pkgmgrinfo_pkginfo_h handle = nullptr;
51     int ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgId.c_str(), uid, &handle);
52     if (ret != PMINFO_R_OK) {
53         ALOGE("pkgmgrinfo_pkginfo_get_usr_pkginfo failed for " << pkgId  << " with " << ret);
54         return;
55     }
56     char *mainAppId;
57     ret = pkgmgrinfo_pkginfo_get_mainappid(handle, &mainAppId);
58     if (ret != PMINFO_R_OK) {
59         ALOGE("pkgmgrinfo_pkginfo_get_mainappid failed  with " << ret);
60         pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
61         return;
62     }
63     if (mainAppId)
64         m_mainAppId = mainAppId;
65
66     char *pkgLabel;
67     ret = pkgmgrinfo_pkginfo_get_label(handle, &pkgLabel);
68     if (ret != PMINFO_R_OK) {
69         ALOGE("pkgmgrinfo_pkginfo_get_label failed  with " << ret);
70         pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
71         return;
72     }
73     if (pkgLabel)
74         m_pkgLabel = pkgLabel;
75
76     pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
77     m_pkgId = pkgId;
78     m_uid = uid;
79 }
80
81 } // namespace AskUser