• 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_client.h"
17 #include <string>
18 #include "common/reflect_registration.h"
19 #include "ipc_skeleton.h"
20 #include "iservice_registry.h"
21 #include "system_ability_definition.h"
22 
23 namespace OHOS {
24 namespace Sharing {
25 
InterIpcClient()26 InterIpcClient::InterIpcClient()
27 {
28     SHARING_LOGD("trace.");
29 }
30 
OnRemoteDied(std::string key)31 void InterIpcClient::OnRemoteDied(std::string key)
32 {
33     SHARING_LOGD("trace.");
34     std::lock_guard<std::mutex> lock(mutex_);
35 
36     if (domainProxy_ != nullptr) {
37         (void)domainProxy_->AsObject()->RemoveDeathRecipient(deathRecipient_);
38         domainProxy_ = nullptr;
39     }
40 
41     deathRecipient_ = nullptr;
42     listenerStub_ = nullptr;
43 
44     if (msgAdapter_ != nullptr) {
45         msgAdapter_->OnRemoteDied();
46     }
47 }
48 
SetKey(std::string key)49 void InterIpcClient::SetKey(std::string key)
50 {
51     SHARING_LOGD("trace.");
52     key_ = key;
53 }
54 
GetSharingProxy()55 sptr<IInterIpc> InterIpcClient::GetSharingProxy()
56 {
57     SHARING_LOGD("trace.");
58     auto saMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
59     if (saMgr == nullptr) {
60         SHARING_LOGE("get system ability manager failed.");
61         return nullptr;
62     }
63 
64     sptr<IRemoteObject> object = saMgr->GetSystemAbility(SHARING_SERVICE_TEMP_SA_ID);
65     if (object == nullptr) {
66         SHARING_LOGE("get system ability failed id: %{public}d.", SHARING_SERVICE_TEMP_SA_ID);
67         return nullptr;
68     }
69 
70     sptr<IInterIpc> proxy = iface_cast<IInterIpc>(object);
71     if (proxy == nullptr) {
72         SHARING_LOGE("iface_cast IInterIpc failed id: %{public}d.", SHARING_SERVICE_TEMP_SA_ID);
73         return nullptr;
74     }
75 
76     deathRecipient_ = new (std::nothrow) InterIpcDeathRecipient(key_);
77     deathRecipient_->SetDeathListener(shared_from_this());
78 
79     if (!object->AddDeathRecipient(deathRecipient_)) {
80         SHARING_LOGE("add IInterIpc death recipient failed.");
81         return nullptr;
82     }
83     SHARING_LOGD("get domain proxy success.");
84 
85     return proxy;
86 }
87 
GetSubProxy(std::string key,std::string className)88 sptr<IInterIpc> InterIpcClient::GetSubProxy(std::string key, std::string className)
89 {
90     SHARING_LOGD("trace.");
91     if (domainProxy_ == nullptr) {
92         SHARING_LOGE("get subsystem ability '%{public}s' proxy null.", className.c_str());
93         return nullptr;
94     }
95 
96     sptr<IRemoteObject> object = domainProxy_->GetSubSystemAbility(key, className);
97     if (!object) {
98         SHARING_LOGE("get subsystem ability type '%{public}s' not exist.", className.c_str());
99         return nullptr;
100     }
101 
102     sptr<IInterIpc> subProxy = iface_cast<IInterIpc>(object);
103     if (!subProxy) {
104         SHARING_LOGE("get subsystem ability '%{public}s' iface_cast null.", className.c_str());
105         return nullptr;
106     }
107 
108     return subProxy;
109 }
110 
CreateListenerObject()111 int32_t InterIpcClient::CreateListenerObject()
112 {
113     SHARING_LOGD("trace.");
114     if (domainProxy_ == nullptr) {
115         SHARING_LOGE("proxy null.");
116         return -1;
117     }
118 
119     if (key_.empty()) {
120         key_ = std::to_string(IPCSkeleton::GetCallingTokenID());
121         SHARING_LOGI("key is empty, using token As key: %{public}s.", key_.c_str());
122     }
123 
124     listenerStub_ = new (std::nothrow) InterIpcClientStub();
125     sptr<IRemoteObject> object = listenerStub_->AsObject();
126     msgAdapter_ = std::make_shared<IpcMsgAdapter>();
127     msgAdapter_->SetLocalStub(listenerStub_);
128     msgAdapter_->SetPeerProxy(domainProxy_);
129 
130     return domainProxy_->SetListenerObject(key_, object);
131 }
132 
Initialize(sptr<IInterIpc> standardProxy)133 void InterIpcClient::Initialize(sptr<IInterIpc> standardProxy)
134 {
135     SHARING_LOGD("trace.");
136     std::lock_guard<std::mutex> lock(mutex_);
137 
138     if (standardProxy != nullptr) {
139         domainProxy_ = standardProxy;
140     }
141 
142     if (domainProxy_) {
143         CreateListenerObject();
144     } else {
145         SHARING_LOGE("new StandardClient proxy null.");
146     }
147 }
148 
CreateSubService(std::string key,std::string clientClassName,std::string serverClassName)149 std::shared_ptr<InterIpcClient> InterIpcClient::CreateSubService(std::string key, std::string clientClassName,
150                                                                  std::string serverClassName)
151 {
152     SHARING_LOGD("trace.");
153     auto client = std::make_shared<InterIpcClient>();
154     client->SetKey(key);
155     auto subProxy = GetSubProxy(key, serverClassName);
156     if (subProxy == nullptr) {
157         return nullptr;
158     }
159 
160     client->Initialize(subProxy);
161 
162     return client;
163 }
164 
GetMsgAdapter()165 std::shared_ptr<IpcMsgAdapter> InterIpcClient::GetMsgAdapter()
166 {
167     SHARING_LOGD("trace.");
168     return msgAdapter_;
169 }
170 
171 } // namespace Sharing
172 } // namespace OHOS