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 #include "storage/bundle_manager_connector.h" 17 18 #include "iservice_registry.h" 19 #include "storage_service_errno.h" 20 #include "storage_service_log.h" 21 #include "system_ability_definition.h" 22 23 namespace OHOS { 24 namespace StorageManager { BundleMgrConnector()25BundleMgrConnector::BundleMgrConnector() {} 26 ~BundleMgrConnector()27BundleMgrConnector::~BundleMgrConnector() 28 { 29 bundleMgr_ = nullptr; 30 deathRecipient_ = nullptr; 31 } 32 GetBundleMgrProxy()33sptr<AppExecFwk::IBundleMgr> BundleMgrConnector::GetBundleMgrProxy() 34 { 35 std::lock_guard<std::mutex> lock(mutex_); 36 if (bundleMgr_ == nullptr) { 37 auto sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); 38 if (sam == nullptr) { 39 LOGE("BundleMgrConnector::GetBundleMgrProxy samgr == nullptr"); 40 return nullptr; 41 } 42 sptr<IRemoteObject> remoteObject = sam->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); 43 if (!remoteObject) { 44 LOGE("BundleMgrConnector::GetBundleMgrProxy remoteObj == nullptr"); 45 return nullptr; 46 } 47 bundleMgr_ = iface_cast<AppExecFwk::IBundleMgr>(remoteObject); 48 if (bundleMgr_ == nullptr) { 49 LOGE("BundleMgrConnector::GetBundleMgrProxy bundleMgr == nullptr"); 50 return nullptr; 51 } 52 deathRecipient_ = new (std::nothrow) BundleMgrDeathRecipient(); 53 if (!deathRecipient_) { 54 LOGE("BundleMgrConnector::GetBundleMgrProxy failed to create death recipient"); 55 bundleMgr_ = nullptr; 56 return nullptr; 57 } 58 bundleMgr_->AsObject()->AddDeathRecipient(deathRecipient_); 59 } 60 return bundleMgr_; 61 } 62 ResetBundleMgrProxy()63int32_t BundleMgrConnector::ResetBundleMgrProxy() 64 { 65 std::lock_guard<std::mutex> lock(mutex_); 66 if ((bundleMgr_ != nullptr) && (bundleMgr_->AsObject() != nullptr)) { 67 bundleMgr_->AsObject()->RemoveDeathRecipient(deathRecipient_); 68 } 69 bundleMgr_ = nullptr; 70 return E_OK; 71 } 72 OnRemoteDied(const wptr<IRemoteObject> & remote)73void BundleMgrDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote) 74 { 75 BundleMgrConnector::GetInstance().ResetBundleMgrProxy(); 76 } 77 } // StorageManager 78 } // OHOS 79