1/* 2 * Copyright (c) 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#include "client_lifecycle.h" 17 18#include "if_system_ability_manager.h" 19#include "iservice_registry.h" 20#include "power_log.h" 21#include "power_mgr_errors.h" 22#include "system_ability_definition.h" 23 24namespace OHOS { 25namespace PowerMgr { 26sptr<IPowerMgr> ClientLifeCycle::proxy_; 27sptr<IRemoteObject::DeathRecipient> ClientLifeCycle::deathRecipient_; 28std::mutex ClientLifeCycle::mutex_; 29 30sptr<IPowerMgr> ClientLifeCycle::GetProxy() 31{ 32 std::lock_guard<std::mutex> lock(mutex_); 33 if (proxy_ == nullptr) { 34 auto ret = Connect(); 35 if (ret != ERR_OK) { 36 POWER_HILOGE(COMP_FWK, "Failed to connect PowerMgrService"); 37 } 38 } 39 return proxy_; 40} 41 42ErrCode ClientLifeCycle::Connect() 43{ 44 if (proxy_ != nullptr) { 45 return ERR_OK; 46 } 47 48 sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); 49 if (sam == nullptr) { 50 POWER_HILOGE(COMP_FWK, "Failed to obtain SystemAbilityMgr"); 51 return E_GET_SYSTEM_ABILITY_MANAGER_FAILED; 52 } 53 sptr<IRemoteObject> remoteObject_ = sam->CheckSystemAbility(POWER_MANAGER_SERVICE_ID); 54 if (remoteObject_ == nullptr) { 55 POWER_HILOGE(COMP_FWK, "Check SystemAbility failed"); 56 return E_GET_POWER_SERVICE_FAILED; 57 } 58 59 deathRecipient_ = sptr<IRemoteObject::DeathRecipient>(new LifeCycleRecipient()); 60 if (deathRecipient_ == nullptr) { 61 POWER_HILOGE(COMP_FWK, "Failed to create LifeCycleRecipient"); 62 return ERR_NO_MEMORY; 63 } 64 if ((remoteObject_->IsProxyObject()) && (!remoteObject_->AddDeathRecipient(deathRecipient_))) { 65 POWER_HILOGE(COMP_FWK, "Add death recipient to PowerMgr service failed"); 66 return E_ADD_DEATH_RECIPIENT_FAILED; 67 } 68 69 proxy_ = iface_cast<IPowerMgr>(remoteObject_); 70 POWER_HILOGI(COMP_FWK, "Connecting PowerMgrService success, pid=%{public}d", getpid()); 71 return ERR_OK; 72} 73 74void ClientLifeCycle::Reset(const wptr<IRemoteObject>& remote) 75{ 76 if (proxy_ == nullptr) { 77 return; 78 } 79 80 auto service = proxy_->AsObject(); 81 if ((service != nullptr) && (service == remote.promote())) { 82 service->RemoveDeathRecipient(deathRecipient_); 83 proxy_ = nullptr; 84 } 85} 86 87void ClientLifeCycle::LifeCycleRecipient::OnRemoteDied(const wptr<IRemoteObject>& remote) 88{ 89 if (remote == nullptr) { 90 POWER_HILOGW(COMP_FWK, "OnRemoteDied failed, remote is nullptr"); 91 return; 92 } 93 std::lock_guard<std::mutex> lock(mutex_); 94 ClientLifeCycle::Reset(remote); 95 POWER_HILOGW(COMP_FWK, "Recv death notice"); 96} 97} // namespace PowerMgr 98} // namespace OHOS 99