• 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_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     sptr<IRemoteObject> object = adapterStub->AsObject();
55 
56     auto adapter = std::make_shared<IpcMsgAdapter>();
57     adapter->SetCallingKey(key);
58     adapter->SetLocalStub(adapterStub);
59     scene->SetIpcAdapter(adapter);
60     interaction->SetIpcAdapter(adapter);
61     interaction->SetRpcKey(key);
62 
63     SHARING_LOGI("AddInteractionKey key: %{public}s  interactionId: %{public}d.", key.c_str(), interaction->GetId());
64     InteractionManager::GetInstance().AddInteractionKey(key, interaction->GetId());
65 
66     return object;
67 }
68 
DoIpcCommand(std::shared_ptr<BaseMsg> msg,std::shared_ptr<BaseMsg> & replyMsg)69 int32_t InterIpcServiceStub::DoIpcCommand(std::shared_ptr<BaseMsg> msg, std::shared_ptr<BaseMsg> &replyMsg)
70 {
71     SHARING_LOGD("trace.");
72     (void)msg;
73     (void)replyMsg;
74     return 0;
75 }
76 
CreateDeathListener(std::string key)77 void InterIpcServiceStub::CreateDeathListener(std::string key)
78 {
79     SHARING_LOGD("trace.");
80     if (deathRecipients_.find(key) != deathRecipients_.end()) {
81         SHARING_LOGI("key: %{public}s.", key.c_str());
82         auto listener = std::make_shared<InterIpcServiceDeathListener>();
83         if (sharedFromThis_ == nullptr) {
84             sharedFromThis_ = InterIpcStub::Ptr(this, [](InterIpcStub *) { SHARING_LOGD("trace."); });
85         }
86         auto service = std::static_pointer_cast<InterIpcServiceStub>(sharedFromThis_);
87         listener->SetService(service);
88         deathRecipients_[key]->SetDeathListener(listener);
89     } else {
90         SHARING_LOGE("key not find %{public}s.", key.c_str());
91     }
92 }
93 
DelPeerProxy(std::string key)94 void InterIpcServiceStub::DelPeerProxy(std::string key)
95 {
96     SHARING_LOGD("Delete SA peer proxy, key: %{public}s.", key.c_str());
97     std::lock_guard<std::mutex> lock(mutex_);
98     if (peerProxys_.find(key) != peerProxys_.end()) {
99         peerProxys_[key]->AsObject()->RemoveDeathRecipient(deathRecipients_[key]);
100         peerProxys_.erase(key);
101         deathRecipients_.erase(key);
102     } else {
103         SHARING_LOGE("Delete SA peer proxy, key: %{public}s not find.", key.c_str());
104     }
105 }
106 
SetListenerObject(std::string key,const sptr<IRemoteObject> & object)107 int32_t InterIpcServiceStub::SetListenerObject(std::string key, const sptr<IRemoteObject> &object)
108 {
109     SHARING_LOGD("trace.");
110     if (object == nullptr) {
111         SHARING_LOGE("domain rpc null listener object.");
112         return -1;
113     }
114 
115     sptr<IInterIpc> peerProxy = iface_cast<IInterIpc>(object);
116     if (peerProxy == nullptr) {
117         SHARING_LOGE("convert null.");
118         return -1;
119     }
120 
121     std::unique_lock<std::mutex> lock(mutex_);
122     peerProxys_[key] = peerProxy;
123     lock.unlock();
124     SHARING_LOGD("peer key: %{public}s, listener num: %{public}zu.", key.c_str(), peerProxys_.size());
125 
126     auto deathRecipient = new (std::nothrow) InterIpcDeathRecipient(key);
127     deathRecipients_[key] = deathRecipient;
128     CreateDeathListener(key);
129 
130     if (peerProxy->AsObject() != nullptr) {
131         if (!peerProxy->AsObject()->AddDeathRecipient(deathRecipient)) {
132             SHARING_LOGE("add death recipient failed.");
133             return -1;
134         }
135     }
136 
137     return ERR_NONE;
138 }
139 
140 } // namespace Sharing
141 } // namespace OHOS