• 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 "inter_ipc_stub.h"
17 #include "common/sharing_log.h"
18 #include "inter_ipc_stub_death_listener.h"
19 #include "ipc_msg_decoder.h"
20 #include "ipc_msg_encoder.h"
21 #include "ipc_skeleton.h"
22 
23 namespace OHOS {
24 namespace Sharing {
25 
InterIpcStub()26 InterIpcStub::InterIpcStub()
27 {
28     SHARING_LOGD("trace.");
29 }
30 
~InterIpcStub()31 InterIpcStub::~InterIpcStub()
32 {
33     SHARING_LOGD("trace.");
34     for (auto deathRecipient : deathRecipients_) {
35         if (deathRecipient.second != nullptr) {
36             deathRecipient.second->SetDeathListener(nullptr);
37             deathRecipient.second = nullptr;
38         }
39     }
40 }
41 
SetStubListener(std::weak_ptr<IInterIpcStubListener> listener)42 void InterIpcStub::SetStubListener(std::weak_ptr<IInterIpcStubListener> listener)
43 {
44     SHARING_LOGD("trace.");
45     stubListener_ = listener;
46 }
47 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)48 int InterIpcStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
49 {
50     SHARING_LOGD("trace.");
51     switch (code) {
52         case InterIpcMsg::INTER_IPC_MSG: {
53             SHARING_LOGD("INTER_IPC_MSG.");
54             DoIpcCommand(data, reply);
55             break;
56         }
57         case InterIpcMsg::SET_LISTENER_OBJ:
58             SHARING_LOGD("SET_LISTENER_OBJ.");
59             SetListenerObject(data, reply);
60             break;
61         case InterIpcMsg::GET_SUBSYSTEM:
62             SHARING_LOGD("GET_SUBSYSTEM.");
63             GetSystemAbility(data, reply);
64             break;
65         default:
66             SHARING_LOGI("none process case.");
67             break;
68     }
69 
70     return 0;
71 }
72 
SetListenerObject(MessageParcel & data,MessageParcel & reply)73 int32_t InterIpcStub::SetListenerObject(MessageParcel &data, MessageParcel &reply)
74 {
75     SHARING_LOGD("trace.");
76     std::string key = data.ReadString();
77     sptr<IRemoteObject> object = data.ReadRemoteObject();
78     (void)reply.WriteInt32(SetListenerObject(key, object));
79 
80     return ERR_NONE;
81 }
82 
DoIpcCommand(MessageParcel & data,MessageParcel & reply)83 int32_t InterIpcStub::DoIpcCommand(MessageParcel &data, MessageParcel &reply)
84 {
85     SHARING_LOGD("trace.");
86     std::shared_ptr<BaseMsg> msg = nullptr;
87     IpcMsgDecoder::GetInstance().MsgDecode(msg, data);
88     if (msg == nullptr) {
89         SHARING_LOGE("msg null!");
90         return -1;
91     }
92 
93     std::shared_ptr<BaseMsg> replyMsg = std::make_shared<BaseMsg>();
94     DoIpcCommand(msg, replyMsg);
95     IpcMsgEncoder::GetInstance().MsgEncode(reply, replyMsg);
96 
97     return 0;
98 }
99 
GetSystemAbility(MessageParcel & data,MessageParcel & reply)100 int32_t InterIpcStub::GetSystemAbility(MessageParcel &data, MessageParcel &reply)
101 {
102     SHARING_LOGD("trace.");
103     std::string key = data.ReadString();
104     std::string className = data.ReadString();
105     SHARING_LOGD("subtype: %{public}s.", className.c_str());
106     (void)reply.WriteRemoteObject(GetSubSystemAbility(key, className));
107 
108     return ERR_NONE;
109 }
110 
SetListenerObject(std::string key,const sptr<IRemoteObject> & object)111 int32_t InterIpcStub::SetListenerObject(std::string key, const sptr<IRemoteObject> &object)
112 {
113     SHARING_LOGD("trace.");
114     return ERR_NONE;
115 }
116 
DoIpcCommand(std::shared_ptr<BaseMsg> msg,std::shared_ptr<BaseMsg> & replyMsg)117 int32_t InterIpcStub::DoIpcCommand(std::shared_ptr<BaseMsg> msg, std::shared_ptr<BaseMsg> &replyMsg)
118 {
119     SHARING_LOGD("traces.");
120     auto listener = stubListener_.lock();
121     if (listener) {
122         listener->OnIpcRequest(msg, replyMsg);
123     } else {
124         SHARING_LOGE("stub listener is null!");
125     }
126 
127     return 0;
128 }
129 
GetSubSystemAbility(std::string key,std::string className)130 sptr<IRemoteObject> InterIpcStub::GetSubSystemAbility(std::string key, std::string className)
131 {
132     SHARING_LOGD("traces.");
133     return nullptr;
134 }
135 
CreateDeathListener(std::string key)136 void InterIpcStub::CreateDeathListener(std::string key)
137 {
138     if (deathRecipients_.find(key) != deathRecipients_.end()) {
139         SHARING_LOGI("key: %{public}s.", key.c_str());
140         deathRecipients_[key]->SetDeathListener(std::make_shared<InterIpcStubDeathListener>(this));
141     } else {
142         SHARING_LOGE("key not find %{public}s.", key.c_str());
143     }
144 }
145 
OnRemoteDied()146 void InterIpcStub::OnRemoteDied()
147 {
148     SHARING_LOGD("base On remote died trace.");
149 }
150 
SendIpcRequest(std::string key,std::shared_ptr<BaseMsg> msg,std::shared_ptr<BaseMsg> & reply)151 int32_t InterIpcStub::SendIpcRequest(std::string key, std::shared_ptr<BaseMsg> msg, std::shared_ptr<BaseMsg> &reply)
152 {
153     SHARING_LOGD("trace.");
154     std::unique_lock<std::mutex> lock(mutex_);
155     auto iter = peerProxys_.find(key);
156     if (iter == peerProxys_.end()) {
157         SHARING_LOGE("peer proxy not found.");
158         return -1;
159     }
160     lock.unlock();
161 
162     if (iter->second == nullptr) {
163         SHARING_LOGE("peer proxy null!");
164         return -1;
165     }
166 
167     int32_t error = iter->second->DoIpcCommand(msg, reply);
168     if (error != ERR_NONE) {
169         SHARING_LOGE("do ipc command failed: %{public}d.", error);
170         return error;
171     }
172 
173     return 0;
174 }
175 
DelPeerProxy(std::string key)176 void InterIpcStub::DelPeerProxy(std::string key)
177 {
178     SHARING_LOGD("Delete sub peer proxy, key: %{public}s.", key.c_str());
179     std::lock_guard<std::mutex> lock(mutex_);
180     if (peerProxys_.find(key) != peerProxys_.end()) {
181         peerProxys_[key]->AsObject()->RemoveDeathRecipient(deathRecipients_[key]);
182         peerProxys_.erase(key);
183         deathRecipients_.erase(key);
184     } else {
185         SHARING_LOGE("Delete sub peer proxy, key: %{public}s not find.", key.c_str());
186     }
187 }
188 
189 } // namespace Sharing
190 } // namespace OHOS