Remove lock when getting the path of the database
[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 "pkgmgrinfo_basic.h"
34 #include "pkgmgrinfo_private.h"
35 #include "shared_object.hh"
36
37 namespace pkgmgr_server {
38 namespace database {
39
40 #ifndef EXPORT_API
41 #define EXPORT_API __attribute__((visibility("default")))
42 #endif
43
44 class EXPORT_API DBHandleProvider {
45  public:
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();
51   std::string GetCertDBPath();
52   int UpdateCache(const tizen_base::Database& db, pid_t pid, uid_t uid,
53       bool write, const std::string& locale);
54   std::vector<std::shared_ptr<package_x>> GetPackages(
55       pid_t pid, pkgmgrinfo_filter_x* filter,
56       const std::string& package);
57   std::vector<std::shared_ptr<application_x>> GetApplications(
58       pid_t pid, pkgmgrinfo_filter_x* filter,
59       const std::string& app);
60   void TrimCache();
61   void RegisterPendingPackageInfo(package_x* info);
62   void UpdatePendingPackageInfo(const tizen_base::Database& db,
63       pid_t pid, uid_t uid, const std::string& locale);
64   bool UpdateCachePkg(const tizen_base::Database& db, uid_t uid,
65       const std::string& pkgid, const std::string& locale);
66   bool UpdateCacheApp(const tizen_base::Database& db, uid_t uid,
67       const std::string& appid, const std::string& locale);
68   bool UpdateCacheAppByPkgid(const tizen_base::Database& db, uid_t uid,
69       const std::string& pkgid, const std::string& locale);
70
71  private:
72   explicit DBHandleProvider(uid_t uid);
73   void ReleaseCache();
74   void AddPackage(std::string package, std::shared_ptr<package_x> info);
75   void AddApplication(std::string app, std::shared_ptr<application_x> info);
76   void InsertPID(pid_t pid);
77   bool ErasePID(pid_t pid);
78   std::shared_ptr<package_x> GetPackageByApp(const char* appid);
79
80  private:
81   static std::unordered_map<uid_t,
82         std::unique_ptr<DBHandleProvider>> provider_map_;
83   static std::string global_parser_filedb_path_;
84   static std::string cert_filedb_path_;
85   static std::unordered_set<pid_t> writer_pid_list_;
86   static std::shared_mutex pid_list_lock_;
87
88   uid_t uid_;
89   std::string user_parser_memdb_path_;
90   std::string user_parser_filedb_path_;
91   bool released_ = true;
92   std::unordered_map<std::string, std::shared_ptr<package_x>> pkg_map_;
93   std::unordered_map<std::string, std::shared_ptr<application_x>> app_map_;
94   std::unordered_map<std::string, std::unordered_set<std::string>> pkg_app_map_;
95   std::unordered_set<std::string> pending_pkg_;
96 };
97
98 }  // namespace database
99 }  // namespace pkgmgr_server
100
101 #endif  // SERVER_DB_HANDLE_PROVIDER_HH_