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_INTER_IPC_DEATH_RECIPIENT_H
17 #define OHOS_SHARING_INTER_IPC_DEATH_RECIPIENT_H
18 
19 #include <string>
20 #include "common/sharing_log.h"
21 #include "iremote_object.h"
22 #include "nocopyable.h"
23 #include "ipc_skeleton.h"
24 
25 namespace OHOS {
26 namespace Sharing {
27 
28 class InterIpcDeathListener {
29 public:
30     using Ptr = std::shared_ptr<InterIpcDeathListener>;
31 
32     virtual ~InterIpcDeathListener() = default;
33 
34     virtual void OnRemoteDied(std::string key) = 0;
35 };
36 
37 class InterIpcDeathRecipient final : public IRemoteObject::DeathRecipient,
38                                       public NoCopyable {
39 public:
40     virtual ~InterIpcDeathRecipient() = default;
41 
InterIpcDeathRecipient(std::string key)42     explicit InterIpcDeathRecipient(std::string key) : key_(key)
43     {
44         SHARING_LOGD("trace.");
45     }
46 
47     void OnRemoteDied(const wptr<IRemoteObject> &object) final
48     {
49         SHARING_LOGD("trace.");
50         if (listener_) {
51             listener_->OnRemoteDied(key_);
52         }
53     }
54 
SetDeathListener(InterIpcDeathListener::Ptr listener)55     void SetDeathListener(InterIpcDeathListener::Ptr listener)
56     {
57         SHARING_LOGD("trace.");
58         listener_ = listener;
59     }
60 
61 private:
62     std::string key_;
63     InterIpcDeathListener::Ptr listener_ = nullptr;
64 };
65 
66 } // namespace Sharing
67 } // namespace OHOS
68 #endif