Revert refactor patch and its release patch
[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 pkgmgr_server {
31 namespace database {
32
33 AppInfoDBHandler::AppInfoDBHandler(uid_t uid, int pid)
34     : AbstractDBHandler(uid, pid), filter_(nullptr), uid_(uid) {}
35
36 AppInfoDBHandler::~AppInfoDBHandler() {}
37
38 std::vector<std::shared_ptr<application_x>> AppInfoDBHandler::DetachAppHandle() {
39   return std::move(handle_list_);
40 }
41
42 void AppInfoDBHandler::SetFilter(pkgmgrinfo_filter_x* filter) {
43   filter_ = filter;
44 }
45
46 int AppInfoDBHandler::GetHandleFromDB(
47     std::vector<std::pair<sqlite3*, uid_t>>& conn_list) {
48   std::vector<std::shared_ptr<application_x>> list;
49   int ret = PMINFO_R_OK;
50   for (auto& conn : conn_list) {
51     ret = pkgmgr_server::internal::appinfo_internal_filter_get_list(conn.first,
52         filter_, conn.second, uid_, GetLocale().c_str(), list);
53     if (ret == PMINFO_R_ERROR) {
54       LOG(DEBUG) << "Failed to appinfo_internal_filter_get_list: " << ret;
55       break;
56     }
57   }
58
59   if (list.empty())
60     ret = PMINFO_R_ENOENT;
61
62   if (ret == PMINFO_R_OK)
63     handle_list_ = std::move(list);
64
65   return ret;
66 }
67
68 int AppInfoDBHandler::GetHandleFromCache(
69     std::vector<std::pair<sqlite3*, uid_t>>& conn_list) {
70   bool is_write_op =
71       GetOpType() == pkgmgr_common::DBOperationType::OPERATION_TYPE_WRITE;
72   int ret = PMINFO_R_OK;
73   std::string application;
74
75   for (auto* it = filter_->list; it != nullptr; it = g_slist_next(it)) {
76     auto node = reinterpret_cast<pkgmgrinfo_node_x*>(it->data);
77     if (node->prop == E_PMINFO_APPINFO_PROP_APP_ID) {
78       application = node->value;
79       break;
80     }
81   }
82
83   std::vector<std::shared_ptr<application_x>> app_list;
84   for (auto& conn : conn_list) {
85     app_list = DBHandleProvider::GetInst(conn.second)
86                   .GetApplications(
87                       GetPID(), is_write_op, filter_, application);
88
89     handle_list_.reserve(app_list.size() + handle_list_.size());
90     std::move(std::begin(app_list), std::end(app_list),
91               std::back_inserter(handle_list_));
92   }
93
94   if (handle_list_.empty())
95     ret = PMINFO_R_ENOENT;
96
97   return ret;
98 }
99
100 int AppInfoDBHandler::Execute() {
101   std::shared_lock<std::shared_timed_mutex> s(lock_);
102   SetOpType(pkgmgr_common::DBOperationType::OPERATION_TYPE_READ);
103   SetDBType(pkgmgr_common::DBType::DB_TYPE_FILE_PKGDB);
104
105   if (!Connect())
106     return PMINFO_R_ERROR;
107
108   std::vector<std::pair<sqlite3*, uid_t>> conn_list = GetConnection();
109
110   bool is_writer = false;
111   for (auto& conn : conn_list) {
112     if (DBHandleProvider::GetInst(conn.second).IsWriter(GetPID()))
113       is_writer = true;
114   }
115   if (is_writer)
116     return GetHandleFromDB(conn_list);
117
118   if (CacheFlag::GetStatus() == CacheFlag::Status::PREPARED) {
119     auto cache_lock = CacheFlag::GetReaderLock();
120     if (cache_lock.try_lock() &&
121         CacheFlag::GetStatus() == CacheFlag::Status::PREPARED)
122       return GetHandleFromCache(conn_list);
123   }
124
125   return GetHandleFromDB(conn_list);
126 }
127
128 }  // namespace database
129 }  // namespace pkgmgr_server