• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "interaction/interprocess/inter_ipc_sub_stub.h"
17 #include "common/sharing_log.h"
18 #include "interaction/interaction_manager.h"
19 #include "ipc_skeleton.h"
20 
21 namespace OHOS {
22 namespace Sharing {
23 
InterIpcSubStub()24 InterIpcSubStub::InterIpcSubStub()
25 {
26     SHARING_LOGD("trace.");
27 }
28 
~InterIpcSubStub()29 InterIpcSubStub::~InterIpcSubStub()
30 {
31     SHARING_LOGD("trace.");
32 }
33 
DoIpcCommand(std::shared_ptr<BaseMsg> msg,std::shared_ptr<BaseMsg> & replyMsg)34 int32_t InterIpcSubStub::DoIpcCommand(std::shared_ptr<BaseMsg> msg, std::shared_ptr<BaseMsg> &replyMsg)
35 {
36     SHARING_LOGD("traces.");
37     auto listener = stubListener_.lock();
38     if (listener) {
39         listener->OnIpcRequest(msg, replyMsg);
40     } else {
41         SHARING_LOGE("stub listener is null.");
42     }
43 
44     return 0;
45 }
46 
SetListenerObject(std::string key,const sptr<IRemoteObject> & object)47 int32_t InterIpcSubStub::SetListenerObject(std::string key, const sptr<IRemoteObject> &object)
48 {
49     SHARING_LOGD("trace.");
50     if (object == nullptr) {
51         SHARING_LOGE("Inter ipc null listener object.");
52         return -1;
53     }
54 
55     sptr<IInterIpc> peerProxy = iface_cast<IInterIpc>(object);
56     if (peerProxy == nullptr) {
57         SHARING_LOGE("convert null.");
58         return -1;
59     }
60 
61     std::unique_lock<std::mutex> lock(mutex_);
62     peerProxys_[key] = peerProxy;
63     lock.unlock();
64 
65     auto interaction = InteractionManager::GetInstance().GetInteraction(key);
66     if (interaction == nullptr) {
67         SHARING_LOGE("interaction is null.");
68         return -1;
69     }
70 
71     auto adapter = interaction->GetIpcAdapter();
72     if (adapter != nullptr) {
73         SHARING_LOGI("adpater finded key: %{public}s.", key.c_str());
74         adapter->SetPeerProxy(peerProxy);
75     } else {
76         SHARING_LOGE("adpater is null key: %{public}s.", key.c_str());
77     }
78 
79     SHARING_LOGD("peer key: %{public}s, listener num: %{public}zu.", key.c_str(), peerProxys_.size());
80     auto deathRecipient = new (std::nothrow) InterIpcDeathRecipient(key);
81     deathRecipients_[key] = deathRecipient;
82     CreateDeathListener(key);
83 
84     if (peerProxy->AsObject() != nullptr) {
85         if (!peerProxy->AsObject()->AddDeathRecipient(deathRecipient)) {
86             SHARING_LOGE("add death recipient failed!");
87             return -1;
88         }
89     }
90 
91     return ERR_NONE;
92 }
93 
OnRemoteDied()94 void InterIpcSubStub::OnRemoteDied()
95 {
96     SHARING_LOGD("sub Stub OnRemoteDied trace.");
97     peerProxys_.clear();
98     auto listener = stubListener_.lock();
99     if (listener) {
100         listener->OnRemoteDied();
101     } else {
102         SHARING_LOGE("stub listener is null.");
103     }
104 }
105 
106 } // namespace Sharing
107 } // namespace OHOS
108