1 /* 2 * Copyright (c) 2024 Huawei Device Co., Ltd.. All rights reserved. 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 #include "egl_bundle_mgr_helper.h" 16 17 #include "iservice_registry.h" 18 #include "system_ability_definition.h" 19 #include "wrapper_log.h" 20 21 namespace OHOS { 22 namespace AppExecFwk { EGLBundleMgrHelper()23EGLBundleMgrHelper::EGLBundleMgrHelper() {} 24 ~EGLBundleMgrHelper()25EGLBundleMgrHelper::~EGLBundleMgrHelper() 26 { 27 if (bundleMgr_ != nullptr && bundleMgr_->AsObject() != nullptr && deathRecipient_ != nullptr) { 28 bundleMgr_->AsObject()->RemoveDeathRecipient(deathRecipient_); 29 } 30 } 31 Connect()32sptr<IBundleMgr> EGLBundleMgrHelper::Connect() 33 { 34 WLOGD("Call EGLBundleMgrHelper::Connect"); 35 std::lock_guard<std::mutex> lock(mutex_); 36 if (bundleMgr_ == nullptr) { 37 sptr<ISystemAbilityManager> systemAbilityManager = 38 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); 39 if (systemAbilityManager == nullptr) { 40 WLOGE("Failed to get system ability manager."); 41 return nullptr; 42 } 43 sptr<IRemoteObject> remoteObject_ = systemAbilityManager->GetSystemAbility( 44 BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); 45 if (remoteObject_ == nullptr || (bundleMgr_ = iface_cast<IBundleMgr>(remoteObject_)) == nullptr) { 46 WLOGE("Failed to get bundle mgr service remote object"); 47 return nullptr; 48 } 49 std::weak_ptr<EGLBundleMgrHelper> weakPtr = shared_from_this(); 50 auto deathCallback = [weakPtr](const wptr<IRemoteObject> &object) { 51 auto sharedPtr = weakPtr.lock(); 52 if (sharedPtr == nullptr) { 53 WLOGE("Bundle helper instance is nullptr"); 54 return; 55 } 56 sharedPtr->OnDeath(); 57 }; 58 deathRecipient_ = new(std::nothrow) EGLBundleMgrServiceDeathRecipient(deathCallback); 59 if (deathRecipient_ == nullptr) { 60 WLOGE("Failed to create death recipient ptr deathRecipient_!"); 61 return nullptr; 62 } 63 if (bundleMgr_->AsObject() != nullptr) { 64 bundleMgr_->AsObject()->AddDeathRecipient(deathRecipient_); 65 } 66 } 67 return bundleMgr_; 68 } 69 OnDeath()70void EGLBundleMgrHelper::OnDeath() 71 { 72 WLOGD("Call EGLBundleMgrHelper::OnDeath"); 73 std::lock_guard<std::mutex> lock(mutex_); 74 if (bundleMgr_ == nullptr || bundleMgr_->AsObject() == nullptr) { 75 WLOGE("bundleMgr_ is nullptr!"); 76 return; 77 } 78 bundleMgr_->AsObject()->RemoveDeathRecipient(deathRecipient_); 79 bundleMgr_ = nullptr; 80 } 81 GetBundleInfoForSelf(AppExecFwk::GetBundleInfoFlag flags,BundleInfo & bundleInfo)82ErrCode EGLBundleMgrHelper::GetBundleInfoForSelf(AppExecFwk::GetBundleInfoFlag flags, BundleInfo &bundleInfo) 83 { 84 WLOGD("Call EGLBundleMgrHealper::GetBundleInfoForSelf."); 85 auto bundleMgr = Connect(); 86 if (bundleMgr == nullptr) { 87 WLOGE("Failed to connect."); 88 return -1; 89 } 90 return bundleMgr->GetBundleInfoForSelf(static_cast<int32_t>(flags), bundleInfo); 91 } 92 EGLBundleMgrServiceDeathRecipient(const std::function<void (const wptr<IRemoteObject> & object)> & deathCallback)93EGLBundleMgrServiceDeathRecipient::EGLBundleMgrServiceDeathRecipient( 94 const std::function<void(const wptr<IRemoteObject>& object)>& deathCallback) 95 : deathCallback_(deathCallback) {} 96 OnRemoteDied(const wptr<IRemoteObject> & object)97void EGLBundleMgrServiceDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& object) 98 { 99 if (deathCallback_ != nullptr) { 100 deathCallback_(object); 101 } 102 } 103 104 } 105 }