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 26 namespace OHOS { 27 namespace Notification { 28 29 30 class BundleDeathRecipient : public IRemoteObject::DeathRecipient { 31 public: 32 BundleDeathRecipient(std::function<void (const wptr<IRemoteObject> &)> callback)33 explicit BundleDeathRecipient(std::function<void(const wptr<IRemoteObject> &)> callback) 34 { 35 callback_ = callback; 36 } 37 ~BundleDeathRecipient()38 ~BundleDeathRecipient() 39 { 40 callback_ = nullptr; 41 } 42 OnRemoteDied(const wptr<IRemoteObject> & object)43 void OnRemoteDied(const wptr<IRemoteObject> &object) 44 { 45 if (callback_ != nullptr) { 46 callback_(object); 47 } 48 } 49 50 private: 51 std::function<void(const wptr<IRemoteObject> &)> callback_; 52 }; 53 54 class BundleResourceHelper : public DelayedSingleton<BundleResourceHelper> { 55 public: 56 /** 57 * @brief Obtains bundle info by bundle name. 58 * 59 * @param bundleName Indicates the bundle name. 60 * @param flag Indicates the bundle flag. 61 * @param bundleInfo Indicates the bundle resource. 62 * @param appIndex Indicates the appindex. 63 * @return Returns the check result. 64 */ 65 ErrCode GetBundleInfo(const std::string &bundleName, 66 AppExecFwk::BundleResourceInfo &bundleResourceInfo, const int32_t appIndex = 0); 67 68 private: 69 void Connect(); 70 void Disconnect(); 71 void OnRemoteDied(const wptr<IRemoteObject> &object); 72 73 sptr<AppExecFwk::IBundleMgr> bundleMgr_ = nullptr; 74 std::mutex connectionMutex_; 75 sptr<BundleDeathRecipient> deathRecipient_ = nullptr; 76 77 DECLARE_DELAYED_SINGLETON(BundleResourceHelper) 78 }; 79 } 80 } 81 #endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_BUNDLE_RESOURCE_HELPER_H 82 83