• 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 "ipc_skeleton.h"
18 #include "interaction/interaction_manager.h"
19 #include "interaction/interaction.h"
20 #include "interaction/scene/base_scene.h"
21 #include "interaction/interprocess/ipc_msg_adapter.h"
22 #include "interaction/interprocess/inter_ipc_sub_stub.h"
23 #include "inter_ipc_service_death_listener.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     return 0;
73 }
74 
CreateDeathListener(std::string key)75 void InterIpcServiceStub::CreateDeathListener(std::string key)
76 {
77     SHARING_LOGD("trace.");
78     if (deathRecipients_.find(key) != deathRecipients_.end()) {
79         SHARING_LOGI("key: %{public}s.", key.c_str());
80         auto listener = std::make_shared<InterIpcServiceDeathListener>();
81         listener->SetService(this);
82         deathRecipients_[key]->SetDeathListener(listener);
83     } else {
84         SHARING_LOGE("key not find %{public}s.", key.c_str());
85     }
86 }
87 
DelPeerProxy(std::string key)88 void InterIpcServiceStub::DelPeerProxy(std::string key)
89 {
90     SHARING_LOGD("Delete SA peer proxy, key: %{public}s.", key.c_str());
91     std::lock_guard<std::mutex> lock(mutex_);
92     if (peerProxys_.find(key) != peerProxys_.end()) {
93         peerProxys_[key]->AsObject()->RemoveDeathRecipient(deathRecipients_[key]);
94         peerProxys_.erase(key);
95         deathRecipients_.erase(key);
96     } else {
97         SHARING_LOGE("Delete SA peer proxy, key: %{public}s not find.", key.c_str());
98     }
99 }
100 
SetListenerObject(std::string key,const sptr<IRemoteObject> & object)101 int32_t InterIpcServiceStub::SetListenerObject(std::string key, const sptr<IRemoteObject> &object)
102 {
103     SHARING_LOGD("trace.");
104     if (object == nullptr) {
105         SHARING_LOGE("domain rpc null listener object.");
106         return -1;
107     }
108 
109     sptr<IInterIpc> peerProxy = iface_cast<IInterIpc>(object);
110     if (peerProxy == nullptr) {
111         SHARING_LOGE("convert null.");
112         return -1;
113     }
114 
115     std::unique_lock<std::mutex> lock(mutex_);
116     peerProxys_[key] = peerProxy;
117     lock.unlock();
118     SHARING_LOGD("peer key: %{public}s, listener num: %{public}zu.", key.c_str(), peerProxys_.size());
119 
120     auto deathRecipient = new (std::nothrow) InterIpcDeathRecipient(key);
121     deathRecipients_[key] = deathRecipient;
122     CreateDeathListener(key);
123 
124     if (peerProxy->AsObject() != nullptr) {
125         if (!peerProxy->AsObject()->AddDeathRecipient(deathRecipient)) {
126             SHARING_LOGE("add death recipient failed.");
127             return -1;
128         }
129     }
130 
131     return ERR_NONE;
132 }
133 
134 }
135 }