Add to cache pkg & appinfo
[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 <mutex>
26 #include <string>
27 #include <unordered_map>
28 #include <unordered_set>
29 #include <vector>
30
31 #include "filter_checker_provider.hh"
32 #include "pkgmgrinfo_basic.h"
33 #include "pkgmgrinfo_private.h"
34 #include "shared_object.hh"
35
36 namespace pkgmgr_server {
37 namespace database {
38
39 #ifndef EXPORT_API
40 #define EXPORT_API __attribute__((visibility("default")))
41 #endif
42
43 class EXPORT_API DBHandleProvider {
44  public:
45   ~DBHandleProvider() = default;
46   static DBHandleProvider& GetInst(uid_t uid);
47   static bool IsCrashedWriteRequest();
48   std::vector<std::pair<std::string, uid_t>> GetParserDBPath(int pid,
49                                                              bool write);
50   std::string GetCertDBPath(int pid, bool write);
51   void SetMemoryMode(pid_t pid);
52   void UnsetMemoryMode(pid_t pid);
53   bool NeedUpdate(pid_t pid, bool write, const std::string& locale);
54   int UpdateCache(sqlite3* db, pid_t pid, uid_t uid, bool write,
55                   const std::string& locale);
56   std::vector<std::shared_ptr<package_x>> GetPackages(
57       pid_t pid, bool write, pkgmgrinfo_filter_x* filter,
58       const std::string& package);
59   std::vector<std::shared_ptr<application_x>> GetApplications(
60       pid_t pid, bool write, pkgmgrinfo_filter_x* filter,
61       const std::string& app);
62   void TrimCache();
63
64  private:
65   explicit DBHandleProvider(uid_t uid);
66   sqlite3* GetMemoryDBHandle(const std::string& filedb_path,
67                              const std::string& memorydb_path);
68   bool IsMemoryDBActive(pid_t pid, bool write);
69   void ReleaseCache();
70   void AddPackage(std::string package, package_x* info);
71   void AddApplication(std::string app, application_x* info);
72
73  private:
74   static std::unordered_map<uid_t, std::unique_ptr<DBHandleProvider>> provider_;
75   static bool is_memory_global_;
76   static std::unique_ptr<sqlite3, decltype(sqlite3_close_v2)*>
77       global_parser_memory_db_handle_;
78   static std::unique_ptr<sqlite3, decltype(sqlite3_close_v2)*>
79       cert_memory_db_handle_;
80   static std::string global_parser_file_db_path_;
81   static std::string cert_file_db_path_;
82   static std::unordered_set<pid_t> pid_list_;
83   static std::recursive_mutex lock_;
84
85   uid_t uid_;
86   bool is_memory_;
87   std::unique_ptr<sqlite3, decltype(sqlite3_close_v2)*>
88       parser_memory_db_handle_;
89   std::string parser_memory_db_path_;
90   std::string parser_file_db_path_;
91   std::string locale_;
92   bool released_ = true;
93   std::unordered_map<std::string, std::vector<std::shared_ptr<package_x>>>
94       pkg_map_;
95   std::unordered_map<std::string, std::vector<std::shared_ptr<package_x>>>
96       old_pkg_map_;
97   std::unordered_map<std::string, std::vector<std::shared_ptr<application_x>>>
98       app_map_;
99   std::unordered_map<std::string, std::vector<std::shared_ptr<application_x>>>
100       old_app_map_;
101 };
102
103 }  // namespace database
104 }  // namespace pkgmgr_server
105
106 #endif  // SERVER_DB_HANDLE_PROVIDER_HH_