• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 "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 
~FormHostStub()32 FormHostStub::~FormHostStub()
33 {}
34 /**
35  * @brief handle remote request.
36  * @param data input param.
37  * @param reply output param.
38  * @param option message option.
39  * @return Returns ERR_OK on success, others on failure.
40  */
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)41 int FormHostStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
42 {
43     HILOG_INFO("code:%{public}u,flags:%{public}d", code, option.GetFlags());
44     std::u16string descriptor = FormHostStub::GetDescriptor();
45     std::u16string remoteDescriptor = data.ReadInterfaceToken();
46     if (descriptor != remoteDescriptor) {
47         HILOG_ERROR("localDescribe not equal to remote");
48         return ERR_APPEXECFWK_FORM_INVALID_PARAM;
49     }
50 
51     switch (code) {
52         case static_cast<uint32_t>(IFormHost::Message::FORM_HOST_ON_ACQUIRED):
53             return HandleAcquired(data, reply);
54         case static_cast<uint32_t>(IFormHost::Message::FORM_HOST_ON_UPDATE):
55             return HandleOnUpdate(data, reply);
56         case static_cast<uint32_t>(IFormHost::Message::FORM_HOST_ON_UNINSTALL):
57             return HandleOnUninstall(data, reply);
58         case static_cast<uint32_t>(IFormHost::Message::FORM_HOST_ON_ACQUIRE_FORM_STATE):
59             return HandleOnAcquireState(data, reply);
60         case static_cast<uint32_t>(IFormHost::Message::FORM_HOST_ON_SHARE_FORM_RESPONSE):
61             return HandleOnShareFormResponse(data, reply);
62         case static_cast<uint32_t>(IFormHost::Message::FORM_HOST_ON_ERROR):
63             return HandleOnError(data, reply);
64         case static_cast<uint32_t>(IFormHost::Message::FORM_HOST_ON_ACQUIRE_FORM_DATA):
65             return HandleOnAcquireDataResponse(data, reply);
66         case static_cast<uint32_t>(IFormHost::Message::FORM_HOST_ON_RECYCLE_FORM):
67             return HandleOnRecycleForm(data, reply);
68         case static_cast<uint32_t>(IFormHost::Message::FORM_HOST_ON_ENABLE_FORM):
69             return HandleOnEnableForm(data, reply);
70         case static_cast<uint32_t>(IFormHost::Message::FORM_HOST_ON_ERROR_FORMS):
71             return HandleOnErrorForms(data, reply);
72         case static_cast<uint32_t>(IFormHost::Message::FORM_HOST_ON_LOCK_FORM):
73             return HandleOnLockForm(data, reply);
74         default:
75             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
76     }
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("ReadParcelable<FormJsInfo> failed");
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("ReadParcelable<FormJsInfo> failed");
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     std::unique_ptr<AAFwk::Want> want(data.ReadParcelable<AAFwk::Want>());
146     if (!want) {
147         HILOG_ERROR("ReadParcelable<Want> failed");
148         reply.WriteInt32(ERR_APPEXECFWK_PARCEL_ERROR);
149         return ERR_APPEXECFWK_PARCEL_ERROR;
150     }
151 
152     auto state = (FormState) data.ReadInt32();
153     OnAcquireState(state, *want);
154     reply.WriteInt32(ERR_OK);
155     return ERR_OK;
156 }
157 
HandleOnShareFormResponse(MessageParcel & data,MessageParcel & reply)158 int32_t FormHostStub::HandleOnShareFormResponse(MessageParcel &data, MessageParcel &reply)
159 {
160     auto requestCode = data.ReadInt64();
161     auto result = data.ReadInt32();
162 
163     OnShareFormResponse(requestCode, result);
164     reply.WriteInt32(ERR_OK);
165     return ERR_OK;
166 }
167 
HandleOnError(MessageParcel & data,MessageParcel & reply)168 int32_t FormHostStub::HandleOnError(MessageParcel &data, MessageParcel &reply)
169 {
170     int32_t errorCode = data.ReadInt32();
171     std::string errorMsg = Str16ToStr8(data.ReadString16());
172 
173     OnError(errorCode, errorMsg);
174     reply.WriteInt32(ERR_OK);
175     return ERR_OK;
176 }
177 
HandleOnAcquireDataResponse(MessageParcel & data,MessageParcel & reply)178 int32_t FormHostStub::HandleOnAcquireDataResponse(MessageParcel &data, MessageParcel &reply)
179 {
180     std::shared_ptr<AAFwk::WantParams> wantParams(data.ReadParcelable<AAFwk::WantParams>());
181     if (wantParams == nullptr) {
182         HILOG_ERROR("ReadParcelable<wantParams> failed");
183         return ERR_APPEXECFWK_PARCEL_ERROR;
184     }
185 
186     auto requestCode = data.ReadInt64();
187     if (requestCode <= 0) {
188         HILOG_ERROR("fail ReadInt64<requestCode>");
189         return ERR_APPEXECFWK_PARCEL_ERROR;
190     }
191 
192     OnAcquireDataResponse(*wantParams, requestCode);
193     reply.WriteInt32(ERR_OK);
194     return ERR_OK;
195 }
196 
HandleOnRecycleForm(MessageParcel & data,MessageParcel & reply)197 int32_t FormHostStub::HandleOnRecycleForm(MessageParcel &data, MessageParcel &reply)
198 {
199     int64_t formId = data.ReadInt64();
200     OnRecycleForm(formId);
201     reply.WriteInt32(ERR_OK);
202     return ERR_OK;
203 }
204 
HandleOnEnableForm(MessageParcel & data,MessageParcel & reply)205 int32_t FormHostStub::HandleOnEnableForm(MessageParcel &data, MessageParcel &reply)
206 {
207     std::vector<int64_t> formIds;
208     bool ret = data.ReadInt64Vector(&formIds);
209     if (!ret) {
210         HILOG_ERROR("fail ReadInt64Vector<formIds>");
211         return ERR_APPEXECFWK_PARCEL_ERROR;
212     }
213     bool enable = data.ReadBool();
214     OnEnableForm(formIds, enable);
215     reply.WriteInt32(ERR_OK);
216     return ERR_OK;
217 }
218 
HandleOnLockForm(MessageParcel & data,MessageParcel & reply)219 int32_t FormHostStub::HandleOnLockForm(MessageParcel &data, MessageParcel &reply)
220 {
221     std::vector<int64_t> formIds;
222     bool ret = data.ReadInt64Vector(&formIds);
223     if (!ret) {
224         HILOG_ERROR("fail ReadInt64Vector<formIds>");
225         return ERR_APPEXECFWK_PARCEL_ERROR;
226     }
227     bool enable = data.ReadBool();
228     OnLockForm(formIds, enable);
229     reply.WriteInt32(ERR_OK);
230     return ERR_OK;
231 }
232 
HandleOnErrorForms(MessageParcel & data,MessageParcel & reply)233 int32_t FormHostStub::HandleOnErrorForms(MessageParcel &data, MessageParcel &reply)
234 {
235     int32_t errorCode = data.ReadInt32();
236     std::string errorMsg = Str16ToStr8(data.ReadString16());
237     std::vector<int64_t> formIds;
238     bool ret = data.ReadInt64Vector(&formIds);
239     if (!ret) {
240         HILOG_ERROR("fail ReadInt64Vector<formIds>");
241         return ERR_APPEXECFWK_PARCEL_ERROR;
242     }
243     OnError(errorCode, errorMsg, formIds);
244     reply.WriteInt32(ERR_OK);
245     return ERR_OK;
246 }
247 
248 }  // namespace AppExecFwk
249 }  // namespace OHOS