1 /*
2  * Copyright (c) 2023-2024 Huawei Device 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 "service_router_mgr_proxy.h"
17 
18 #include "appexecfwk_errors.h"
19 #include "hilog_tag_wrapper.h"
20 #include "parcel_macro.h"
21 #include "service_router_mgr_interface.h"
22 
23 namespace OHOS {
24 namespace AbilityRuntime {
ServiceRouterMgrProxy(const sptr<IRemoteObject> & object)25 ServiceRouterMgrProxy::ServiceRouterMgrProxy(const sptr<IRemoteObject> &object)
26     : IRemoteProxy<IServiceRouterManager>(object)
27 {
28     TAG_LOGD(AAFwkTag::SER_ROUTER, "created");
29 }
30 
~ServiceRouterMgrProxy()31 ServiceRouterMgrProxy::~ServiceRouterMgrProxy()
32 {
33     TAG_LOGD(AAFwkTag::SER_ROUTER, "destroyed");
34 }
35 
QueryBusinessAbilityInfos(const BusinessAbilityFilter & filter,std::vector<BusinessAbilityInfo> & abilityInfos)36 int32_t ServiceRouterMgrProxy::QueryBusinessAbilityInfos(const BusinessAbilityFilter &filter,
37     std::vector<BusinessAbilityInfo> &abilityInfos)
38 {
39     TAG_LOGD(AAFwkTag::SER_ROUTER, "Called");
40     MessageParcel data;
41     if (!data.WriteInterfaceToken(GetDescriptor())) {
42         TAG_LOGE(AAFwkTag::SER_ROUTER, "Write interfaceToken failed");
43         return ERR_APPEXECFWK_PARCEL_ERROR;
44     }
45     if (!data.WriteParcelable(&filter)) {
46         TAG_LOGE(AAFwkTag::SER_ROUTER, "Write filter failed");
47         return ERR_APPEXECFWK_PARCEL_ERROR;
48     }
49     int32_t res = GetParcelableInfos<BusinessAbilityInfo>(ServiceRouterMgrProxy::Message::QUERY_BUSINESS_ABILITY_INFOS,
50         data, abilityInfos);
51     if (res != OHOS::NO_ERROR) {
52         TAG_LOGE(AAFwkTag::SER_ROUTER, "QueryBusinessAbilityInfos error: %{public}d", res);
53     }
54     return res;
55 }
56 
QueryPurposeInfos(const Want & want,const std::string purposeName,std::vector<PurposeInfo> & purposeInfos)57 int32_t ServiceRouterMgrProxy::QueryPurposeInfos(const Want &want, const std::string purposeName,
58     std::vector<PurposeInfo> &purposeInfos)
59 {
60     TAG_LOGD(AAFwkTag::SER_ROUTER, "Called");
61     MessageParcel data;
62     if (!data.WriteInterfaceToken(GetDescriptor())) {
63         TAG_LOGE(AAFwkTag::SER_ROUTER, "Write interfaceToken failed");
64         return ERR_APPEXECFWK_PARCEL_ERROR;
65     }
66     if (!data.WriteParcelable(&want)) {
67         TAG_LOGE(AAFwkTag::SER_ROUTER, "Write want failed");
68         return ERR_APPEXECFWK_PARCEL_ERROR;
69     }
70     if (!data.WriteString(purposeName)) {
71         TAG_LOGE(AAFwkTag::SER_ROUTER, "Write purposeName failed");
72         return false;
73     }
74     int32_t res = GetParcelableInfos<PurposeInfo>(ServiceRouterMgrProxy::Message::QUERY_PURPOSE_INFOS, data,
75         purposeInfos);
76     if (res != OHOS::NO_ERROR) {
77         TAG_LOGE(AAFwkTag::SER_ROUTER, "QueryPurposeInfos error: %{public}d", res);
78     }
79     return res;
80 }
81 
StartUIExtensionAbility(const sptr<SessionInfo> & sessionInfo,int32_t userId)82 int32_t ServiceRouterMgrProxy::StartUIExtensionAbility(const sptr<SessionInfo> &sessionInfo, int32_t userId)
83 {
84     MessageParcel data;
85     MessageParcel reply;
86     MessageOption option;
87     if (!data.WriteInterfaceToken(GetDescriptor())) {
88         TAG_LOGE(AAFwkTag::SER_ROUTER, "Write interfaceToken failed");
89         return ERR_APPEXECFWK_PARCEL_ERROR;
90     }
91 
92     if (sessionInfo) {
93         if (!data.WriteBool(true) || !data.WriteParcelable(sessionInfo)) {
94             TAG_LOGE(AAFwkTag::SER_ROUTER, "Flag or sessionInfo write failed");
95             return ERR_APPEXECFWK_PARCEL_ERROR;
96         }
97     } else {
98         if (!data.WriteBool(false)) {
99             TAG_LOGE(AAFwkTag::SER_ROUTER, "Flag write failed");
100             return ERR_APPEXECFWK_PARCEL_ERROR;
101         }
102     }
103 
104     if (!data.WriteInt32(userId)) {
105         TAG_LOGE(AAFwkTag::SER_ROUTER, "UserId write failed.");
106         return ERR_APPEXECFWK_PARCEL_ERROR;
107     }
108 
109     int32_t error = SendRequest(ServiceRouterMgrProxy::Message::START_UI_EXTENSION, data, reply, option);
110     if (error != NO_ERROR) {
111         TAG_LOGE(AAFwkTag::SER_ROUTER, "Send request error: %{public}d", error);
112         return error;
113     }
114     return reply.ReadInt32();
115 }
116 
ConnectUIExtensionAbility(const Want & want,const sptr<IAbilityConnection> & connect,const sptr<SessionInfo> & sessionInfo,int32_t userId)117 int32_t ServiceRouterMgrProxy::ConnectUIExtensionAbility(const Want &want, const sptr<IAbilityConnection> &connect,
118     const sptr<SessionInfo> &sessionInfo, int32_t userId)
119 {
120     MessageParcel data;
121     MessageParcel reply;
122     MessageOption option;
123 
124     if (!data.WriteInterfaceToken(GetDescriptor()) || !data.WriteParcelable(&want)) {
125         TAG_LOGE(AAFwkTag::SER_ROUTER, "Write interfaceToken or want failed");
126         return ERR_APPEXECFWK_PARCEL_ERROR;
127     }
128 
129     if (!connect) {
130         TAG_LOGE(AAFwkTag::SER_ROUTER, "null connect");
131         return ERR_APPEXECFWK_PARCEL_ERROR;
132     }
133 
134     if (connect->AsObject()) {
135         if (!data.WriteBool(true) || !data.WriteRemoteObject(connect->AsObject())) {
136             TAG_LOGE(AAFwkTag::SER_ROUTER, "Flag or connect write failed.");
137             return ERR_APPEXECFWK_PARCEL_ERROR;
138         }
139     } else {
140         if (!data.WriteBool(false)) {
141             TAG_LOGE(AAFwkTag::SER_ROUTER, "Flag write failed");
142             return ERR_APPEXECFWK_PARCEL_ERROR;
143         }
144     }
145     if (sessionInfo) {
146         if (!data.WriteBool(true) || !data.WriteParcelable(sessionInfo)) {
147             TAG_LOGE(AAFwkTag::SER_ROUTER, "Flag or sessionInfo write failed");
148             return ERR_APPEXECFWK_PARCEL_ERROR;
149         }
150     } else {
151         if (!data.WriteBool(false)) {
152             TAG_LOGE(AAFwkTag::SER_ROUTER, "Flag write failed");
153             return ERR_APPEXECFWK_PARCEL_ERROR;
154         }
155     }
156     if (!data.WriteInt32(userId)) {
157         TAG_LOGE(AAFwkTag::SER_ROUTER, "UserId write failed");
158         return ERR_APPEXECFWK_PARCEL_ERROR;
159     }
160     int32_t error = SendRequest(ServiceRouterMgrProxy::Message::CONNECT_UI_EXTENSION, data, reply, option);
161     if (error != NO_ERROR) {
162         TAG_LOGE(AAFwkTag::SER_ROUTER, "Send request error: %{public}d", error);
163         return error;
164     }
165     return reply.ReadInt32();
166 }
167 
SendRequest(ServiceRouterMgrProxy::Message code,MessageParcel & data,MessageParcel & reply,MessageOption & option)168 int32_t ServiceRouterMgrProxy::SendRequest(ServiceRouterMgrProxy::Message code, MessageParcel &data,
169     MessageParcel &reply, MessageOption &option)
170 {
171     TAG_LOGD(AAFwkTag::SER_ROUTER, "Called");
172     sptr<IRemoteObject> remote = Remote();
173     if (remote == nullptr) {
174         TAG_LOGE(AAFwkTag::SER_ROUTER, "null remote");
175         return ERR_APPEXECFWK_FAILED_GET_REMOTE_PROXY;
176     }
177     int32_t result = remote->SendRequest(static_cast<uint32_t>(code), data, reply, option);
178     if (result != NO_ERROR) {
179         TAG_LOGE(
180             AAFwkTag::SER_ROUTER, "Send %{public}d cmd to service failed, transact error:%{public}d", code, result);
181     }
182     return result;
183 }
184 
185 template <typename T>
GetParcelableInfos(ServiceRouterMgrProxy::Message code,MessageParcel & data,std::vector<T> & parcelableInfos)186 int32_t ServiceRouterMgrProxy::GetParcelableInfos(
187     ServiceRouterMgrProxy::Message code, MessageParcel &data, std::vector<T> &parcelableInfos)
188 {
189     MessageParcel reply;
190     MessageOption option(MessageOption::TF_SYNC);
191     int32_t result = SendRequest(code, data, reply, option);
192     if (result != OHOS::NO_ERROR) {
193         TAG_LOGE(AAFwkTag::SER_ROUTER, "SendRequest result failed");
194         return result;
195     }
196 
197     int32_t res = reply.ReadInt32();
198     if (res != ERR_OK) {
199         TAG_LOGE(AAFwkTag::SER_ROUTER, "reply error: %{public}d", res);
200         return res;
201     }
202 
203     int32_t infosSize = reply.ReadInt32();
204     for (int32_t j = 0; j < infosSize; j++) {
205         std::unique_ptr<T> info(reply.ReadParcelable<T>());
206         if (!info) {
207             TAG_LOGE(AAFwkTag::SER_ROUTER, "Read parcelableInfos failed");
208             return ERR_APPEXECFWK_PARCEL_ERROR;
209         }
210         parcelableInfos.emplace_back(*info);
211     }
212     TAG_LOGI(AAFwkTag::SER_ROUTER, "Get parcelableInfos success");
213     return OHOS::NO_ERROR;
214 }
215 }  // namespace AbilityRuntime
216 }  // namespace OHOS
217