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 #include "ffrt.h" 31 32 namespace OHOS { 33 namespace Notification { 34 class BundleManagerHelper : public DelayedSingleton<BundleManagerHelper> { 35 public: 36 /** 37 * @brief Obtains the bundle name base on the specified uid. 38 * 39 * @param uid Indicates the specified uid. 40 * @return Returns the bundle name. 41 */ 42 std::string GetBundleNameByUid(int32_t uid); 43 44 /** 45 * @brief Check whether the caller is a system application base on the specified uid. 46 * 47 * @param uid Indicates the specified uid. 48 * @return Returns the check result. 49 */ 50 bool IsSystemApp(int32_t uid); 51 52 /** 53 * @brief Check API compatibility. 54 * 55 * @param bundleOption Indicates the bundle option. 56 * @return Returns the check result. 57 */ 58 bool CheckApiCompatibility(const sptr<NotificationBundleOption> &bundleOption); 59 60 /** 61 * @brief Obtains the default uid. 62 * 63 * @param bundle Indicates the bundle name. 64 * @param userId Indicates the user id. 65 * @return Returns the uid. 66 */ 67 int32_t GetDefaultUidByBundleName(const std::string &bundle, const int32_t userId); 68 69 /** 70 * @brief Obtains the default uid. 71 * 72 * @param bundle Indicates the bundle name. 73 * @param userId Indicates the user id. 74 * @param appIndex Indicates the app Index. 75 * @return Returns the uid. 76 */ 77 int32_t GetDefaultUidByBundleName(const std::string &bundle, const int32_t userId, const int32_t appIndex); 78 79 /** 80 * @brief Obtains the bundle info. 81 * 82 * @param bundle Indicates the bundle name. 83 * @param userId Indicates the user id. 84 * @param bundleInfo Indicates the bundle info. 85 * @return Returns the uid. 86 */ 87 bool GetBundleInfoByBundleName(const std::string bundle, const int32_t userId, AppExecFwk::BundleInfo &bundleInfo); 88 89 #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED 90 /** 91 * @brief Check whether the specified bundle has the distributed notification supported. 92 * 93 * @param bundleName Indicates the bundle name. 94 * @param userId Indicates the user id. 95 * @return Returns the check result. 96 */ 97 bool GetDistributedNotificationEnabled(const std::string &bundleName, const int32_t userId); 98 #endif 99 100 /** 101 * @brief Obtains bundle info by bundle name. 102 * 103 * @param bundleName Indicates the bundle name. 104 * @param flag Indicates the bundle flag. 105 * @param bundleInfo Indicates the bundle info. 106 * @param userId Indicates the user id. 107 * @return Returns the check result. 108 */ 109 bool GetBundleInfo(const std::string &bundleName, const AppExecFwk::BundleFlag flag, 110 int32_t userId, AppExecFwk::BundleInfo &bundleInfo); 111 112 /** 113 * @brief Obtains BundleInfo of all bundles available in the system through the proxy object. 114 * @param flag Indicates the flag used to specify information contained in the BundleInfo that will be returned. 115 * @param bundleInfos Indicates all of the obtained BundleInfo objects. 116 * @param userId Indicates the user ID. 117 * @return Returns true if the BundleInfos is successfully obtained, returns false otherwise. 118 */ 119 bool GetBundleInfos( 120 const AppExecFwk::BundleFlag flag, std::vector<AppExecFwk::BundleInfo> &bundleInfos, int32_t userId); 121 122 /** 123 * @brief Obtains the app index by uid. 124 * @param uid Indicates uid. 125 * @return Returns the query result if succeed, retrun 0(main index) otherwise. 126 */ 127 int32_t GetAppIndexByUid(const int32_t uid); 128 129 /** 130 * @brief Check API compatibility. 131 * 132 * @param bundleName Indicates the bundle name. 133 * @param uid Indicates the bundle uid. 134 * @return Returns the check result. 135 */ 136 bool CheckApiCompatibility(const std::string &bundleName, const int32_t &uid); 137 138 /** 139 * @brief GetBundleInfoV9. 140 * @param bundle bundle name. 141 * @param flag query condation. 142 * @param bundleInfo bundle info. 143 * @param userId userId. 144 * @return Returns the query result if succeed, retrun 0(main index) otherwise. 145 */ 146 bool GetBundleInfoV9(const std::string bundle, const int32_t flag, 147 AppExecFwk::BundleInfo &bundleInfo, const int32_t userId); 148 149 /** 150 * @brief CheckSystemApp. 151 * @param bundleName bundle name. 152 * @param userId userId. 153 * @return Returns the query result. if systemapp, retrun true. 154 */ 155 bool CheckSystemApp(const std::string& bundleName, int32_t userId); 156 157 /** 158 * @brief GetApplicationInfo. 159 * @param bundleName bundle name. 160 * @param flag query condation. 161 * @param userId userId. 162 * @param appInfo application info. 163 * @return Returns the query result. if succeed, retrun 0. 164 */ 165 ErrCode GetApplicationInfo(const std::string &bundleName, int32_t flags, int32_t userId, 166 AppExecFwk::ApplicationInfo &appInfo); 167 168 /** 169 * @brief GetBundleResourceInfo. 170 * @param bundleName bundle name. 171 * @param bundleResourceInfo bundle resource. 172 * @param appIndex app index. 173 * @return Returns the query result. if succeed, retrun 0. 174 */ 175 ErrCode GetBundleResourceInfo(const std::string &bundleName, 176 AppExecFwk::BundleResourceInfo &bundleResourceInfo, const int32_t appIndex); 177 178 private: 179 void Connect(); 180 void Disconnect(); 181 182 void OnRemoteDied(const wptr<IRemoteObject> &object); 183 184 private: 185 sptr<AppExecFwk::IBundleMgr> bundleMgr_ = nullptr; 186 ffrt::mutex connectionMutex_; 187 sptr<RemoteDeathRecipient> deathRecipient_ = nullptr; 188 189 DECLARE_DELAYED_SINGLETON(BundleManagerHelper) 190 }; 191 } // namespace Notification 192 } // namespace OHOS 193 #endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_ANS_INCLUDE_BUNDLE_MANAGER_HELPER_H 194