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 "inter_ipc_service_stub.h"
17 #include "inter_ipc_service_death_listener.h"
18 #include "interaction/interaction.h"
19 #include "interaction/interaction_manager.h"
20 #include "interaction/interprocess/inter_ipc_sub_stub.h"
21 #include "interaction/interprocess/ipc_msg_adapter.h"
22 #include "interaction/scene/base_scene.h"
23 #include "ipc_skeleton.h"
24
25 namespace OHOS {
26 namespace Sharing {
27
InterIpcServiceStub()28 InterIpcServiceStub::InterIpcServiceStub()
29 {
30 SHARING_LOGD("trace.");
31 }
32
~InterIpcServiceStub()33 InterIpcServiceStub::~InterIpcServiceStub()
34 {
35 SHARING_LOGD("trace.");
36 }
37
GetSubSystemAbility(std::string key,std::string className)38 sptr<IRemoteObject> InterIpcServiceStub::GetSubSystemAbility(std::string key, std::string className)
39 {
40 SHARING_LOGD("trace.");
41 auto interaction = InteractionManager::GetInstance().CreateInteraction(className);
42 if (interaction == nullptr) {
43 SHARING_LOGE("create interaction error.");
44 return nullptr;
45 }
46
47 auto scene = interaction->GetScene();
48 if (scene == nullptr) {
49 SHARING_LOGE("create scene error.");
50 return nullptr;
51 }
52
53 sptr<InterIpcStub> adapterStub = new (std::nothrow) InterIpcSubStub();
54 if (adapterStub == nullptr) {
55 SHARING_LOGE("adapterStub create failed.");
56 return nullptr;
57 }
58 sptr<IRemoteObject> object = adapterStub->AsObject();
59
60 auto adapter = std::make_shared<IpcMsgAdapter>();
61 adapter->SetCallingKey(key);
62 adapter->SetLocalStub(adapterStub);
63 scene->SetIpcAdapter(adapter);
64 interaction->SetIpcAdapter(adapter);
65 interaction->SetRpcKey(key);
66
67 SHARING_LOGI("AddInteractionKey key: %{public}s interactionId: %{public}d.", key.c_str(), interaction->GetId());
68 InteractionManager::GetInstance().AddInteractionKey(key, interaction->GetId());
69
70 return object;
71 }
72
DoIpcCommand(std::shared_ptr<BaseMsg> msg,std::shared_ptr<BaseMsg> & replyMsg)73 int32_t InterIpcServiceStub::DoIpcCommand(std::shared_ptr<BaseMsg> msg, std::shared_ptr<BaseMsg> &replyMsg)
74 {
75 SHARING_LOGD("trace.");
76 (void)msg;
77 (void)replyMsg;
78 return 0;
79 }
80
CreateDeathListener(std::string key)81 void InterIpcServiceStub::CreateDeathListener(std::string key)
82 {
83 SHARING_LOGD("trace.");
84 if (deathRecipients_.find(key) != deathRecipients_.end()) {
85 SHARING_LOGI("key: %{public}s.", key.c_str());
86 auto listener = std::make_shared<InterIpcServiceDeathListener>();
87 if (sharedFromThis_ == nullptr) {
88 sharedFromThis_ = InterIpcStub::Ptr(this, [](InterIpcStub *) { SHARING_LOGD("trace."); });
89 }
90 auto service = std::static_pointer_cast<InterIpcServiceStub>(sharedFromThis_);
91 listener->SetService(service);
92 deathRecipients_[key]->SetDeathListener(listener);
93 } else {
94 SHARING_LOGE("key not find %{public}s.", key.c_str());
95 }
96 }
97
DelPeerProxy(std::string key)98 void InterIpcServiceStub::DelPeerProxy(std::string key)
99 {
100 SHARING_LOGD("Delete SA peer proxy, key: %{public}s.", key.c_str());
101 std::lock_guard<std::mutex> lock(mutex_);
102 if (peerProxys_.find(key) != peerProxys_.end()) {
103 peerProxys_[key]->AsObject()->RemoveDeathRecipient(deathRecipients_[key]);
104 peerProxys_.erase(key);
105 deathRecipients_.erase(key);
106 } else {
107 SHARING_LOGE("Delete SA peer proxy, key: %{public}s not find.", key.c_str());
108 }
109 }
110
SetListenerObject(std::string key,const sptr<IRemoteObject> & object)111 int32_t InterIpcServiceStub::SetListenerObject(std::string key, const sptr<IRemoteObject> &object)
112 {
113 SHARING_LOGD("trace.");
114 if (object == nullptr) {
115 SHARING_LOGE("domain rpc null listener object.");
116 return -1;
117 }
118
119 sptr<IInterIpc> peerProxy = iface_cast<IInterIpc>(object);
120 if (peerProxy == nullptr) {
121 SHARING_LOGE("convert null.");
122 return -1;
123 }
124
125 std::unique_lock<std::mutex> lock(mutex_);
126 peerProxys_[key] = peerProxy;
127 lock.unlock();
128 SHARING_LOGD("peer key: %{public}s, listener num: %{public}zu.", key.c_str(), peerProxys_.size());
129
130 auto deathRecipient = new (std::nothrow) InterIpcDeathRecipient(key);
131 if (deathRecipient == nullptr) {
132 SHARING_LOGE("deathRecipient create failed.");
133 return -1;
134 }
135 deathRecipients_[key] = deathRecipient;
136 CreateDeathListener(key);
137
138 if (peerProxy->AsObject() != nullptr) {
139 if (!peerProxy->AsObject()->AddDeathRecipient(deathRecipient)) {
140 SHARING_LOGE("add death recipient failed.");
141 return -1;
142 }
143 }
144
145 return ERR_NONE;
146 }
147
148 } // namespace Sharing
149 } // namespace OHOS