de4dcb3e1203ff6517e9f0997ccc7fa8cce1bf0f
[platform/core/appfw/pkgmgr-info.git] / src / server / database / appinfo_db_handler.cc
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
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 #include "appinfo_db_handler.hh"
18
19 #include <shared_mutex>
20 #include <vector>
21
22 #include "cache_flag.hh"
23 #include "db_handle_provider.hh"
24 #include "utils/logging.hh"
25
26 #include "pkgmgrinfo_basic.h"
27 #include "pkgmgrinfo_internal.h"
28 #include "pkgmgrinfo_internal.hh"
29
30 namespace {
31
32 uid_t globaluser_uid = -1;
33
34 uid_t GetGlobalUID() {
35   if (globaluser_uid == (uid_t)-1)
36     globaluser_uid = tzplatform_getuid(TZ_SYS_GLOBALAPP_USER);
37
38   return globaluser_uid;
39 }
40
41 }  // namespace
42
43 namespace pkgmgr_server {
44 namespace database {
45
46 AppInfoDBHandler::AppInfoDBHandler(uid_t uid, int pid)
47     : AbstractDBHandler(uid, pid), filter_(nullptr), uid_(uid) {}
48
49 AppInfoDBHandler::~AppInfoDBHandler() {}
50
51 std::vector<std::shared_ptr<application_x>> AppInfoDBHandler::DetachAppHandle() {
52   return std::move(handle_list_);
53 }
54
55 void AppInfoDBHandler::SetFilter(pkgmgrinfo_filter_x* filter) {
56   filter_ = filter;
57 }
58
59 int AppInfoDBHandler::GetHandleFromDB() {
60   if (!Connect()) {
61     LOG(ERROR) << "Failed to connect database";
62     return PMINFO_R_ERROR;
63   }
64
65   std::vector<std::pair<sqlite3*, uid_t>> conn_list = GetConnection();
66   std::vector<std::shared_ptr<application_x>> list;
67   int ret = PMINFO_R_OK;
68   for (auto& conn : conn_list) {
69     ret = pkgmgr_server::internal::appinfo_internal_filter_get_list(conn.first,
70         filter_, conn.second, uid_, GetLocale().c_str(), list);
71     if (ret == PMINFO_R_ERROR) {
72       LOG(DEBUG) << "Failed to appinfo_internal_filter_get_list: " << ret;
73       break;
74     }
75   }
76
77   if (list.empty())
78     ret = PMINFO_R_ENOENT;
79
80   if (ret == PMINFO_R_OK)
81     handle_list_ = std::move(list);
82
83   return ret;
84 }
85
86 void AppInfoDBHandler::GetApplicationFromCache(uid_t uid,
87     const std::string& application) {
88   std::vector<std::shared_ptr<application_x>> app_list;
89   app_list = DBHandleProvider::GetInst(uid).GetApplications(GetPID(),
90       filter_, application);
91
92   handle_list_.reserve(app_list.size() + handle_list_.size());
93   std::move(std::begin(app_list), std::end(app_list),
94             std::back_inserter(handle_list_));
95 }
96
97 int AppInfoDBHandler::GetHandleFromCache() {
98   std::string application;
99
100   for (auto* it = filter_->list; it != nullptr; it = g_slist_next(it)) {
101     auto node = reinterpret_cast<pkgmgrinfo_node_x*>(it->data);
102     if (node->prop == E_PMINFO_APPINFO_PROP_APP_ID) {
103       application = node->value;
104       break;
105     }
106   }
107
108   if (uid_ > REGULAR_USER)
109     GetApplicationFromCache(uid_, application);
110
111   GetApplicationFromCache(GetGlobalUID(), application);
112
113   if (handle_list_.empty())
114     return PMINFO_R_ENOENT;
115
116   return PMINFO_R_OK;
117 }
118
119 int AppInfoDBHandler::Execute() {
120   std::shared_lock<std::shared_mutex> s(lock_);
121   SetOpType(pkgmgr_common::DBOperationType::OPERATION_TYPE_READ);
122   SetDBType(pkgmgr_common::DBType::DB_TYPE_FILE_PKGDB);
123
124   if (DBHandleProvider::IsWriter(GetPID()))
125     return GetHandleFromDB();
126
127   if (uid_ <= GetDefaultUser() &&
128       CacheFlag::GetStatus() == CacheFlag::Status::PREPARED) {
129     auto cache_lock = CacheFlag::GetReaderLock();
130     if (cache_lock.try_lock() &&
131         CacheFlag::GetStatus() == CacheFlag::Status::PREPARED)
132       return GetHandleFromCache();
133   }
134
135   return GetHandleFromDB();
136 }
137
138 }  // namespace database
139 }  // namespace pkgmgr_server