1 /*
2  * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_ANS_INCLUDE_BUNDLE_MANAGER_HELPER_H
17 #define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_ANS_INCLUDE_BUNDLE_MANAGER_HELPER_H
18 
19 #include <memory>
20 #include <mutex>
21 #include <string>
22 
23 #include "bundle_mgr_interface.h"
24 #include "ipc_skeleton.h"
25 #include "iremote_object.h"
26 #include "notification_bundle_option.h"
27 #include "refbase.h"
28 #include "remote_death_recipient.h"
29 #include "singleton.h"
30 
31 namespace OHOS {
32 namespace Notification {
33 class BundleManagerHelper : public DelayedSingleton<BundleManagerHelper> {
34 public:
35     /**
36      * @brief Obtains the bundle name base on the specified uid.
37      *
38      * @param uid Indicates the specified uid.
39      * @return Returns the bundle name.
40      */
41     std::string GetBundleNameByUid(int32_t uid);
42 
43     /**
44      * @brief Check whether the caller is a system application base on the specified uid.
45      *
46      * @param uid Indicates the specified uid.
47      * @return Returns the check result.
48      */
49     bool IsSystemApp(int32_t uid);
50 
51     /**
52      * @brief Check API compatibility.
53      *
54      * @param bundleOption Indicates the bundle option.
55      * @return Returns the check result.
56      */
57     bool CheckApiCompatibility(const sptr<NotificationBundleOption> &bundleOption);
58 
59     /**
60      * @brief Obtains the default uid.
61      *
62      * @param bundle Indicates the bundle name.
63      * @param userId Indicates the user id.
64      * @return Returns the uid.
65      */
66     int32_t GetDefaultUidByBundleName(const std::string &bundle, const int32_t userId);
67 
68     /**
69      * @brief Obtains the default uid.
70      *
71      * @param bundle Indicates the bundle name.
72      * @param userId Indicates the user id.
73      * @param appIndex Indicates the app Index.
74      * @return Returns the uid.
75      */
76     int32_t GetDefaultUidByBundleName(const std::string &bundle, const int32_t userId, const int32_t appIndex);
77 
78     /**
79      * @brief Obtains the bundle info.
80      *
81      * @param bundle Indicates the bundle name.
82      * @param userId Indicates the user id.
83      * @param bundleInfo Indicates the bundle info.
84      * @return Returns the uid.
85      */
86     bool GetBundleInfoByBundleName(const std::string bundle, const int32_t userId, AppExecFwk::BundleInfo &bundleInfo);
87 
88 #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED
89     /**
90      * @brief Check whether the specified bundle has the distributed notification supported.
91      *
92      * @param bundleName Indicates the bundle name.
93      * @param userId Indicates the user id.
94      * @return Returns the check result.
95      */
96     bool GetDistributedNotificationEnabled(const std::string &bundleName, const int32_t userId);
97 #endif
98 
99     /**
100      * @brief Obtains bundle info by bundle name.
101      *
102      * @param bundleName Indicates the bundle name.
103      * @param flag Indicates the bundle flag.
104      * @param bundleInfo Indicates the bundle info.
105      * @param userId Indicates the user id.
106      * @return Returns the check result.
107      */
108     bool GetBundleInfo(const std::string &bundleName, const AppExecFwk::BundleFlag flag,
109         int32_t userId, AppExecFwk::BundleInfo &bundleInfo);
110 
111     /**
112      * @brief Obtains BundleInfo of all bundles available in the system through the proxy object.
113      * @param flag Indicates the flag used to specify information contained in the BundleInfo that will be returned.
114      * @param bundleInfos Indicates all of the obtained BundleInfo objects.
115      * @param userId Indicates the user ID.
116      * @return Returns true if the BundleInfos is successfully obtained, returns false otherwise.
117      */
118     bool GetBundleInfos(
119         const AppExecFwk::BundleFlag flag, std::vector<AppExecFwk::BundleInfo> &bundleInfos, int32_t userId);
120 
121     /**
122      * @brief Check API compatibility.
123      *
124      * @param bundleName Indicates the bundle name.
125      * @param uid Indicates the bundle uid.
126      * @return Returns the check result.
127      */
128     bool CheckApiCompatibility(const std::string &bundleName, const int32_t &uid);
129 
130     /**
131      * @brief Obtains the app index by uid.
132      * @param uid Indicates uid.
133      * @return Returns the query result if succeed, retrun 0(main index) otherwise.
134      */
135     int32_t GetAppIndexByUid(const int32_t uid);
136 
137 private:
138     void Connect();
139     void Disconnect();
140 
141     void OnRemoteDied(const wptr<IRemoteObject> &object);
142 
143 private:
144     sptr<AppExecFwk::IBundleMgr> bundleMgr_ = nullptr;
145     std::mutex connectionMutex_;
146     sptr<RemoteDeathRecipient> deathRecipient_ = nullptr;
147 
148     DECLARE_DELAYED_SINGLETON(BundleManagerHelper)
149 };
150 }  // namespace Notification
151 }  // namespace OHOS
152 #endif  // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_ANS_INCLUDE_BUNDLE_MANAGER_HELPER_H
153