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 OHOS_FORM_FWK_FORM_BMS_HELPER_H
17 #define OHOS_FORM_FWK_FORM_BMS_HELPER_H
18 
19 #include <singleton.h>
20 #include "ability_manager_interface.h"
21 #include "bundle_mgr_interface.h"
22 #include "bundle_installer_interface.h"
23 #include "form_bundle_event_callback.h"
24 #include "want.h"
25 
26 namespace OHOS {
27 namespace AppExecFwk {
28 using Want = OHOS::AAFwk::Want;
29 
30 /**
31  * @class FormBmsHelper
32  * Bms helpler.
33  */
34 class FormBmsHelper final : public DelayedRefSingleton<FormBmsHelper> {
35     DECLARE_DELAYED_REF_SINGLETON(FormBmsHelper)
36 
37 public:
38     DISALLOW_COPY_AND_MOVE(FormBmsHelper);
39 
40     /**
41      * @brief Notify module removable.
42      * @param bundleName Provider ability bundleName.
43      * @param moduleName Provider ability moduleName.
44      */
45     void NotifyModuleRemovable(const std::string &bundleName, const std::string &moduleName);
46     /**
47      * @brief Notify module not removable.
48      * @param bundleName Provider ability bundleName.
49      * @param moduleName Provider ability moduleName.
50      */
51     void NotifyModuleNotRemovable(const std::string &bundleName, const std::string &moduleName);
52 
53     /**
54      * @brief Acquire a bundle manager, if it not existed,
55      * @return returns the bundle manager ipc object, or nullptr for failed.
56      */
57     sptr<IBundleMgr> GetBundleMgr();
58 
59     /**
60      * @brief Acquire a bundle install manager, if it not existed,
61      * @return returns the bundle manager ipc object, or nullptr for failed.
62      */
63     sptr<IBundleInstaller> GetBundleInstaller();
64 
65     /**
66      * @brief Add the bundle manager instance for debug.
67      * @param bundleManager the bundle manager ipc object.
68      */
69     void SetBundleManager(const sptr<IBundleMgr> &bundleManager);
70 
71     /**
72      * @brief Get bundle package info.
73      * @param bundleName Indicates the bundle name.
74      * @param userId Indicates the user ID.
75      * @param bundlePackInfo Indicates the obtained BundlePackInfo object.
76      * @return Returns true on success, false on failure.
77      */
78     bool GetBundlePackInfo(const std::string &bundleName, int32_t userId, BundlePackInfo &bundlePackInfo);
79 
80     /**
81      * @brief Get ability info
82      * @param want The desired action to look for.
83      * @param userId Indicates the user ID.
84      * @param abilityInfo Indicates the obtained AbilityInfo object.
85      * @param extensionInfo Indicates the obtained extension.
86      * @return Returns true on success, false on failure.
87      */
88     bool GetAbilityInfo(const Want &want, int32_t userId, AbilityInfo &abilityInfo,
89         ExtensionAbilityInfo &extensionInfo);
90 
91     /**
92      * @brief Get ability info by action
93      * @param action The desired action to look for.
94      * @param userId Indicates the user ID.
95      * @param abilityInfo Indicates the obtained AbilityInfo object.
96      * @param extensionAbilityInfo Indicates the obtained extension.
97      * @return Returns true on success, false on failure.
98      */
99     bool GetAbilityInfoByAction(const std::string &action, int32_t userId, AbilityInfo &abilityInfo,
100         ExtensionAbilityInfo &extensionAbilityInfo);
101 
102     /**
103      * @brief Obtains the BundleInfo with abilities based on a given bundle name.
104      * @param bundleName Indicates the application bundle name to be queried.
105      * @param userId Indicates the user ID.
106      * @param bundleInfo Indicates the obtained BundleInfo object.
107      * @return Returns true if the BundleInfo is successfully obtained; returns false otherwise.
108      */
109     bool GetBundleInfo(const std::string &bundleName, int32_t userId, BundleInfo &bundleInfo);
110 
111     /**
112      * @brief Obtains the BundleInfo with Permission based on a given bundle name.
113      * @param bundleName Indicates the application bundle name to be queried.
114      * @param userId Indicates the user ID.
115      * @param bundleInfo Indicates the obtained BundleInfo object.
116      * @return Returns true if the BundleInfo is successfully obtained; returns false otherwise.
117      */
118     bool GetBundleInfoWithPermission(const std::string &bundleName, int32_t userId, BundleInfo &bundleInfo);
119 
120     /**
121      * @brief Obtains the BundleInfo with default info based on a given bundle name.
122      * @param bundleName Indicates the application bundle name to be queried.
123      * @param userId Indicates the user ID.
124      * @param bundleInfo Indicates the obtained BundleInfo object.
125      * @return Returns true if the BundleInfo is successfully obtained; returns false otherwise.
126      */
127     bool GetBundleInfoDefault(const std::string& bundleName, int32_t userId, BundleInfo &bundleInfo);
128 
129     /**
130      * @brief Obtains the BundleInfo by flags based on a given bundle name.
131      * @param bundleName Indicates the application bundle name to be queried.
132      * @param flags Indicates the flags.
133      * @param userId Indicates the user ID.
134      * @param bundleInfo Indicates the obtained BundleInfo object.
135      * @return Returns true if the BundleInfo is successfully obtained; returns false otherwise.
136      */
137     bool GetBundleInfoByFlags(const std::string& bundleName, int32_t flags, int32_t userId, BundleInfo &bundleInfo);
138 
139     /**
140      * @brief Obtains the BundleInfo based on a given bundle name.
141      * @param bundleName Indicates the application bundle name to be queried.
142      * @param userId Indicates the user ID.
143      * @param bundleInfo Indicates the obtained BundleInfo object.
144      * @return Returns ERR_OK if the BundleInfo is successfully obtained.
145      */
146     ErrCode GetBundleInfoV9(const std::string& bundleName, int32_t userId, BundleInfo &bundleInfo);
147 
148     /**
149      * @brief Get caller bundle name.
150      * @param callerBundleName Indicates the caller bundle name.
151      * @return Returns ERR_OK on success, others on failure.
152      */
153     int32_t GetCallerBundleName(std::string &callerBundleName);
154 
155     /**
156      * @brief Obtains the application UID based on the given bundle name and user ID.
157      * @param bundleName Indicates the bundle name of the application.
158      * @param userId Indicates the user ID.
159      * @return Returns the uid if successfully obtained; returns -1 otherwise.
160      */
161     int32_t GetUidByBundleName(const std::string &bundleName, int32_t userId);
162 
163     bool GetCompileMode(const std::string &bundleName, const std::string &moduleName,
164         int32_t userId, int32_t &compileMode);
165 
166     bool GetCompatibleVersion(const std::string& bundleName, int32_t userId, int32_t& compatibleVersion);
167 
168     int32_t GetBundleNameByUid(const int32_t uid, std::string &bundleName);
169 
170     ErrCode GetProxyDataInfos(const std::string &bundleName, const std::string &moduleName,
171         int32_t userId, std::vector<ProxyData> &proxyData);
172 
173     ErrCode GetAllProxyDataInfos(int32_t userId, std::vector<ProxyData> &proxyData);
174 
175     ErrCode GetApplicationInfo(const std::string &bundleName, int32_t userId, ApplicationInfo &appInfo);
176 
177     ErrCode RegisterBundleEventCallback();
178     ErrCode UnregisterBundleEventCallback();
179     static constexpr int64_t INVALID_UID = -1;
180 private:
181     /**
182      * @brief Generate module key.
183      * @param bundleName Provider ability bundleName.
184      * @param moduleName Provider ability moduleName.
185      * @return Module key.
186      */
187     std::string GenerateModuleKey(const std::string &bundleName, const std::string &moduleName) const;
188 
189 private:
190     sptr<IBundleMgr> iBundleMgr_ = nullptr;
191     sptr<IBundleInstaller> bundleInstallerProxy_ = nullptr;
192     sptr<FormBundleEventCallback> formBundleEventCallback_ = nullptr;
193     std::mutex ibundleMutex_;
194     std::mutex registerMutex_;
195     bool hasRegisterBundleEvent_ = false;
196 };
197 }  // namespace AppExecFwk
198 }  // namespace OHOS
199 
200 #endif // OHOS_FORM_FWK_FORM_BMS_HELPER_H
201