Release version 0.26.12
[platform/core/appfw/pkgmgr-info.git] / src / server / database / db_handle_provider.hh
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 #ifndef SERVER_DB_HANDLE_PROVIDER_HH_
18 #define SERVER_DB_HANDLE_PROVIDER_HH_
19
20 #include <sqlite3.h>
21 #include <stdlib.h>
22 #include <sys/types.h>
23
24 #include <memory>
25 #include <shared_mutex>
26 #include <string>
27 #include <unordered_map>
28 #include <unordered_set>
29 #include <vector>
30
31 #include <database.hpp>
32 #include "filter_checker_provider.hh"
33 #include "pkg_write_type.hh"
34 #include "pkgmgrinfo_basic.h"
35 #include "pkgmgrinfo_private.h"
36 #include "shared_object.hh"
37
38 namespace pkgmgr_server {
39 namespace database {
40
41 #ifndef EXPORT_API
42 #define EXPORT_API __attribute__((visibility("default")))
43 #endif
44
45 class EXPORT_API DBHandleProvider {
46  public:
47   ~DBHandleProvider() = default;
48   static DBHandleProvider& GetInst(uid_t uid);
49   static std::unordered_set<pid_t> CrashedWriteRequestPIDs();
50   static bool IsWriter(pid_t pid);
51   std::vector<std::pair<std::string, uid_t>> GetParserDBPath();
52   std::string GetCertDBPath();
53   int UpdateCache(const tizen_base::Database& db, pid_t pid, uid_t uid,
54       bool write, const std::string& locale);
55   std::vector<std::shared_ptr<package_x>> GetPackages(
56       pid_t pid, pkgmgrinfo_filter_x* filter,
57       const std::string& package);
58   std::vector<std::shared_ptr<application_x>> GetApplications(
59       pid_t pid, pkgmgrinfo_filter_x* filter,
60       const std::string& app);
61   void TrimCache();
62   void RegisterPendingPackageInfo(const tizen_base::Database& db,
63       package_x* info, pid_t pid, uid_t uid, const std::string& locale,
64       pkgmgr_common::PkgWriteType write_type);
65   void UpdatePendingPackageInfo(const tizen_base::Database& db,
66       uid_t uid, const std::string& locale,
67       const std::vector<std::string>& pkgids);
68   void UpdateCrashedWriterPackageInfo(const tizen_base::Database& db,
69       uid_t uid, const std::string& locale,
70       const std::unordered_set<pid_t>& pids);
71   bool UpdateCachePkg(const tizen_base::Database& db, uid_t uid,
72       const std::string& pkgid, const std::string& locale);
73   bool UpdateCacheApp(const tizen_base::Database& db, uid_t uid,
74       const std::string& appid, const std::string& locale);
75   bool UpdateCacheAppByPkgid(const tizen_base::Database& db, uid_t uid,
76       const std::string& pkgid, const std::string& locale);
77
78  private:
79   explicit DBHandleProvider(uid_t uid);
80   void ReleaseCache();
81   void AddPackage(std::shared_ptr<package_x> info);
82   void AddApplication(std::shared_ptr<application_x> info);
83   void ErasePackage(const std::string& pkgid);
84   void InsertWriterPID(pid_t pid);
85   bool EraseWriterPID(pid_t pid);
86   std::shared_ptr<package_x> GetPackageByApp(const char* appid);
87
88  private:
89   static std::unordered_map<uid_t,
90         std::unique_ptr<DBHandleProvider>> provider_map_;
91   static std::string global_parser_filedb_path_;
92   static std::string cert_filedb_path_;
93   static std::unordered_set<pid_t> writer_pid_list_;
94   static std::shared_mutex pid_list_lock_;
95
96   uid_t uid_;
97   std::string user_parser_memdb_path_;
98   std::string user_parser_filedb_path_;
99   bool released_ = true;
100   std::unordered_map<std::string, std::shared_ptr<package_x>> pkg_map_;
101   std::unordered_map<std::string, std::shared_ptr<application_x>> app_map_;
102   std::unordered_map<std::string, std::unordered_set<std::string>> pkg_app_map_;
103   std::unordered_map<std::string, pid_t> pending_pkg_;
104 };
105
106 }  // namespace database
107 }  // namespace pkgmgr_server
108
109 #endif  // SERVER_DB_HANDLE_PROVIDER_HH_