2 * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef SERVER_DB_HANDLE_PROVIDER_HH_
18 #define SERVER_DB_HANDLE_PROVIDER_HH_
22 #include <sys/types.h>
25 #include <shared_mutex>
27 #include <unordered_map>
28 #include <unordered_set>
31 #include <database.hpp>
32 #include "filter_checker_provider.hh"
33 #include "pkgmgrinfo_basic.h"
34 #include "pkgmgrinfo_private.h"
35 #include "shared_object.hh"
37 namespace pkgmgr_server {
41 #define EXPORT_API __attribute__((visibility("default")))
44 class EXPORT_API DBHandleProvider {
46 ~DBHandleProvider() = default;
47 static DBHandleProvider& GetInst(uid_t uid);
48 static bool IsCrashedWriteRequest();
49 static bool IsWriter(pid_t pid);
50 std::vector<std::pair<std::string, uid_t>> GetParserDBPath(int pid,
52 std::string GetCertDBPath(int pid, bool write);
53 void SetMemoryMode(pid_t pid);
54 void UnsetMemoryMode(pid_t pid);
55 int UpdateCache(const tizen_base::Database& db, pid_t pid, uid_t uid,
56 bool write, const std::string& locale);
57 std::vector<std::shared_ptr<package_x>> GetPackages(
58 pid_t pid, pkgmgrinfo_filter_x* filter,
59 const std::string& package);
60 std::vector<std::shared_ptr<application_x>> GetApplications(
61 pid_t pid, pkgmgrinfo_filter_x* filter,
62 const std::string& app);
64 void RegisterPendingPackageInfo(package_x* info);
65 void UpdatePendingPackageInfo(const tizen_base::Database& db,
66 pid_t pid, uid_t uid, const std::string& locale);
67 bool UpdateCachePkg(const tizen_base::Database& db, uid_t uid,
68 const std::string& pkgid, const std::string& locale);
69 bool UpdateCacheApp(const tizen_base::Database& db, uid_t uid,
70 const std::string& appid, const std::string& locale);
71 bool UpdateCacheAppByPkgid(const tizen_base::Database& db, uid_t uid,
72 const std::string& pkgid, const std::string& locale);
75 explicit DBHandleProvider(uid_t uid);
76 sqlite3* CreateMemoryDBHandle(const std::string& filedb_path,
77 const std::string& memorydb_path);
78 bool IsMemoryDBActive(pid_t pid, bool write);
80 void AddPackage(std::string package, std::shared_ptr<package_x> info);
81 void AddApplication(std::string app, std::shared_ptr<application_x> info);
82 void InsertPID(pid_t pid);
83 bool ErasePID(pid_t pid);
86 static std::unordered_map<uid_t,
87 std::unique_ptr<DBHandleProvider>> provider_map_;
88 static bool is_global_memdb_set_;
89 static std::unique_ptr<sqlite3, decltype(sqlite3_close_v2)*>
90 global_parser_memdb_handle_;
91 static std::unique_ptr<sqlite3, decltype(sqlite3_close_v2)*>
93 static std::string global_parser_filedb_path_;
94 static std::string cert_filedb_path_;
95 static std::unordered_set<pid_t> writer_pid_list_;
96 static std::recursive_mutex lock_;
97 static std::shared_mutex pid_list_lock_;
100 bool is_user_memdb_set_;
101 std::unique_ptr<sqlite3, decltype(sqlite3_close_v2)*>
102 parser_memdb_handle_;
103 std::string user_parser_memdb_path_;
104 std::string user_parser_filedb_path_;
105 bool released_ = true;
106 std::unordered_map<std::string, std::shared_ptr<package_x>> pkg_map_;
107 std::unordered_map<std::string, std::shared_ptr<application_x>> app_map_;
108 std::unordered_map<std::string, std::unordered_set<std::string>> pkg_app_map_;
109 std::unordered_set<std::string> pending_pkg_;
112 } // namespace database
113 } // namespace pkgmgr_server
115 #endif // SERVER_DB_HANDLE_PROVIDER_HH_