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