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 "query_app_info_callback_proxy.h"
17
18 #include "nfc_service_ipc_interface_code.h"
19 #include "loghelper.h"
20
21 namespace OHOS {
22 namespace NFC {
23 static const int MAX_HAP_LIST_LEN = 1000;
24
QueryAppInfoCallbackProxy(const sptr<IRemoteObject> & remote)25 QueryAppInfoCallbackProxy::QueryAppInfoCallbackProxy(const sptr<IRemoteObject> &remote)
26 : IRemoteProxy<IQueryAppInfoCallback>(remote)
27 {}
28
OnQueryAppInfo(std::string type,std::vector<int> techList,std::vector<AAFwk::Want> & hceAppList,std::vector<AppExecFwk::ElementName> & elementNameList)29 bool QueryAppInfoCallbackProxy::OnQueryAppInfo(std::string type, std::vector<int> techList,
30 std::vector<AAFwk::Want> &hceAppList, std::vector<AppExecFwk::ElementName> &elementNameList)
31 {
32 MessageOption option = {MessageOption::TF_SYNC};
33 MessageParcel data;
34 MessageParcel reply;
35 if (!data.WriteInterfaceToken(GetDescriptor())) {
36 ErrorLog("OnQueryAppInfo:WriteInterfaceToken token error");
37 return false;
38 }
39 data.WriteInt32(0);
40 data.WriteString(type);
41 DebugLog("query %{pubic}s app.", type.c_str());
42 if (type.compare(KEY_TAG_APP) == 0) {
43 data.WriteInt32Vector(techList);
44 int error = Remote()->SendRequest(
45 static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_QUERY_APP_INFO_MSG_CALLBACK),
46 data, reply, option);
47 if (error != ERR_NONE) {
48 ErrorLog("QueryAppInfoCallbackProxy::OnQueryAppInfo, Set Attr error: %{public}d", error);
49 return false;
50 }
51 int elementNameListLen = reply.ReadInt32();
52 InfoLog("QueryAppInfoCallbackProxy::OnQueryAppInfo recv %{public}d app need to add", elementNameListLen);
53 if (elementNameListLen > MAX_HAP_LIST_LEN) {
54 return false;
55 }
56 for (int i = 0; i < elementNameListLen; i++) {
57 AppExecFwk::ElementName *elementName = AppExecFwk::ElementName::Unmarshalling(reply);
58 if (elementName == nullptr) {
59 ErrorLog("elementName nullptr");
60 return false;
61 }
62 elementNameList.push_back(*elementName);
63 delete elementName;
64 elementName = nullptr;
65 }
66 return true;
67 } else if (type.compare(KEY_HCE_APP) == 0) {
68 int error = Remote()->SendRequest(
69 static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_QUERY_APP_INFO_MSG_CALLBACK),
70 data, reply, option);
71 if (error != ERR_NONE) {
72 ErrorLog("QueryAppInfoCallbackProxy::OnQueryAppInfo, Set Attr error: %{public}d", error);
73 return false;
74 }
75 int appLen = reply.ReadInt32();
76 InfoLog("QueryAppInfoCallbackProxy::OnQueryAppInfo recv %{public}d app need to add", appLen);
77 if (appLen > MAX_HAP_LIST_LEN) {
78 return false;
79 }
80 for (int i = 0; i < appLen; i++) {
81 AAFwk::Want *want = AAFwk::Want::Unmarshalling(reply);
82 if (want == nullptr) {
83 ErrorLog("want nullptr");
84 return false;
85 }
86 hceAppList.push_back(*want);
87 delete want;
88 want = nullptr;
89 }
90 return true;
91 }
92 return false;
93 }
94 } // namespace NFC
95 } // namespace OHOS
96