• 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     if (deathRecipient_ == nullptr) {
78         SHARING_LOGE("deathRecipient create failed.");
79         return nullptr;
80     }
81     deathRecipient_->SetDeathListener(shared_from_this());
82 
83     if (!object->AddDeathRecipient(deathRecipient_)) {
84         SHARING_LOGE("add IInterIpc death recipient failed.");
85         return nullptr;
86     }
87     SHARING_LOGD("get domain proxy success.");
88 
89     return proxy;
90 }
91 
GetSubProxy(std::string key,std::string className)92 sptr<IInterIpc> InterIpcClient::GetSubProxy(std::string key, std::string className)
93 {
94     SHARING_LOGD("trace.");
95     if (domainProxy_ == nullptr) {
96         SHARING_LOGE("get subsystem ability '%{public}s' proxy null.", className.c_str());
97         return nullptr;
98     }
99 
100     sptr<IRemoteObject> object = domainProxy_->GetSubSystemAbility(key, className);
101     if (!object) {
102         SHARING_LOGE("get subsystem ability type '%{public}s' not exist.", className.c_str());
103         return nullptr;
104     }
105 
106     sptr<IInterIpc> subProxy = iface_cast<IInterIpc>(object);
107     if (!subProxy) {
108         SHARING_LOGE("get subsystem ability '%{public}s' iface_cast null.", className.c_str());
109         return nullptr;
110     }
111 
112     return subProxy;
113 }
114 
CreateListenerObject()115 int32_t InterIpcClient::CreateListenerObject()
116 {
117     SHARING_LOGD("trace.");
118     if (domainProxy_ == nullptr) {
119         SHARING_LOGE("proxy null.");
120         return -1;
121     }
122 
123     if (key_.empty()) {
124         key_ = std::to_string(IPCSkeleton::GetCallingTokenID());
125         SHARING_LOGI("key is empty, using token As key: %{public}s.", key_.c_str());
126     }
127 
128     listenerStub_ = new (std::nothrow) InterIpcClientStub();
129     if (listenerStub_ == nullptr) {
130         SHARING_LOGE("listenerStub create failed.");
131         return -1;
132     }
133     sptr<IRemoteObject> object = listenerStub_->AsObject();
134     msgAdapter_ = std::make_shared<IpcMsgAdapter>();
135     msgAdapter_->SetLocalStub(listenerStub_);
136     msgAdapter_->SetPeerProxy(domainProxy_);
137 
138     return domainProxy_->SetListenerObject(key_, object);
139 }
140 
Initialize(sptr<IInterIpc> standardProxy)141 void InterIpcClient::Initialize(sptr<IInterIpc> standardProxy)
142 {
143     SHARING_LOGD("trace.");
144     std::lock_guard<std::mutex> lock(mutex_);
145 
146     if (standardProxy != nullptr) {
147         domainProxy_ = standardProxy;
148     }
149 
150     if (domainProxy_) {
151         CreateListenerObject();
152     } else {
153         SHARING_LOGE("new StandardClient proxy null.");
154     }
155 }
156 
CreateSubService(std::string key,std::string clientClassName,std::string serverClassName)157 std::shared_ptr<InterIpcClient> InterIpcClient::CreateSubService(std::string key, std::string clientClassName,
158                                                                  std::string serverClassName)
159 {
160     SHARING_LOGD("trace.");
161     auto client = std::make_shared<InterIpcClient>();
162     client->SetKey(key);
163     auto subProxy = GetSubProxy(key, serverClassName);
164     if (subProxy == nullptr) {
165         return nullptr;
166     }
167 
168     client->Initialize(subProxy);
169 
170     return client;
171 }
172 
GetMsgAdapter()173 std::shared_ptr<IpcMsgAdapter> InterIpcClient::GetMsgAdapter()
174 {
175     SHARING_LOGD("trace.");
176     return msgAdapter_;
177 }
178 
179 } // namespace Sharing
180 } // namespace OHOS