1 /*
2 * Copyright (c) 2021-2022 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 "form_host_stub.h"
17
18 #include "appexecfwk_errors.h"
19 #include "app_scheduler_interface.h"
20 #include "errors.h"
21 #include "fms_log_wrapper.h"
22 #include "form_mgr_errors.h"
23 #include "ipc_skeleton.h"
24 #include "ipc_types.h"
25 #include "iremote_object.h"
26
27 namespace OHOS {
28 namespace AppExecFwk {
FormHostStub()29 FormHostStub::FormHostStub()
30 {
31 memberFuncMap_[static_cast<uint32_t>(IFormHost::Message::FORM_HOST_ON_ACQUIRED)] =
32 &FormHostStub::HandleAcquired;
33 memberFuncMap_[static_cast<uint32_t>(IFormHost::Message::FORM_HOST_ON_UPDATE)] =
34 &FormHostStub::HandleOnUpdate;
35 memberFuncMap_[static_cast<uint32_t>(IFormHost::Message::FORM_HOST_ON_UNINSTALL)] =
36 &FormHostStub::HandleOnUninstall;
37 memberFuncMap_[static_cast<uint32_t>(IFormHost::Message::FORM_HOST_ON_ACQUIRE_FORM_STATE)] =
38 &FormHostStub::HandleOnAcquireState;
39 memberFuncMap_[static_cast<uint32_t>(IFormHost::Message::FORM_HOST_ON_SHARE_FORM_RESPONSE)] =
40 &FormHostStub::HandleOnShareFormResponse;
41 memberFuncMap_[static_cast<uint32_t>(IFormHost::Message::FORM_HOST_ON_ERROR)] =
42 &FormHostStub::HandleOnError;
43 memberFuncMap_[static_cast<uint32_t>(IFormHost::Message::FORM_HOST_ON_ACQUIRE_FORM_DATA)] =
44 &FormHostStub::HandleOnAcquireDataResponse;
45 }
46
~FormHostStub()47 FormHostStub::~FormHostStub()
48 {
49 memberFuncMap_.clear();
50 }
51 /**
52 * @brief handle remote request.
53 * @param data input param.
54 * @param reply output param.
55 * @param option message option.
56 * @return Returns ERR_OK on success, others on failure.
57 */
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)58 int FormHostStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
59 {
60 HILOG_INFO("FormHostStub::OnReceived, code = %{public}u, flags= %{public}d.", code, option.GetFlags());
61 std::u16string descriptor = FormHostStub::GetDescriptor();
62 std::u16string remoteDescriptor = data.ReadInterfaceToken();
63 if (descriptor != remoteDescriptor) {
64 HILOG_ERROR("%{public}s failed, local descriptor is not equal to remote", __func__);
65 return ERR_APPEXECFWK_FORM_INVALID_PARAM;
66 }
67
68 auto itFunc = memberFuncMap_.find(code);
69 if (itFunc != memberFuncMap_.end()) {
70 auto memberFunc = itFunc->second;
71 if (memberFunc != nullptr) {
72 return (this->*memberFunc)(data, reply);
73 }
74 }
75
76 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
77 }
78 /**
79 * @brief handle OnAcquired event.
80 * @param data input param.
81 * @param reply output param.
82 * @return Returns ERR_OK on success, others on failure.
83 */
HandleAcquired(MessageParcel & data,MessageParcel & reply)84 int FormHostStub::HandleAcquired(MessageParcel &data, MessageParcel &reply)
85 {
86 std::unique_ptr<FormJsInfo> formInfo(data.ReadParcelable<FormJsInfo>());
87 if (!formInfo) {
88 HILOG_ERROR("%{public}s, failed to ReadParcelable<FormJsInfo>", __func__);
89 return ERR_APPEXECFWK_PARCEL_ERROR;
90 }
91
92 sptr<IRemoteObject> token = nullptr;
93 if (data.ReadBool()) {
94 token = data.ReadRemoteObject();
95 }
96
97 OnAcquired(*formInfo, token);
98 reply.WriteInt32(ERR_OK);
99 return ERR_OK;
100 }
101 /**
102 * @brief handle OnUpdate event.
103 * @param data input param.
104 * @param reply output param.
105 * @return Returns ERR_OK on success, others on failure.
106 */
HandleOnUpdate(MessageParcel & data,MessageParcel & reply)107 int FormHostStub::HandleOnUpdate(MessageParcel &data, MessageParcel &reply)
108 {
109 std::unique_ptr<FormJsInfo> formInfo(data.ReadParcelable<FormJsInfo>());
110 if (!formInfo) {
111 HILOG_ERROR("%{public}s, failed to ReadParcelable<FormJsInfo>", __func__);
112 return ERR_APPEXECFWK_PARCEL_ERROR;
113 }
114 OnUpdate(*formInfo);
115 reply.WriteInt32(ERR_OK);
116 return ERR_OK;
117 }
118
119 /**
120 * @brief handle OnUnInstall event.
121 * @param data input param.
122 * @param reply output param.
123 * @return Returns ERR_OK on success, others on failure.
124 */
HandleOnUninstall(MessageParcel & data,MessageParcel & reply)125 int FormHostStub::HandleOnUninstall(MessageParcel &data, MessageParcel &reply)
126 {
127 std::vector<int64_t> formIds;
128 bool ret = data.ReadInt64Vector(&formIds);
129 if (ret) {
130 OnUninstall(formIds);
131 reply.WriteInt32(ERR_OK);
132 return ERR_OK;
133 }
134 return ERR_APPEXECFWK_PARCEL_ERROR;
135 }
136
137 /**
138 * @brief handle OnAcquireState message.
139 * @param data input param.
140 * @param reply output param.
141 * @return Returns ERR_OK on success, others on failure.
142 */
HandleOnAcquireState(MessageParcel & data,MessageParcel & reply)143 int FormHostStub::HandleOnAcquireState(MessageParcel &data, MessageParcel &reply)
144 {
145 auto state = (FormState) data.ReadInt32();
146
147 std::unique_ptr<AAFwk::Want> want(data.ReadParcelable<AAFwk::Want>());
148 if (!want) {
149 HILOG_ERROR("%{public}s, failed to ReadParcelable<Want>", __func__);
150 reply.WriteInt32(ERR_APPEXECFWK_PARCEL_ERROR);
151 return ERR_APPEXECFWK_PARCEL_ERROR;
152 }
153
154 OnAcquireState(state, *want);
155 reply.WriteInt32(ERR_OK);
156 return ERR_OK;
157 }
158
HandleOnShareFormResponse(MessageParcel & data,MessageParcel & reply)159 int32_t FormHostStub::HandleOnShareFormResponse(MessageParcel &data, MessageParcel &reply)
160 {
161 auto requestCode = data.ReadInt64();
162 auto result = data.ReadInt32();
163
164 OnShareFormResponse(requestCode, result);
165 reply.WriteInt32(ERR_OK);
166 return ERR_OK;
167 }
168
HandleOnError(MessageParcel & data,MessageParcel & reply)169 int32_t FormHostStub::HandleOnError(MessageParcel &data, MessageParcel &reply)
170 {
171 int32_t errorCode = data.ReadInt32();
172 std::string errorMsg = Str16ToStr8(data.ReadString16());
173
174 OnError(errorCode, errorMsg);
175 reply.WriteInt32(ERR_OK);
176 return ERR_OK;
177 }
178
HandleOnAcquireDataResponse(MessageParcel & data,MessageParcel & reply)179 int32_t FormHostStub::HandleOnAcquireDataResponse(MessageParcel &data, MessageParcel &reply)
180 {
181 std::shared_ptr<AAFwk::WantParams> wantParams(data.ReadParcelable<AAFwk::WantParams>());
182 if (wantParams == nullptr) {
183 HILOG_ERROR("failed to ReadParcelable<wantParams>");
184 return ERR_APPEXECFWK_PARCEL_ERROR;
185 }
186
187 auto requestCode = data.ReadInt64();
188 if (requestCode <= 0) {
189 HILOG_ERROR("failed to ReadInt64<requestCode>");
190 return ERR_APPEXECFWK_PARCEL_ERROR;
191 }
192
193 OnAcquireDataResponse(*wantParams, requestCode);
194 reply.WriteInt32(ERR_OK);
195 return ERR_OK;
196 }
197 } // namespace AppExecFwk
198 } // namespace OHOS