1 /* 2 * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development 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 OHOS_SHARING_DOMAIN_RPC_DEATH_RECIPIENT_H 17 #define OHOS_SHARING_DOMAIN_RPC_DEATH_RECIPIENT_H 18 19 #include "common/sharing_log.h" 20 #include "iremote_object.h" 21 #include "nocopyable.h" 22 23 namespace OHOS { 24 namespace Sharing { 25 26 class DomainRpcDeathListener { 27 public: 28 using Ptr = std::shared_ptr<DomainRpcDeathListener>; 29 30 virtual ~DomainRpcDeathListener() = default; 31 32 virtual void OnRemoteDied(std::string deviceId) = 0; 33 }; 34 35 class DomainRpcDeathRecipient final : public IRemoteObject::DeathRecipient, 36 public NoCopyable { 37 public: DomainRpcDeathRecipient(std::string deviceId)38 explicit DomainRpcDeathRecipient(std::string deviceId) : deviceId_(deviceId) {} 39 virtual ~DomainRpcDeathRecipient() = default; 40 OnRemoteDied(const wptr<IRemoteObject> & object)41 void OnRemoteDied(const wptr<IRemoteObject> &object) final 42 { 43 SHARING_LOGD("on remote died."); 44 if (listener_) { 45 listener_->OnRemoteDied(deviceId_); 46 } 47 } 48 SetDeathListener(DomainRpcDeathListener::Ptr listener)49 void SetDeathListener(DomainRpcDeathListener::Ptr listener) 50 { 51 SHARING_LOGD("trace."); 52 listener_ = listener; 53 } 54 55 private: 56 std::string deviceId_; 57 DomainRpcDeathListener::Ptr listener_ = nullptr; 58 }; 59 60 } // namespace Sharing 61 } // namespace OHOS 62 #endif 63