• 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         default:
71             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
72     }
73 }
74 /**
75  * @brief handle OnAcquired event.
76  * @param data input param.
77  * @param reply output param.
78  * @return Returns ERR_OK on success, others on failure.
79  */
HandleAcquired(MessageParcel & data,MessageParcel & reply)80 int FormHostStub::HandleAcquired(MessageParcel &data, MessageParcel &reply)
81 {
82     std::unique_ptr<FormJsInfo> formInfo(data.ReadParcelable<FormJsInfo>());
83     if (!formInfo) {
84         HILOG_ERROR("ReadParcelable<FormJsInfo> failed");
85         return ERR_APPEXECFWK_PARCEL_ERROR;
86     }
87 
88     sptr<IRemoteObject> token = nullptr;
89     if (data.ReadBool()) {
90         token = data.ReadRemoteObject();
91     }
92 
93     OnAcquired(*formInfo, token);
94     reply.WriteInt32(ERR_OK);
95     return ERR_OK;
96 }
97 /**
98  * @brief handle OnUpdate event.
99  * @param data input param.
100  * @param reply output param.
101  * @return Returns ERR_OK on success, others on failure.
102  */
HandleOnUpdate(MessageParcel & data,MessageParcel & reply)103 int FormHostStub::HandleOnUpdate(MessageParcel &data, MessageParcel &reply)
104 {
105     std::unique_ptr<FormJsInfo> formInfo(data.ReadParcelable<FormJsInfo>());
106     if (!formInfo) {
107         HILOG_ERROR("ReadParcelable<FormJsInfo> failed");
108         return ERR_APPEXECFWK_PARCEL_ERROR;
109     }
110     OnUpdate(*formInfo);
111     reply.WriteInt32(ERR_OK);
112     return ERR_OK;
113 }
114 
115 /**
116  * @brief handle OnUnInstall event.
117  * @param data input param.
118  * @param reply output param.
119  * @return Returns ERR_OK on success, others on failure.
120  */
HandleOnUninstall(MessageParcel & data,MessageParcel & reply)121 int FormHostStub::HandleOnUninstall(MessageParcel &data, MessageParcel &reply)
122 {
123     std::vector<int64_t> formIds;
124     bool ret = data.ReadInt64Vector(&formIds);
125     if (ret) {
126         OnUninstall(formIds);
127         reply.WriteInt32(ERR_OK);
128         return ERR_OK;
129     }
130     return ERR_APPEXECFWK_PARCEL_ERROR;
131 }
132 
133 /**
134  * @brief handle OnAcquireState message.
135  * @param data input param.
136  * @param reply output param.
137  * @return Returns ERR_OK on success, others on failure.
138  */
HandleOnAcquireState(MessageParcel & data,MessageParcel & reply)139 int FormHostStub::HandleOnAcquireState(MessageParcel &data, MessageParcel &reply)
140 {
141     auto state = (FormState) data.ReadInt32();
142 
143     std::unique_ptr<AAFwk::Want> want(data.ReadParcelable<AAFwk::Want>());
144     if (!want) {
145         HILOG_ERROR("ReadParcelable<Want> failed");
146         reply.WriteInt32(ERR_APPEXECFWK_PARCEL_ERROR);
147         return ERR_APPEXECFWK_PARCEL_ERROR;
148     }
149 
150     OnAcquireState(state, *want);
151     reply.WriteInt32(ERR_OK);
152     return ERR_OK;
153 }
154 
HandleOnShareFormResponse(MessageParcel & data,MessageParcel & reply)155 int32_t FormHostStub::HandleOnShareFormResponse(MessageParcel &data, MessageParcel &reply)
156 {
157     auto requestCode = data.ReadInt64();
158     auto result = data.ReadInt32();
159 
160     OnShareFormResponse(requestCode, result);
161     reply.WriteInt32(ERR_OK);
162     return ERR_OK;
163 }
164 
HandleOnError(MessageParcel & data,MessageParcel & reply)165 int32_t FormHostStub::HandleOnError(MessageParcel &data, MessageParcel &reply)
166 {
167     int32_t errorCode = data.ReadInt32();
168     std::string errorMsg = Str16ToStr8(data.ReadString16());
169 
170     OnError(errorCode, errorMsg);
171     reply.WriteInt32(ERR_OK);
172     return ERR_OK;
173 }
174 
HandleOnAcquireDataResponse(MessageParcel & data,MessageParcel & reply)175 int32_t FormHostStub::HandleOnAcquireDataResponse(MessageParcel &data, MessageParcel &reply)
176 {
177     std::shared_ptr<AAFwk::WantParams> wantParams(data.ReadParcelable<AAFwk::WantParams>());
178     if (wantParams == nullptr) {
179         HILOG_ERROR("ReadParcelable<wantParams> failed");
180         return ERR_APPEXECFWK_PARCEL_ERROR;
181     }
182 
183     auto requestCode = data.ReadInt64();
184     if (requestCode <= 0) {
185         HILOG_ERROR("fail ReadInt64<requestCode>");
186         return ERR_APPEXECFWK_PARCEL_ERROR;
187     }
188 
189     OnAcquireDataResponse(*wantParams, requestCode);
190     reply.WriteInt32(ERR_OK);
191     return ERR_OK;
192 }
193 
HandleOnRecycleForm(MessageParcel & data,MessageParcel & reply)194 int32_t FormHostStub::HandleOnRecycleForm(MessageParcel &data, MessageParcel &reply)
195 {
196     int64_t formId = data.ReadInt64();
197     OnRecycleForm(formId);
198     reply.WriteInt32(ERR_OK);
199     return ERR_OK;
200 }
201 
HandleOnEnableForm(MessageParcel & data,MessageParcel & reply)202 int32_t FormHostStub::HandleOnEnableForm(MessageParcel &data, MessageParcel &reply)
203 {
204     std::vector<int64_t> formIds;
205     bool ret = data.ReadInt64Vector(&formIds);
206     if (!ret) {
207         HILOG_ERROR("fail ReadInt64Vector<formIds>");
208         return ERR_APPEXECFWK_PARCEL_ERROR;
209     }
210     bool enable = data.ReadBool();
211     OnEnableForm(formIds, enable);
212     reply.WriteInt32(ERR_OK);
213     return ERR_OK;
214 }
215 }  // namespace AppExecFwk
216 }  // namespace OHOS