1 /* 2 * Copyright (C) 2024 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_BUNDLE_RESOURCE_HELPER_H 17 #define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_BUNDLE_RESOURCE_HELPER_H 18 19 #include "bundle_mgr_interface.h" 20 #include "ipc_skeleton.h" 21 #include "iremote_object.h" 22 #include "refbase.h" 23 #include "singleton.h" 24 #include "ans_log_wrapper.h" 25 #include "ffrt.h" 26 27 namespace OHOS { 28 namespace Notification { 29 30 31 class BundleDeathRecipient : public IRemoteObject::DeathRecipient { 32 public: 33 BundleDeathRecipient(std::function<void (const wptr<IRemoteObject> &)> callback)34 explicit BundleDeathRecipient(std::function<void(const wptr<IRemoteObject> &)> callback) 35 { 36 callback_ = callback; 37 } 38 ~BundleDeathRecipient()39 ~BundleDeathRecipient() 40 { 41 callback_ = nullptr; 42 } 43 OnRemoteDied(const wptr<IRemoteObject> & object)44 void OnRemoteDied(const wptr<IRemoteObject> &object) 45 { 46 if (callback_ != nullptr) { 47 callback_(object); 48 } 49 } 50 51 private: 52 std::function<void(const wptr<IRemoteObject> &)> callback_; 53 }; 54 55 class BundleResourceHelper : public DelayedSingleton<BundleResourceHelper> { 56 public: 57 /** 58 * @brief Obtains bundle info by bundle name. 59 * 60 * @param bundleName Indicates the bundle name. 61 * @param flag Indicates the bundle flag. 62 * @param bundleInfo Indicates the bundle resource. 63 * @param appIndex Indicates the appindex. 64 * @return Returns the check result. 65 */ 66 ErrCode GetBundleInfo(const std::string &bundleName, 67 AppExecFwk::BundleResourceInfo &bundleResourceInfo, const int32_t appIndex = 0); 68 69 /** 70 * @brief Obtains all installed bundle info. 71 * 72 * @param flag Indicates the bundle flag. 73 * @param bundleInfo Indicates the bundle resource. 74 * @param userId Indicates the userId. 75 * @return Returns the invock result. 76 */ 77 ErrCode GetAllBundleInfos(int32_t flags, std::vector<AppExecFwk::BundleInfo> &bundleInfos, int32_t userId); 78 79 /** 80 * @brief Obtains all installed bundle info. 81 * 82 * @param bundlesName Indicates the bundle name. 83 * @param userId Indicates the userId. 84 * @return Returns the invock result. 85 */ 86 ErrCode GetAllInstalledBundles(std::vector<std::pair<std::string, std::string>>& bundlesName, int32_t userId); 87 88 ErrCode GetApplicationInfo(const std::string &appName, int32_t flags, int32_t userId, 89 AppExecFwk::ApplicationInfo &appInfo); 90 91 bool CheckSystemApp(const std::string& bundleName, int32_t userId); 92 93 ErrCode GetBundleInfoV9(const std::string& bundleName, int32_t userId, 94 AppExecFwk::BundleInfo& bundleInfo); 95 96 private: 97 void Connect(); 98 void Disconnect(); 99 void OnRemoteDied(const wptr<IRemoteObject> &object); 100 101 sptr<AppExecFwk::IBundleMgr> bundleMgr_ = nullptr; 102 ffrt::mutex connectionMutex_; 103 sptr<BundleDeathRecipient> deathRecipient_ = nullptr; 104 105 DECLARE_DELAYED_SINGLETON(BundleResourceHelper) 106 }; 107 } 108 } 109 #endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_BUNDLE_RESOURCE_HELPER_H 110 111