Fix delivering PID logic
[platform/core/appfw/pkgmgr-info.git] / src / common / 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 <string>
25 #include <memory>
26 #include <mutex>
27 #include <unordered_map>
28 #include <vector>
29
30 #include "shared_object.hh"
31
32 namespace pkgmgr_common {
33
34 #ifndef EXPORT_API
35 #define EXPORT_API __attribute__((visibility("default")))
36 #endif
37
38 class EXPORT_API DBHandleProvider {
39  public:
40   ~DBHandleProvider() = default;
41   static DBHandleProvider& GetInst(uid_t uid);
42   std::vector<std::string> GetParserDBPath(int pid);
43   std::string GetCertDBPath(int pid);
44   void SetMemoryMode(int pid, bool flag);
45
46  private:
47   DBHandleProvider(uid_t uid);
48   sqlite3* GetMemoryDBHandle(const std::string& filedb_path,
49       const std::string& memorydb_path);
50
51  private:
52   static std::unordered_map<uid_t, std::unique_ptr<DBHandleProvider>> provider_;
53   uid_t uid_;
54   std::unique_ptr<sqlite3, decltype(sqlite3_close_v2)*> global_parser_memory_db_handle_;
55   std::unique_ptr<sqlite3, decltype(sqlite3_close_v2)*> parser_memory_db_handle_;
56   std::unique_ptr<sqlite3, decltype(sqlite3_close_v2)*> cert_memory_db_handle_;
57   std::string parser_memory_db_path_;
58   std::string global_parser_memory_db_path_;
59   std::string parser_file_db_path_;
60   std::string global_parser_file_db_path_;
61   std::string cert_memory_db_path_;
62   std::string cert_file_db_path_;
63   static bool is_memory_;
64   static std::vector<pid_t> pid_list_;
65   std::mutex lock_;
66 };
67
68 }  // namespace pkgmgr_common
69
70 #endif  // SERVER_DB_HANDLE_PROVIDER_HH_