1 /*
2 * Copyright (c) 2023 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 "app_log_wrapper.h"
19 #include "appexecfwk_errors.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 APP_LOGD("ServiceRouterMgrProxy instance is created");
29 }
30
~ServiceRouterMgrProxy()31 ServiceRouterMgrProxy::~ServiceRouterMgrProxy()
32 {
33 APP_LOGD("ServiceRouterMgrProxy instance is 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 APP_LOGD("ServiceRouterMgrProxy QueryBusinessAbilityInfos");
40 MessageParcel data;
41 if (!data.WriteInterfaceToken(GetDescriptor())) {
42 APP_LOGE("write interfaceToken failed");
43 return ERR_APPEXECFWK_PARCEL_ERROR;
44 }
45 if (!data.WriteParcelable(&filter)) {
46 APP_LOGE("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 APP_LOGE("fail to QueryBusinessAbilityInfos from server, error code: %{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 APP_LOGD("ServiceRouterMgrProxy QueryPurposeInfos");
61 MessageParcel data;
62 if (!data.WriteInterfaceToken(GetDescriptor())) {
63 APP_LOGE("write interfaceToken failed");
64 return ERR_APPEXECFWK_PARCEL_ERROR;
65 }
66 if (!data.WriteParcelable(&want)) {
67 APP_LOGE("write want failed");
68 return ERR_APPEXECFWK_PARCEL_ERROR;
69 }
70 if (!data.WriteString(purposeName)) {
71 APP_LOGE("write purposeName fail");
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 APP_LOGE("fail to QueryPurposeInfos from server, error code: %{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 APP_LOGE("write interfaceToken failed");
89 return ERR_APPEXECFWK_PARCEL_ERROR;
90 }
91
92 if (sessionInfo) {
93 if (!data.WriteBool(true) || !data.WriteParcelable(sessionInfo)) {
94 APP_LOGE("flag and sessionInfo write failed.");
95 return ERR_APPEXECFWK_PARCEL_ERROR;
96 }
97 } else {
98 if (!data.WriteBool(false)) {
99 APP_LOGE("flag write failed.");
100 return ERR_APPEXECFWK_PARCEL_ERROR;
101 }
102 }
103
104 if (!data.WriteInt32(userId)) {
105 APP_LOGE("StartExtensionAbility, userId write failed.");
106 return ERR_APPEXECFWK_PARCEL_ERROR;
107 }
108 if (!Remote()) {
109 APP_LOGE("StartExtensionAbility, Remote error.");
110 return ERR_APPEXECFWK_PARCEL_ERROR;
111 }
112
113 sptr<IRemoteObject> remote = Remote();
114 if (remote == nullptr) {
115 APP_LOGE("StartExtensionAbility, Remote() is NULL");
116 return ERR_APPEXECFWK_PARCEL_ERROR;
117 }
118
119 int32_t error = remote->SendRequest(ServiceRouterMgrProxy::Message::START_UI_EXTENSION, data, reply, option);
120 if (error != NO_ERROR) {
121 APP_LOGE("StartExtensionAbility, Send request error: %{public}d", error);
122 return error;
123 }
124 return reply.ReadInt32();
125 }
126
ConnectUIExtensionAbility(const Want & want,const sptr<IAbilityConnection> & connect,const sptr<SessionInfo> & sessionInfo,int32_t userId)127 int32_t ServiceRouterMgrProxy::ConnectUIExtensionAbility(const Want &want, const sptr<IAbilityConnection> &connect,
128 const sptr<SessionInfo> &sessionInfo, int32_t userId)
129 {
130 MessageParcel data;
131 MessageParcel reply;
132 MessageOption option;
133
134 if (!data.WriteInterfaceToken(GetDescriptor()) || !data.WriteParcelable(&want)) {
135 APP_LOGE("write interfaceToken or want failed");
136 return ERR_APPEXECFWK_PARCEL_ERROR;
137 }
138
139 if (!connect) {
140 APP_LOGE("connect ability fail, connect is nullptr");
141 return ERR_APPEXECFWK_PARCEL_ERROR;
142 }
143
144 if (connect->AsObject()) {
145 if (!data.WriteBool(true) || !data.WriteRemoteObject(connect->AsObject())) {
146 APP_LOGE("flag and connect write failed.");
147 return ERR_APPEXECFWK_PARCEL_ERROR;
148 }
149 } else {
150 if (!data.WriteBool(false)) {
151 APP_LOGE("flag write failed.");
152 return ERR_APPEXECFWK_PARCEL_ERROR;
153 }
154 }
155 if (sessionInfo) {
156 if (!data.WriteBool(true) || !data.WriteParcelable(sessionInfo)) {
157 APP_LOGE("flag and sessionInfo write failed.");
158 return ERR_APPEXECFWK_PARCEL_ERROR;
159 }
160 } else {
161 if (!data.WriteBool(false)) {
162 APP_LOGE("flag write failed.");
163 return ERR_APPEXECFWK_PARCEL_ERROR;
164 }
165 }
166 if (!data.WriteInt32(userId)) {
167 APP_LOGE("%{public}s, userId write failed.", __func__);
168 return ERR_APPEXECFWK_PARCEL_ERROR;
169 }
170 if (!Remote()) {
171 APP_LOGE("connect ability fail, remote is nullptr");
172 return ERR_APPEXECFWK_PARCEL_ERROR;
173 }
174 int32_t error = Remote()->SendRequest(ServiceRouterMgrProxy::Message::CONNECT_UI_EXTENSION, data, reply, option);
175 if (error != NO_ERROR) {
176 APP_LOGE("%{public}s, Send request error: %{public}d", __func__, error);
177 return error;
178 }
179 return reply.ReadInt32();
180 }
181
SendRequest(ServiceRouterMgrProxy::Message code,MessageParcel & data,MessageParcel & reply)182 int32_t ServiceRouterMgrProxy::SendRequest(ServiceRouterMgrProxy::Message code, MessageParcel &data,
183 MessageParcel &reply)
184 {
185 APP_LOGI("ServiceRouterMgrProxy SendRequest");
186 sptr<IRemoteObject> remote = Remote();
187 MessageOption option(MessageOption::TF_SYNC);
188 if (remote == nullptr) {
189 APP_LOGE("fail to send %{public}d cmd to service, remote object is null", code);
190 return ERR_APPEXECFWK_FAILED_GET_REMOTE_PROXY;
191 }
192 int32_t result = remote->SendRequest(static_cast<uint32_t>(code), data, reply, option);
193 if (result != OHOS::NO_ERROR) {
194 APP_LOGE("fail to send %{public}d cmd to service, transact error:%{public}d", code, result);
195 }
196 return result;
197 }
198
199 template <typename T>
GetParcelableInfos(ServiceRouterMgrProxy::Message code,MessageParcel & data,std::vector<T> & parcelableInfos)200 int32_t ServiceRouterMgrProxy::GetParcelableInfos(
201 ServiceRouterMgrProxy::Message code, MessageParcel &data, std::vector<T> &parcelableInfos)
202 {
203 MessageParcel reply;
204 int32_t result = SendRequest(code, data, reply);
205 if (result != OHOS::NO_ERROR) {
206 APP_LOGE("SendRequest result false");
207 return result;
208 }
209
210 int32_t res = reply.ReadInt32();
211 if (res != ERR_OK) {
212 APP_LOGE("reply's result is %{public}d", res);
213 return res;
214 }
215
216 int32_t infosSize = reply.ReadInt32();
217 for (int32_t j = 0; j < infosSize; j++) {
218 std::unique_ptr<T> info(reply.ReadParcelable<T>());
219 if (!info) {
220 APP_LOGE("Read parcelableInfos failed");
221 return ERR_APPEXECFWK_PARCEL_ERROR;
222 }
223 parcelableInfos.emplace_back(*info);
224 }
225 APP_LOGI("get parcelableInfos success");
226 return OHOS::NO_ERROR;
227 }
228 } // namespace AbilityRuntime
229 } // namespace OHOS